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.
 
 
 
 
 

490 lines
22 KiB

  1. using OpenCvSharp;
  2. using ProductionControl.UI;
  3. using ProductionControl.UIExtend;
  4. using ProductionControl.Utils;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.Drawing.Imaging;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Web.Configuration;
  18. using System.Windows.Forms;
  19. namespace ProductionControl
  20. {
  21. public partial class FrmProductInfo : Form
  22. {
  23. Service.ClassesService svcClasses = new Service.ClassesService();
  24. Service.ProductService service = new Service.ProductService();
  25. Models.Product model = new Models.Product();
  26. string MapPath = "";
  27. public FrmProductInfo(Models.Product m=null)
  28. {
  29. InitializeComponent();
  30. this.dgvBatchList.AutoGenerateColumns = false;
  31. checkCustomerVer();
  32. //this.cbMarkType.SelectedIndex = -1;
  33. if (m!=null)
  34. {
  35. model = m;
  36. this.Text += $" (ID:{m.Id})";
  37. this.txtCode.Text = m.Code;
  38. this.txtName.Text = m.Name;
  39. this.txtSpec.Text = m.Spec;
  40. this.cmbHoleCount.Text = m.HoleCount.ToString();
  41. this.txtBatchId.Text = m.BatchId;
  42. this.numTargetCount.Value = m.TargetCount;
  43. this.toolTip1.SetToolTip(this.numTargetCount,"已完成:"+m.CompleteCount);
  44. this.numTensionBaseValue.Value = (decimal)m.TensionBaseValue;
  45. this.numTensionUpFloatValue.Value = (decimal)m.TensionUpFloatValue;
  46. this.numTensionDownFloatValue.Value = (decimal)m.TensionDownFloatValue;
  47. this.numHeightBaseValue.Value = (decimal)m.HeightBaseValue;
  48. this.numHeightUpFloatValue.Value = (decimal)m.HeightUpFloatValue;
  49. this.numHeightDownFloatValue.Value = (decimal)m.HeightDownFloatValue;
  50. this.txtHeightBaseDec.Text = m.HeightBaseDec;
  51. this.numLineWidthBaseValue.Value = (decimal)m.LineWidthBaseValue;
  52. this.numLineWidthUpFloatValue.Value = (decimal)m.LineWidthUpFloatValue;
  53. this.numLineWidthDownFloatValue.Value = (decimal)m.LineWidthDownFloatValue;
  54. this.numPTBaseValue.Value = (decimal)m.PTBaseValue;
  55. this.numPTUpFloatValue.Value = (decimal)m.PTUpFloatValue;
  56. this.numPTDownFloatValue.Value = (decimal)m.PTDownFloatValue;
  57. //if (m.Type > 0)
  58. //{
  59. // this.txtCode.Enabled = false;
  60. // this.txtName.Width = this.txtCode.Width;
  61. // this.txtSpec.Enabled = false;
  62. //}
  63. this.tsbtnAddFile.Enabled = true;
  64. this.tsbtnNewBatchId.Enabled = true;
  65. this.dgvBatchList.DataSource = new BindingSource(m.BatchHistoryList, null);
  66. //2023-11-2 mark
  67. this.cbMarkType.SelectedIndex = m.MarkType;
  68. this.numMarkSize.Value = (decimal)m.MarkSize;
  69. //2023-12-18 主栅
  70. this.cbMarkDisturb.Checked = (m.MarkDisturb == 1);
  71. this.cbMainGrid.Checked = (m.MainGrid == 1);
  72. this.cbHavePad.Checked = (m.HavePad == 1);
  73. Models.Attachment attachmentFile = model.AttachmentList.FirstOrDefault(x => x.Type == 0);
  74. if (attachmentFile!=null)
  75. {
  76. this.btnOpenFile.Text = attachmentFile.Name;
  77. this.btnOpenFile.Visible = true;
  78. //为兼容老版本,发现图纸图片地址为空,检索图片地址
  79. if (string.IsNullOrEmpty(model.MapPath))
  80. {
  81. string targFilePath = Application.StartupPath + $"\\Attachment\\product\\{model.Id}\\";
  82. targFilePath += attachmentFile.Name;
  83. model.MapPath = targFilePath.Substring(0, targFilePath.Length - 4) + ".bmp";
  84. }
  85. }
  86. }
  87. initDataView();
  88. }
  89. private void FrmProductInfo_Load(object sender, EventArgs e)
  90. {
  91. //this.cbMarkType.SelectedIndex = -1;
  92. }
  93. private void checkCustomerVer()
  94. {
  95. if (Config.CustomerVer != "B")
  96. {
  97. tabControl1.TabPages.RemoveAt(3);
  98. }
  99. }
  100. private void initDataView()
  101. {
  102. //显示的数据
  103. this.cmbClasses.DisplayMember = "Name";
  104. this.cmbClasses.ValueMember = "Id";
  105. var list = svcClasses.GetListNav(0);
  106. this.cmbClasses.DataSource = list;
  107. if (model.ClassesId>0)
  108. this.cmbClasses.SelectedValue = model.ClassesId;
  109. //缺陷项
  110. var lstDefect = Utils.EnumUtil.GetArrayList<DefectCodeEnum>();
  111. foreach(DictionaryEntry item in lstDefect)
  112. {
  113. DefectCountOfSizeControl userCon = new DefectCountOfSizeControl();
  114. userCon.Code = item.Value.ToString();
  115. userCon.Title = ((DefectNameEnum)(int)item.Key).ToString();
  116. if (model != null && model.QualifiedCriterionList!=null)
  117. {
  118. var qalifiedItem = model.QualifiedCriterionList.FirstOrDefault(m => m.DefectCode == userCon.Code);
  119. if(qalifiedItem != null)
  120. {
  121. userCon.Checked = true;
  122. userCon.SizeValue = (decimal)qalifiedItem.Size;
  123. userCon.MaxDefectCount = qalifiedItem.MaxDefectCount;
  124. }
  125. }
  126. this.flpQualifiedPannel.Controls.Add(userCon);
  127. }
  128. //模型文件
  129. string strDefectModelFile = Application.StartupPath + @"\onnxFiles\";
  130. if(!Directory.Exists(strDefectModelFile))
  131. Directory.CreateDirectory(strDefectModelFile);
  132. string[] onnxFiles=Directory.GetFiles(strDefectModelFile,"*.onnx");
  133. string onlyName;
  134. foreach (string onnxFile in onnxFiles)
  135. {
  136. onlyName = Path.GetFileName(onnxFile);
  137. cmbDefectModelFile.Items.Add(onlyName);
  138. if (!string.IsNullOrWhiteSpace(model.DefectModelFile) && onlyName.ToLower()== model.DefectModelFile.ToLower())
  139. this.cmbDefectModelFile.SelectedItem = model.DefectModelFile;
  140. }
  141. }
  142. private void btnSave_Click(object sender, EventArgs e)
  143. {
  144. try
  145. {
  146. string szCode = this.txtCode.Text.Trim();
  147. string szName = this.txtName.Text.Trim();
  148. string szSpec = this.txtSpec.Text.Trim();
  149. string szHoleCount = this.cmbHoleCount.Text.Trim();
  150. string szBatchId = this.txtBatchId.Text.Trim();
  151. int liTargetCount = (int)this.numTargetCount.Value;
  152. string szHeightBaseDec=this.txtHeightBaseDec.Text.Trim().Trim(new char[] { ';', ',' });
  153. if (szCode == "" || szName == "")
  154. throw new Exception("请填写料号和名称!");
  155. if(this.cmbClasses.SelectedIndex<0)
  156. throw new Exception("请选择产品类型!");
  157. if (szHoleCount==""||! Util.IsNumber(szHoleCount))
  158. throw new Exception("请正确填写产品目数!");
  159. if (this.cmbDefectModelFile.SelectedIndex < 0)
  160. throw new Exception("请选择检测模型文件!");
  161. if (this.cbMarkType.SelectedIndex < 0)
  162. throw new Exception("请选择Mark!");
  163. //if (szBatchId == "")// || liTargetCount < 1
  164. // throw new Exception("请填写批次号");// 和批次目标数量!");
  165. if (szHeightBaseDec != "")
  166. {
  167. double num;
  168. string[] szs= szHeightBaseDec.Split(new char[] { ';',','});
  169. foreach(string s in szs)
  170. {
  171. if (!double.TryParse(s, out num))
  172. throw new Exception(s+" 非数值!");
  173. }
  174. }
  175. model.Code = szCode;
  176. model.Name = szName;
  177. model.Spec = szSpec;
  178. model.ClassesId = (int)this.cmbClasses.SelectedValue;
  179. model.HoleCount = Convert.ToInt32(szHoleCount);
  180. model.DefectModelFile = this.cmbDefectModelFile.Text.Trim();
  181. model.TensionBaseValue= (double)this.numTensionBaseValue.Value;
  182. model.TensionUpFloatValue = (double)this.numTensionUpFloatValue.Value;
  183. model.TensionDownFloatValue = (double)this.numTensionDownFloatValue.Value;
  184. model.HeightBaseValue = (double)this.numHeightBaseValue.Value;
  185. model.HeightUpFloatValue = (double)this.numHeightUpFloatValue.Value;
  186. model.HeightDownFloatValue = (double)this.numHeightDownFloatValue.Value;
  187. model.HeightBaseDec = szHeightBaseDec;
  188. model.LineWidthBaseValue = (double)this.numLineWidthBaseValue.Value;
  189. model.LineWidthUpFloatValue = (double)this.numLineWidthUpFloatValue.Value;
  190. model.LineWidthDownFloatValue = (double)this.numLineWidthDownFloatValue.Value;
  191. model.PTBaseValue = (double)this.numPTBaseValue.Value;
  192. model.PTUpFloatValue = (double)this.numPTUpFloatValue.Value;
  193. model.PTDownFloatValue = (double)this.numPTDownFloatValue.Value;
  194. if (model.QualifiedCriterionList == null)
  195. model.QualifiedCriterionList = new List<Models.QualifiedCriterion>();
  196. else
  197. model.QualifiedCriterionList.Clear();
  198. if (string.IsNullOrEmpty(model.MapPath))
  199. {
  200. model.MapPath = "";
  201. model.GetPointList = "";
  202. }
  203. foreach (DefectCountOfSizeControl defectControl in this.flpQualifiedPannel.Controls)
  204. {
  205. if (defectControl.Checked)
  206. {
  207. model.QualifiedCriterionList.Add(
  208. new Models.QualifiedCriterion()
  209. {
  210. DefectCode = defectControl.Code,
  211. Size = (float)defectControl.SizeValue,
  212. MaxDefectCount = (int)defectControl.MaxDefectCount,
  213. ModifyUserCode = Config.loginUser.Code,
  214. CreateUserCode = Config.loginUser.Code
  215. });
  216. }
  217. }
  218. model.ModifyUserCode = Config.loginUser.Code;
  219. //2023-11-2 mark
  220. model.MarkType = this.cbMarkType.SelectedIndex;
  221. model.MarkSize = (double)this.numMarkSize.Value;
  222. //2023-12-18 主栅
  223. model.MarkDisturb = this.cbMarkDisturb.Checked?1:0;
  224. model.MainGrid = this.cbMainGrid.Checked ? 1 : 0;
  225. model.HavePad = this.cbHavePad.Checked?1: 0;
  226. bool result;
  227. if (model.Id == 0)
  228. {
  229. model.BatchId = szBatchId;
  230. model.TargetCount = liTargetCount;
  231. model.CompleteCount = 0;
  232. model.CreateUserCode = Config.loginUser.Code;
  233. result = service.InsertNav(model);
  234. }
  235. else
  236. {
  237. if (model.BatchId != szBatchId && !string.IsNullOrWhiteSpace(model.BatchId))
  238. {
  239. if (model.CompleteCount<model.TargetCount
  240. && MessageBox.Show($"本批次完成数量未达到目标数量,确定更换批次号?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
  241. return;
  242. if (model.BatchHistoryList.FirstOrDefault(m => m.BatchId == szBatchId) != null)
  243. throw new Exception("当前批次号与历史中所用批次号重复,请重新填写!");
  244. model.BatchHistoryList.Add(new Models.BatchHistory()
  245. {
  246. BatchId = model.BatchId,
  247. TargetCount = model.TargetCount,
  248. CompleteCount = model.CompleteCount,
  249. CreateUserCode = Config.loginUser.Code,
  250. ModifyUserCode = Config.loginUser.Code
  251. });
  252. model.CompleteCount = 0;
  253. }
  254. model.BatchId = szBatchId;
  255. model.TargetCount = liTargetCount;
  256. result = service.UpdateNav(model);
  257. }
  258. if (!result)
  259. throw new Exception("保存失败!");
  260. this.tsbtnAddFile.Enabled = true;
  261. this.tsbtnNewBatchId.Enabled = true;
  262. MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  263. this.DialogResult = DialogResult.OK;
  264. this.Close();
  265. }
  266. catch (Exception ex)
  267. {
  268. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  269. }
  270. }
  271. private void btnCancel_Click(object sender, EventArgs e)
  272. {
  273. this.Close();
  274. }
  275. private void tsbtnNewBatchId_Click(object sender, EventArgs e)
  276. {
  277. tabControl1.SelectedIndex = 2;
  278. Application.DoEvents();
  279. }
  280. private void btnAddFile_Click(object sender, EventArgs e)
  281. {
  282. try
  283. {
  284. if (this.cbMarkType.SelectedIndex < 0)
  285. throw new Exception("请选择Mark!");
  286. //.dwg /.dxf /.dws /.dwt
  287. string filePath = FileUtil.selectFile("gbx,gbr文件|*.gb?");
  288. if (string.IsNullOrWhiteSpace(filePath))
  289. return;
  290. string fileName=Path.GetFileName(filePath);
  291. string fileExtend=Path.GetExtension(filePath);
  292. string fileOnlyName = fileName.Substring(0, fileName.Length - fileExtend.Length);
  293. //
  294. string targFilePath = Application.StartupPath + $"\\Attachment\\product\\{model.Id}\\";
  295. if (!Directory.Exists(targFilePath))
  296. Directory.CreateDirectory(targFilePath);
  297. else//删除原文件
  298. FileUtil.delFilesInFolder(targFilePath);
  299. targFilePath += fileName;
  300. //File.Copy(res.file_path, defectFileName + ".bmp", true);
  301. bool result = API.CopyFile(filePath, targFilePath, false);//更快 //false-覆盖
  302. if (!result)
  303. throw new Exception("移动文件失败!");
  304. //后台线程转BMP与JPG
  305. string tempClass = this.cmbClasses.Text;
  306. int tempIndex = this.cbMarkType.SelectedIndex;
  307. double tempValue = (double)this.numMarkSize.Value;
  308. //2023-12-18
  309. int tMarkDistrub = this.cbMarkDisturb.Checked ? 1 : 0;
  310. int tMainGrid = this.cbMainGrid.Checked ? 1 : 0;
  311. //2024-1-24
  312. int tHavePad = this.cbHavePad.Checked ? 1 : 0;
  313. Task.Run(() =>
  314. {
  315. Yolo5.Yolo_Class yolo = new Yolo5.Yolo_Class();
  316. string bmpPath = targFilePath.Substring(0, targFilePath.Length - 4) + ".bmp";
  317. //2023-11-3 mark
  318. //yolo.gerber2image(targFilePath, bmpPath);
  319. double[] markParam = new double[7];
  320. if(tempClass.Contains("乳剂"))
  321. markParam[0] = 2;
  322. else if (tempClass.Contains("PI"))
  323. markParam[0] = 1;
  324. else
  325. markParam[0] = 0;
  326. if (tempIndex == 0)
  327. {
  328. //默认情况 实心圆,0.5mm
  329. markParam[1] = 2;
  330. markParam[2] = 0.5;
  331. markParam[3] = 1;
  332. markParam[4] = 0;
  333. markParam[5] = 0;
  334. markParam[6] = 0;
  335. }
  336. else
  337. {
  338. markParam[1] = tempIndex - 1;
  339. markParam[2] = tempValue;
  340. markParam[3] = tempIndex == 1 ? 0 : 1;
  341. markParam[4] = tMarkDistrub;
  342. markParam[5] = tMainGrid;
  343. markParam[6] = tHavePad;
  344. }
  345. yolo.gerber2image(targFilePath, bmpPath, markParam);
  346. //换背景JPG
  347. Mat mat = Cv2.ImRead(bmpPath);
  348. Cv2.CvtColor(mat, mat, ColorConversionCodes.RGB2GRAY);//转灰度图
  349. for (int i = 0; i < mat.Height; i++)
  350. {
  351. for (int j = 0; j < mat.Width; j++)
  352. {
  353. if (mat.At<byte>(i, j) == 255)//白色
  354. mat.Set<byte>(i, j, 0);
  355. else
  356. mat.Set<byte>(i, j, 255);
  357. }
  358. }
  359. model.MapPath = bmpPath;
  360. //灰转彩
  361. //Cv2.CvtColor(mat, mat, ColorConversionCodes.GRAY2RGB);
  362. //for (int i = 0; i < mat.Height; i++)
  363. //{
  364. // for (int j = 0; j < mat.Width; j++)
  365. // {
  366. // if (mat.At<byte>(i, j) == 255)//白色
  367. // mat.Set<byte>(i, j, 0); //黄色
  368. // }
  369. //}
  370. bmpPath = targFilePath.Substring(0, targFilePath.Length - 4) + ".jpg";
  371. //model.MapPath = bmpPath;
  372. OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat).Save(bmpPath, ImageFormat.Jpeg);
  373. });//不能加.Start();
  374. Models.Attachment attachmentFile = model.AttachmentList.FirstOrDefault(m => m.Type == 0);
  375. if (attachmentFile!=null)
  376. {
  377. attachmentFile.Type = 0;//图纸
  378. attachmentFile.NameTimestamp= $"{model.Id}\\{fileOnlyName}";
  379. attachmentFile.Name = fileName;
  380. attachmentFile.ExtendName = fileExtend;
  381. }
  382. else
  383. {
  384. model.AttachmentList.Add(new Models.Attachment()
  385. {
  386. TBName = "product",
  387. Type = 0,
  388. NameTimestamp = $"{model.Id}\\{fileOnlyName}",
  389. Name = fileName,
  390. ExtendName = fileExtend,
  391. CreateUserCode = Config.loginUser.Code,
  392. ModifyUserCode = Config.loginUser.Code
  393. });
  394. }
  395. if (!service.UpdateNav(model))
  396. throw new Exception("保存文件失败!");
  397. model=service.GetModelNav(model.Code);
  398. this.btnOpenFile.Text = fileName;
  399. this.btnOpenFile.Visible = true;
  400. }
  401. catch (Exception ex)
  402. {
  403. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  404. }
  405. }
  406. private void btnOpenFile_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  407. {
  408. Models.Attachment attachmentFile = model.AttachmentList.FirstOrDefault(m => m.Type == 0);
  409. if (attachmentFile == null )
  410. MessageBox.Show("还未上传!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  411. string filePath = Application.StartupPath + $"\\Attachment\\product\\{model.Id}\\" + attachmentFile.NameTimestamp + attachmentFile.ExtendName;
  412. if(File.Exists(filePath))
  413. Process.Start(filePath);
  414. }
  415. private void tbtnGetPos_Click(object sender, EventArgs e)
  416. {
  417. if ((model.MapPath == null)||(string.IsNullOrEmpty(model.MapPath)))
  418. {
  419. MessageBox.Show("还未上传图纸!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  420. return;
  421. }
  422. var list = model.GetPointList.Split(',');
  423. List<double> dList = new List<double>();
  424. if (list.Length < 28)
  425. {
  426. for (int i = 0; i < 28; i++)
  427. {
  428. dList.Add(0);
  429. }
  430. }
  431. else
  432. {
  433. for (int i = 0; i < list.Length; i++)
  434. {
  435. dList.Add(double.Parse(list[i]));
  436. }
  437. }
  438. FrmGetPosByPic frm = new FrmGetPosByPic(model.MapPath, dList.ToArray());
  439. frm.ShowDialog();
  440. model.GetPointList = string.Join(",", frm.GetPoints()); ;
  441. model.MapPath = frm.GetMapPath();
  442. if (!service.UpdateNav(model))
  443. throw new Exception("保存文件失败!");
  444. model = service.GetModelNav(model.Code);
  445. }
  446. }
  447. }