版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

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