ScanGridControl.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9. namespace Metis.AutoAnalysis
  10. {
  11. public class ScanGridControl : UserControl
  12. {
  13. List<ScanCellControl> _cellList = new List<ScanCellControl>();
  14. public int NumRow;
  15. public int NumCol;
  16. public double VsnWitch;
  17. public double VsnHeight;
  18. private SampleStageModel _setting;
  19. private double _width => _setting.Width;
  20. private double _height => _setting.Height;
  21. private double _dia => _setting.Diameter;
  22. private double _scal;
  23. private ScanRegionControl _scanRegion;
  24. VisionRectControl _vsnRect;
  25. private ScanCellControl _selectCell;
  26. public ScanGridControl()
  27. {
  28. this.Paint += ScanGridControl_Paint;
  29. }
  30. private void ScanGridControl_Paint(object sender, PaintEventArgs e)
  31. {
  32. if (_cellList.Count < 1) return;
  33. foreach (var cell in _cellList)
  34. { cell.Draw(e.Graphics); }
  35. _scanRegion.Draw(e.Graphics);
  36. _vsnRect.Draw(e.Graphics);
  37. if (_selectCell != null)
  38. e.Graphics.DrawRectangle(new Pen(Color.Blue), _selectCell.X, _selectCell.Y, _selectCell.Width - 1, _selectCell.Height - 1);
  39. }
  40. public void InitGrid(SampleStageModel stageSettingNow)
  41. {
  42. _setting = stageSettingNow;
  43. _selectCell = null;
  44. Controls.Clear();
  45. _cellList.Clear();
  46. DoubleBuffered = true;
  47. _scal = Math.Min(Parent.Width / _width, Parent.Height / _height);
  48. _scal = Math.Min(Parent.Height / _dia, _scal);
  49. float offxGrid = 0;
  50. float offyGrid = 0;
  51. float offxC = 0;
  52. float offyC = 0;
  53. if (_dia > _width)
  54. {
  55. offxGrid = (float)((_dia - _width) * _scal) / 2;
  56. }
  57. else
  58. {
  59. offxC = (float)((_width - _dia) * _scal) / 2;
  60. }
  61. if (_dia > _height)
  62. {
  63. offyGrid = (float)((_dia - _height) * _scal) / 2;
  64. }
  65. else
  66. {
  67. offyC = (float)((_height - _dia) * _scal) / 2;
  68. }
  69. float w = (float)(_width * _scal / NumCol - 1);
  70. float h = (float)(_height * _scal / NumRow - 1);
  71. for (int i = 0; i < NumRow * NumCol; i++)
  72. {
  73. var cell = new ScanCellControl();
  74. cell.Width = w;
  75. cell.Height = h;
  76. cell.BackColor = Color.White;
  77. var x = 1 + (w + 1) * (i % NumCol);
  78. if (i / NumCol % 2 > 0)
  79. { x = 1 + (w + 1) * (NumCol - i % NumCol - 1); }
  80. var y = 1 + (h + 1) * (i / NumCol);
  81. cell.X = x + offxGrid;
  82. cell.Y = y + offyGrid;
  83. _cellList.Add(cell);
  84. }
  85. _scanRegion = new ScanRegionControl();
  86. _scanRegion.Dia = (float)(_dia * _scal);
  87. _scanRegion.X = offxC;
  88. _scanRegion.Y = offyC;
  89. Width = (int)(Math.Max(_width, _dia) * _scal);
  90. Height = (int)(Math.Max(_height, _dia) * _scal);
  91. Location = new Point((Parent.Width - Width) / 2, (Parent.Height - Height) / 2);
  92. _vsnRect = new VisionRectControl();
  93. _vsnRect.Width = (float)(VsnWitch * _scal / 1000);
  94. _vsnRect.Height = (float)(VsnHeight * _scal / 1000);
  95. if (NumCol == 1)
  96. { VisionAt(NumRow / 2); }
  97. else if (NumRow == 1)
  98. { VisionAt(NumCol / 2); }
  99. else
  100. {
  101. VisionAt(NumCol * (NumRow / 2) + NumCol / 2);
  102. }
  103. this.Invalidate();
  104. }
  105. public void SetDone(int i, int id = 0)
  106. {
  107. _cellList[i].BackColor = Color.Lime;
  108. if (id > 0)
  109. _cellList[i].Id = id;
  110. Invalidate();
  111. }
  112. public void SetResult(int i, int id = 0)
  113. {
  114. _cellList[i].BackColor = Color.DeepPink;
  115. if (id > 0)
  116. _cellList[i].Id = id;
  117. Invalidate();
  118. }
  119. public void SetDoning(int i, int Id)
  120. {
  121. _vsnRect.Active = false;
  122. _selectCell = null;
  123. _cellList[i].BackColor = Color.Red;
  124. _cellList[i].Id = Id;
  125. Invalidate();
  126. }
  127. public void VisionAt(int i)
  128. {
  129. _vsnRect.Cell = _cellList[i];
  130. _vsnRect.Active = true;
  131. _selectCell = _cellList[i];
  132. Invalidate();
  133. }
  134. public void Reset()
  135. {
  136. _cellList.ForEach((c) => c.Reset());
  137. Invalidate();
  138. }
  139. public int GetHitId(int x, int y)
  140. {
  141. var cell = _cellList.FirstOrDefault((c) => c.InRegion(x, y));
  142. if (cell == null) return 0;
  143. return cell.Id;
  144. }
  145. public int GetIndex(int x, int y)
  146. {
  147. var i = _cellList.FindIndex((c) => c.InRegion(x, y));
  148. if (i > -1 && _cellList.Count > i)
  149. {
  150. _selectCell = _cellList[i];
  151. Invalidate();
  152. }
  153. return i;
  154. }
  155. public class VisionRectControl
  156. {
  157. public bool Active = true;
  158. public ScanCellControl Cell;
  159. public float Width = 0;
  160. public float Height = 0;
  161. public void Draw(Graphics g)
  162. {
  163. if (!Active)
  164. return;
  165. var x = Cell.X;
  166. var y = Cell.Y;
  167. var pen = new Pen(Color.Blue);
  168. // g.DrawRectangle(pen, x, y, Cell.Width - 1, Cell.Height - 1);
  169. x = Cell.X + (Cell.Width - Width) / 2;
  170. y = Cell.Y + (Cell.Height - Height) / 2;
  171. pen = new Pen(Color.Red);
  172. g.DrawRectangle(pen, x, y, Width, Height);
  173. }
  174. }
  175. public class ScanCellControl
  176. {
  177. public int Id;
  178. public float X = 0;
  179. public float Y = 0;
  180. public float Width = 0;
  181. public float Height = 0;
  182. public Color BackColor = Color.Black;
  183. static Font _font = new Font("Verdana", 9);
  184. public bool InRegion(int x, int y)
  185. {
  186. var region = new RectangleF(X, Y, Width, Height);
  187. return region.Contains(x, y);
  188. }
  189. public void Reset()
  190. {
  191. BackColor = Color.White;
  192. Id = 0;
  193. }
  194. public void Draw(Graphics g)
  195. {
  196. var brush = new SolidBrush(BackColor);
  197. g.FillRectangle(brush, X, Y, Width, Height);
  198. if (Id > 0)
  199. {
  200. SizeF fSize = g.MeasureString(Id.ToString(), _font);
  201. g.DrawString(Id.ToString(), _font, new SolidBrush(Color.Black), new PointF(X + (Width - fSize.Width) / 2, Y + (Height - fSize.Height) / 2));
  202. }
  203. }
  204. }
  205. public class ScanRegionControl
  206. {
  207. public float X = 0;
  208. public float Y = 0;
  209. public float Dia = 0;
  210. public void Draw(Graphics g)
  211. {
  212. var pen = new Pen(Color.Green);
  213. pen.Width = 2;
  214. pen.DashStyle = DashStyle.Dash;
  215. g.DrawEllipse(pen, X + 1, Y + 1, Dia - 1, Dia - 1);
  216. }
  217. }
  218. }
  219. }