FrmCalibrate.cs 11.0 KB
using System;
using System.Collections.Generic; 
using System.Drawing; 
using System.Windows.Forms;
using TSA_V.Common;
using TSA_V.DeviceLibrary;

namespace TSA_V.frmBoard
{
    public partial class FrmCalibrate : FrmBase
    {
        private BoardInfo currBoard = null;
        /// <summary>
        /// 1=已确认左上角位置
        /// 2=已确认右下角位置
        /// 3=正在校准中
        /// </summary>
        private int CurrStep = 0;
        public FrmCalibrate(BoardInfo board)
        {
            InitializeComponent();
            this.currBoard = board;
        }

        private void FrmCalibrate_Load(object sender, EventArgs e)
        {

            picBoard.Image = currBoard.GetImage();
            picBoard.SizeMode = PictureBoxSizeMode.StretchImage;
            SetPicSize();
            LoadPoint();
            timer1.Start();
            xyMoveControl1.loadTypeList(ResourceCulture.GetPTypes(), ResourceCulture.GetDirTypes(), ResourceCulture.GetXYControlMap());
            xyMoveControl1.ShowPointEvent += XyMoveControl1_ShowPointEvent;
            xyMoveControl1.PointType = 2;
            xyMoveControl1.PolaritiesType = 0;
            xyMoveControl1.PointSizeX = 20;
            xyMoveControl1.PointSizeY = 20;
            xyMoveControl1.PenWidth = 4;
            xyMoveControl1.XValue = 300;
            xyMoveControl1.YValue = 300;
            btnLeftUp.Visible = true;
            btnRightBottom.Visible = false;
            btnCal.Visible = false;
            xyMoveControl1.ShowCurrPoint();
            CurrStep = 0;
            LoadCalInfo();

        }

        private bool XyMoveControl1_ShowPointEvent(ProjectorPInfo p)
        {
            FrmProjectorScreen.instance.ClearPoint();
            FrmProjectorScreen.instance.ShowPoint(p);
            return true;
        }
        private void SetPicSize()
        {
            int yuliuP = 30;
            int xishu = 100;
            picBoard.Width = currBoard.boardWidth * xishu;
            picBoard.Height = currBoard.boardLength * xishu;
            int maxWidth = panBoard.Width - yuliuP * 2;
            int maxHeight = panBoard.Height - yuliuP * 2;
            if (picBoard.Width > maxWidth)
            {
                picBoard.Height = currBoard.boardLength * xishu * maxWidth / (currBoard.boardWidth * xishu);
                picBoard.Width = maxWidth;
            }
            if (picBoard.Height > maxHeight)
            {
                picBoard.Width = currBoard.boardWidth * xishu * maxHeight / (currBoard.boardLength * xishu);
                picBoard.Height = maxHeight;
            }
            picBoard.Location=new Point( (panBoard.Width - picBoard.Width )/ 2,(panBoard.Height-picBoard.Height)/2);
        }

        private void LoadCalInfo()
        {
            if (currBoard.calInfo == null)
            {
                CurrStep = 0;
            }
            else
            {
                if (CurrStep == 0)
                {
                    Point p = new Point();
                    if (currBoard.calInfo.leftUpPoint != null)
                    {
                        p = currBoard.calInfo.leftUpPoint;
                        xyMoveControl1.XValue = p.X;
                        xyMoveControl1.YValue = p.Y;
                    }
                    else
                    {
                        xyMoveControl1.XValue = 300;
                        xyMoveControl1.YValue = 300;
                    }
                    xyMoveControl1.ShowCurrPoint();
                    btnLeftUp.Visible = true;
                    btnRightBottom.Visible = false;
                    btnCal.Visible = false;
                    lblMsg.Text = ResourceCulture.GetString("CalMsg1", "第一步:请将光标移动至如图示电路板左上角位置,确认后点击设置按钮");
                }
                else if (CurrStep == 1)
                {
                    Point p = new Point();
                    if (currBoard.calInfo.rightBottomPoint != null)
                    {
                        p = currBoard.calInfo.rightBottomPoint;
                        xyMoveControl1.XValue = p.X;
                        xyMoveControl1.YValue = p.Y;
                    }
                    else
                    {
                        p = currBoard.calInfo.leftUpPoint;
                        xyMoveControl1.XValue = p.X;
                        xyMoveControl1.YValue = p.Y;
                    }
                    xyMoveControl1.ShowCurrPoint();
                    btnLeftUp.Visible = false;
                    btnRightBottom.Visible = true;
                    btnCal.Visible = false;
                    lblMsg.Text = ResourceCulture.GetString("CalMsg2", "第二步:请将光标移动至如图示电路板右下角位置,确认后点击设置按钮");
                }
                else if (CurrStep == 2)
                {
                    btnLeftUp.Visible = false;
                    btnRightBottom.Visible = false;
                    btnCal.Visible = true;
                    lblMsg.Text = ResourceCulture.GetString("CalMsg3", "第三步:请点击开始校准按钮,自动计算投影坐标");
                }
            }
        }
        private void LoadPoint()
        {
            Crop();
        }
        private void Crop()
        {
            try
            {
                Color color = lastColor;
                Graphics g = panBoard.CreateGraphics();
                g.Clear(panBoard.BackColor);
                int lineWidth = 5;
                int lineLength = 20;
                if (CurrStep == 0 || CurrStep == 2)
                {
                    Point point1 = new Point(picBoard.Location.X, picBoard.Location.Y);
                    g.DrawLine(new Pen(color, lineWidth), new Point(point1.X - lineLength, point1.Y), new Point(point1.X + lineLength, point1.Y));
                    g.DrawLine(new Pen(color, lineWidth), new Point(point1.X, point1.Y - lineLength), new Point(point1.X, point1.Y + lineLength));
                }
                if (CurrStep == 1 || CurrStep == 2)
                {
                    Point point2 = new Point(picBoard.Location.X + picBoard.Width, picBoard.Location.Y + picBoard.Height);
                    g.DrawLine(new Pen(color, lineWidth), new Point(point2.X - lineLength, point2.Y), new Point(point2.X + lineLength, point2.Y));
                    g.DrawLine(new Pen(color, lineWidth), new Point(point2.X, point2.Y - lineLength), new Point(point2.X, point2.Y + lineLength));
                }
                g.Flush();
                g.Dispose();
                if (lastColor.Equals(Color.Red))
                {
                    lastColor = Color.Orange;
                }
                else
                {
                    lastColor = Color.Red;
                }

            }
            catch(Exception ex)
            {
                LogUtil.error("画标定位置出错:" + ex.ToString());
            }
        }
        private Color lastColor = Color.Red;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!this.Visible)
            {
                return;
            }
            Crop();

        }

        private void btnLeftUp_Click(object sender, EventArgs e)
        {
            Point p = new Point((int)xyMoveControl1.XValue, (int)xyMoveControl1.YValue);
            if (currBoard.calInfo == null)
            {
                currBoard.calInfo = new CalibrateBean();
                currBoard.calInfo.leftUpPoint = p;
                currBoard.calInfo.rightBottomPoint = new Point(p.X + 300, p.Y + 300  ) ;
            }
            else
            { 
                currBoard.calInfo.leftUpPoint = p;
            } 
            currBoard.calInfo.CurrStep = 1;
            //BoardManager.Update(currBoard);
            LogUtil.info($"程序{currBoard.boardName}设置左上角坐标为:{p.X},{p.Y}");
            CurrStep = 1;
            LoadCalInfo();
        }

        private void btnRightBottom_Click(object sender, EventArgs e)
        {
            Point p = new Point((int)xyMoveControl1.XValue, (int)xyMoveControl1.YValue);
            if (currBoard.calInfo == null)
            {
                currBoard.calInfo = new CalibrateBean();
            }
            currBoard.calInfo.rightBottomPoint = p;
            currBoard.calInfo.CurrStep = 2;
            //BoardManager.Update(currBoard);
            LogUtil.info($"程序{currBoard.boardName}设置右下角坐标为:{p.X},{p.Y}");
            CurrStep = 2;
            LoadCalInfo();
        }

        private void btnCal_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            try
            {
            if (currBoard.calInfo == null || currBoard.calInfo.leftUpPoint == null)
            {
                CurrStep = 0;
                LoadCalInfo();
                return;
            }
            else if (currBoard.calInfo.rightBottomPoint == null)
            {
                CurrStep = 1;
                LoadCalInfo();
                return;
            }
            else
            {
                CalibrationProcess();
                }

            }
            catch (Exception ex)
            {
                LogUtil.error("校准出错:" + ex.ToString());
            }
            finally
            {
                this.Enabled = true;
            }
        }
        private void CalibrationProcess()
        {

            try
            {
                List<SMTPointInfo> pointList = new List<SMTPointInfo>(currBoard.smtList);

                List<SMTPointInfo> checkOKList = currBoard.getCalPoint();


                LogUtil.info($" 【{currBoard.boardName}】左上角(0,0)坐标【{checkOKList[0].NodePositionX},{checkOKList[0].NodePositionY}】右下角" +
                    $"({checkOKList[1].PositionX},{checkOKList[1].PositionY})【{checkOKList[1].NodePositionX},{checkOKList[1].NodePositionY}】");
            
                for (int i = 0; i < pointList.Count; i++)
                {
                    pointList[i] =XYConvertManager.CalPointNodePosition(pointList[i], checkOKList);
                }
                currBoard.smtList = pointList;
                BoardManager.Update(currBoard);
                FrmProjectorScreen.instance.ClearPoint();
                FrmProjectorScreen.instance.ShowPoint(false, currBoard.PointColor, pointList.ToArray());
                this.Cursor = Cursors.Default;
                //重新加载
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.TrainOK, "坐标校准完成!"));

                //记录校准信息
                XYConvertManager.SaveData(currBoard);

                DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                LogUtil.error("校准坐标出错:" + ex.ToString());
            }
        }


        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmCalibrate_Shown(object sender, EventArgs e)
        { 
            xyMoveControl1.ShowCurrPoint();
        }
    }

}