版博士V2.0程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

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