革博士V2程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

473 líneas
23 KiB

  1. using DocumentFormat.OpenXml.EMMA;
  2. using GeBoShi.SysCtrl;
  3. using GeBoShi.UI.InageShow;
  4. using HZH_Controls.Controls;
  5. using Models;
  6. using Newtonsoft.Json.Linq;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Data;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Security.Policy;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows.Forms;
  18. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  19. namespace GeBoShi.UI.Product
  20. {
  21. public partial class ProductInfoFrm : Form
  22. {
  23. Service.ProductService service = new Service.ProductService();
  24. Models.Product model = new Models.Product();
  25. public ProductInfoFrm(Models.Product m = null)
  26. {
  27. InitializeComponent();
  28. UIStyle.SetUIStyle(this);
  29. this.uiTitel1.FatherForm = this;
  30. //this.TrackBarLightValue.Location.Y = label2.Location.Y;
  31. #region dataGridView设置
  32. dataGridView1.AllowUserToAddRows = dataGridView1.AllowUserToDeleteRows = false;//用户添加删除行
  33. dataGridView2.AllowUserToAddRows = dataGridView2.AllowUserToDeleteRows = false;
  34. dataGridView1.AllowUserToResizeRows = dataGridView2.AllowUserToResizeRows = false;//用户调整行大小
  35. //dataGridView1.AllowUserToResizeColumns = false;//用户调整列大小
  36. //显示行号与列宽度自动调整
  37. dataGridView1.RowHeadersVisible = dataGridView2.RowHeadersVisible = true;
  38. dataGridView1.RowHeadersWidth = dataGridView2.RowHeadersWidth = 50;
  39. //dataGridView1.ColumnHeadersHeightSizeMode = dataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  40. dataGridView1.RowHeadersWidthSizeMode = dataGridView2.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变
  41. dataGridView1.RowTemplate.Height = dataGridView2.RowTemplate.Height = 30;
  42. #endregion
  43. initData();
  44. if (m != null)
  45. {
  46. model = m;
  47. //显示模型
  48. foreach (string onnxFile in this.cmbDefectModelFile.Items)
  49. if (!string.IsNullOrWhiteSpace(model.ModelName) && onnxFile.ToLower() == model.ModelName.ToLower())
  50. this.cmbDefectModelFile.SelectedItem = model.ModelName;
  51. numColorNo.Value = model.Color;
  52. txtName.Text = model.Name;
  53. cmbClasses.Text = model.Material;
  54. tbColorName.Text = model.ColorName;
  55. int[] rgb = new int[3];
  56. if (!string.IsNullOrEmpty(model.ColorValue))
  57. {
  58. for (int i = 0; i < rgb.Length; i++)
  59. rgb[i] = Convert.ToInt32(model.ColorValue.Split(',')[i]);
  60. tbColorName.ForeColor = Color.FromArgb(rgb[0], rgb[1], rgb[2]);
  61. }
  62. tbSpec.Text = model.Spec;
  63. TrackBarLightValue.Value = model.LightValue;
  64. TrackBarExposureTime.Value = (int)model.ExposureTime;
  65. TrackBarGain.Value = (int)model.Gain;
  66. TrackBarTensionValue.Value = (int)model.TensionValue;
  67. numStopDis.Value = (decimal)model.ThicknessDetectionStopDis;
  68. numReelLen.Value = (decimal)model.residueWarnningLen;
  69. numDefectAreaLimit.Value = (decimal)model.DefectAreaLimit;
  70. numDefectCntLength.Value = (decimal)model.DefectCntLength;
  71. numDefectCountLimit.Value = (decimal)model.DefectCountLimit;
  72. tbWarnDefect.Text = model.WarnDefect;
  73. cbGetHD.Checked = model.OpenThicknessDetection;
  74. numStopDis.Value = (int)model.ThicknessDetectionStopDis;
  75. }
  76. }
  77. private void initData()
  78. {
  79. //模型文件
  80. string strDefectModelFile = ConfMgr.Instance.SysConfigParams.AIModelPath;
  81. if(!Directory.Exists(strDefectModelFile))
  82. {
  83. MessageBox.Show("模型路径错误:" + strDefectModelFile, "警告");
  84. return;
  85. }
  86. string[] onnxFiles = Directory.GetFiles(strDefectModelFile, "*.trt");
  87. //标签文件
  88. string[] labelFiles = Directory.GetFiles(strDefectModelFile, "*.json");
  89. string errorStr = "";
  90. foreach (string onnxFile in onnxFiles)
  91. {
  92. string onlyName;
  93. onlyName = Path.GetFileName(onnxFile);
  94. cmbDefectModelFile.Items.Add(onlyName);
  95. string findname = onlyName.Replace(".trt", ".json");
  96. //存在对应label文件
  97. if (labelFiles.Count(p => Path.GetFileName(p) == findname) <= 0)
  98. {
  99. errorStr += $"{onlyName},";
  100. }
  101. }
  102. if (!string.IsNullOrEmpty(errorStr))
  103. MessageBox.Show("模型缺少词典文件:" + errorStr, "警告");
  104. //加载材质
  105. string configPath = ConfMgr.Instance.ConfigDir + $"\\material.json";
  106. if (File.Exists(configPath))
  107. {
  108. string lsTmp = File.ReadAllText(configPath);
  109. JArray defectItemList = JArray.Parse(lsTmp);
  110. cmbClasses.Items.Clear();
  111. foreach (JObject item in defectItemList)
  112. {
  113. string name = item.Value<string>("name");
  114. cmbClasses.Items.Add(name);
  115. }
  116. }
  117. else
  118. MessageBox.Show("模型材质文件:" + configPath, "警告");
  119. }
  120. private void cmbClasses_SelectedIndexChanged(object sender, EventArgs e)
  121. {
  122. }
  123. private void ProductInfoFrm_Load(object sender, EventArgs e)
  124. {
  125. }
  126. private void ProductInfoFrm_SizeChanged(object sender, EventArgs e)
  127. {
  128. if (this.WindowState == FormWindowState.Maximized)
  129. {
  130. this.WindowState = FormWindowState.Normal;
  131. this.Top = 0;
  132. this.Left = 0;
  133. this.Width = SystemInformation.WorkingArea.Width;
  134. this.Height = SystemInformation.WorkingArea.Height;
  135. }
  136. }
  137. private void tsbtnExit_Click(object sender, EventArgs e)
  138. {
  139. this.Close();
  140. }
  141. private void tsbtnSave_Click(object sender, EventArgs e)
  142. {
  143. try
  144. {
  145. if (this.cmbClasses.SelectedIndex < 0) throw new Exception("请选择材质!");
  146. if (string.IsNullOrEmpty(this.txtName.Text)) throw new Exception("请填写名称!");
  147. if (string.IsNullOrEmpty(this.tbColorName.Text)) throw new Exception("请填写颜色!");
  148. if (this.cmbDefectModelFile.SelectedIndex < 0) throw new Exception("请选择模型!");
  149. model.Code = $"2.0-{txtName.Text}-{cmbClasses.Text}";
  150. model.Color = (int)numColorNo.Value;
  151. model.Name = txtName.Text;
  152. model.Material = cmbClasses.Text;
  153. model.ColorName = tbColorName.Text;
  154. model.ModelName = cmbDefectModelFile.Text;
  155. model.Spec = tbSpec.Text;
  156. model.LightValue = (int)TrackBarLightValue.Value;
  157. model.ExposureTime = (double)TrackBarExposureTime.Value;
  158. model.Gain = (double)TrackBarGain.Value;
  159. model.TensionValue = (double)TrackBarTensionValue.Value;
  160. model.OpenThicknessDetection = cbGetHD.Checked;
  161. model.ThicknessDetectionStopDis = (int)numStopDis.Value;
  162. model.DefectAreaLimit = (int)numDefectAreaLimit.Value;
  163. model.DefectCntLength = (double)numDefectCntLength.Value;
  164. model.DefectCountLimit = (int)numDefectCountLimit.Value;
  165. model.WarnDefect = tbWarnDefect.Text;
  166. model.residueWarnningLen = (double)numReelLen.Value;
  167. //缺陷阈值
  168. if (model.QualifiedLimitList == null)
  169. model.QualifiedLimitList = new List<Models.QualifiedLimit>();
  170. else
  171. model.QualifiedLimitList.Clear();
  172. QualifiedLimit qualifiedLimit = new QualifiedLimit();
  173. string configPath = ConfMgr.Instance.SysConfigParams.AIModelPath + $"\\{tbLabelFile.Text}";
  174. string lsTmp = File.ReadAllText(configPath);
  175. JArray defectItemList = JArray.Parse(lsTmp);
  176. for (int i = 0; i < dataGridView1.Rows.Count; i++)
  177. {
  178. string code2 = dataGridView1.Rows[i].Cells["Code"].Value.ToString();
  179. string nameCode = defectItemList.FirstOrDefault(x => x.Value<string>("code") == code2).Value<string>("name");
  180. qualifiedLimit = new Models.QualifiedLimit()
  181. {
  182. Code = dataGridView1.Rows[i].Cells["Code"].Value.ToString(),
  183. ZXD = Utils.IsDecimal(dataGridView1.Rows[i].Cells["ZXD"].Value) ? Convert.ToDouble(dataGridView1.Rows[i].Cells["ZXD"].Value) : 0,
  184. Area = Utils.IsDecimal(dataGridView1.Rows[i].Cells["Area"].Value) ? Convert.ToDouble(dataGridView1.Rows[i].Cells["Area"].Value) : 0,
  185. ContrastLower = Utils.IsDecimal(dataGridView1.Rows[i].Cells["DBDL"].Value) ? Utils.PercentToContrast(Convert.ToDouble(dataGridView1.Rows[i].Cells["DBDL"].Value)) : 0,
  186. ContrastTop = Utils.IsDecimal(dataGridView1.Rows[i].Cells["DBDH"].Value) ? Utils.PercentToContrast(Convert.ToDouble(dataGridView1.Rows[i].Cells["DBDH"].Value)) : 0,
  187. IsOR = Convert.ToBoolean(dataGridView1.Rows[i].Cells["OrAnd"].Value),
  188. NameCode = nameCode,
  189. DefectWarnLength = Utils.IsNumber(dataGridView1.Rows[i].Cells["DefectLength"].Value) ? Convert.ToInt32(dataGridView1.Rows[i].Cells["DefectLength"].Value) : 0,
  190. DefectWarnCnt = Utils.IsDecimal(dataGridView1.Rows[i].Cells["DefectWarn"].Value) ? Convert.ToInt32(dataGridView1.Rows[i].Cells["DefectWarn"].Value) : 0,
  191. ModifyUserCode = SysMgr.Instance.UserMgr.LoginUser.Code,
  192. CreateUserCode = SysMgr.Instance.UserMgr.LoginUser.Code
  193. };
  194. if (qualifiedLimit.ContrastLower + qualifiedLimit.ContrastTop > 0 && qualifiedLimit.ContrastTop < qualifiedLimit.ContrastLower)
  195. throw new Exception($"检测标准中第{i + 1}行中对比度上限值({qualifiedLimit.ContrastTop})不可小于下限值({qualifiedLimit.ContrastLower})!");
  196. model.QualifiedLimitList.Add(qualifiedLimit);
  197. }
  198. //产品等级
  199. if (model.GradeLimitList == null)
  200. model.GradeLimitList = new List<Models.GradeLimit>();
  201. else
  202. model.GradeLimitList.Clear();
  203. for (int i = 0; i < dataGridView2.Rows.Count; i++)
  204. {
  205. model.GradeLimitList.Add(
  206. new Models.GradeLimit()
  207. {
  208. Code = dataGridView2.Rows[i].Cells["Code2"].Value.ToString(),
  209. A = Utils.IsNumber(dataGridView2.Rows[i].Cells["A"].Value) ? Convert.ToInt32(dataGridView2.Rows[i].Cells["A"].Value) : 0,
  210. B = Utils.IsNumber(dataGridView2.Rows[i].Cells["B"].Value) ? Convert.ToInt32(dataGridView2.Rows[i].Cells["B"].Value) : 0,
  211. C = Utils.IsNumber(dataGridView2.Rows[i].Cells["C"].Value) ? Convert.ToInt32(dataGridView2.Rows[i].Cells["C"].Value) : 0,
  212. D = Utils.IsNumber(dataGridView2.Rows[i].Cells["D"].Value) ? Convert.ToInt32(dataGridView2.Rows[i].Cells["D"].Value) : 0,
  213. E = Utils.IsNumber(dataGridView2.Rows[i].Cells["E"].Value) ? Convert.ToInt32(dataGridView2.Rows[i].Cells["E"].Value) : 0,
  214. ModifyUserCode = SysMgr.Instance.UserMgr.LoginUser.Code,
  215. CreateUserCode = SysMgr.Instance.UserMgr.LoginUser.Code
  216. });
  217. }
  218. //
  219. model.ModifyUserCode = SysMgr.Instance.UserMgr.LoginUser.Code;
  220. bool result;
  221. if (model.Id == 0)
  222. {
  223. model.CreateUserCode = SysMgr.Instance.UserMgr.LoginUser.Code;
  224. result = service.InsertNav(model);
  225. }
  226. else
  227. {
  228. result = service.UpdateNav(model);
  229. }
  230. if (!result)
  231. throw new Exception("数据保存失败!");
  232. else
  233. MessageBox.Show("保存成功!", "保存");
  234. }
  235. catch (Exception ex)
  236. {
  237. MessageBox.Show("保存出错:" + ex.Message, "警告");
  238. }
  239. }
  240. private void cmbDefectModelFile_TextChanged(object sender, EventArgs e)
  241. {
  242. try
  243. {
  244. tbLabelFile.Text = cmbDefectModelFile.Text.Replace(".trt", ".json");
  245. //加载缺陷
  246. string configPath = ConfMgr.Instance.SysConfigParams.AIModelPath + $"\\{tbLabelFile.Text}";
  247. string lsTmp = File.ReadAllText(configPath);
  248. JArray defectItemList = JArray.Parse(lsTmp);
  249. //加行
  250. dataGridView1.Rows.Clear();
  251. dataGridView2.Rows.Clear();
  252. cbDefectName.Items.Clear();
  253. foreach (JObject item in defectItemList)
  254. {
  255. string code = item.Value<string>("code");
  256. string name = item.Value<string>("name");
  257. //color = item.Value<string>("color");
  258. dataGridView1.Rows.Add();
  259. dataGridView1.Rows[dataGridView1.RowCount - 1].HeaderCell.Value = name;
  260. dataGridView1[0, dataGridView1.RowCount - 1].Value = code;
  261. dataGridView2.Rows.Add();
  262. dataGridView2.Rows[dataGridView2.RowCount - 1].HeaderCell.Value = name;
  263. dataGridView2[0, dataGridView2.RowCount - 1].Value = code;
  264. cbDefectName.Items.Add(name);
  265. }
  266. //模型是否是产品模型
  267. if (model.ModelName == cmbDefectModelFile.Text)
  268. {
  269. //加载参数
  270. string code;
  271. QualifiedLimit item1;
  272. for (int i = 0; i < dataGridView1.Rows.Count; i++)
  273. {
  274. code = dataGridView1.Rows[i].Cells["Code"].Value.ToString();
  275. item1 = model.QualifiedLimitList.FirstOrDefault(m => m.Code == code);
  276. if (item1 != null)
  277. {
  278. dataGridView1.Rows[i].Cells["ZXD"].Value = item1.ZXD;
  279. dataGridView1.Rows[i].Cells["Area"].Value = item1.Area;
  280. dataGridView1.Rows[i].Cells["DBDH"].Value = Utils.ContrastToPercent(item1.ContrastTop);
  281. dataGridView1.Rows[i].Cells["DBDL"].Value = Utils.ContrastToPercent(item1.ContrastLower);
  282. dataGridView1.Rows[i].Cells["OrAnd"].Value = item1.IsOR;
  283. dataGridView1.Rows[i].Cells["DefectLength"].Value = item1.DefectWarnLength;
  284. dataGridView1.Rows[i].Cells["DefectWarn"].Value = item1.DefectWarnCnt;
  285. }
  286. else
  287. {
  288. dataGridView1.Rows[i].Cells["ZXD"].Value = 0.4;
  289. dataGridView1.Rows[i].Cells["Area"].Value = 2;
  290. dataGridView1.Rows[i].Cells["DBDH"].Value = 51;
  291. dataGridView1.Rows[i].Cells["DBDL"].Value = 49;
  292. dataGridView1.Rows[i].Cells["OrAnd"].Value = false;
  293. dataGridView1.Rows[i].Cells["DefectLength"].Value = 0;
  294. dataGridView1.Rows[i].Cells["DefectWarn"].Value = 0;
  295. }
  296. }
  297. GradeLimit item2;
  298. for (int i = 0; i < dataGridView2.Rows.Count; i++)
  299. {
  300. code = dataGridView2.Rows[i].Cells["Code2"].Value.ToString();
  301. item2 = model.GradeLimitList.FirstOrDefault(m => m.Code == code);
  302. if (item2 != null)
  303. {
  304. dataGridView2.Rows[i].Cells["A"].Value = item2.A;
  305. dataGridView2.Rows[i].Cells["B"].Value = item2.B;
  306. dataGridView2.Rows[i].Cells["C"].Value = item2.C;
  307. dataGridView2.Rows[i].Cells["D"].Value = item2.D;
  308. dataGridView2.Rows[i].Cells["E"].Value = item2.E;
  309. }
  310. }
  311. }
  312. else
  313. {
  314. //加载默认参数
  315. string code;
  316. QualifiedLimit item1;
  317. for (int i = 0; i < dataGridView1.Rows.Count; i++)
  318. {
  319. dataGridView1.Rows[i].Cells["ZXD"].Value = 0.4;
  320. dataGridView1.Rows[i].Cells["Area"].Value = 2;
  321. dataGridView1.Rows[i].Cells["DBDH"].Value = 51;
  322. dataGridView1.Rows[i].Cells["DBDL"].Value = 49;
  323. dataGridView1.Rows[i].Cells["OrAnd"].Value = false;
  324. dataGridView1.Rows[i].Cells["DefectLength"].Value = 0;
  325. dataGridView1.Rows[i].Cells["DefectWarn"].Value = 0;
  326. }
  327. GradeLimit item2;
  328. for (int i = 0; i < dataGridView2.Rows.Count; i++)
  329. {
  330. dataGridView2.Rows[i].Cells["A"].Value = 0;
  331. dataGridView2.Rows[i].Cells["B"].Value = 0;
  332. dataGridView2.Rows[i].Cells["C"].Value = 0;
  333. dataGridView2.Rows[i].Cells["D"].Value = 0;
  334. dataGridView2.Rows[i].Cells["E"].Value = 0;
  335. }
  336. }
  337. }
  338. catch (Exception ex)
  339. {
  340. MessageBox.Show("载入出错:" + ex.Message, "警告");
  341. }
  342. }
  343. private void btnColor_Click(object sender, EventArgs e)
  344. {
  345. ColorDialog dlg = new ColorDialog();
  346. DialogResult result = dlg.ShowDialog();
  347. if(result == DialogResult.OK)
  348. {
  349. // 获取用户所选颜色
  350. Color selectedColor = dlg.Color;
  351. // 在 label1 中显示所选颜色的 RGB 值
  352. tbColorName.ForeColor = selectedColor;
  353. }
  354. }
  355. private void btnClearDefect_Path_Click(object sender, EventArgs e)
  356. {
  357. tbWarnDefect.Text = "";
  358. }
  359. private void btnAddWarn_Click(object sender, EventArgs e)
  360. {
  361. if(tbWarnDefect.Text == "")
  362. {
  363. tbWarnDefect.Text += cbDefectName.Text;
  364. }
  365. else
  366. tbWarnDefect.Text += $",{cbDefectName.Text}";
  367. }
  368. private void TrackBarLightValue_ValueChanged(object sender, EventArgs e)
  369. {
  370. UCTrackBar trackBar = (UCTrackBar)sender;
  371. if(trackBar.Name == "TrackBarLightValue")
  372. {
  373. numericUpDown1.Value = (decimal)trackBar.Value;
  374. }
  375. else if (trackBar.Name == "TrackBarExposureTime")
  376. {
  377. numericUpDown2.Value = (decimal)trackBar.Value;
  378. }
  379. else if (trackBar.Name == "TrackBarGain")
  380. {
  381. numericUpDown3.Value = (decimal)trackBar.Value;
  382. }
  383. else if (trackBar.Name == "TrackBarTensionValue")
  384. {
  385. numericUpDown4.Value = (decimal)trackBar.Value;
  386. }
  387. }
  388. private void numericUpDown1_ValueChanged(object sender, EventArgs e)
  389. {
  390. NumericUpDown num = (NumericUpDown)sender;
  391. if (num.Name == "numericUpDown1")
  392. {
  393. TrackBarLightValue.Value = (float)num.Value > TrackBarLightValue.MaxValue? TrackBarLightValue.MaxValue : (float)num.Value;
  394. }
  395. else if (num.Name == "numericUpDown2")
  396. {
  397. TrackBarExposureTime.Value = (float)num.Value > TrackBarExposureTime.MaxValue ? TrackBarExposureTime.MaxValue : (float)num.Value;
  398. }
  399. else if (num.Name == "numericUpDown3")
  400. {
  401. TrackBarGain.Value = (float)num.Value > TrackBarGain.MaxValue ? TrackBarGain.MaxValue : (float)num.Value;
  402. }
  403. else if (num.Name == "numericUpDown4")
  404. {
  405. TrackBarTensionValue.Value = (float)num.Value > TrackBarTensionValue.MaxValue ? TrackBarTensionValue.MaxValue : (float)num.Value;
  406. }
  407. }
  408. private void button1_Click(object sender, EventArgs e)
  409. {
  410. if (this.model == null || this.model.Id == 0)
  411. return;
  412. //加载缺陷
  413. string configPath = ConfMgr.Instance.SysConfigParams.AIModelPath + $"\\{tbLabelFile.Text}";
  414. string lsTmp = File.ReadAllText(configPath);
  415. JArray defectItemList = JArray.Parse(lsTmp);
  416. EditPauseFrm frm = new EditPauseFrm(model.DefectPauseOption, defectItemList);
  417. //frm.Render();
  418. frm.Text = "选择瑕疵";
  419. frm.ShowDialog();
  420. if (frm.DialogResult == DialogResult.OK)
  421. {
  422. model.DefectPauseOption = frm.lstCodes;
  423. }
  424. frm.Dispose();
  425. }
  426. private void cmbDefectModelFile_SelectedIndexChanged(object sender, EventArgs e)
  427. {
  428. }
  429. }
  430. }