版博士V2.0程序
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

485 řádky
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. for (int i = 0; i < ItemName.Count; i++)
  182. {
  183. showPointNum += $"{ItemName[i]}({count[i]}),";
  184. if ((i+1) % 8 == 0)
  185. showPointNum += "\r\n";
  186. }
  187. stLabel.Text = showPointNum.Remove(showPointNum.Length - 1);
  188. }
  189. #region 工具栏
  190. private void tsbtnClear_Click(object sender, EventArgs e)
  191. {
  192. if (MessageBox.Show("是否清空所有点位?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
  193. {
  194. pickARoi.Clear();
  195. DefectPoints.Clear();
  196. this.imageBox1.ClearColorPoints();
  197. this.imageBox1.RefreshWindow(Img);
  198. ShowPointCount();
  199. ShowPoints();
  200. ReflashTable();
  201. this.Refresh();
  202. }
  203. }
  204. private void tsbtnShowAll_Click(object sender, EventArgs e)
  205. {
  206. InitViewPoints();
  207. ShowPointCount();
  208. }
  209. private void tsbtnClose_Click(object sender, EventArgs e)
  210. {
  211. this.Close();
  212. }
  213. private void tsbtnSave_Click(object sender, EventArgs e)
  214. {
  215. //pickARoi = this.imageBox1.GetLowPoints();
  216. //if (pickARoi.Count == 14)
  217. //{
  218. // _Points = new double[14 * 2];
  219. // for (int i = 0; i < 14; i++)
  220. // {
  221. // _Points[2 * i] = Math.Round((pickARoi[i] as ImageBox.Point).Column, 3);
  222. // _Points[2 * i + 1] = Math.Round((pickARoi[i] as ImageBox.Point).Row, 3);
  223. // }
  224. // this.DialogResult = DialogResult.OK;
  225. //}
  226. //else if (pickARoi.Count >0)
  227. // MessageBox.Show("点位设置错误,点数不对!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
  228. //else
  229. //{
  230. // _Points = new double[14 * 2];
  231. // for (int i = 0; i < 14; i++)
  232. // {
  233. // _Points[2 * i] = 0;
  234. // _Points[2 * i + 1] = 0;
  235. // }
  236. //}
  237. try
  238. {
  239. //老点位使用
  240. bool result;
  241. if (model.Id == 0)
  242. {
  243. model.TestDefectPointsList = DefectPoints;
  244. result = service.InsertNav(model);
  245. }
  246. else
  247. {
  248. model.TestDefectPointsList = DefectPoints;
  249. result = service.UpdateNav(model);
  250. }
  251. if (!result)
  252. throw new Exception("保存失败!");
  253. MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  254. this.DialogResult = DialogResult.OK;
  255. }
  256. catch (Exception ex)
  257. {
  258. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  259. }
  260. }
  261. #endregion
  262. #region 工具按钮
  263. private void btnAdd_Click(object sender, EventArgs e)
  264. {
  265. try
  266. {
  267. //List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList();
  268. //DefectPoints.Add(point);
  269. DefectPoints.Add(
  270. new Models.TestDefectPoints()
  271. {
  272. PointCode = this.cbPointName.Text,
  273. X = Math.Round((imageBox1.GetPoints()[0] as ImageBox.Point).Column, 3),
  274. Y = Math.Round((imageBox1.GetPoints()[0] as ImageBox.Point).Row,3),
  275. ModifyUserCode = SysMgr.Instance.UserMgr.LoginUser.Code,
  276. CreateUserCode = SysMgr.Instance.UserMgr.LoginUser.Code
  277. });
  278. ShowPoints();
  279. ReflashTable();
  280. ShowPointCount();
  281. }
  282. catch (Exception ex)
  283. {
  284. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  285. }
  286. }
  287. private void ShowPoints()
  288. {
  289. List<ImageBox.ColorDrawParam> lROI = new List<ImageBox.ColorDrawParam>();
  290. //按点位类型
  291. //一个点位类型所有点位数据
  292. List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList();
  293. for (int i = 0; i < t.Count; i++)
  294. {
  295. ColorDrawParam param = new ColorDrawParam();
  296. param.baseDraw = new ImageBox.Point((float)t[i].Y, (float)t[i].X);
  297. param.color = pointColors[this.cbPointName.SelectedIndex];
  298. lROI.Add(param);
  299. }
  300. if(imageBox1.Image == null)
  301. imageBox1.RefreshWindow(imageBox1.Image, lROI);
  302. else
  303. imageBox1.RefreshWindow(imageBox1.Image, lROI, ImageModeEnum.Part);
  304. }
  305. private void cbPointName_SelectedIndexChanged(object sender, EventArgs e)
  306. {
  307. ShowPoints();
  308. ReflashTable();
  309. }
  310. private void ReflashTable()
  311. {
  312. List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList();
  313. dgvPointTable.Rows.Clear();
  314. for (int i = 0; i < t.Count; i++)
  315. {
  316. ColorDrawParam param = new ColorDrawParam();
  317. param.baseDraw = new ImageBox.Point((float)t[i].Y, (float)t[i].X);
  318. param.color = pointColors[this.cbPointName.SelectedIndex];
  319. int index = dgvPointTable.Rows.Add();
  320. dgvPointTable.Rows[index].Cells[0].Value = t[i].PointCode;
  321. dgvPointTable.Rows[index].Cells[1].Value = t[i].X;
  322. dgvPointTable.Rows[index].Cells[2].Value = t[i].Y;
  323. }
  324. }
  325. private void btnInsert_Click(object sender, EventArgs e)
  326. {
  327. try {
  328. if (this.dgvPointTable.SelectedRows.Count != 1)
  329. throw new Exception("请选择要插入的位置!");
  330. int liInsertIndex = this.dgvPointTable.Rows.Count;
  331. if (this.dgvPointTable.SelectedRows.Count > 0)
  332. liInsertIndex = this.dgvPointTable.SelectedRows[0].Index;
  333. List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList();
  334. int findIndex = DefectPoints.FindIndex(temp => (temp.X == t[liInsertIndex].X && temp.Y == t[liInsertIndex].Y) );
  335. //DefectPoints.Add(point);
  336. DefectPoints.Insert(findIndex,
  337. new Models.TestDefectPoints()
  338. {
  339. PointCode = this.cbPointName.Text,
  340. X = Math.Round((imageBox1.GetPoints()[0] as ImageBox.Point).Column, 3),
  341. Y = Math.Round((imageBox1.GetPoints()[0] as ImageBox.Point).Row, 3),
  342. ModifyUserCode = SysMgr.Instance.UserMgr.LoginUser.Code,
  343. CreateUserCode = SysMgr.Instance.UserMgr.LoginUser.Code
  344. });
  345. ShowPoints();
  346. ReflashTable();
  347. ShowPointCount();
  348. }
  349. catch (Exception ex)
  350. {
  351. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  352. }
  353. }
  354. private void btnDel_Click(object sender, EventArgs e)
  355. {
  356. try
  357. {
  358. if (this.dgvPointTable.SelectedRows.Count != 1)
  359. throw new Exception("请选择要删除的位置!");
  360. int liRowIndex = this.dgvPointTable.SelectedRows[0].Index;
  361. List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList();
  362. int findIndex = DefectPoints.FindIndex(temp => (temp.X == t[liRowIndex].X && temp.Y == t[liRowIndex].Y));
  363. DefectPoints.RemoveAt(findIndex);
  364. ReflashTable();
  365. if (liRowIndex >= dgvPointTable.Rows.Count - 1)
  366. liRowIndex = dgvPointTable.Rows.Count - 1;
  367. dgvPointTable.CurrentCell = dgvPointTable.Rows[liRowIndex].Cells[1];
  368. if (dgvPointTable.Rows.Count < 1)
  369. return;
  370. if (liRowIndex >= dgvPointTable.Rows.Count)
  371. liRowIndex = dgvPointTable.Rows.Count - 1;
  372. dgvPointTable.CurrentCell = dgvPointTable.Rows[liRowIndex].Cells[1];
  373. ShowPoints();
  374. ShowPointCount();
  375. }
  376. catch (Exception ex)
  377. {
  378. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  379. }
  380. }
  381. private void btnUp_Click(object sender, EventArgs e)
  382. {
  383. try
  384. {
  385. if (this.dgvPointTable.SelectedRows.Count != 1)
  386. throw new Exception("请选择要调整顺序的位置!");
  387. int liSelIndex = this.dgvPointTable.SelectedRows[0].Index;
  388. if (liSelIndex == 0)
  389. return;
  390. List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList();
  391. int findIndex = DefectPoints.FindIndex(temp => (temp.X == t[liSelIndex].X && temp.Y == t[liSelIndex].Y));
  392. int findIndexPre = DefectPoints.FindIndex(temp => (temp.X == t[liSelIndex - 1].X && temp.Y == t[liSelIndex - 1].Y));
  393. DefectPoints.Insert(findIndexPre, DefectPoints[findIndex]);
  394. DefectPoints.RemoveAt(findIndex + 1);
  395. ReflashTable();
  396. this.dgvPointTable.Rows[liSelIndex - 1].Selected = true;
  397. dgvPointTable.CurrentCell = dgvPointTable.Rows[liSelIndex - 1].Cells[1];
  398. ShowPoints();
  399. }
  400. catch (Exception ex)
  401. {
  402. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  403. }
  404. }
  405. private void btnDown_Click(object sender, EventArgs e)
  406. {
  407. try
  408. {
  409. if (this.dgvPointTable.SelectedRows.Count != 1)
  410. throw new Exception("请选择要调整顺序的位置!");
  411. int liSelIndex = this.dgvPointTable.SelectedRows[0].Index;
  412. if (liSelIndex == this.dgvPointTable.Rows.Count - 1)
  413. return;
  414. List<TestDefectPoints> t = model.TestDefectPointsList.Where(q => q.PointCode == this.cbPointName.Text).ToList();
  415. int findIndex = DefectPoints.FindIndex(temp => (temp.X == t[liSelIndex].X && temp.Y == t[liSelIndex].Y));
  416. int findIndexAft = DefectPoints.FindIndex(temp => (temp.X == t[liSelIndex + 1].X && temp.Y == t[liSelIndex + 1].Y));
  417. DefectPoints.Insert(findIndexAft + 1, DefectPoints[findIndex]);
  418. DefectPoints.RemoveAt(findIndex);
  419. ReflashTable();
  420. this.dgvPointTable.Rows[liSelIndex + 1].Selected = true;
  421. dgvPointTable.CurrentCell = dgvPointTable.Rows[liSelIndex + 1].Cells[1];
  422. ShowPoints();
  423. }
  424. catch (Exception ex)
  425. {
  426. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  427. }
  428. }
  429. #endregion
  430. }
  431. }