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.
 
 
 
 
 

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