PointDisplay.cs 8.5 KB
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.Common;
using TSA_V.DeviceLibrary;

namespace TSA_V
{
    public class PointDisplay
    {
        private System.Windows.Forms.Panel panBoard;
        private System.Windows.Forms.PictureBox picBoard;
        public int selectIndex = 0;
        public bool ImageNormal = true;
        public bool ShowName = true;
        public float imageXiShu = 1;

        public double CurrBeiLu = 1;
        private Image PicImage = null;
        private List<SMTPointInfo> PointList = new List<SMTPointInfo>();
        private int Width = 0;
        private int Height = 0;

        public bool IsWorkForm = false;
        public void SetImage(Image image)
        {
            picBoard.Image = image;
            this.PicImage = image;
        }
        public void SetPic(PictureBox p, Panel pan)
        {
            this.picBoard = p;
            this.panBoard = pan;
            picBoard.Tag = false;
            picBoard.SizeMode = PictureBoxSizeMode.StretchImage;

            //    picBoard.MouseWheel += picBoard_MouseWheel; 
        }

        private void SetPicSize()
        {
            int xishu = 100;
            picBoard.Width = Width * xishu;
            picBoard.Height = Height * xishu;
            if (picBoard.Width > panBoard.Width)
            {
                picBoard.Height = Height * xishu * panBoard.Width / (Width * xishu);
                picBoard.Width = panBoard.Width;
            }
            if (picBoard.Height > panBoard.Height)
            {
                picBoard.Width = Width * xishu * panBoard.Height / (Height * xishu);
                picBoard.Height = panBoard.Height;
            }
        }

        public void LoadPoint(int width, int height, List<SMTPointInfo> pointList = null)
        {
            this.PointList = pointList;
            Width = width;
            Height = height;
            CurrBeiLu = 1;
            //picBoard.SizeMode = PictureBoxSizeMode.AutoSize;
            if (pointList == null)
            {
                return;
            }

            if (!ImageNormal)
            {
                picBoard.Width = picBoard.Image.Width;
                picBoard.Height = picBoard.Image.Height;
            }
            else
            {
                //int xishu = 100;

                //picBoard.Width = width * xishu;
                //picBoard.Height = height * xishu;
                //if (picBoard.Width > panBoard.Width)
                //{
                //    picBoard.Height = height * xishu * panBoard.Width / (width * xishu);
                //    picBoard.Width = panBoard.Width;
                //}
                //if (picBoard.Height > (panBoard.Height - 10))
                //{
                //    picBoard.Width = width * xishu * (panBoard.Height - 10) / (height * xishu);
                //    picBoard.Height = (panBoard.Height - 10);
                //}
                //picBoard.Width = (int)(picBoard.Width * CurrBeiLu);
                //picBoard.Height = (int)(picBoard.Height * CurrBeiLu);
                SetPicSize();
            }

            //显示点 
            picBoard.Refresh();
            //panBoard.Refresh();

            imageXiShu = (float)picBoard.Width / (float)width;

            Graphics grfx = picBoard.CreateGraphics();
            //float preX = 0;
            //float preY = 0;
            int index = 0;
            foreach (SMTPointInfo weld in pointList)
            {
                float x = (float)Math.Abs(weld.PositionX) * imageXiShu;
                float y = (float)Math.Abs(weld.PositionY) * imageXiShu;

                int lineLength = 8;
                Color color = Color.HotPink;
                Brush brushes = Brushes.HotPink;
                if (weld.CheckOK)
                {
                    color = Color.Red;
                    brushes = Brushes.Red;
                }
                if (index.Equals(selectIndex))
                {
                    color = Color.Orange;
                    brushes = Brushes.Orange;
                }
                grfx.DrawLine(new Pen(color, 2), x, y - lineLength, x, y + lineLength);
                grfx.DrawLine(new Pen(color, 2), x - lineLength, y, x + lineLength, y);
                //写字 
                if (index.Equals(selectIndex) || ShowName)
                {
                    grfx.DrawString(weld.pointName, new Font("Arial ", 9, FontStyle.Bold), brushes, x - lineLength / 2 + 2, y + lineLength / 2 + 2);
                }
                index++;
            }

            grfx.SmoothingMode = SmoothingMode.HighQuality;
            grfx.Dispose();
        }

        public Dictionary<int, double> pointXMap = new Dictionary<int, double>();
        public Dictionary<int, double> pointYMap = new Dictionary<int, double>();
        public void LoadWorkFromPoint(BoardInfo board, SMTPointInfo smtPoint,bool isOnlyWork=true )
        {
            if (picBoard == null)
            {
                return;
            }
            try
            {
                picBoard.SizeMode = PictureBoxSizeMode.StretchImage; 
              
                this.Width = board.boardWidth;
                this.Height = board.boardLength;
                this.PointList = board.smtList;

                SetPicSize();
                 
                // updatePicPointSize(pWidth, pHeight);

                bool isFindCurrPoint = false;
                //显示点
                Graphics g = (picBoard).CreateGraphics();
                picBoard.Image = board.GetImage();

                picBoard.Refresh();
                int orgType = board.orgType;

                //double orgX = board.getImageOrgX();
                //double orgY = board.getImageOrgY();
                Graphics grfx = picBoard.CreateGraphics();
                float preX = 0;
                float preY = 0;
                int index = 0;
                pointXMap = new Dictionary<int, double>();
                pointYMap = new Dictionary<int, double>();
                List<SMTPointInfo> list = board.smtList;
                if (isOnlyWork)
                {
                    list = board.GetSmtList();
                }
                foreach (SMTPointInfo weld in list)
                {
                    float x = (float)Math.Abs(weld.PositionX) * picBoard.Width / Width;
                    float y = (float)Math.Abs(weld.PositionY) * picBoard.Width / Width;
                    pointXMap.Add(weld.pointNum, x);
                    pointYMap.Add(weld.pointNum, y);
                    if (weld.PartNum.Equals(smtPoint.PartNum))
                    //if (prepareWeld && weld.pointName.Equals(smtPoint.pointName))
                    {
                        int lineLength = 12;
                        grfx.DrawLine(new Pen(Color.Red, 4), x, y - lineLength, x, y + lineLength);
                        grfx.DrawLine(new Pen(Color.Red, 4), x - lineLength, y, x + lineLength, y);
                        isFindCurrPoint = true; ;
                        g.DrawString(weld.pointName, new Font("Arial ", 8, FontStyle.Bold), Brushes.Red, x - lineLength / 2 + 5, y + lineLength / 2 + 2);
                    }
                    else
                    {
                        if (isFindCurrPoint)
                        {
                            //未开始
                            int lineLength = 8;
                            grfx.DrawLine(new Pen(Color.Orange, 2), x, y - lineLength, x, y + lineLength);
                            grfx.DrawLine(new Pen(Color.Orange, 2), x - lineLength, y, x + lineLength, y);
                        }
                        else
                        {
                            //已完成
                            int lineLength = 6;
                            grfx.DrawLine(new Pen(Color.HotPink, 2), x, y - lineLength, x, y + lineLength);
                            grfx.DrawLine(new Pen(Color.HotPink, 2), x - lineLength, y, x + lineLength, y);
                            //g.FillEllipse(Brushes.Red, x - pointHight / 2, y - pointHight / 2, pointHight + pointWidth, pointHight + pointWidth);
                        }
                    }
                    preX = x;
                    preY = y;
                    index++;
                }
                this.PicImage = (Bitmap)picBoard.Image;
                grfx.Dispose();
                g.Dispose();
            }
            catch (Exception ex)
            {
                LogUtil.error("画图出错:" + ex.ToString());
            }
        } 
    }
}