革博士V2程序
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1459 рядки
63 KiB

  1. using CCWin.SkinControl;
  2. using DocumentFormat.OpenXml.EMMA;
  3. using DocumentFormat.OpenXml.Office2010.ExcelAc;
  4. using DocumentFormat.OpenXml.Office2019.Excel.RichData;
  5. using GeBoShi.SysCtrl;
  6. using GeBoShi.UI;
  7. using GeBoShi.UI.DataQuery;
  8. using GeBoShi.UI.InageShow;
  9. using GeBoShi.UI.Load;
  10. using GeBoShi.UI.Product;
  11. using GeBoShi.UI.SysSet;
  12. using HZH_Controls;
  13. using HZH_Controls.Controls;
  14. using HZH_Controls.Forms;
  15. using ImageToolKits;
  16. using MaiMuControl;
  17. using MaiMuControl.Device;
  18. using MaiMuControl.Device.CamDev;
  19. using Models;
  20. using OpenCvSharp;
  21. using OpenCvSharp.Extensions;
  22. using System;
  23. using System.Collections;
  24. using System.Collections.Generic;
  25. using System.ComponentModel;
  26. using System.Data;
  27. using System.Diagnostics;
  28. using System.Drawing;
  29. using System.IO;
  30. using System.Linq;
  31. using System.Runtime.InteropServices;
  32. using System.Text;
  33. using System.Threading;
  34. using System.Threading.Tasks;
  35. using System.Web.UI.WebControls.WebParts;
  36. using System.Windows.Forms;
  37. using System.Windows.Forms.DataVisualization.Charting;
  38. namespace GeBoShi
  39. {
  40. public partial class Main : Form
  41. {
  42. [DllImport("user32.dll ", EntryPoint = "SetParent")]
  43. private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); //将外部窗体嵌入程序
  44. [DllImport("user32.dll")]
  45. public static extern IntPtr FindWindow(string lpszClass, string lpszWindow); //按照窗体类名或窗体标题查找窗体
  46. object AddTextLock = new object();
  47. bool backgroundWorkerDo;
  48. private Stopwatch RunStartTime = new Stopwatch();
  49. /// <summary>
  50. /// 显示颜色现在对应30色,确保label在30个以内
  51. /// </summary>
  52. Color[] ChartColor = new Color[30] { Color.Red, Color.Blue, Color.Green, Color.YellowGreen, Color.Orange,
  53. Color.Peru, Color.DarkRed,Color.GreenYellow, Color.Pink , Color.Brown,
  54. Color.CadetBlue, Color.OrangeRed, Color.Cyan, Color.Lime, Color.Magenta, Color.Tan, Color.Sienna,
  55. Color.DarkGray, Color.SaddleBrown, Color.DarkBlue, Color.Firebrick,Color.Gainsboro,Color.Honeydew,Color.Khaki,Color.Lavender,
  56. Color.LightGoldenrodYellow,Color.Navy, Color.Khaki, Color.IndianRed,Color.Lavender};
  57. Process process = null;
  58. IntPtr appWin;
  59. public Main()
  60. {
  61. InitializeComponent();
  62. UIStyle.SetUIStyle(this);
  63. //process = Process.Start(@"C:\WINDOWS\system32\osk.exe");
  64. //process.WaitForInputIdle();
  65. //appWin = FindWindow(null, "屏幕键盘");
  66. //SetParent(appWin, this.keyPanel.Handle);
  67. this.keyPanel.Visible = false;
  68. this.uiTitel1.FatherForm = this;
  69. this.uiTitel1.UserMenuStrip = this.menuStrip1;
  70. this.uiTitel1.ShowContrlBox(true, true, true);
  71. this.uiTitel1.ShowMenu();
  72. this.DoubleBuffered = true;//设置本窗体
  73. SetStyle(ControlStyles.UserPaint, true);
  74. SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
  75. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
  76. SysMgr.Instance.MainRuning += main_running;
  77. SysMgr.Instance.AutoRuning += auto_runing;
  78. #region dataGridView设置
  79. skinDataGridView1.AllowUserToAddRows = skinDataGridView1.AllowUserToDeleteRows = false;//用户添加删除行
  80. skinDataGridView1.AllowUserToResizeRows = false;//用户调整行大小 //显示行号与列宽度自动调整
  81. skinDataGridView1.RowHeadersVisible = true;
  82. skinDataGridView1.RowHeadersWidth = 50;
  83. //dataGridView1.ColumnHeadersHeightSizeMode = dataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  84. skinDataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变
  85. skinDataGridView1.RowTemplate.Height = 30;
  86. //skinDataGridView1.RowPostPaint += (sender, e) =>//序号列头
  87. //{
  88. // showRowNum_onDataGrid_RowPostPaint(this.skinDataGridView1, sender, e);
  89. //};
  90. for (int i = 0; i < skinDataGridView1.Columns.Count; i++)//禁止点击列头排序
  91. skinDataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
  92. this.skinDataGridView1.CellClick += skinDataGridView1_CellClick; ;
  93. #endregion
  94. this.lblDefectLen.Top = toolStrip1.Top + 10;
  95. this.lblDefectLen.Left = toolStrip1.Width - 300;
  96. this.lblDefectSpeed.Top = toolStrip1.Top + 10 + lblDefectLen.Height + 5;
  97. this.lblDefectSpeed.Left = toolStrip1.Width - 300;
  98. this.lbFK.Text = "当前幅宽:";
  99. imageBox3.SetIconsVisible(false);
  100. imageBox3.SetStatusVisible(false);
  101. imageBox3.SetStripVisible(false);
  102. }
  103. #region 初始化
  104. /// <summary>
  105. /// 初始化
  106. /// </summary>
  107. /// <returns></returns>
  108. private bool InitSystem()
  109. {
  110. //进入初始化,初始化失败则中断
  111. InitFrm initFrm = new InitFrm();
  112. initFrm.ShowDialog();
  113. if (initFrm.DialogResult == DialogResult.OK)
  114. {
  115. //初始化成功
  116. }
  117. else
  118. {
  119. initFrm.Dispose();
  120. return false;
  121. }
  122. initFrm.Dispose();
  123. return true;
  124. }
  125. #endregion
  126. #region 界面刷新
  127. private void showRowNum_onDataGrid_RowPostPaint(DataGridView dgv, object sender, DataGridViewRowPostPaintEventArgs e)
  128. {
  129. Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth - 4, e.RowBounds.Height);
  130. TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgv.RowHeadersDefaultCellStyle.Font, rectangle, dgv.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
  131. }
  132. private void ResetUI(bool clearSN)
  133. {
  134. //保持产品名称和料号
  135. //txtProductName.Text = txtProductCode.Text = txtProductSN.Text = "";
  136. if (clearSN)
  137. cbPdtSN.Text = "";
  138. this.lstLog.Items.Clear();
  139. this.tsbtnImpStop.Enabled = false;
  140. this.tsbtnStart.Enabled = true;
  141. this.tsbtnStop.Enabled = false;
  142. this.cbPdtSN.Enabled = true;
  143. this.cbProductCode.Enabled = true;
  144. this.cbProductBatch.Enabled = true;
  145. this.cbProductReelNum.Enabled = true;
  146. this.cbProductNum.Enabled = true;
  147. this.pictureBox1.Image = null;
  148. pictureBox1.Refresh();
  149. //this.pictureBox2.Image = null;
  150. //pictureBox2.Refresh();
  151. //this.pictureBox3.Image = null;
  152. //pictureBox3.Refresh();
  153. //this.pictureBox4.Image = null;
  154. //pictureBox4.Refresh();
  155. tslabelTime.Text = "实时测试队列";
  156. skinDataGridView1.DataSource = null;
  157. this.skinDataGridView1.Rows.Clear();
  158. ResetChart();
  159. lblDefectLen.Text = $"检测长度: 0 米";
  160. lblDefectSpeed.Text = $"检测速度: 0 米/分钟";
  161. ucSwitch1.Checked = SysMgr.Instance.GetDefectPause();
  162. ucSwitch2.Checked = SysMgr.Instance.GetDefectDB();
  163. this.Refresh();
  164. imageBox3.RefreshWindow(null);
  165. }
  166. void ResetChart()
  167. {
  168. chart1.Series.Clear();
  169. chart1.Series.Add("SELECT");
  170. chart1.Series["SELECT"].ChartType = SeriesChartType.Point;
  171. chart1.Series["SELECT"].MarkerSize = 25;
  172. chart1.Series["SELECT"].MarkerColor = Color.Black;
  173. chart1.Series["SELECT"].MarkerStyle = MarkerStyle.Star6;
  174. //chart1.Series["SELECT"].Enabled = false;
  175. chart1.ChartAreas[0].AxisX.Title = "宽度(cm)";
  176. chart1.ChartAreas[0].AxisY.Title = "长度(m)";
  177. chart3.Series.Clear();
  178. chart3.Series.Add("面宽");
  179. chart3.ChartAreas[0].AxisX.Title = "长度(m)";
  180. chart3.ChartAreas[0].AxisY.Title = "宽度(cm)";
  181. chart3.Series["面宽"].ChartType = SeriesChartType.Line;
  182. chart3.Series["面宽"].MarkerSize = 5;
  183. chart3.Series["面宽"].MarkerColor = Color.Blue;
  184. chart3.Series["面宽"].MarkerStyle = MarkerStyle.Circle;
  185. }
  186. #endregion
  187. #region 权限判断
  188. private void UserRightCheck()
  189. {
  190. this.tsmPdtMgr.Enabled = SysMgr.Instance.UserMgr.RightControl(MaiMuControl.SysStatusMgr.UserMgr.UserRightEnum.Product);
  191. this.tsmSysDataFind.Visible = SysMgr.Instance.UserMgr.RightControl(MaiMuControl.SysStatusMgr.UserMgr.UserRightEnum.Statistics);
  192. this.tsmUserList.Enabled = SysMgr.Instance.UserMgr.RightControl(MaiMuControl.SysStatusMgr.UserMgr.UserRightEnum.User);
  193. this.tsmUserRight.Enabled = SysMgr.Instance.UserMgr.RightControl(MaiMuControl.SysStatusMgr.UserMgr.UserRightEnum.Role);
  194. if (this.tsmUserList.Enabled == false && this.tsmUserRight.Enabled == false)
  195. this.tsmSysUserMgr.Visible = false;
  196. else
  197. this.tsmSysUserMgr.Visible = true;
  198. this.tsmDebug.Enabled = SysMgr.Instance.UserMgr.RightControl(MaiMuControl.SysStatusMgr.UserMgr.UserRightEnum.Debug);
  199. this.tsmSysSet.Enabled = SysMgr.Instance.UserMgr.RightControl(MaiMuControl.SysStatusMgr.UserMgr.UserRightEnum.ParamsSetting);
  200. if (this.tsmDebug.Enabled == false && this.tsmSysSet.Enabled == false)
  201. this.stmSysSet.Visible = false;
  202. else
  203. this.stmSysSet.Visible = true;
  204. }
  205. #endregion
  206. #region 运行判断
  207. /// <summary>
  208. /// 系统是否在运行
  209. /// </summary>
  210. /// <returns></returns>
  211. private bool SystemIsRun()
  212. {
  213. //if (SysMgr.Instance.IsRuning)
  214. //{
  215. // FrmDialog.ShowDialog(this, "设备正在运行中!", "警告", false);
  216. // //MessageBox.Show("设备正在运行中!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  217. // return false;
  218. //}
  219. return true;
  220. }
  221. #endregion
  222. #region 界面事件
  223. private void Main_Load(object sender, EventArgs e)
  224. {
  225. //登入
  226. if (SysMgr.Instance.LoginSystem())
  227. {
  228. //登入成功
  229. if (InitSystem())
  230. {
  231. this.Cursor = Cursors.WaitCursor;
  232. //this.WindowState = FormWindowState.Normal;
  233. this.Top = 0;
  234. this.Left = 0;
  235. this.Width = SystemInformation.WorkingArea.Width;
  236. this.Height = SystemInformation.WorkingArea.Height;
  237. //this.WindowState = FormWindowState.Maximized;
  238. //this.splitContainer1.SplitterDistance = this.Width / 3;
  239. ResetUI(true);
  240. this.Show();
  241. this.tsslLoginInfo.Text = $"操作员:{SysMgr.Instance.UserMgr.LoginUser.Code}({SysMgr.Instance.UserMgr.LoginUser.Name})";
  242. this.statusStrip1.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.HorizontalStackWithOverflow;
  243. this.tsslLoginTime.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
  244. this.tsslLoginInfo.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
  245. this.tsState.Text = "";
  246. //更新数据库格式
  247. Service.InitDB.initDB(ConfMgr.Instance.DBConStr);
  248. //权限判断
  249. UserRightCheck();
  250. //开启后台
  251. //timer1.Enabled = true;
  252. backgroundWorkerDo = true;
  253. backgroundWorker1.RunWorkerAsync();
  254. //s_BackWorker = true;
  255. //backgroundWorker.RunWorkerAsync();
  256. //开启云端连接
  257. //SysMgr.Instance.InitCloudConnect();
  258. //开启产品服务
  259. //SysMgr.Instance.InitPdtService();
  260. //开启处理事件反馈
  261. SysMgr.Instance.InitDefectEvent();
  262. //加载产品料号
  263. SysMgr.Instance.LoadProductCodeList();
  264. //加载产品选择
  265. LoadPdtList(SysMgr.Instance.ProductCodeList);
  266. this.Cursor = Cursors.Default;
  267. //运行前检测磁盘空间
  268. SysMgr.CheckDisk(this);
  269. }
  270. else
  271. {
  272. Application.Exit();
  273. }
  274. }
  275. else
  276. {
  277. Application.Exit();
  278. }
  279. }
  280. private void Main_FormClosing(object sender, FormClosingEventArgs e)
  281. {
  282. if (SystemIsRun())
  283. {
  284. DialogResult dr = DialogResult.Cancel;
  285. if (SysMgr.Instance.IsInit)
  286. {
  287. dr = FrmDialog.ShowDialog(this, "确定要退出当前系统?", "警告", true);
  288. }
  289. else
  290. dr = DialogResult.OK;
  291. if (dr == DialogResult.Cancel)
  292. {
  293. e.Cancel = true;
  294. }
  295. else
  296. {
  297. backgroundWorkerDo = false;
  298. //Thread.Sleep(300);
  299. Thread.Sleep(300);
  300. if (SysMgr.Instance.IsInit)
  301. SysMgr.Instance.Close();
  302. //SysMgr.Instance.MainRuning -= main_running;
  303. }
  304. }
  305. else
  306. e.Cancel = true;
  307. }
  308. private void Main_SizeChanged(object sender, EventArgs e)
  309. {
  310. if (this.WindowState == FormWindowState.Maximized)
  311. {
  312. this.WindowState = FormWindowState.Normal;
  313. this.Top = 0;
  314. this.Left = 0;
  315. this.Width = SystemInformation.WorkingArea.Width;
  316. this.Height = SystemInformation.WorkingArea.Height;
  317. }
  318. }
  319. #endregion
  320. #region 菜单
  321. #region 退出
  322. private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
  323. {
  324. this.Close();
  325. }
  326. #endregion
  327. #region 用户
  328. private void tsmUserRight_Click(object sender, EventArgs e)
  329. {
  330. SysMgr.Instance.UserPermissiomMgr();
  331. }
  332. private void tsmUserList_Click(object sender, EventArgs e)
  333. {
  334. SysMgr.Instance.UserListMgr();
  335. }
  336. #endregion
  337. #region 系统设置
  338. private void MainFrm_Paint(object sender, PaintEventArgs e)
  339. {
  340. this.tsbtnReset.Visible = ConfMgr.Instance.SysConfigParams.ShowResetBtn;
  341. }
  342. private void tsmSysSet_Click(object sender, EventArgs e)
  343. {
  344. if (SystemIsRun())
  345. {
  346. //SysMgr.Instance.StatusMgr.GotoSetParams();
  347. //SysMgr.Instance.SendStatus();
  348. SysSetFrm frm = new SysSetFrm();
  349. frm.ShowDialog();
  350. MainFrm_Paint(null, null);
  351. //SysMgr.Instance.StatusMgr.GotoNormalStandby();
  352. //SysMgr.Instance.SendStatus();
  353. }
  354. }
  355. private void tsmDebug_Click(object sender, EventArgs e)
  356. {
  357. }
  358. #endregion
  359. #endregion
  360. #region 检测管理
  361. private string[] pdtlistData;
  362. private void LoadPdtList(List<string> list)
  363. {
  364. this.cbDefectCode.Items.Clear();
  365. while (true)
  366. {
  367. bool bk = true;
  368. for (int i = 0; i < list.Count; i++)
  369. {
  370. if (list[i] == null || string.IsNullOrEmpty(list[i]))
  371. {
  372. list.Remove(list[i]);
  373. bk = false;
  374. }
  375. }
  376. if (bk)
  377. {
  378. break;
  379. }
  380. }
  381. this.pdtlistData = list.ToArray();
  382. cbDefectCode.Items.AddRange(pdtlistData);//比使用DataSource速度要快一些
  383. cbDefectCode.TextUpdate += cobList_TextUpdate;//重新绑定事件
  384. cbDefectCode.KeyDown += CobList_KeyDown;
  385. this.cbDefectCode.Text = "";
  386. cbDefectCode.Focus();
  387. cbDefectCode.SelectAll();
  388. }
  389. private void tsmPdtMgr_Click(object sender, EventArgs e)
  390. {
  391. if (SystemIsRun())
  392. {
  393. ProductListFrm frm = new ProductListFrm();
  394. frm.ShowDialog();
  395. //加载产品料号
  396. SysMgr.Instance.LoadProductCodeList();
  397. //加载产品选择
  398. LoadPdtList(SysMgr.Instance.ProductCodeList);
  399. }
  400. }
  401. private void CobList_KeyDown(object sender, KeyEventArgs e)
  402. {
  403. ComboBox ctrl = sender as ComboBox;
  404. if (e.KeyCode == Keys.Enter)
  405. {
  406. if (ctrl.Items.Count == 1)
  407. ctrl.Text = ctrl.Items[0].ToString();
  408. }
  409. }
  410. private void cobList_TextUpdate(object sender, EventArgs e)
  411. {
  412. ComboBox ctrl = sender as ComboBox;
  413. if (ctrl.Text != null)
  414. {
  415. string str = ctrl.Text; //获取cb_material控件输入内
  416. //清空combobox
  417. ctrl.DataSource = null;
  418. ctrl.Items.Clear();
  419. string[] workOrderFiltered;
  420. workOrderFiltered = pdtlistData.Where(x => x.IndexOf(str, StringComparison.CurrentCultureIgnoreCase) != -1).ToArray();//忽略大小写
  421. ctrl.Items.AddRange(workOrderFiltered);//比使用DataSource速度要快一些
  422. // 不存在符合条件时
  423. //设置光标位置,若不设置:光标位置始终保持在第一列,造成输入关键词的倒序排列
  424. ctrl.Cursor = Cursors.Default; //保持鼠标指针原来状态,有时候鼠标指针会被下拉框覆盖,所以要进行一次设置
  425. if (workOrderFiltered.Length > 0)
  426. {
  427. if (!ctrl.DroppedDown)
  428. ctrl.DroppedDown = true; // 自动弹出下拉框
  429. }
  430. ctrl.SelectionStart = str.Length; // 设置光标位置,若不设置:光标位置始终保持在第一列,造成输入关键词的倒序排列
  431. }
  432. }
  433. #endregion
  434. #region 按钮
  435. private void tsbtnStart_Click(object sender, EventArgs e)
  436. {
  437. if (!SysMgr.Instance.IsRuning)
  438. {
  439. //低报警自动复位
  440. if (SysMgr.Instance.StatusMgr.Status == SystemStsEnum.Warning)
  441. {
  442. if (SysMgr.Instance.StatusMgr.Warning == WarningEnum.Low)
  443. {
  444. SysMgr.Instance.StatusMgr.ClearWarning();
  445. SysMgr.Instance.StatusMgr.GotoNormalStandby();
  446. }
  447. }
  448. //提示正在报警中
  449. if (SysMgr.Instance.StatusMgr.Status == SystemStsEnum.Warning)
  450. {
  451. FrmDialog.ShowDialog(this, "系统报警中!无法启动运行!", "警告", false);
  452. return;
  453. }
  454. this.Cursor = Cursors.WaitCursor;
  455. System.Windows.Forms.Application.DoEvents();
  456. if (SysMgr.Instance.StatusMgr.Status != SystemStsEnum.Pause)
  457. {
  458. ResetUI(false);
  459. AddTextEvent("运行", $"启动自动检测流程");
  460. this.lstLog.Refresh();
  461. string name = this.cbProductCode.Text;
  462. string batch = this.cbProductBatch.Text;
  463. string reel = this.cbProductReelNum.Text;
  464. string len = this.cbProductNum.Text;
  465. string defname = this.cbDefectCode.Text;
  466. if(string.IsNullOrEmpty(name))
  467. {
  468. name = "default";
  469. }
  470. if (SysMgr.Instance.StartRun(this, this.cbPdtSN.Text, ref defname, ref name, ref batch, ref reel, ref len))
  471. {
  472. this.tsbtnImpStop.Enabled = true;
  473. this.tsbtnStop.Enabled = true;
  474. this.tsbtnStart.Enabled = false;
  475. //锁定信息
  476. this.cbPdtSN.Enabled = false;
  477. this.cbProductCode.Enabled = false;
  478. this.cbProductBatch.Enabled = false;
  479. this.cbProductReelNum.Enabled = false;
  480. this.cbProductNum.Enabled = false;
  481. this.cbDefectCode.Enabled = false;
  482. //记录开始运行时间
  483. RunStartTime.Restart();
  484. //重新显示
  485. this.cbProductCode.Text = name;
  486. this.cbProductBatch.Text = batch;
  487. this.cbProductReelNum.Text = reel;
  488. this.cbProductNum.Text = len;
  489. this.cbDefectCode.Text = defname;
  490. ResetChart();
  491. }
  492. }
  493. else //暂停状态开始
  494. {
  495. if (SysMgr.Instance.ReStartRun())
  496. {
  497. this.tsbtnStop.Enabled = true;
  498. this.tsbtnStart.Enabled = false;
  499. }
  500. }
  501. this.Cursor = Cursors.Default;
  502. }
  503. }
  504. private void tsbtnReset_Click(object sender, EventArgs e)
  505. {
  506. }
  507. private void tsbtnStop_Click(object sender, EventArgs e)
  508. {
  509. AddTextEvent("暂停", "手动暂停");
  510. this.tsbtnStart.Enabled = true;
  511. SysMgr.Instance.Stop();
  512. SysMgr.Instance.StatusMgr.GotoPause();
  513. SysMgr.Instance.SendStatus();
  514. SysMgr.Instance.LedPause();
  515. }
  516. private void tsbtnWarning_Click(object sender, EventArgs e)
  517. {
  518. if (SysMgr.Instance.StatusMgr.Warning != WarningEnum.Normal)
  519. {
  520. AddTextEvent("清除", "手动清除报警");
  521. SysMgr.Instance.StatusMgr.ClearWarning();
  522. SysMgr.Instance.LedReady();
  523. if (SysMgr.Instance.IsAuto)
  524. SysMgr.Instance.StatusMgr.GotoPause();
  525. else
  526. SysMgr.Instance.StatusMgr.GotoNormalStandby();
  527. SysMgr.Instance.SendStatus();
  528. }
  529. }
  530. /// <summary>
  531. /// 结束验布
  532. /// </summary>
  533. /// <param name="sender"></param>
  534. /// <param name="e"></param>
  535. private void tsbtnImpStop_Click(object sender, EventArgs e)
  536. {
  537. AddTextEvent("结束验布", "手动结束验布");
  538. SysMgr.Instance.StatusMgr.GotoPause();
  539. SysMgr.Instance.DefectEnd(this);
  540. this.tsbtnStop.Enabled = false;
  541. this.tsbtnStart.Enabled = true;
  542. this.tsbtnImpStop.Enabled = false;
  543. SysMgr.Instance.StatusMgr.GotoNormalStandby();
  544. SysMgr.Instance.SendStatus();
  545. SysMgr.Instance.LedReady();
  546. }
  547. int cnnnt = 10;
  548. private void tsbGoHome_Click(object sender, EventArgs e)
  549. {
  550. #if false
  551. //测试散点
  552. chart1.Series.Clear();
  553. chart1.ChartAreas[0].AxisX.Title = "宽度(cm)";
  554. chart1.ChartAreas[0].AxisY.Title = "长度(m)";
  555. chart1.Series.Add("皱印");
  556. chart1.Series["皱印"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
  557. chart1.Series["皱印"].MarkerSize = 10;
  558. chart1.Series["皱印"].MarkerColor = Color.Red;
  559. chart1.Series["皱印"].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
  560. for (int i = 0; i < cnnnt; i++)
  561. this.chart1.Series["皱印"].Points.AddXY(i%5,i);
  562. chart1.Series.Add("划伤");
  563. chart1.Series["划伤"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
  564. chart1.Series["划伤"].MarkerSize = 10;
  565. chart1.Series["划伤"].MarkerColor = Color.Blue;
  566. chart1.Series["划伤"].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
  567. for (int i = 0; i < cnnnt; i++)
  568. this.chart1.Series["划伤"].Points.AddXY(i % 5 + 0.5, i + 0.3);
  569. chart1.ChartAreas[0].AxisY.Maximum = ((int)(this.chart1.Series["划伤"].Points.FindMaxByValue().YValues.Max() /10) + 1) * 10 + 5;
  570. chart1.ChartAreas[0].AxisY.ScaleView.Scroll(System.Windows.Forms.DataVisualization.Charting.ScrollType.Last);
  571. cnnnt +=20;
  572. MemoryStream ms = new MemoryStream();
  573. chart1.SaveImage(ms, ChartImageFormat.Bmp);
  574. Bitmap bmp = new Bitmap(ms);
  575. bmp.Save("111.jpg");
  576. ms.Close();
  577. #endif
  578. //测试散点
  579. chart3.Series.Clear();
  580. chart3.ChartAreas[0].AxisX.Title = "长度(m)";
  581. chart3.ChartAreas[0].AxisY.Title = "宽度(cm)";
  582. chart3.Series.Add("面宽");
  583. string tt = "面宽";
  584. if (chart3.Series.FindByName(tt) == null)
  585. chart3.Series.Add("面宽");
  586. else
  587. {
  588. chart3.Series["面宽"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
  589. chart3.Series["面宽"].MarkerSize = 5;
  590. chart3.Series["面宽"].MarkerColor = Color.Blue;
  591. chart3.Series["面宽"].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
  592. Random rm = new Random();
  593. for (int i = 0; i < cnnnt; i++)
  594. this.chart3.Series["面宽"].Points.AddXY(i, rm.Next(140, 160));
  595. cnnnt += 20;
  596. }
  597. //chart3.Series[0].f
  598. //chart3.ChartAreas["ChartArea1"].AxisX.ScaleView.Scroll(ScrollType.Last);
  599. }
  600. private void 本地数据库查询ToolStripMenuItem_Click(object sender, EventArgs e)
  601. {
  602. DataQueryFrm frm = new DataQueryFrm();
  603. frm.WindowState = FormWindowState.Maximized;
  604. frm.ShowDialog();
  605. }
  606. #endregion
  607. #region 显示图像和消息
  608. private void lstLog_DrawItem(object sender, DrawItemEventArgs e)
  609. {
  610. e.DrawBackground();
  611. if (e.Index < 0) return;
  612. try
  613. {
  614. string str = lstLog.Items[e.Index].ToString();
  615. e.Graphics.DrawString(str.Substring(1), e.Font,
  616. new SolidBrush(str[0] == 'R' ? Color.Red : (str[0] == 'Y' ? Color.Orange : Color.Black)),
  617. e.Bounds);
  618. }
  619. catch (Exception ex)
  620. {
  621. e.Graphics.DrawString(ex.Message, e.Font,
  622. new SolidBrush(Color.Red),
  623. e.Bounds);
  624. }
  625. }
  626. /// <summary>
  627. /// 异步输出日志
  628. /// </summary>
  629. /// <param name="tag">模板标识</param>
  630. /// <param name="msg">内容</param>
  631. /// <param name="type"></param>
  632. private void AddTextEvent(string tag, string msg, WarningEnum level = WarningEnum.Normal, bool Show = true)
  633. {
  634. lock (AddTextLock)
  635. {
  636. if (tag != null && tag != "")
  637. tag = $" - [{tag}]";
  638. var now = DateTime.Now;
  639. msg = now.ToString("HH:mm:ss fff") + tag + " - " + msg;
  640. msg = (level == WarningEnum.Normal ? "B" : level == WarningEnum.Low ? "Y" : "R") + msg;
  641. if (this.lstLog.Items.Count > 300)
  642. this.lstLog.Items.Clear();
  643. lstLog.Items.Insert(0, msg);
  644. }
  645. }
  646. private void main_running(object sender, MainEventArgs e)
  647. {
  648. ShowMessage(e);
  649. }
  650. private delegate void ShowMessageDelegate(MainEventArgs e);
  651. private void ShowMessage(MainEventArgs e)
  652. {
  653. if (InvokeRequired)
  654. {
  655. ShowMessageDelegate smd = new ShowMessageDelegate(ShowMessage);
  656. this.BeginInvoke(smd, e);
  657. }
  658. else
  659. {
  660. AddTextEvent(e.Tag, e.Message, e.Warning, e.show);
  661. }
  662. }
  663. /// <summary>
  664. /// 自动运行流程显示
  665. /// </summary>
  666. /// <param name="sender"></param>
  667. /// <param name="e"></param>
  668. private void auto_runing(object sender, RunEventArgs e)
  669. {
  670. ShowUIEvent(e);
  671. }
  672. private delegate void ShowUIEventDelegate(RunEventArgs e);
  673. Bitmap bmp0;
  674. Bitmap bmp1;
  675. Bitmap bmp2;
  676. private void ReDrawDefectPoints(List<object[]> DataRowlist)
  677. {
  678. foreach (var rowItem in DataRowlist)
  679. {
  680. this.skinDataGridView1.Rows.Insert(0, rowItem);
  681. if (chart1.Series.FindByName((string)rowItem[3]) == null)//rowItem[3] 为name
  682. {
  683. chart1.Series.Add((string)rowItem[3]);
  684. var obj = SysMgr.Instance.GetDefectLabel((string)rowItem[3]);
  685. int labelIndex = obj.Value<int>("id");
  686. chart1.Series[(string)rowItem[3]].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
  687. chart1.Series[(string)rowItem[3]].MarkerSize = 5;
  688. chart1.Series[(string)rowItem[3]].MarkerColor = ChartColor[labelIndex];
  689. chart1.Series[(string)rowItem[3]].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
  690. }
  691. chart1.Series[(string)rowItem[3]].Points.AddXY(rowItem[4], rowItem[5]);
  692. }
  693. //chart1.ChartAreas[0].AxisY.Maximum = ((int)(this.chart1.Series["划伤"].Points.FindMaxByValue().YValues.Max() / 10) + 1) * 10 + 5;
  694. chart1.ChartAreas[0].AxisY.ScaleView.Scroll(System.Windows.Forms.DataVisualization.Charting.ScrollType.Last);
  695. }
  696. private void LoadDefectLabelAndPrint(Product md, bool reLabel = false)
  697. {
  698. dataGridView1.Columns.Clear();
  699. DataTable dt = new DataTable(); //建立个数据表
  700. dt.Columns.Add(new DataColumn("二次判断项", typeof(string)));//在表中添加string类型的列
  701. dataGridView1.DataSource = dt;
  702. //插入按钮
  703. DataGridViewButtonColumn btn1 = new DataGridViewButtonColumn();
  704. btn1.Name = "Pause";
  705. btn1.HeaderText = "判断";
  706. //btn1.DefaultCellStyle.va = "启用";
  707. btn1.Text = "开启";
  708. btn1.UseColumnTextForButtonValue = false;
  709. btn1.DefaultCellStyle.NullValue = "开启";
  710. btn1.DefaultCellStyle.BackColor = Color.LimeGreen;
  711. dataGridView1.Columns.Add(btn1);
  712. DataGridViewButtonColumn btn2 = new DataGridViewButtonColumn();
  713. btn2.Name = "Print";
  714. btn2.HeaderText = "打标";
  715. btn2.Text = "开启";
  716. btn2.UseColumnTextForButtonValue = false;
  717. btn2.DefaultCellStyle.NullValue = "开启";
  718. btn2.DefaultCellStyle.BackColor = Color.LimeGreen;
  719. dataGridView1.Columns.Add(btn2);
  720. List<string> list = new List<string>();
  721. DataTable dts = (DataTable)dataGridView1.DataSource;
  722. dts.Clear();
  723. foreach (var item in md.QualifiedLimitList)
  724. {
  725. if (md.DefectPauseOption == null || md.DefectPauseOption.Count==0 || md.DefectPauseOption.Contains(item.Code))
  726. {
  727. //新增
  728. DataRow dr;//行
  729. dr = dts.NewRow();
  730. dr["二次判断项"] = item.NameCode;
  731. dts.Rows.Add(dr);//在表的对象的行里添加此行
  732. list.Add(item.NameCode);
  733. }
  734. }
  735. dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  736. dataGridView1.Columns[0].DefaultCellStyle.ForeColor = Color.Black;
  737. dataGridView1.Columns[0].DefaultCellStyle.SelectionForeColor = Color.Black;
  738. if (reLabel)
  739. SysMgr.Instance.LoadDefectItemsPuase(list);
  740. }
  741. private void ReDrawDefectPoints(List<DefectInfo> DataRowlist)
  742. {
  743. try
  744. {
  745. //重新绘制
  746. chart1.Series.Clear();
  747. chart1.Series.Add("SELECT");
  748. chart1.Series["SELECT"].ChartType = SeriesChartType.Point;
  749. chart1.Series["SELECT"].MarkerSize = 25;
  750. chart1.Series["SELECT"].MarkerColor = Color.Black;
  751. chart1.Series["SELECT"].MarkerStyle = MarkerStyle.Star6;
  752. //chart1.Series["SELECT"].Enabled = false;
  753. chart1.ChartAreas[0].AxisX.Title = "宽度(cm)";
  754. chart1.ChartAreas[0].AxisY.Title = "长度(m)";
  755. foreach (var rowItem in DataRowlist)
  756. {
  757. //this.skinDataGridView1.Rows.Insert(0, rowItem);
  758. if (chart1.Series.FindByName((string)rowItem.Name) == null)//rowItem[3] 为name
  759. {
  760. chart1.Series.Add((string)rowItem.Name);
  761. var obj = SysMgr.Instance.GetDefectLabel((string)rowItem.Name);
  762. int labelIndex = obj.Value<int>("id");
  763. chart1.Series[(string)rowItem.Name].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
  764. chart1.Series[(string)rowItem.Name].MarkerSize = 5;
  765. chart1.Series[(string)rowItem.Name].MarkerColor = ChartColor[labelIndex];
  766. chart1.Series[(string)rowItem.Name].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
  767. }
  768. chart1.Series[(string)rowItem.Name].Points.AddXY(rowItem.CentreX, rowItem.CentreY / 100);
  769. }
  770. //chart1.ChartAreas[0].AxisY.Maximum = ((int)(this.chart1.Series["划伤"].Points.FindMaxByValue().YValues.Max() / 10) + 1) * 10 + 5;
  771. chart1.ChartAreas[0].AxisY.ScaleView.Scroll(System.Windows.Forms.DataVisualization.Charting.ScrollType.Last);
  772. }
  773. catch
  774. { }
  775. }
  776. private void ShowUIEvent(RunEventArgs e)
  777. {
  778. if (InvokeRequired)
  779. {
  780. ShowUIEventDelegate smd = new ShowUIEventDelegate(ShowUIEvent);
  781. this.BeginInvoke(smd, e);
  782. }
  783. else
  784. {
  785. try
  786. {
  787. if (e.Cmd == 6) //显示主图
  788. {
  789. //this.BeginInvoke(new System.Action(() =>
  790. //{
  791. if (e.PicIndex == 1)
  792. {
  793. bmp1 = e.Pic.ToBitmap();
  794. //this.pictureBox2.Image = bmp1;
  795. //this.pictureBox2.Refresh();
  796. }
  797. else if (e.PicIndex == 2)
  798. {
  799. bmp2 = e.Pic.ToBitmap();
  800. //this.pictureBox3.Image = bmp2;
  801. //this.pictureBox3.Refresh();
  802. }
  803. else if (e.PicIndex == 0)
  804. {
  805. bmp0 = e.Pic.ToBitmap();
  806. this.pictureBox1.Image = bmp0;
  807. this.pictureBox1.Refresh();
  808. }
  809. //}));
  810. }
  811. else if (e.Cmd == 10) //测试结束
  812. {
  813. this.tsbtnStart.Enabled = true;
  814. this.tsbtnStop.Enabled = false;
  815. this.tsbtnImpStop.Enabled = false;
  816. this.cbPdtSN.Enabled = true;
  817. this.cbProductCode.Enabled = true;
  818. this.cbProductBatch.Enabled = true;
  819. this.cbProductReelNum.Enabled = true;
  820. this.cbProductNum.Enabled = true;
  821. this.cbDefectCode.Enabled = true;
  822. }
  823. else if (e.Cmd == 11) //显示缺陷列表
  824. {
  825. //this.BeginInvoke(new System.Action(() =>
  826. //{
  827. ReDrawDefectPoints(e.DataRowlist);
  828. //显示最新缺陷
  829. var defectInfo = SysMgr.Instance.GetDefectInfo((long)this.skinDataGridView1.Rows[0].Cells["colUid"].Value);
  830. if (defectInfo == null)
  831. {
  832. return;
  833. }
  834. //OpenCvSharp.Mat img = BitmapConverter.ToMat((Bitmap)defectInfo.image);
  835. //imageBox3.RefreshWindow(img, ImageBox.ImageModeEnum.Zoom);//可以不显示区域
  836. //this.imageBox3.Image = (defectInfo.image);
  837. //}));
  838. }
  839. else if(e.Cmd == 12) //显示处理图
  840. {
  841. //this.BeginInvoke(new System.Action(() =>
  842. //{
  843. bmp0 = (Bitmap)e.Defectimg.Clone();
  844. this.pictureBox1.Image = bmp0;
  845. this.pictureBox1.Refresh();
  846. //}));
  847. }
  848. else if (e.Cmd == 13) //显示门幅
  849. {
  850. //this.BeginInvoke(new System.Action(() =>
  851. //{
  852. List<float> ydata = new List<float>();
  853. this.lbFK.Text = "当前幅宽:" + $"{e.Points[e.Points.Count - 1][1]}";
  854. this.chart3.Series["面宽"].Points.Clear();
  855. foreach (var Item in e.Points)
  856. {
  857. ydata.Add(Item[1]);
  858. float x = Item[0] / 100; //cm -> m
  859. this.chart3.Series["面宽"].Points.AddXY((float)Math.Round(x, 2), (float)Math.Round(Item[1], 2));
  860. }
  861. chart3.ChartAreas[0].AxisY.Maximum = Math.Round(ydata.Max() + 10, 0);
  862. chart3.ChartAreas[0].AxisY.Minimum = Math.Round(ydata.Min() - 10, 0);
  863. //chart3.ChartAreas["ChartArea1"].AxisX.ScaleView.Scroll(ScrollType.Last);
  864. //}));
  865. }
  866. else if (e.Cmd == 14) //显示速度
  867. {
  868. //this.BeginInvoke(new System.Action(() =>
  869. //{
  870. lblDefectLen.Text = $"检测长度: {Math.Round(e.Len, 2)} 米";
  871. lblDefectSpeed.Text = $"检测速度: {Math.Round(e.Speed,2)} 米/分钟";
  872. //}));
  873. }
  874. else if (e.Cmd == 15) //显示处理图 new
  875. {
  876. //this.BeginInvoke(new System.Action(() =>
  877. //{
  878. bmp0 = (Bitmap)e.DefectMat.ToBitmap();
  879. this.pictureBox1.Image = bmp0;
  880. this.pictureBox1.Refresh();
  881. //}));
  882. }
  883. else if (e.Cmd == 16) //显示缓存
  884. {
  885. this.tsState.Text = $"相机1:{e.Cam1ImgCnt},相机2:{e.Cam2ImgCnt}";
  886. this.tslabelTime.Text = e._DefinfoCnt;
  887. //this.stsSaveQueue.Text = $"存图队列:{e.SaveImgCnt}";
  888. }
  889. else if (e.Cmd == 17) //显示光源亮度
  890. {
  891. this.ucTrackBar1.Value = e.LightValue;
  892. }
  893. else if (e.Cmd == 18) //显示二次判断和打标
  894. {
  895. //this.BeginInvoke(new System.Action(() =>
  896. //{
  897. // LoadDefectLabelAndPrint(e.MD);
  898. //}));
  899. }
  900. else if (e.Cmd == 19) // 二次判定数据修改
  901. {
  902. //不能使用同步Invoke方式,会使相机超时丢帧
  903. this.BeginInvoke(new System.Action(() =>
  904. {
  905. reFreashFrm(e.ImgIndex, e.LstEditDefect, e.Records, e.LstEditInfo);
  906. #if false
  907. int liDefectCount = e.LstEditDefect.Count;
  908. if(e.LstEditInfo != null || e.LstEditDefect != null)
  909. {
  910. string oldCode;
  911. for (int i = 0; i < this.skinDataGridView1.Rows.Count; i++)
  912. {
  913. if ((int)this.skinDataGridView1.Rows[i].Cells["colIndex"].Value != e.ImgIndex)
  914. continue;
  915. long uid = (long)this.skinDataGridView1.Rows[i].Cells["colUid"].Value;
  916. foreach (var row in e.LstEditDefect)
  917. {
  918. //AddTextEvent($"二次确认", $"修改第({i + 1})行瑕疵名称,{uid} {row.uid}");
  919. if (row.uid == uid)
  920. {
  921. oldCode = this.skinDataGridView1.Rows[i].Cells["colCode"].Value.ToString();
  922. //AddTextEvent($"二次确认", $"修改第({i + 1})行瑕疵名称 ({this.skinDataGridView1.Rows[i].Cells["colDefectName"].Value})->({row.Name})");
  923. this.skinDataGridView1.Rows[i].Cells["colCode"].Value = row.Code;
  924. this.skinDataGridView1.Rows[i].Cells["colDefectName"].Value = row.Name;
  925. //this.uiDataGridView1.Refresh();
  926. if (!string.IsNullOrWhiteSpace(row.TagFilePath) && oldCode != row.Code)
  927. File.Move(row.TagFilePath, row.TagFilePath.Replace($"_类别{oldCode}", $"_类别{row.Code}"));//
  928. break;
  929. }
  930. }
  931. if (e.LstEditInfo != null)
  932. {
  933. foreach (var item in e.LstEditInfo)
  934. {
  935. if (item.uid == uid)
  936. {
  937. this.skinDataGridView1.Rows.RemoveAt(i);
  938. i--;
  939. break;
  940. }
  941. }
  942. }
  943. }
  944. //SysMgr.Instance.DelDefectEdit(e.Records, frmDefect.lstDel);
  945. //double len = (double)this.lblLen.Tag;
  946. //this.reDrawDefectPoints(curRecord.DefectInfoList, new double[] { 0, Math.Round(curRecord.FaceWidthMax + 0.005f, 2) }, new double[] { 0, len });
  947. //AddTextEvent($"二次检测", $"本次忽略{frmDefect.lstDel.Count}个瑕疵,本张图由{liDefectCount} -> {e.LstEditDefect.Count},总数{e.Records.DefectInfoList.Count}");
  948. }
  949. //this.uiMiniPagination1.TotalCount = curRecord.DefectTotalCount = curRecord.DefectInfoList.Count;
  950. //
  951. //double len = Math.Round((res.photoIndex + 1) * bmpHeight * 1.0d / Config.cm2px_y + 0.005f, 2);
  952. //重新绘制分布图
  953. ReDrawDefectPoints(e.Records.DefectInfoList);
  954. //自动继续运行设备(这里临时暂停后不能再急停,否则无法继续)
  955. SysMgr.Instance.StartMotion();
  956. #endif
  957. }));
  958. }
  959. else if (e.Cmd == 20) //显示存图缓存
  960. {
  961. //this.tsState.Text = $"相机1:{e.Cam1ImgCnt},相机2:{e.Cam2ImgCnt}";
  962. this.stsSaveQueue.Text = $"存图队列:{e.SaveImgCnt}";
  963. }
  964. else if (e.Cmd == 21) //显示二次判定界面
  965. {
  966. this.BeginInvoke(new System.Action(() =>
  967. {
  968. var pimage = (Bitmap)e.TagImg.ToBitmap().Clone();
  969. List<DefectInfo> ld = new List<DefectInfo>();
  970. foreach (var item in e.LstEditDefect)
  971. {
  972. ld.Add(item);
  973. }
  974. int index = e.ImgIndex;
  975. //Task.Run(() =>
  976. //{
  977. int liDefectCount = ld.Count;
  978. ImageShowFrm frmDefect = new ImageShowFrm(index, ld, pimage);
  979. if (frmDefect.ShowDialog() == DialogResult.OK)
  980. {
  981. SysMgr.Instance.DelDefectEdit(e.Records, frmDefect.lstDel);
  982. reFreashFrm(index, ld, e.Records, frmDefect.lstDel);
  983. AddTextEvent($"二次检测2", $"本次忽略{frmDefect.lstDel.Count}个瑕疵,本张图由{liDefectCount} -> {ld.Count},总数{e.Records.DefectInfoList.Count}");
  984. }
  985. else
  986. {
  987. reFreashFrm(index, ld, e.Records, null);
  988. }
  989. //});
  990. }));
  991. }
  992. //Application.DoEvents();
  993. }
  994. catch { }
  995. }
  996. }
  997. #endregion
  998. #region 二次确认界面刷新
  999. void reFreashFrm(int index, List<DefectInfo> lstEditDefect, Records currRecord, List<DefectInfo> lstEditInfo)
  1000. {
  1001. int err = 0;
  1002. try
  1003. {
  1004. //int liDefectCount = lstEditDefect.Count;
  1005. //ImageShowFrm frmDefect = new ImageShowFrm(e.LstEditDefect, e.TagImg.ToBitmap());
  1006. //frmDefect.Show();
  1007. //if (frmDefect.ShowDialog() == DialogResult.OK)
  1008. if (lstEditInfo != null || lstEditDefect != null)
  1009. {
  1010. err = 1;
  1011. string oldCode;
  1012. for (int i = 0; i < this.skinDataGridView1.Rows.Count; i++)
  1013. {
  1014. if ((int)this.skinDataGridView1.Rows[i].Cells["colIndex"].Value != index)
  1015. continue;
  1016. err = 2;
  1017. long uid = (long)this.skinDataGridView1.Rows[i].Cells["colUid"].Value;
  1018. foreach (var row in lstEditDefect)
  1019. {
  1020. //AddTextEvent($"二次确认", $"修改第({i + 1})行瑕疵名称,{uid} {row.uid}");
  1021. if (row.uid == uid)
  1022. {
  1023. err = 3;
  1024. oldCode = this.skinDataGridView1.Rows[i].Cells["colCode"].Value.ToString();
  1025. //AddTextEvent($"二次确认", $"修改第({i + 1})行瑕疵名称 ({this.skinDataGridView1.Rows[i].Cells["colDefectName"].Value})->({row.Name})");
  1026. this.skinDataGridView1.Rows[i].Cells["colCode"].Value = row.Code;
  1027. this.skinDataGridView1.Rows[i].Cells["colDefectName"].Value = row.Name;
  1028. //this.uiDataGridView1.Refresh();
  1029. if (!string.IsNullOrWhiteSpace(row.TagFilePath) && oldCode != row.Code)
  1030. File.Move(row.TagFilePath, row.TagFilePath.Replace($"_类别{oldCode}", $"_类别{row.Code}"));//
  1031. break;
  1032. }
  1033. }
  1034. err = 4;
  1035. if (lstEditInfo != null)
  1036. {
  1037. foreach (var item in lstEditInfo)
  1038. {
  1039. if (item.uid == uid)
  1040. {
  1041. this.skinDataGridView1.Rows.RemoveAt(i);
  1042. i--;
  1043. break;
  1044. }
  1045. }
  1046. }
  1047. }
  1048. //SysMgr.Instance.DelDefectEdit(e.Records, frmDefect.lstDel);
  1049. //double len = (double)this.lblLen.Tag;
  1050. //this.reDrawDefectPoints(curRecord.DefectInfoList, new double[] { 0, Math.Round(curRecord.FaceWidthMax + 0.005f, 2) }, new double[] { 0, len });
  1051. //AddTextEvent($"二次检测", $"本次忽略{frmDefect.lstDel.Count}个瑕疵,本张图由{liDefectCount} -> {e.LstEditDefect.Count},总数{e.Records.DefectInfoList.Count}");
  1052. }
  1053. //this.uiMiniPagination1.TotalCount = curRecord.DefectTotalCount = curRecord.DefectInfoList.Count;
  1054. //
  1055. //double len = Math.Round((res.photoIndex + 1) * bmpHeight * 1.0d / Config.cm2px_y + 0.005f, 2);
  1056. //重新绘制分布图
  1057. err = 5;
  1058. ReDrawDefectPoints(currRecord.DefectInfoList);
  1059. err = 6;
  1060. //自动继续运行设备(这里临时暂停后不能再急停,否则无法继续)
  1061. SysMgr.Instance.StartMotion();
  1062. }catch(Exception ex)
  1063. {
  1064. MessageBox.Show($"err {err}:{ex.Message}");
  1065. }
  1066. }
  1067. #endregion
  1068. #region 后台
  1069. GetPN pn = new GetPN();
  1070. private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  1071. {
  1072. while (backgroundWorkerDo)
  1073. {
  1074. try
  1075. {
  1076. this.Invoke(new MethodInvoker(() =>
  1077. {
  1078. this.tsslLoginTime.Text = $" 当前时间:{DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss")}";
  1079. }));
  1080. //检测按钮和实时状态
  1081. if (SysMgr.Instance.IsInit)
  1082. {
  1083. bool sts = SysMgr.Instance.WarningShowLed(false);
  1084. if (sts)
  1085. {
  1086. this.Invoke(new MethodInvoker(() =>
  1087. {
  1088. this.tsbtnStart.Enabled = true;
  1089. this.tsbtnStop.Enabled = false;
  1090. }));
  1091. }
  1092. int stsno = 0;
  1093. stsno = SysMgr.Instance.ButtonIOTrg(false);
  1094. if (stsno == 1)
  1095. {
  1096. this.Invoke(new MethodInvoker(() =>
  1097. {
  1098. tsbtnStart_Click(null, null);
  1099. }));
  1100. }
  1101. else if (stsno == 2)
  1102. {
  1103. this.Invoke(new MethodInvoker(() =>
  1104. {
  1105. this.tsbtnStart.Enabled = true;
  1106. }));
  1107. SysMgr.Instance.LedPause();
  1108. }
  1109. else if (stsno == 3)
  1110. {
  1111. this.Invoke(new MethodInvoker(() =>
  1112. {
  1113. tsbtnReset_Click(null, null);
  1114. }));
  1115. }
  1116. }
  1117. if (pn.P(SysMgr.Instance.IsRuning))
  1118. {
  1119. //继续计时
  1120. RunStartTime.Start();
  1121. }
  1122. if (pn.N(SysMgr.Instance.IsRuning))
  1123. {
  1124. //停止计时
  1125. RunStartTime.Stop();
  1126. }
  1127. if (SysMgr.Instance.IsRuning)
  1128. {
  1129. ////实时显示
  1130. //RunStartTime.Stop();
  1131. //this.Invoke(new MethodInvoker(() =>
  1132. //{
  1133. // tslabelTime.Text = "实时测试:" + ((double)RunStartTime.ElapsedMilliseconds / 1000.0).ToString("0.000") + "s";
  1134. // //this.Refresh();
  1135. //}));
  1136. //RunStartTime.Start();
  1137. }
  1138. Thread.Sleep(100);
  1139. //Application.DoEvents();
  1140. }
  1141. catch { }
  1142. }
  1143. }
  1144. /// <summary>
  1145. /// 信号上升沿下降沿捕获
  1146. /// </summary>
  1147. class GetPN
  1148. {
  1149. bool _P = false;
  1150. bool _N = false;
  1151. /// <summary>
  1152. /// 判断上升沿
  1153. /// </summary>
  1154. /// <param name="Value"></param>
  1155. /// <returns></returns>
  1156. public bool P(bool Value)
  1157. {
  1158. if (Value && !_P)
  1159. {
  1160. _P = true;
  1161. return true;
  1162. }
  1163. if (!Value)
  1164. _P = false;
  1165. return false;
  1166. }
  1167. /// <summary>
  1168. /// 判断下降沿
  1169. /// </summary>
  1170. /// <param name="Value"></param>
  1171. /// <returns></returns>
  1172. public bool N(bool Value)
  1173. {
  1174. if (!Value && _N)
  1175. {
  1176. _N = false;
  1177. return true;
  1178. }
  1179. if (Value)
  1180. _N = true;
  1181. return false;
  1182. }
  1183. }
  1184. #endregion
  1185. #region 表格点击
  1186. private void skinDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  1187. {
  1188. if (e.RowIndex < 0)
  1189. return;
  1190. var defectInfo = SysMgr.Instance.GetDefectInfo((long)this.skinDataGridView1.CurrentRow.Cells["colUid"].Value);
  1191. if (defectInfo == null)
  1192. {
  1193. DialogResult dr = DialogResult.Cancel;
  1194. dr = FrmDialog.ShowDialog(this, "此记录已不存在!", "警告", true);
  1195. return;
  1196. }
  1197. OpenCvSharp.Mat img = defectInfo.image;
  1198. imageBox3.RefreshWindow(img, ImageBox.ImageModeEnum.Zoom);//可以不显示区域
  1199. //this.pictureBox4.Image = (defectInfo.image);
  1200. //选中
  1201. this.Invoke(new System.Action(() =>
  1202. {
  1203. if (chart1.Series != null && chart1.Series.FindByName("SELECT") != null)
  1204. {
  1205. chart1.Series["SELECT"].Points.Clear();
  1206. chart1.Series["SELECT"].Points.AddXY(defectInfo.CentreX, defectInfo.CentreY / 100);
  1207. chart1.Refresh();
  1208. }
  1209. }));
  1210. }
  1211. #endregion
  1212. #region 二次判断
  1213. private void ucSwitch1_CheckedChanged(object sender, EventArgs e)
  1214. {
  1215. SysMgr.Instance.SetDefectPause(ucSwitch1.Checked);
  1216. }
  1217. #endregion
  1218. #region 打标
  1219. private void ucSwitch2_CheckedChanged(object sender, EventArgs e)
  1220. {
  1221. SysMgr.Instance.SetDefectDB(ucSwitch2.Checked);
  1222. }
  1223. #endregion
  1224. #region 控制光源
  1225. private void ucTrackBar1_ValueChanged(object sender, EventArgs e)
  1226. {
  1227. SysMgr.Instance.SetLightValue((int)ucTrackBar1.Value);
  1228. }
  1229. #endregion
  1230. #region 二次判断和打标表格点击
  1231. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  1232. {
  1233. //for (int i = 0; i < this.dataGridView1.RowCount; i++)
  1234. //{
  1235. // namelist.Add(this.dataGridView1.Rows[i].Cells[0].Value.ToString());
  1236. //}
  1237. int Index = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引
  1238. if (Index < this.dataGridView1.Rows.Count && this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString() == "判断")
  1239. {
  1240. List<string> namelist = new List<string>();
  1241. DataGridViewButtonCell btn = (DataGridViewButtonCell)this.dataGridView1.CurrentCell;
  1242. if (btn.FormattedValue.ToString() == "开启")
  1243. {
  1244. this.dataGridView1.CurrentCell.Value = "禁用";
  1245. this.dataGridView1.CurrentCell.Style.BackColor = Color.Red;
  1246. this.dataGridView1.CurrentCell.Style.ForeColor = Color.Red;
  1247. }
  1248. else
  1249. {
  1250. this.dataGridView1.CurrentCell.Value = "开启";
  1251. this.dataGridView1.CurrentCell.Style.BackColor = Color.LimeGreen;
  1252. this.dataGridView1.CurrentCell.Style.ForeColor = Color.Black;
  1253. }
  1254. for (int i = 0; i < this.dataGridView1.RowCount; i++)
  1255. {
  1256. DataGridViewButtonCell btn2 = (DataGridViewButtonCell)this.dataGridView1.Rows[i].Cells[1];
  1257. if (btn2.FormattedValue.ToString() == "开启")
  1258. namelist.Add(this.dataGridView1.Rows[i].Cells[0].Value.ToString());
  1259. }
  1260. //开启该项二次判断功能
  1261. SysMgr.Instance.LoadDefectItemsPuase(namelist);
  1262. }
  1263. else if (Index < this.dataGridView1.Rows.Count && this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString() == "打标")
  1264. {
  1265. List<string> namelist = new List<string>();
  1266. DataGridViewButtonCell btn = (DataGridViewButtonCell)this.dataGridView1.CurrentCell;
  1267. if (btn.FormattedValue.ToString() == "开启")
  1268. {
  1269. this.dataGridView1.CurrentCell.Value = "禁用";
  1270. this.dataGridView1.CurrentCell.Style.BackColor = Color.Red;
  1271. this.dataGridView1.CurrentCell.Style.ForeColor = Color.Red;
  1272. }
  1273. else
  1274. {
  1275. this.dataGridView1.CurrentCell.Value = "开启";
  1276. this.dataGridView1.CurrentCell.Style.BackColor = Color.LimeGreen;
  1277. this.dataGridView1.CurrentCell.Style.ForeColor = Color.Black;
  1278. }
  1279. for (int i = 0; i < this.dataGridView1.RowCount; i++)
  1280. {
  1281. DataGridViewButtonCell btn2 = (DataGridViewButtonCell)this.dataGridView1.Rows[i].Cells[1];
  1282. if (btn2.FormattedValue.ToString() == "开启")
  1283. namelist.Add(this.dataGridView1.Rows[i].Cells[0].Value.ToString());
  1284. }
  1285. //开启该项打标功能
  1286. SysMgr.Instance.LoadDefectItemsDB(namelist);
  1287. }
  1288. }
  1289. #endregion
  1290. #region 选检测标准之后自动加载二次判断
  1291. private Service.ProductService PdtService = new Service.ProductService();
  1292. private void cbDefectCode_SelectedIndexChanged(object sender, EventArgs e)
  1293. {
  1294. Product model = SysMgr.Instance.GetProduct(cbDefectCode.Text);
  1295. if (model != null)
  1296. {
  1297. LoadDefectLabelAndPrint(model, true);
  1298. }
  1299. }
  1300. #endregion
  1301. #region 模拟键盘
  1302. private KeyBoardType keyBoardType = KeyBoardType.UCKeyBorderAll_EN;
  1303. [Description("键盘打开样式"), Category("自定义")]
  1304. public KeyBoardType KeyBoardType
  1305. {
  1306. get { return keyBoardType; }
  1307. set { keyBoardType = value; }
  1308. }
  1309. private void showKeyPanel(Control ctl)
  1310. {
  1311. //keyPanel.Top = ctl.Top + ctl.Height + 5;
  1312. //keyPanel.Left = ctl.Left;
  1313. //keyPanel.Visible = true;
  1314. //keyPanel.BringToFront();
  1315. process = Process.Start(@"C:\WINDOWS\system32\osk.exe");
  1316. }
  1317. //Froms.FrmAnchor m_frmAnchor;
  1318. private void cbPdtSN_Click(object sender, EventArgs e)
  1319. {
  1320. showKeyPanel((Control)sender);
  1321. }
  1322. #endregion
  1323. private void skinDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  1324. {
  1325. }
  1326. private void tsmSysmgr_Click(object sender, EventArgs e)
  1327. {
  1328. }
  1329. }
  1330. }