版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

448 wiersze
18 KiB

  1. using CCWin.Win32;
  2. using CCWin.Win32.Const;
  3. using ImageToolKits;
  4. using MaiMuAOI.SysCtrl;
  5. using MaiMuAOI.SysUI;
  6. using Models;
  7. using OpenCvSharp;
  8. using OpenCvSharp.Flann;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Drawing;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows.Forms;
  19. using static ImageToolKits.ImageBox;
  20. namespace ProductionControl.UI
  21. {
  22. public partial class FrmGetPosByPic : Form
  23. {
  24. List<ImageBox.BaseDrawParam> pickARoi = new List<ImageBox.BaseDrawParam>();
  25. Mat Img;
  26. string _Path;
  27. double[] _Points;
  28. Service.ProductService service = new Service.ProductService();
  29. Models.Product model = new Models.Product();
  30. List<TestDefectPoints> DefectPoints = new List<TestDefectPoints>();
  31. public FrmGetPosByPic(Models.Product m, string map, double[] pointlist)
  32. {
  33. _Path = map;
  34. _Points = pointlist;
  35. pickARoi.Clear();
  36. InitializeComponent();
  37. UIStyle.SetUIStyle(this);
  38. this.uiTitel1.FatherForm = this;
  39. //lbPoslist.Items.Clear();
  40. //this.imageBox1.SetLowLevelMode(14);
  41. this.imageBox1.SetPointsLevelMode();
  42. imageBox1.DrawOverAllEventHandler += imgBox_DrawOverAllEventHandler;
  43. //显示行号与列宽度自动调整
  44. dgvPointTable.DefaultCellStyle.ForeColor = Color.Black;
  45. dgvPointTable.RowHeadersVisible = true;
  46. dgvPointTable.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
  47. dgvPointTable.RowPostPaint += (sender, e) =>
  48. {
  49. SysMgr.showRowNum_onDataGrid_RowPostPaint(this.dgvPointTable, sender, e);
  50. };
  51. model = m;
  52. if((model != null )&&(model.TestDefectPointsList != null))
  53. {
  54. DefectPoints = model.TestDefectPointsList;
  55. }
  56. else if((model != null) && (model.TestDefectPointsList == null))
  57. {
  58. model.TestDefectPointsList = new List<TestDefectPoints>();
  59. }
  60. cbPointName.SelectedIndex = 0;
  61. }
  62. public string GetMapPath()
  63. { return _Path; }
  64. public double[] GetPoints()
  65. {
  66. return _Points;
  67. }
  68. void imgBox_DrawOverAllEventHandler(object sender, List<ImageBox.BaseDrawParam> e)
  69. {
  70. if (e.Count > 0)
  71. {
  72. imageBox1.RefreshWindow(imageBox1.Image, e, ImageBox.ImageModeEnum.Part);
  73. }
  74. }
  75. private void FrmGetPosByPic_Load(object sender, EventArgs e)
  76. {
  77. imageBox1.Select();
  78. if (File.Exists(_Path))
  79. {
  80. Img = new Mat(_Path);
  81. imageBox1.RefreshWindow(Img);
  82. InitViewPoints();
  83. ShowPointCount();
  84. }
  85. else
  86. {
  87. string maop_path = ConfMgr.SelectFile();
  88. if (File.Exists(maop_path))
  89. {
  90. _Path = maop_path;
  91. Img = new Mat(maop_path);
  92. this.imageBox1.RefreshWindow(Img);
  93. }
  94. else
  95. {
  96. MessageBox.Show("无图纸图片加载!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
  97. return;
  98. }
  99. }
  100. //imageBox1.Refresh();
  101. //imageBox1.DisplayROIs(roi);
  102. //this.Refresh();
  103. }
  104. Color[] pointColors = new Color[15] { Color.Red, Color.Blue, Color.Green, Color.YellowGreen, Color.Orange,
  105. Color.Peru, Color.DarkRed,Color.AliceBlue, Color.Pink , Color.Brown,
  106. Color.CadetBlue, Color.OrangeRed, Color.Cyan, Color.Lime, Color.Magenta};
  107. void InitViewPoints()
  108. {
  109. List<ImageBox.ColorDrawParam> lROI = new List<ImageBox.ColorDrawParam>();
  110. //按点位类型
  111. var tempStuList = model.TestDefectPointsList.GroupBy(q => q.PointCode);
  112. foreach ( var tempPoint in tempStuList )
  113. {
  114. //一个点位类型所有点位数据
  115. List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == tempPoint.Key).ToList();
  116. for (int i = 0; i < t.Count; i++)
  117. {
  118. ColorDrawParam param = new ColorDrawParam();
  119. param.baseDraw = new ImageBox.Point((float)t[i].Y, (float)t[i].X);
  120. int index = 0;
  121. for (int j = 0; j < pointColors.Length; j++)
  122. {
  123. if (this.cbPointName.Items[j].ToString() == tempPoint.Key)
  124. {
  125. index = j;
  126. break;
  127. }
  128. }
  129. param.color = pointColors[index];
  130. lROI.Add(param);
  131. }
  132. }
  133. imageBox1.RefreshWindow(imageBox1.Image, lROI);
  134. }
  135. void ShowPointCount()
  136. {
  137. //按点位类型
  138. var tempStuList = model.TestDefectPointsList.GroupBy(q => q.PointCode);
  139. int[] count = new int[15];
  140. foreach (var tempPoint in tempStuList)
  141. {
  142. //一个点位类型所有点位数据
  143. List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == tempPoint.Key).ToList();
  144. for (int j = 0; j < pointColors.Length; j++)
  145. {
  146. if (this.cbPointName.Items[j].ToString() == tempPoint.Key)
  147. {
  148. count[j] = t.Count;
  149. break;
  150. }
  151. }
  152. }
  153. stLabel.Text = $"Y轴方向PT值检测({count[0]}),线宽正面({count[1]}),反面检测({count[2]}),X轴方向PT值检测({count[3]})," +
  154. $"主栅连接线检测(主栅间隔)({count[4]}),主栅宽度检测({count[5]}),主栅间距({count[6]}),细栅间距检测({count[7]}),\r\n背极宽度({count[8]}),"+
  155. $"主栅长度检测({count[9]}),Mark点横向间距({count[10]}),Mark点竖向间距({count[11]}),鱼叉口长({count[12]}),鱼叉口宽({count[13]}),鱼叉口间距({count[14]})";
  156. }
  157. #region 工具栏
  158. private void tsbtnClear_Click(object sender, EventArgs e)
  159. {
  160. if (MessageBox.Show("是否清空所有点位?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
  161. {
  162. pickARoi.Clear();
  163. DefectPoints.Clear();
  164. this.imageBox1.ClearColorPoints();
  165. this.imageBox1.RefreshWindow(Img);
  166. ShowPointCount();
  167. ShowPoints();
  168. ReflashTable();
  169. this.Refresh();
  170. }
  171. }
  172. private void tsbtnShowAll_Click(object sender, EventArgs e)
  173. {
  174. InitViewPoints();
  175. ShowPointCount();
  176. }
  177. private void tsbtnClose_Click(object sender, EventArgs e)
  178. {
  179. this.Close();
  180. }
  181. private void tsbtnSave_Click(object sender, EventArgs e)
  182. {
  183. //pickARoi = this.imageBox1.GetLowPoints();
  184. //if (pickARoi.Count == 14)
  185. //{
  186. // _Points = new double[14 * 2];
  187. // for (int i = 0; i < 14; i++)
  188. // {
  189. // _Points[2 * i] = Math.Round((pickARoi[i] as ImageBox.Point).Column, 3);
  190. // _Points[2 * i + 1] = Math.Round((pickARoi[i] as ImageBox.Point).Row, 3);
  191. // }
  192. // this.DialogResult = DialogResult.OK;
  193. //}
  194. //else if (pickARoi.Count >0)
  195. // MessageBox.Show("点位设置错误,点数不对!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
  196. //else
  197. //{
  198. // _Points = new double[14 * 2];
  199. // for (int i = 0; i < 14; i++)
  200. // {
  201. // _Points[2 * i] = 0;
  202. // _Points[2 * i + 1] = 0;
  203. // }
  204. //}
  205. try
  206. {
  207. //老点位使用
  208. bool result;
  209. if (model.Id == 0)
  210. {
  211. model.TestDefectPointsList = DefectPoints;
  212. result = service.InsertNav(model);
  213. }
  214. else
  215. {
  216. model.TestDefectPointsList = DefectPoints;
  217. result = service.UpdateNav(model);
  218. }
  219. if (!result)
  220. throw new Exception("保存失败!");
  221. MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  222. this.DialogResult = DialogResult.OK;
  223. }
  224. catch (Exception ex)
  225. {
  226. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  227. }
  228. }
  229. #endregion
  230. #region 工具按钮
  231. private void btnAdd_Click(object sender, EventArgs e)
  232. {
  233. try
  234. {
  235. //List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList();
  236. //DefectPoints.Add(point);
  237. DefectPoints.Add(
  238. new Models.TestDefectPoints()
  239. {
  240. PointCode = this.cbPointName.Text,
  241. X = Math.Round((imageBox1.GetPoints()[0] as ImageBox.Point).Column, 3),
  242. Y = Math.Round((imageBox1.GetPoints()[0] as ImageBox.Point).Row,3),
  243. ModifyUserCode = SysMgr.Instance.UserMgr.LoginUser.Code,
  244. CreateUserCode = SysMgr.Instance.UserMgr.LoginUser.Code
  245. });
  246. ShowPoints();
  247. ReflashTable();
  248. ShowPointCount();
  249. }
  250. catch (Exception ex)
  251. {
  252. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  253. }
  254. }
  255. private void ShowPoints()
  256. {
  257. List<ImageBox.ColorDrawParam> lROI = new List<ImageBox.ColorDrawParam>();
  258. //按点位类型
  259. //一个点位类型所有点位数据
  260. List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList();
  261. for (int i = 0; i < t.Count; i++)
  262. {
  263. ColorDrawParam param = new ColorDrawParam();
  264. param.baseDraw = new ImageBox.Point((float)t[i].Y, (float)t[i].X);
  265. param.color = pointColors[this.cbPointName.SelectedIndex];
  266. lROI.Add(param);
  267. }
  268. if(imageBox1.Image == null)
  269. imageBox1.RefreshWindow(imageBox1.Image, lROI);
  270. else
  271. imageBox1.RefreshWindow(imageBox1.Image, lROI, ImageModeEnum.Part);
  272. }
  273. private void cbPointName_SelectedIndexChanged(object sender, EventArgs e)
  274. {
  275. ShowPoints();
  276. ReflashTable();
  277. }
  278. private void ReflashTable()
  279. {
  280. List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList();
  281. dgvPointTable.Rows.Clear();
  282. for (int i = 0; i < t.Count; i++)
  283. {
  284. ColorDrawParam param = new ColorDrawParam();
  285. param.baseDraw = new ImageBox.Point((float)t[i].Y, (float)t[i].X);
  286. param.color = pointColors[this.cbPointName.SelectedIndex];
  287. int index = dgvPointTable.Rows.Add();
  288. dgvPointTable.Rows[index].Cells[0].Value = t[i].PointCode;
  289. dgvPointTable.Rows[index].Cells[1].Value = t[i].X;
  290. dgvPointTable.Rows[index].Cells[2].Value = t[i].Y;
  291. }
  292. }
  293. private void btnInsert_Click(object sender, EventArgs e)
  294. {
  295. try {
  296. if (this.dgvPointTable.SelectedRows.Count != 1)
  297. throw new Exception("请选择要插入的位置!");
  298. int liInsertIndex = this.dgvPointTable.Rows.Count;
  299. if (this.dgvPointTable.SelectedRows.Count > 0)
  300. liInsertIndex = this.dgvPointTable.SelectedRows[0].Index;
  301. List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList();
  302. int findIndex = DefectPoints.FindIndex(temp => (temp.X == t[liInsertIndex].X && temp.Y == t[liInsertIndex].Y) );
  303. //DefectPoints.Add(point);
  304. DefectPoints.Insert(findIndex,
  305. new Models.TestDefectPoints()
  306. {
  307. PointCode = this.cbPointName.Text,
  308. X = Math.Round((imageBox1.GetPoints()[0] as ImageBox.Point).Column, 3),
  309. Y = Math.Round((imageBox1.GetPoints()[0] as ImageBox.Point).Row, 3),
  310. ModifyUserCode = SysMgr.Instance.UserMgr.LoginUser.Code,
  311. CreateUserCode = SysMgr.Instance.UserMgr.LoginUser.Code
  312. });
  313. ShowPoints();
  314. ReflashTable();
  315. ShowPointCount();
  316. }
  317. catch (Exception ex)
  318. {
  319. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  320. }
  321. }
  322. private void btnDel_Click(object sender, EventArgs e)
  323. {
  324. try
  325. {
  326. if (this.dgvPointTable.SelectedRows.Count != 1)
  327. throw new Exception("请选择要删除的位置!");
  328. int liRowIndex = this.dgvPointTable.SelectedRows[0].Index;
  329. List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList();
  330. int findIndex = DefectPoints.FindIndex(temp => (temp.X == t[liRowIndex].X && temp.Y == t[liRowIndex].Y));
  331. DefectPoints.RemoveAt(findIndex);
  332. ReflashTable();
  333. if (liRowIndex >= dgvPointTable.Rows.Count - 1)
  334. liRowIndex = dgvPointTable.Rows.Count - 1;
  335. dgvPointTable.CurrentCell = dgvPointTable.Rows[liRowIndex].Cells[1];
  336. if (dgvPointTable.Rows.Count < 1)
  337. return;
  338. if (liRowIndex >= dgvPointTable.Rows.Count)
  339. liRowIndex = dgvPointTable.Rows.Count - 1;
  340. dgvPointTable.CurrentCell = dgvPointTable.Rows[liRowIndex].Cells[1];
  341. ShowPoints();
  342. ShowPointCount();
  343. }
  344. catch (Exception ex)
  345. {
  346. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  347. }
  348. }
  349. private void btnUp_Click(object sender, EventArgs e)
  350. {
  351. try
  352. {
  353. if (this.dgvPointTable.SelectedRows.Count != 1)
  354. throw new Exception("请选择要调整顺序的位置!");
  355. int liSelIndex = this.dgvPointTable.SelectedRows[0].Index;
  356. if (liSelIndex == 0)
  357. return;
  358. List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList();
  359. int findIndex = DefectPoints.FindIndex(temp => (temp.X == t[liSelIndex].X && temp.Y == t[liSelIndex].Y));
  360. int findIndexPre = DefectPoints.FindIndex(temp => (temp.X == t[liSelIndex - 1].X && temp.Y == t[liSelIndex - 1].Y));
  361. DefectPoints.Insert(findIndexPre, DefectPoints[findIndex]);
  362. DefectPoints.RemoveAt(findIndex + 1);
  363. ReflashTable();
  364. this.dgvPointTable.Rows[liSelIndex - 1].Selected = true;
  365. dgvPointTable.CurrentCell = dgvPointTable.Rows[liSelIndex - 1].Cells[1];
  366. ShowPoints();
  367. }
  368. catch (Exception ex)
  369. {
  370. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  371. }
  372. }
  373. private void btnDown_Click(object sender, EventArgs e)
  374. {
  375. try
  376. {
  377. if (this.dgvPointTable.SelectedRows.Count != 1)
  378. throw new Exception("请选择要调整顺序的位置!");
  379. int liSelIndex = this.dgvPointTable.SelectedRows[0].Index;
  380. if (liSelIndex == this.dgvPointTable.Rows.Count - 1)
  381. return;
  382. List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList();
  383. int findIndex = DefectPoints.FindIndex(temp => (temp.X == t[liSelIndex].X && temp.Y == t[liSelIndex].Y));
  384. int findIndexAft = DefectPoints.FindIndex(temp => (temp.X == t[liSelIndex + 1].X && temp.Y == t[liSelIndex + 1].Y));
  385. DefectPoints.Insert(findIndexAft + 1, DefectPoints[findIndex]);
  386. DefectPoints.RemoveAt(findIndex);
  387. ReflashTable();
  388. this.dgvPointTable.Rows[liSelIndex + 1].Selected = true;
  389. dgvPointTable.CurrentCell = dgvPointTable.Rows[liSelIndex + 1].Cells[1];
  390. ShowPoints();
  391. }
  392. catch (Exception ex)
  393. {
  394. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  395. }
  396. }
  397. #endregion
  398. }
  399. }