版博士V2.0程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

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