版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

786 lines
34 KiB

  1. using Devart.Common;
  2. using ImageToolKits;
  3. using MaiMuAOI.SysCtrl;
  4. using MaiMuControl.Device;
  5. using Models;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using OpenCvSharp;
  9. using OpenCvSharp.Flann;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.ComponentModel;
  13. using System.Data;
  14. using System.Drawing;
  15. using System.Drawing.Imaging;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows.Forms;
  21. using Yolo5;
  22. using static Mysqlx.Expect.Open.Types.Condition.Types;
  23. namespace MaiMuAOI.SysUI.DefectPicShow
  24. {
  25. public partial class DistributionFrm : Form
  26. {
  27. private Order _order;
  28. private Dictionary<string, List<List<string>>> defectInfoDir = new Dictionary<string, List<List<string>>>();
  29. private List<DefectStruct> defectList = new List<DefectStruct>();
  30. private Dictionary<string, Bitmap> defectBmpsDir = new Dictionary<string, Bitmap>();
  31. private Yolo_Class yolo = new Yolo_Class();
  32. private int currDefectIndex = 0;
  33. private Color[] colorList = { Color.Red, Color.Green, Color.DarkViolet , Color.Magenta, Color.Orange, Color.Brown,
  34. Color.Olive, Color.PaleGreen, Color.CadetBlue,Color.Aqua,Color.YellowGreen,Color.Blue,
  35. Color.SpringGreen,Color.Fuchsia,Color.Wheat,Color.AliceBlue,Color.Azure };
  36. private string[] DefectCodes = { "dk", "zw","xws","gsyc","qk", "zk","pp","hs","yx", "xb", "sx","ds","gsdl" ,"cjdk"};
  37. #region 表格数据类型
  38. private class DefectStruct
  39. {
  40. //Defect_{order.SN}_{res.index}_X{res.Xmm}_Y{res.Ymm}
  41. public string Key { get; set; }
  42. public int CurrIndex { get; set; }//当前缺陷在本大图中的索引0-n
  43. public int MainIndex { get; set; }//大图
  44. public double MainX { get; set; }//大图X 大相机拍照时原始X,Y轴坐标
  45. public double MainY { get; set; }//大图X
  46. //["4","3.9","-0.8","dk","0.39"],["index","X","Y","缺陷类型","缺陷的置信度"]
  47. public int Index { get; set; }
  48. public double X { get; set; }
  49. public double Y { get; set; }
  50. public string DefectName { get; set; }//缺陷类型
  51. public double DefectCC { get; set; }//缺陷的置信度
  52. //2023-10-30 修复情况
  53. public string Restoratory { get; set; }
  54. public double Area { get; set; }
  55. }
  56. #endregion
  57. public DistributionFrm(Order order)
  58. {
  59. InitializeComponent();
  60. UIStyle.SetUIStyle(this);
  61. this.uiTitel1.FatherForm = this;
  62. _order = order;
  63. imageBox0.SetIconsVisible(false);
  64. imageBox0.ChangeCursorModeIsHand(true);
  65. //imageBox0.set
  66. imageBox1.SetIconsVisible(false);
  67. imageBox1.ChangeCursorModeIsHand(true);
  68. #region 图片列表设置
  69. this.dgvProcess.AutoGenerateColumns = false;
  70. //显示行号与列宽度自动调整
  71. dgvProcess.RowHeadersVisible = true;
  72. dgvProcess.RowHeadersWidth = 30;
  73. dgvProcess.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  74. //dgvProcess.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变
  75. dgvProcess.RowPostPaint += (sender, e) =>//序号列头
  76. {
  77. showRowNum_onDataGrid_RowPostPaint(this.dgvProcess, sender, e);
  78. };
  79. for (int i = 0; i < dgvProcess.Columns.Count; i++)//禁止点击列头排序
  80. dgvProcess.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
  81. dgvProcess.DefaultCellStyle.ForeColor = Color.Black;
  82. #endregion
  83. #region 图纸列表设置
  84. this.dataGridView1.AutoGenerateColumns = false;
  85. //显示行号与列宽度自动调整
  86. dataGridView1.RowHeadersVisible = true;
  87. dataGridView1.RowHeadersWidth = 30;
  88. dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  89. //dgvProcess.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变
  90. dataGridView1.RowPostPaint += (sender, e) =>//序号列头
  91. {
  92. showRowNum_onDataGrid_RowPostPaint(this.dataGridView1, sender, e);
  93. };
  94. for (int i = 0; i < dataGridView1.Columns.Count; i++)//禁止点击列头排序
  95. dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
  96. dataGridView1.DefaultCellStyle.ForeColor = Color.Black;
  97. #endregion
  98. //图片查看功能现阶段禁用,存在bug
  99. tabControl1.TabPages.Remove(this.tabPage1);
  100. }
  101. //数据表重绘
  102. private void showRowNum_onDataGrid_RowPostPaint(DataGridView dgv, object sender, DataGridViewRowPostPaintEventArgs e)
  103. {
  104. Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth - 4, e.RowBounds.Height);
  105. TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgv.RowHeadersDefaultCellStyle.Font, rectangle, dgv.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
  106. }
  107. #region 界面调整
  108. private void DistributionFrm_SizeChanged(object sender, EventArgs e)
  109. {
  110. if (this.WindowState == FormWindowState.Maximized)
  111. {
  112. this.WindowState = FormWindowState.Normal;
  113. this.Top = 0;
  114. this.Left = 0;
  115. this.Width = SystemInformation.WorkingArea.Width;
  116. this.Height = SystemInformation.WorkingArea.Height;
  117. }
  118. }
  119. #endregion
  120. #region 获取缺陷代码对应缺陷项
  121. public string getDefectName(string defectCode)
  122. {
  123. defectCode = defectCode.ToLower();
  124. switch (defectCode)
  125. {
  126. case "dk":
  127. return "堵孔";
  128. case "zw":
  129. return "脏污";
  130. case "xws":
  131. return "纤维丝";
  132. case "gsyc":
  133. return "钢丝异常";
  134. case "qk":
  135. return "缺口";
  136. case "zk":
  137. return "针孔";
  138. case "pp":
  139. return "泡泡";
  140. case "hs":
  141. return "划伤";
  142. case "yx":
  143. return "压线";
  144. case "xb":
  145. return "斜边";
  146. case "sx":
  147. return "栅线";
  148. case "ds":
  149. return "断栅";
  150. default:
  151. return "未知";
  152. }
  153. }
  154. #endregion
  155. #region 数据获取
  156. private bool GetDefectDataFromSN(string sn, string date)
  157. {
  158. try
  159. {
  160. JObject data = new JObject();
  161. string path, key;
  162. string[] files;
  163. #region 获取所有缺陷数据
  164. //文件名格式:$"Size_SN{order.SN}_I{res.index}_X{res.Defects_X}_Y{res.Defects_Y}_.....json/bmp
  165. //--缺陷
  166. path = $"{ConfMgr.Instance.SysConfigParams.DefectRepairImag.SavePath}\\{date}\\{sn}\\";
  167. if (Directory.Exists(path))
  168. {
  169. files = Directory.GetFiles(path, $"Defect_SN{sn}_*.json", SearchOption.TopDirectoryOnly);
  170. //对文件名中的数字(index)进行大小排序
  171. files = files.OrderBy(s => int.Parse(Path.GetFileNameWithoutExtension(s).Split('_')[2].Substring(1))).ToArray();
  172. foreach (string file in files)
  173. {
  174. key = Path.GetFileNameWithoutExtension(file);
  175. if (File.Exists(path + key + ".bmp"))
  176. data.Add(key, JArray.Parse(File.ReadAllText(file)));
  177. //加载小图
  178. //LoadDefectPic(new Bitmap(path + key + ".bmp"), key);
  179. }
  180. }
  181. //--缺陷加载小图
  182. path = $"{ConfMgr.Instance.SysConfigParams.DefectSmallImag.SavePath}\\{date}\\{sn}\\";
  183. if (Directory.Exists(path))
  184. {
  185. files = Directory.GetFiles(path, $"Defect_SN{sn}_*.bmp", SearchOption.TopDirectoryOnly);
  186. //对文件名中的数字(index)进行大小排序
  187. files = files.OrderBy(s => int.Parse(Path.GetFileNameWithoutExtension(s).Split('_')[2].Substring(1))).ToArray();
  188. foreach (string file in files)
  189. {
  190. key = Path.GetFileNameWithoutExtension(file);
  191. //加载小图
  192. LoadDefectPic(new Bitmap(file), key);
  193. }
  194. }
  195. //--比对
  196. path = $"{ConfMgr.Instance.SysConfigParams.SizeRepairImag.SavePath}\\{date}\\{sn}\\";
  197. if (Directory.Exists(path))
  198. {
  199. files = Directory.GetFiles(path, $"Size_SN{sn}_*.json", SearchOption.TopDirectoryOnly);
  200. //对文件名中的数字(index)进行大小排序
  201. files = files.OrderBy(s => int.Parse(Path.GetFileNameWithoutExtension(s).Split('_')[2].Substring(1))).ToArray();
  202. foreach (string file in files)
  203. {
  204. key = Path.GetFileNameWithoutExtension(file);
  205. if (File.Exists(path + key + ".bmp"))
  206. data.Add(key, JArray.Parse(File.ReadAllText(file)));
  207. }
  208. }
  209. #endregion
  210. #region 加载图片和数据
  211. string file_path;
  212. foreach (var kv in data)
  213. {
  214. defectInfoDir.Add(kv.Key, JsonConvert.DeserializeObject<List<List<string>>>(kv.Value.ToString()));
  215. file_path = $"{ConfMgr.Instance.SysConfigParams.DefectRepairImag.SavePath}\\{date}\\{sn}\\{kv.Key}.bmp";
  216. if (!File.Exists(file_path))
  217. throw new Exception(kv.Key + " 缺陷文件不存在!");
  218. Bitmap bmp = new Bitmap(file_path);
  219. defectBmpsDir.Add(kv.Key, bmp);
  220. }
  221. defectList.Clear();
  222. if (File.Exists(ConfMgr.Instance.SysConfigParams.ImageProcessDataPath))
  223. {
  224. double X, Y;
  225. int MainIndex;
  226. foreach (var item in defectInfoDir)
  227. {
  228. //Key : Defect_SN{order.SN}_I{res.index}_X{res.Xmm}_Y{res.Ymm}_C{res.defectCount}
  229. string[] str = item.Key.Split('_');
  230. MainIndex = Convert.ToInt32(str[2].Substring(1));
  231. X = Convert.ToDouble(str[3].Substring(1));
  232. Y = Convert.ToDouble(str[4].Substring(1));
  233. var arr = yolo.ImageXY2RepairTable(item.Value, X, Y, ConfMgr.Instance.SysConfigParams.ImageProcessDataPath);
  234. int i = 0;
  235. foreach (var arr2 in arr)
  236. {
  237. var obj = new DefectStruct();
  238. obj.Key = item.Key;
  239. obj.CurrIndex = i++;
  240. obj.MainIndex = MainIndex;
  241. obj.MainX = X;
  242. obj.MainY = Y;
  243. obj.Index = Convert.ToInt32(arr2[0].ToString());
  244. obj.X = Convert.ToDouble(arr2[1].ToString());
  245. obj.Y = Convert.ToDouble(arr2[2].ToString());
  246. obj.DefectName = getDefectName(arr2[3].ToString());
  247. obj.DefectCC = Convert.ToDouble(arr2[4].ToString());
  248. defectList.Add(obj);
  249. }
  250. }
  251. this.Invoke(new System.Action(() =>
  252. {
  253. this.dgvProcess.DataSource = null;
  254. //注:这样绑定List中的对象必需得用{get;set;}方式定义属性,否则单独格为空内容
  255. this.dgvProcess.DataSource = new BindingSource(defectList, null);
  256. this.gpbDefectList.Text = $"缺陷明细({defectList.Count}条)";
  257. }));
  258. gotoDefctListIndex(currDefectIndex);
  259. }
  260. else
  261. throw new Exception("转换文件不存在!Path:" + ConfMgr.Instance.SysConfigParams.ImageProcessDataPath);
  262. /*
  263. string file_path;
  264. bool first = true;
  265. foreach (var kv in data)
  266. {
  267. defectInfoDir.Add(kv.Key, JsonConvert.DeserializeObject<List<List<string>>>(kv.Value.ToString()));
  268. file_path = $"{ConfMgr.Instance.SysConfigParams.DefectRepairImag.SavePath}\\{date}\\{sn}\\{kv.Key}.bmp";
  269. if (!File.Exists(file_path))
  270. throw new Exception(kv.Key + " 缺陷文件不存在!");
  271. Bitmap bmp = new Bitmap(file_path);
  272. defectBmpsDir.Add(kv.Key, bmp);
  273. //["4","3.9","-0.8","dk","0.39"],["index","X","Y","缺陷类型","缺陷的置信度"]
  274. JArray arr = kv.Value as JArray;//二维数组
  275. foreach (JArray arr2 in arr)
  276. {
  277. var obj = new DefectStruct();
  278. obj.Key = kv.Key;
  279. obj.MainIndex = int.Parse(kv.Key.Split('_')[2].Substring(1));
  280. obj.Index = Convert.ToInt32(arr2[0].ToString());
  281. obj.X = Convert.ToDouble(arr2[1].ToString());
  282. obj.Y = Convert.ToDouble(arr2[2].ToString());
  283. obj.DefectName = getDefectName(arr2[3].ToString());
  284. obj.DefectCC = Convert.ToDouble(arr2[4].ToString());
  285. defectList.Add(obj);
  286. }
  287. if (first)
  288. {
  289. first = false;
  290. this.Invoke(new System.Action(() =>
  291. {
  292. this.reloadPic(bmp, defectInfoDir[defectList[0].Key], defectList[0]);
  293. //Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);
  294. //mat = yolo.RepairTableDrawRec(defectInfor2RepairTable, defectInfo.MainX, defectInfo.MainY, defectInfo.CurrIndex, mat);
  295. }));
  296. }
  297. }
  298. this.Invoke(new System.Action(() =>
  299. {
  300. //注:这样绑定List中的对象必需得用{get;set;}方式定义属性,否则单独格为空内容
  301. this.dgvProcess.DataSource = new BindingSource(defectList, null);
  302. this.gpbDefectList.Text = $"缺陷明细({defectList.Count}条)";
  303. //2023-11-1 图纸对比缺陷标红
  304. for (int i = 0; i < defectList.Count; i++)
  305. {
  306. string defName = (string)this.dgvProcess.Rows[i].Cells["colDefectName"].Value;
  307. if ((defName == getDefectName("ds")) || ((defName == getDefectName("yx"))))
  308. this.dgvProcess.Rows[i].DefaultCellStyle.BackColor = Color.Red;
  309. }
  310. }));*/
  311. #endregion
  312. }
  313. catch (Exception ex)
  314. {
  315. MessageBox.Show(ex.Message, "加载错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  316. return false;
  317. }
  318. return true;
  319. }
  320. #endregion
  321. #region 图片加载
  322. private void reloadPic(Bitmap bmp, List<List<string>> defectInfor2RepairTable, DefectStruct defectInfo)
  323. {
  324. //绘框
  325. if (defectInfor2RepairTable != null && defectInfo != null)
  326. {
  327. Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);
  328. mat = yolo.RepairTableDrawRec(defectInfor2RepairTable, defectInfo.MainX, defectInfo.MainY, defectInfo.CurrIndex, mat);
  329. //mat.ImWrite("ttt.bmp");
  330. imageBox0.RefreshWindow(mat);
  331. }
  332. }
  333. #endregion
  334. #region 表格事件
  335. private void dgvProcess_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  336. {
  337. int preIndex = currDefectIndex;
  338. currDefectIndex = e.RowIndex;
  339. gotoDefctListIndex(currDefectIndex, preIndex);
  340. //图片对应不上,不使用
  341. //Control[] cts = this.pnlBmpList.Controls.Find("imgDefect_" + (currDefectIndex), true);
  342. //if((cts != null)&&(cts.Length>0))
  343. //{
  344. // cts[0].Select();
  345. //}
  346. }
  347. private void data_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  348. {
  349. int preIndex = currDefectIndex;
  350. currDefectIndex = e.RowIndex;
  351. gotoDefctListIndex2(currDefectIndex, preIndex);
  352. //图片对应不上,不使用
  353. //Control[] cts = this.pnlBmpList.Controls.Find("imgDefect_" + (currDefectIndex), true);
  354. //if((cts != null)&&(cts.Length>0))
  355. //{
  356. // cts[0].Select();
  357. //}
  358. Control[] cts = this.flowLayoutPanel1.Controls.Find("imgDefect_" + (defectList[currDefectIndex].Index), true);
  359. if ((cts != null) && (cts.Length > 0))
  360. {
  361. cts[0].Select();
  362. }
  363. }
  364. #endregion
  365. #region 选择缺陷
  366. private void gotoDefctListIndex(int index, int preIndex = -1)
  367. {
  368. try
  369. {
  370. this.Invoke(new System.Action(() =>
  371. {
  372. string key = defectList[index].Key;
  373. if (defectBmpsDir.ContainsKey(key))
  374. {
  375. reloadPic(defectBmpsDir[key], defectInfoDir[key], defectList[index]);
  376. }
  377. if (preIndex > -1)
  378. this.dgvProcess.Rows[preIndex].DefaultCellStyle.BackColor = Color.White;
  379. this.dgvProcess.Rows[index].DefaultCellStyle.BackColor = Color.LightGreen;
  380. this.dgvProcess.Rows[index].Selected = true;
  381. //this.dgvProcess.CurrentCell = this.dgvProcess.Rows[index].Cells["colResult"];
  382. //this.dgvProcess.Rows[index].Cells["colResult"].Value = "已检";
  383. //reDrawByIndex(index);
  384. }));
  385. if ((defectList[index].X <= 0) || (defectList[index].Y <= 0))
  386. {
  387. return;
  388. }
  389. }
  390. catch (Exception ex)
  391. {
  392. }
  393. }
  394. private void gotoDefctListIndex2(int index, int preIndex = -1)
  395. {
  396. try
  397. {
  398. this.Invoke(new System.Action(() =>
  399. {
  400. if (preIndex > -1)
  401. this.dataGridView1.Rows[preIndex].DefaultCellStyle.BackColor = Color.White;
  402. this.dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.LightGreen;
  403. this.dataGridView1.Rows[index].Selected = true;
  404. reDrawByIndex(index);
  405. }));
  406. }
  407. catch (Exception ex)
  408. {
  409. }
  410. }
  411. #endregion
  412. #region 图纸显示
  413. private string _imgPath;
  414. private void reDraw(string imgPath)
  415. {
  416. _imgPath = imgPath;
  417. Bitmap bmp;
  418. Graphics g;
  419. using (System.Drawing.Image img = System.Drawing.Image.FromFile(imgPath))
  420. {
  421. if (IsIndexedPixelFormat(img.PixelFormat))
  422. {
  423. bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
  424. g = Graphics.FromImage(bmp);
  425. g.DrawImage(img, 0, 0);
  426. }
  427. else
  428. {
  429. bmp = new Bitmap(imgPath);
  430. g = Graphics.FromImage(bmp);
  431. }
  432. }
  433. var list = _order.DefectInfoList;
  434. int x, y, w, h;
  435. int i = 0;
  436. int index = 0;
  437. foreach (string code in DefectCodes)
  438. {
  439. var lstDefectCode = list.Where(m => m.Code == code ).ToList();
  440. foreach (var info in lstDefectCode)
  441. {
  442. x = (int)info.X - 10;
  443. y = (int)info.Y - 10;
  444. w = h = 20;
  445. if (x < 0) x = 0;
  446. if (y < 0) y = 0;
  447. if (w > bmp.Width - 1) w = bmp.Width - 1;
  448. if (h > bmp.Height - 1) h = bmp.Height - 1;
  449. //g.DrawRectangle(new Pen(item.ForeColor, 1.0f), x,y,w,h);
  450. g.FillEllipse(new SolidBrush(colorList[i]), x, y, w, h);
  451. var obj = new DefectStruct();
  452. //obj.Key = info.Pid;
  453. obj.CurrIndex = index++;
  454. //obj.MainIndex = MainIndex;
  455. //obj.MainX = X;
  456. //obj.MainY = Y;
  457. //obj.Index = Convert.ToInt32(arr2[0].ToString());
  458. obj.X = info.X;
  459. obj.Y = info.Y;
  460. obj.Index = info.Index;
  461. obj.DefectName = getDefectName(info.Code);
  462. obj.DefectCC = info.ZXD;
  463. obj.Area = info.Area;
  464. defectList.Add(obj);
  465. }
  466. i++;
  467. }
  468. g.Flush();
  469. g.Dispose();
  470. this.Invoke(new System.Action(() =>
  471. {
  472. this.dataGridView1.DataSource = null;
  473. //注:这样绑定List中的对象必需得用{get;set;}方式定义属性,否则单独格为空内容
  474. this.dataGridView1.DataSource = new BindingSource(defectList, null);
  475. this.groupBox1.Text = $"缺陷明细({defectList.Count}条)";
  476. }));
  477. //
  478. //reloadPic(bmp);
  479. //ucImageView1.loadImage(bmp);
  480. OpenCvSharp.Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);//用bitmap转换为mat
  481. imageBox1.RefreshWindow(mat);
  482. }
  483. private void reDrawByIndex(int index)
  484. {
  485. Bitmap bmp;
  486. Graphics g;
  487. using (System.Drawing.Image img = System.Drawing.Image.FromFile(_imgPath))
  488. {
  489. if (IsIndexedPixelFormat(img.PixelFormat))
  490. {
  491. bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
  492. g = Graphics.FromImage(bmp);
  493. g.DrawImage(img, 0, 0);
  494. }
  495. else
  496. {
  497. bmp = new Bitmap(_imgPath);
  498. g = Graphics.FromImage(bmp);
  499. }
  500. }
  501. var list = _order.DefectInfoList;
  502. int x, y, w, h;
  503. int i = 0;
  504. int t_index = 0;
  505. foreach (string code in DefectCodes)
  506. {
  507. var lstDefectCode = list.Where(m => m.Code == code).ToList();
  508. foreach (var info in lstDefectCode)
  509. {
  510. if (t_index == index)
  511. {
  512. x = (int)info.X - 20;
  513. y = (int)info.Y - 20;
  514. w = h = 40;
  515. if (x < 0) x = 0;
  516. if (y < 0) y = 0;
  517. if (w > bmp.Width - 1) w = bmp.Width - 1;
  518. if (h > bmp.Height - 1) h = bmp.Height - 1;
  519. //g.DrawRectangle(new Pen(item.ForeColor, 1.0f), x,y,w,h);
  520. g.FillRectangle(new SolidBrush(Color.LimeGreen), x, y, w, h);
  521. }
  522. else
  523. {
  524. x = (int)info.X - 10;
  525. y = (int)info.Y - 10;
  526. w = h = 20;
  527. if (x < 0) x = 0;
  528. if (y < 0) y = 0;
  529. if (w > bmp.Width - 1) w = bmp.Width - 1;
  530. if (h > bmp.Height - 1) h = bmp.Height - 1;
  531. //g.DrawRectangle(new Pen(item.ForeColor, 1.0f), x,y,w,h);
  532. g.FillEllipse(new SolidBrush(colorList[i]), x, y, w, h);
  533. }
  534. t_index++;
  535. }
  536. i++;
  537. }
  538. g.Flush();
  539. g.Dispose();
  540. //
  541. //reloadPic(bmp);
  542. //ucImageView1.loadImage(bmp);
  543. OpenCvSharp.Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);//用bitmap转换为mat
  544. imageBox1.RefreshWindow(mat, ImageBox.ImageModeEnum.Part);
  545. }
  546. /// <summary>
  547. /// 判断图片是否索引像素格式,是否是引发异常的像素格式
  548. /// </summary>
  549. /// <param name="imagePixelFormat">图片的像素格式</param>
  550. /// <returns></returns>
  551. private bool IsIndexedPixelFormat(System.Drawing.Imaging.PixelFormat imagePixelFormat)
  552. {
  553. PixelFormat[] pixelFormatArray = {
  554. PixelFormat.Format1bppIndexed
  555. ,PixelFormat.Format4bppIndexed
  556. ,PixelFormat.Format8bppIndexed
  557. ,PixelFormat.Undefined
  558. ,PixelFormat.DontCare
  559. ,PixelFormat.Format16bppArgb1555
  560. ,PixelFormat.Format16bppGrayScale
  561. };
  562. foreach (PixelFormat pf in pixelFormatArray)
  563. {
  564. if (imagePixelFormat == pf)
  565. {
  566. return true;
  567. }
  568. }
  569. return false;
  570. }
  571. #endregion
  572. #region 界面加载
  573. private void DistributionFrm_Load(object sender, EventArgs e)
  574. {
  575. this.gboxDefectList.Tag = 0;
  576. this.pnlBmpList.Controls.Clear();
  577. this.flowLayoutPanel1.Controls.Clear();
  578. this.Cursor = Cursors.WaitCursor;
  579. #region 加载图纸缺陷查看
  580. Models.Attachment attachmentFile = _order.ProductInfo.AttachmentList.FirstOrDefault(x => x.Type == 0);
  581. string gbxBmpPath = "";
  582. if (attachmentFile != null)
  583. {
  584. gbxBmpPath = ConfMgr.Instance.ProjectDir + $"\\{attachmentFile.NameTimestamp}";
  585. if (!File.Exists(gbxBmpPath + ".bmp"))
  586. gbxBmpPath = "";
  587. }
  588. if (string.IsNullOrEmpty(gbxBmpPath))
  589. {
  590. MessageBox.Show("图纸文件不存在!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  591. //this.Close();
  592. }
  593. else
  594. {
  595. if (!File.Exists(gbxBmpPath + ".jpg"))//转背景后的图纸文件
  596. {
  597. //换背景JPG
  598. Mat mat = Cv2.ImRead(gbxBmpPath + ".bmp");
  599. Cv2.CvtColor(mat, mat, ColorConversionCodes.RGB2GRAY);//转灰度图
  600. for (int i = 0; i < mat.Height; i++)
  601. {
  602. for (int j = 0; j < mat.Width; j++)
  603. {
  604. if (mat.At<byte>(i, j) == 255)//白色
  605. mat.Set<byte>(i, j, 0);
  606. else
  607. mat.Set<byte>(i, j, 255);
  608. }
  609. }
  610. OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat).Save(gbxBmpPath + ".jpg", ImageFormat.Jpeg);
  611. }
  612. string imagePath = gbxBmpPath + ".jpg";
  613. reDraw(imagePath);
  614. //--缺陷加载小图
  615. string[] files;
  616. string key;
  617. string path = $"{ConfMgr.Instance.SysConfigParams.DefectSmallImag.SavePath}\\{_order.CreateTime.ToString("yyyMMdd")}\\{_order.SN}\\";
  618. if (Directory.Exists(path))
  619. {
  620. files = Directory.GetFiles(path, $"Defect_SN{_order.SN}_*.bmp", SearchOption.TopDirectoryOnly);
  621. //files = Directory.GetFiles(path, $"*.bmp", SearchOption.TopDirectoryOnly);
  622. //对文件名中的数字(index)进行大小排序
  623. //files = files.OrderBy(s => int.Parse(Path.GetFileNameWithoutExtension(s))).ToArray();
  624. files = files.OrderBy(s => int.Parse(Path.GetFileNameWithoutExtension(s).Split('_')[2].Substring(1))).ToArray();
  625. foreach (string file in files)
  626. {
  627. key = Path.GetFileNameWithoutExtension(file).Split('_')[7];
  628. //MessageBox.Show($"{file}, {key}");
  629. //加载小图
  630. LoadDefectPic(new Bitmap(file), key);
  631. }
  632. }
  633. }
  634. #endregion
  635. #if false
  636. #region 加载大图缺陷查看
  637. if (!GetDefectDataFromSN(_order.SN, _order.CreateTime.ToString("yyyMMdd")))
  638. {
  639. //this.Close();
  640. }
  641. #endregion
  642. #endif
  643. pnlBmpList.VerticalScroll.Value = 0;
  644. flowLayoutPanel1.VerticalScroll.Value = 0;
  645. this.Cursor = Cursors.Default;
  646. }
  647. #endregion
  648. #region 加载小图
  649. private void LoadDefectPic(Bitmap bitmap, string name)
  650. {
  651. {
  652. //MessageBox.Show($"0");
  653. //加载缺陷小图
  654. //纵向显示
  655. int imgWidth = this.flowLayoutPanel1.ClientSize.Width - 50;
  656. //MessageBox.Show($"01-{this.flowLayoutPanel1.ClientSize.Width}");
  657. int imgHeight = (int)((bitmap.Height * 1.0f / bitmap.Width) * imgWidth + 0.5);
  658. //MessageBox.Show($"02 - {bitmap.Height} - {bitmap.Width}");
  659. int splitWidth = 20;
  660. //MessageBox.Show($"03");
  661. int pnlWidth = this.flowLayoutPanel1.ClientSize.Width;
  662. //MessageBox.Show($"04 - {this.flowLayoutPanel1.ClientSize.Width}");
  663. //int index = (int)this.groupBox2.Tag;
  664. //MessageBox.Show($"1 - {this.groupBox2.Tag}");
  665. PictureBox picbox = new PictureBox();
  666. picbox.Width = imgWidth;
  667. picbox.Height = imgHeight;
  668. picbox.Image = (Bitmap)bitmap.Clone();
  669. picbox.Name = "imgDefect_" + name;
  670. picbox.Tag = name;
  671. //MessageBox.Show($"2");
  672. picbox.Click += new EventHandler(defectBmpBox_Click);
  673. picbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  674. picbox.BorderStyle = BorderStyle.FixedSingle;
  675. //picbox.Margin = new Padding((pnlWidth - picbox.Height) / 2, splitWidth, 0, 0);
  676. picbox.Margin = new Padding((pnlWidth - picbox.Width) / 2, splitWidth, 0, 0);
  677. picbox.MouseHover += simpleTip_MouseHover;
  678. picbox.Visible = true;
  679. //MessageBox.Show($"3");
  680. this.flowLayoutPanel1.Controls.Add(picbox);
  681. flowLayoutPanel1.VerticalScroll.Value = flowLayoutPanel1.VerticalScroll.Maximum;
  682. //this.groupBox2.Tag = ++index;
  683. //this.groupBox2.Text = $"缺陷图像:{index} 张";
  684. //MessageBox.Show($"4");
  685. }
  686. }
  687. private void defectBmpBox_Click(object sender, EventArgs e)
  688. {
  689. //对应不上,不使用
  690. //PictureBox pb = sender as PictureBox;
  691. //int index = int.Parse(pb.Name.Replace("imgDefect_", ""));
  692. //dgvProcess_CellDoubleClick(null, new DataGridViewCellEventArgs(1, index));
  693. PictureBox pb = sender as PictureBox;
  694. int index = int.Parse(pb.Name.Replace("imgDefect_", ""));
  695. //MessageBox.Show($"{index}");
  696. for (int i = 0;i < defectList.Count; i++)
  697. {
  698. if (defectList[i].Index == index)
  699. {
  700. //MessageBox.Show($"111{index}");
  701. data_CellDoubleClick(null, new DataGridViewCellEventArgs(1, i));
  702. return;
  703. }
  704. }
  705. }
  706. private void simpleTip_MouseHover(object sender, EventArgs e)
  707. {
  708. PictureBox pb = sender as PictureBox;
  709. // 创建the ToolTip
  710. ToolTip toolTip1 = new ToolTip();
  711. // 设置显示样式
  712. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  713. toolTip1.InitialDelay = 500;//事件触发多久后出现提示
  714. toolTip1.ReshowDelay = 500;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  715. toolTip1.ShowAlways = true;//是否显示提示框
  716. // 设置伴随的对象.
  717. toolTip1.SetToolTip(pb, pb.Name + "[" + pb.Tag.ToString() + "]");//设置提示按钮和提示内容
  718. }
  719. #endregion
  720. }
  721. }