| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 | using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Drawing;using System.Drawing.Drawing2D;namespace Metis.AutoAnalysis{    public class ScanGridControl : UserControl    {        List<ScanCellControl> _cellList = new List<ScanCellControl>();        public int NumRow;        public int NumCol;        public double VsnWitch;        public double VsnHeight;        private SampleStageModel _setting;        private double _width => _setting.Width;        private double _height => _setting.Height;        private double _dia => _setting.Diameter;        private double _scal;        private ScanRegionControl _scanRegion;        VisionRectControl _vsnRect;        private ScanCellControl _selectCell;        public ScanGridControl()        {            this.Paint += ScanGridControl_Paint;        }        private void ScanGridControl_Paint(object sender, PaintEventArgs e)        {            if (_cellList.Count < 1) return;            foreach (var cell in _cellList)            { cell.Draw(e.Graphics); }            _scanRegion.Draw(e.Graphics);            _vsnRect.Draw(e.Graphics);            if (_selectCell != null)                e.Graphics.DrawRectangle(new Pen(Color.Blue), _selectCell.X, _selectCell.Y, _selectCell.Width - 1, _selectCell.Height - 1);        }        public void InitGrid(SampleStageModel stageSettingNow)        {            _setting = stageSettingNow;            _selectCell = null;            Controls.Clear();            _cellList.Clear();            DoubleBuffered = true;            _scal = Math.Min(Parent.Width / _width, Parent.Height / _height);            _scal = Math.Min(Parent.Height / _dia, _scal);            float offxGrid = 0;            float offyGrid = 0;            float offxC = 0;            float offyC = 0;            if (_dia > _width)            {                offxGrid = (float)((_dia - _width) * _scal) / 2;            }            else            {                offxC = (float)((_width - _dia) * _scal) / 2;            }            if (_dia > _height)            {                offyGrid = (float)((_dia - _height) * _scal) / 2;            }            else            {                offyC = (float)((_height - _dia) * _scal) / 2;            }            float w = (float)(_width * _scal / NumCol - 1);            float h = (float)(_height * _scal / NumRow - 1);            for (int i = 0; i < NumRow * NumCol; i++)            {                var cell = new ScanCellControl();                cell.Width = w;                cell.Height = h;                cell.BackColor = Color.White;                var x = 1 + (w + 1) * (i % NumCol);                if (i / NumCol % 2 > 0)                { x = 1 + (w + 1) * (NumCol - i % NumCol - 1); }                var y = 1 + (h + 1) * (i / NumCol);                cell.X = x + offxGrid;                cell.Y = y + offyGrid;                _cellList.Add(cell);            }            _scanRegion = new ScanRegionControl();            _scanRegion.Dia = (float)(_dia * _scal);            _scanRegion.X = offxC;            _scanRegion.Y = offyC;            Width = (int)(Math.Max(_width, _dia) * _scal);            Height = (int)(Math.Max(_height, _dia) * _scal);            Location = new Point((Parent.Width - Width) / 2, (Parent.Height - Height) / 2);            _vsnRect = new VisionRectControl();            _vsnRect.Width = (float)(VsnWitch * _scal / 1000);            _vsnRect.Height = (float)(VsnHeight * _scal / 1000);            if (NumCol == 1)            { VisionAt(NumRow / 2); }            else if (NumRow == 1)            { VisionAt(NumCol / 2); }            else            {                VisionAt(NumCol * (NumRow / 2) + NumCol / 2);            }            this.Invalidate();        }        public void SetDone(int i, int id = 0)        {            _cellList[i].BackColor = Color.Lime;            if (id > 0)                _cellList[i].Id = id;            Invalidate();        }        public void SetResult(int i, int id = 0)        {            _cellList[i].BackColor = Color.DeepPink;            if (id > 0)                _cellList[i].Id = id;            Invalidate();        }        public void SetDoning(int i, int Id)        {            _vsnRect.Active = false;            _selectCell = null;            _cellList[i].BackColor = Color.Red;            _cellList[i].Id = Id;            Invalidate();        }        public void VisionAt(int i)        {            _vsnRect.Cell = _cellList[i];            _vsnRect.Active = true;            _selectCell = _cellList[i];            Invalidate();        }        public void Reset()        {            _cellList.ForEach((c) => c.Reset());            Invalidate();        }        public int GetHitId(int x, int y)        {            var cell = _cellList.FirstOrDefault((c) => c.InRegion(x, y));            if (cell == null) return 0;            return cell.Id;        }        public int GetIndex(int x, int y)        {            var i = _cellList.FindIndex((c) => c.InRegion(x, y));            if (i > -1 && _cellList.Count > i)            {                _selectCell = _cellList[i];                Invalidate();            }            return i;        }        public class VisionRectControl        {            public bool Active = true;            public ScanCellControl Cell;            public float Width = 0;            public float Height = 0;            public void Draw(Graphics g)            {                if (!Active)                    return;                var x = Cell.X;                var y = Cell.Y;                var pen = new Pen(Color.Blue);                // g.DrawRectangle(pen, x, y, Cell.Width - 1, Cell.Height - 1);                x = Cell.X + (Cell.Width - Width) / 2;                y = Cell.Y + (Cell.Height - Height) / 2;                pen = new Pen(Color.Red);                g.DrawRectangle(pen, x, y, Width, Height);            }        }        public class ScanCellControl        {            public int Id;            public float X = 0;            public float Y = 0;            public float Width = 0;            public float Height = 0;            public Color BackColor = Color.Black;            static Font _font = new Font("Verdana", 9);            public bool InRegion(int x, int y)            {                var region = new RectangleF(X, Y, Width, Height);                return region.Contains(x, y);            }            public void Reset()            {                BackColor = Color.White;                Id = 0;            }            public void Draw(Graphics g)            {                var brush = new SolidBrush(BackColor);                g.FillRectangle(brush, X, Y, Width, Height);                if (Id > 0)                {                    SizeF fSize = g.MeasureString(Id.ToString(), _font);                    g.DrawString(Id.ToString(), _font, new SolidBrush(Color.Black), new PointF(X + (Width - fSize.Width) / 2, Y + (Height - fSize.Height) / 2));                }            }        }        public class ScanRegionControl        {            public float X = 0;            public float Y = 0;            public float Dia = 0;            public void Draw(Graphics g)            {                var pen = new Pen(Color.Green);                pen.Width = 2;                pen.DashStyle = DashStyle.Dash;                g.DrawEllipse(pen, X + 1, Y + 1, Dia - 1, Dia - 1);            }        }    }}
 |