革博士程序V1仓库
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.

404 line
19 KiB

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