版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

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