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 _cellList = new List(); 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); } } } }