革博士V2程序
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.

882 line
44 KiB

  1. using GeBoShi.SysCtrl;
  2. using ClosedXML.Excel;
  3. using HZH_Controls.Controls;
  4. using Models;
  5. using Newtonsoft.Json.Linq;
  6. using Service;
  7. using SqlSugar;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Linq.Expressions;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows.Forms;
  19. using System.Windows.Forms.DataVisualization.Charting;
  20. using DocumentFormat.OpenXml.Spreadsheet;
  21. using Color = System.Drawing.Color;
  22. namespace GeBoShi.UI.DataQuery
  23. {
  24. public partial class DataQueryFrm : Form
  25. {
  26. RecordsService service = new RecordsService();
  27. ProductService productService = new ProductService();
  28. Color[] ChartColor = new Color[30] { Color.Red, Color.Blue, Color.Green, Color.YellowGreen, Color.Orange,
  29. Color.Peru, Color.DarkRed,Color.GreenYellow, Color.Pink , Color.Brown,
  30. Color.CadetBlue, Color.OrangeRed, Color.Cyan, Color.Lime, Color.Magenta, Color.Tan, Color.Sienna,
  31. Color.DarkGray, Color.SaddleBrown, Color.DarkBlue, Color.Firebrick,Color.Gainsboro,Color.Honeydew,Color.Khaki,Color.Lavender,
  32. Color.LightGoldenrodYellow,Color.Navy, Color.Khaki, Color.IndianRed,Color.Lavender};
  33. public DataQueryFrm()
  34. {
  35. InitializeComponent();
  36. UIStyle.SetUIStyle(this);
  37. this.uiTitel1.FatherForm = this;
  38. this.lineChartDefect.Size = new Size(600, 800);
  39. this.lineChartFaceWidth.Size = new Size(1000, 300);
  40. #region dataGridView设置
  41. uiDataGridView1.AutoGenerateColumns = false;//自动创建列
  42. uiDataGridView1.AllowUserToAddRows = uiDataGridView1.AllowUserToDeleteRows = false;//用户添加删除行
  43. uiDataGridView1.AllowUserToResizeRows = true;//用户调整行大小
  44. uiDataGridView1.AllowUserToResizeColumns = false;//用户调整列大小
  45. uiDataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//只可选中整行,不是单元格
  46. //显示行号与列宽度自动调整
  47. uiDataGridView1.RowHeadersVisible = true;
  48. uiDataGridView1.RowHeadersWidth = 30;
  49. uiDataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变
  50. uiDataGridView1.RowPostPaint += (sender, e) =>//序号列头
  51. {
  52. SysMgr.showRowNum_onDataGrid_RowPostPaint(this.uiDataGridView1, sender, e);
  53. };
  54. uiDataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  55. //for (int i = 0; i < dataGridView1.Columns.Count; i++)//禁止点击列头排序
  56. // uiDataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
  57. //行列交叉处标题
  58. //if (uiDataGridView1.RowHeadersVisible) uiDataGridView1.TopLeftHeaderCell.Value = "SPH/CYL";
  59. //事件
  60. this.uiDataGridView1.DataBindingComplete += this.uiDataGridView1_DataBindingComplete;//bing data时发生,可修改单元格内容
  61. //this.uiDataGridView1.SelectIndexChange += uiDataGridView1_SelectIndexChange;//选择行时发行
  62. //this.uiDataGridView1.CellClick += UiDataGridView1_CellClick;
  63. #endregion
  64. }
  65. private void uiDataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
  66. {
  67. var list = uiDataGridView1.DataSource as List<Records>;
  68. for (int i = 0; i < uiDataGridView1.Rows.Count; i++)
  69. {
  70. if (list[i].Grade < 1)
  71. uiDataGridView1.Rows[i].Cells["colGrade"].Value = "";
  72. else if (list[i].Grade <= 5)
  73. uiDataGridView1.Rows[i].Cells["colGrade"].Value = (char)(list[i].Grade + 64);
  74. else
  75. uiDataGridView1.Rows[i].Cells["colGrade"].Value = "不合格";
  76. //if (list[i].RoleInfo != null)
  77. // uiDataGridView1.Rows[i].Cells["colRoleName"].Value = list[i].RoleInfo.Name;
  78. }
  79. }
  80. private Expression<Func<Records, bool>> createQueryExpression()
  81. {
  82. return Expressionable.Create<Records>()
  83. .And(it => it.CreateTime >= uiDatePicker1.Value.Date)
  84. .And(it => it.CreateTime < uiDatePicker2.Value.Date.AddDays(1))
  85. .AndIF(!string.IsNullOrWhiteSpace(txtBarcode.Text), it => it.BarCode.Contains(txtBarcode.Text.Trim()))
  86. .AndIF(!string.IsNullOrWhiteSpace(txtBatchId.Text), it => it.BatchId.Contains(txtBatchId.Text.Trim()))
  87. .AndIF(!string.IsNullOrWhiteSpace(txtReelId.Text), it => it.ReelId.Contains(txtReelId.Text.Trim()))
  88. .ToExpression();//注意 这一句 不能少
  89. }
  90. private void skinButton2_Click(object sender, EventArgs e)
  91. {
  92. //未连接数据库,通过模拟数据来实现
  93. //一般通过ORM的分页去取数据来填充
  94. //pageIndex:第几页,和界面对应,从1开始,取数据可能要用pageIndex
  95. //count:单页数据量,也就是PageSize值
  96. int count = 20;
  97. int totalCount = 0;
  98. var list = service.GetListNav( 1 , count, ref totalCount, createQueryExpression());
  99. uiDataGridView1.DataSource = list;
  100. pageCtrl1.RecordCount = totalCount;
  101. }
  102. private void skinButton1_Click(object sender, EventArgs e)
  103. {
  104. try
  105. {
  106. if (this.uiDataGridView1.CurrentRow == null)
  107. return;
  108. Models.Records record = service.GetModelNav(int.Parse(this.uiDataGridView1.CurrentRow.Cells[0].Value.ToString()));
  109. if (record.DefectInfoList.Count < 0)
  110. throw new Exception("当前记录无缺陷!");
  111. string path = ConfMgr.SelectFolder();
  112. if (string.IsNullOrWhiteSpace(path))
  113. return;
  114. //var list = uiDataGridView1.DataSource as List<Records>;
  115. //var table = ExcelUtil.ConvertToDataTable<Records>(list);
  116. //{ 名称=x.Name,Xcm=x.X,Ym=x.Y/100,宽cm=x.Width,高cm=x.Height,面积=x.Area, 置信度 =x.ZXD}
  117. var list = record.DefectInfoList;//.Select(x => new { x.Name,x.X,x.Y,x.Width,x.Height,x.Area, x.ZXD}).ToList();
  118. //绘图1
  119. double len = Math.Round(record.Len * 100, 2);//cm
  120. this.reDrawDefectPoints(record, new double[] { 0, Math.Round(record.FaceWidthMax + 0.005f, 2) }, new double[] { 0, len });
  121. //绘图2
  122. //var points = Array.ConvertAll(record.FaceWidthListStr.Split(new[] { ',', }, StringSplitOptions.RemoveEmptyEntries),Double.Parse).ToList();
  123. //reDrawFaceWidth(record.FacePointList,
  124. // new double[] { 0, Math.Round(len + 0.005f, 2) },
  125. // new double[] { record.FaceWidthMin, Math.Round(record.FaceWidthMax + 0.005f, 2) });
  126. reDrawFaceWidth(record.FacePointList,
  127. new double[] { 0, Math.Round(len + 0.005f, 2) },
  128. new double[] { 130, 160 });
  129. //
  130. foreach (var item in list)
  131. {
  132. item.Name = SysMgr.Instance.GetDefectName(record.ProductInfo.ModelName, item.Code);
  133. //item.Height = item.Height / 100; //单位错误,保证单位一致
  134. }
  135. //
  136. string Grade = "";
  137. if (record.Grade < 1) Grade = "";
  138. else if (record.Grade <= 5) Grade = (char)(record.Grade + 64) + "";
  139. else Grade = "不合格";
  140. JsonProductDefects data = new JsonProductDefects()
  141. {
  142. ProName = record.BarCodeName,
  143. BatchId = record.BatchId,
  144. ReelId = record.ReelId,
  145. Len = record.Len.ToString(),
  146. Speed = Math.Round(record.Len / record.TimeLen, 2).ToString(),
  147. Grade = Grade,
  148. DateTime = record.CreateTime.ToString("yyyy年MM月dd日 HH:mm")
  149. };
  150. data.DefectTotal = record.DefectInfoList.GroupBy(x => x.Name).Select(g => new JDefectTotal { Name = g.Key, Count = g.Count() }).ToList();
  151. data.DefectDetail = record.DefectInfoList.Select(x => new JDefectDetail
  152. {
  153. Index = x.PhotoIndex,
  154. Name = x.Name,
  155. X = x.X,
  156. Y = Math.Round(x.Y / 100.0d, 2),
  157. Width = x.Width * 10,
  158. Height = x.Height * 10,
  159. ZXD = x.ZXD,
  160. Area = x.Area * 100,
  161. Contrast = x.Contrast
  162. })
  163. .OrderBy(x => x.Index).ThenBy(x => x.Y).ToList();
  164. data.Pdt = productService.GetModelNav(record.ProductId);
  165. data.xyPix = $"X:{ConfMgr.Instance.SysConfigParams.Cm2px_x},Y:{ConfMgr.Instance.SysConfigParams.Cm2px_y}";
  166. var image1 = captureControl(this.lineChartDefect);
  167. var image2 = captureControl(this.lineChartFaceWidth);
  168. var filePath = $"{path}缺陷列表_{record.BarCode}_{record.BarCodeName}.xlsx";
  169. exportTabel(data, image1, image2, filePath, record.ProductInfo.ModelName);
  170. //if (!res)
  171. // throw new Exception("导出失败!");
  172. MessageBox.Show("导出成功!", "导出完成");
  173. System.Diagnostics.Process.Start(filePath);
  174. }
  175. catch (Exception ex)
  176. {
  177. MessageBox.Show($"{ex.Message}", "导出失败");
  178. //UIMessageTip.ShowError(ex.Message, 2000);
  179. //API.OutputDebugString(ex.StackTrace);
  180. }
  181. }
  182. public void exportTabel(JsonProductDefects ProductDefects, byte[] defectImage, byte[] faceWidthImage, string savePath, string modelName)
  183. {
  184. //try
  185. //{
  186. if (ProductDefects == null)
  187. throw new Exception("传入的参数为空");
  188. using (var workbook = new XLWorkbook())
  189. {
  190. var wsDefectsDetail = workbook.Worksheets.Add("正面疵点列表");
  191. wsDefectsDetail.RowHeight = 20;
  192. wsDefectsDetail.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
  193. wsDefectsDetail.Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
  194. wsDefectsDetail.Style.Font.FontName = "宋体";
  195. wsDefectsDetail.Column("A").Width = 12;
  196. wsDefectsDetail.Column("B").Width = 25;
  197. wsDefectsDetail.Column("C").Width = 10;
  198. wsDefectsDetail.Column("D").Width = 10;
  199. wsDefectsDetail.Column("E").Width = 10;
  200. wsDefectsDetail.Column("F").Width = 10;
  201. wsDefectsDetail.Column("G").Width = 12;
  202. wsDefectsDetail.Column("H").Width = 14;
  203. wsDefectsDetail.Column("I").Width = 14;
  204. int rowIndex = 1;
  205. int cellIndex = 1;
  206. #region 第一行
  207. var row1_cell1 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex);
  208. row1_cell1.Value = "产品疵点缺陷分布图表";
  209. row1_cell1.Style.Font.Bold = true;
  210. //row1_cell1.Style.Font.FontName = "宋体";
  211. row1_cell1.Style.Font.FontSize = 12;
  212. var mergeRange_row1 = wsDefectsDetail.Range("A1:I1").Row(1).Merge();
  213. mergeRange_row1.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  214. #endregion
  215. #region 第二行
  216. rowIndex++;
  217. var row2_cell1 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex);
  218. row2_cell1.Value = "产品品名";
  219. row2_cell1.Style.Font.Bold = true;
  220. row2_cell1.Style.Font.FontSize = 10;
  221. row2_cell1.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  222. var row2_cell2 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 1);
  223. row2_cell2.DataType = XLDataType.Text;
  224. row2_cell2.Value = ProductDefects.ProName;
  225. row2_cell2.Style.Font.FontSize = 10;
  226. row2_cell2.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  227. var row2_cell3 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 2);
  228. row2_cell3.Value = "产品批号";
  229. row2_cell3.Style = row2_cell1.Style;
  230. var row2_cell4 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 3);
  231. row2_cell4.Value = "'" + ProductDefects.BatchId;
  232. row2_cell4.Style = row2_cell2.Style;
  233. var row2_cell5 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 4);
  234. row2_cell5.Value = "产品卷号";
  235. row2_cell5.Style = row2_cell1.Style;
  236. var row2_cell6 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 5);
  237. //row2_cell6.SetDataType(XLDataType.Text);//类型设置不起作用 用"'"+内容代替
  238. //row2_cell6.DataType = XLDataType.Text;
  239. row2_cell6.Value = "'" + ProductDefects.ReelId;
  240. row2_cell6.Style = row2_cell2.Style;
  241. var row2_cell7 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 6);
  242. row2_cell7.Value = "长度(米)";
  243. row2_cell7.Style = row2_cell1.Style;
  244. var row2_cell8 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 7);
  245. row2_cell8.Value = ProductDefects.Len;
  246. row2_cell8.Style = row2_cell2.Style;
  247. //NULL
  248. var row2_cell9 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 8);
  249. row2_cell9.Style = row2_cell2.Style;
  250. #endregion
  251. #region 第三行
  252. rowIndex++;
  253. var row3_cell1 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex);
  254. row3_cell1.Value = "检验时间";
  255. row3_cell1.Style = row2_cell1.Style;
  256. var row3_cell2 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 1);
  257. row3_cell2.Value = "'" + ProductDefects.DateTime;
  258. row3_cell2.Style = row2_cell2.Style;
  259. var row3_cell3 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 2);
  260. row3_cell3.Value = "检验长度";
  261. row3_cell3.Style = row2_cell1.Style;
  262. var row3_cell4 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 3);
  263. row3_cell4.Value = ProductDefects.Len;
  264. row3_cell4.Style = row2_cell2.Style;
  265. var row3_cell5 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 4);
  266. row3_cell5.Value = "平均速度";
  267. row3_cell5.Style = row2_cell1.Style;
  268. var row3_cell6 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 5);
  269. row3_cell6.Value = ProductDefects.Speed;
  270. row3_cell6.Style = row2_cell2.Style;
  271. var row3_cell7 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 6);
  272. row3_cell7.Value = "等级";
  273. row3_cell7.Style = row2_cell1.Style;
  274. var row3_cell8 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 7);
  275. row3_cell8.Value = ProductDefects.Grade;
  276. row3_cell8.Style = row2_cell2.Style;
  277. var row3_cell9 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 8);
  278. row3_cell9.Style = row2_cell2.Style;
  279. #endregion
  280. #region 第四第五行
  281. rowIndex++;
  282. var row4_cell1 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex);
  283. row4_cell1.Value = "设备参数";
  284. row4_cell1.Style = row2_cell1.Style;
  285. row4_cell1.Style.Font.Bold = true;
  286. row4_cell1.Style.Font.FontSize = 10;
  287. row4_cell1.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  288. var mergeRange_row4 = wsDefectsDetail.Range("A4:A5").Column(1).Merge();
  289. mergeRange_row4.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  290. var row4_cell2 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 1);
  291. row4_cell2.Value = "光源亮度";
  292. row4_cell2.Style = row2_cell1.Style;
  293. var row4_cell3 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 2);
  294. row4_cell3.Value = "曝光时间";
  295. row4_cell3.Style = row2_cell1.Style;
  296. var row4_cell4 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 3);
  297. row4_cell4.Value = "增益";
  298. row4_cell4.Style = row2_cell1.Style;
  299. var row4_cell5 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 4);
  300. row4_cell5.Value = "行频比";
  301. row4_cell5.Style = row2_cell1.Style;
  302. var row4_cell6 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 5);
  303. row4_cell6.Value = "物面分辨率";
  304. row4_cell6.Style = row2_cell1.Style;
  305. var row4_cell7 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 6);
  306. row4_cell7.Value = "触发计数";
  307. row4_cell7.Style = row2_cell1.Style;
  308. var row4_cell8 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 7);
  309. row4_cell8.Value = "采集计数";
  310. row4_cell8.Style = row2_cell1.Style;
  311. var row4_cell9 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 8);
  312. row4_cell9.Value = "";
  313. row4_cell9.Style = row2_cell1.Style;
  314. rowIndex++;
  315. var row5_cell2 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 1);
  316. row5_cell2.Value = ProductDefects.Pdt.LightValue;
  317. row5_cell2.Style = row2_cell1.Style;
  318. var row5_cell3 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 2);
  319. row5_cell3.Value = ProductDefects.Pdt.ExposureTime;
  320. row5_cell3.Style = row2_cell1.Style;
  321. var row5_cell4 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 3);
  322. row5_cell4.Value = ProductDefects.Pdt.Gain;
  323. row5_cell4.Style = row2_cell1.Style;
  324. var row5_cell5 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 4);
  325. row5_cell5.Value = "";
  326. row5_cell5.Style = row2_cell1.Style;
  327. var row5_cell6 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 5);
  328. row5_cell6.Value = ProductDefects.xyPix;
  329. row5_cell6.Style = row2_cell1.Style;
  330. var row5_cell7 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 6);
  331. row5_cell7.Value = "";
  332. row5_cell7.Style = row2_cell1.Style;
  333. var row5_cell8 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 7);
  334. row5_cell8.Value = "";
  335. row5_cell8.Style = row2_cell1.Style;
  336. var row5_cell9 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 8);
  337. row5_cell9.Value = "";
  338. row5_cell9.Style = row2_cell1.Style;
  339. #endregion
  340. #region 第六行后
  341. rowIndex++;
  342. if (ProductDefects.DefectTotal != null && ProductDefects.DefectTotal.Count > 0)
  343. {
  344. cellIndex = 1;
  345. int DefectTotalCount = ProductDefects.DefectTotal.Count;
  346. //最少5行,固定4列
  347. var row6_cell1 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex);
  348. row6_cell1.Value = "检测参数";
  349. row6_cell1.Style.Font.Bold = true;
  350. row6_cell1.Style.Font.FontSize = 10;
  351. row6_cell1.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  352. string cellstr = $"A6:A{7 + ProductDefects.DefectTotal.Count}";
  353. var mergeRange_row6 = wsDefectsDetail.Range(cellstr).Column(1).Merge();
  354. mergeRange_row6.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  355. var row6_cell2 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 1);
  356. row6_cell2.Value = "筛选标准";
  357. row6_cell2.Style.Font.Bold = true;
  358. row6_cell2.Style.Font.FontSize = 10;
  359. row6_cell2.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  360. var mergeRange_row6_2 = wsDefectsDetail.Range($"B6:I6").Row(1).Merge();
  361. mergeRange_row6_2.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  362. //第七行
  363. rowIndex++;
  364. var row7_cell2 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 1);
  365. row7_cell2.Value = "缺陷类型";
  366. row7_cell2.Style = row2_cell1.Style;
  367. var row7_cell3 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 2);
  368. row7_cell3.Value = "置信度";
  369. row7_cell3.Style = row2_cell1.Style;
  370. var row7_cell4 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 3);
  371. row7_cell4.Value = "面积";
  372. row7_cell4.Style = row2_cell1.Style;
  373. var row7_cell5 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 4);
  374. row7_cell5.Value = "对比度下限";
  375. row7_cell5.Style = row2_cell1.Style;
  376. var row7_cell6 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 5);
  377. row7_cell6.Value = "对比度上限";
  378. row7_cell6.Style = row2_cell1.Style;
  379. var row7_cell7 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 6);
  380. row7_cell7.Value = "所用模型版本";
  381. row7_cell7.Style = row2_cell1.Style;
  382. var row7_cell8 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 7);
  383. row7_cell8.Value = "或向选择";
  384. row7_cell8.Style = row2_cell1.Style;
  385. var row7_cell9 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 8);
  386. row7_cell9.Value = "本次检出数量";
  387. row7_cell9.Style = row2_cell1.Style;
  388. //第八行之后
  389. rowIndex++;
  390. for (int j = 1; j <= ProductDefects.DefectTotal.Count; j++) // 行
  391. {
  392. var temprowcel2 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 1);
  393. var temprowcel3 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 2);
  394. var temprowcel4 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 3);
  395. var temprowcel5 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 4);
  396. var temprowcel6 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 5);
  397. var temprowcel7 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 6);
  398. var temprowcel8 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 7);
  399. var temprowcel9 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 8);
  400. var tempItemDefectTotal = ProductDefects.DefectTotal[j - 1];
  401. temprowcel2.Value = tempItemDefectTotal.Name;
  402. temprowcel2.Style.Font.Bold = true;
  403. temprowcel2.Style.Font.FontSize = 10;
  404. temprowcel2.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  405. var temp = ProductDefects.Pdt.QualifiedLimitList.Find(x => SysMgr.Instance.GetDefectName(modelName, x.Code) == tempItemDefectTotal.Name);
  406. temprowcel3.Value = temp.ZXD;
  407. temprowcel3.Style.Font.Bold = true;
  408. temprowcel3.Style.Font.FontSize = 10;
  409. temprowcel3.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  410. temprowcel4.Value = temp.Area;
  411. temprowcel4.Style.Font.Bold = true;
  412. temprowcel4.Style.Font.FontSize = 10;
  413. temprowcel4.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  414. temprowcel5.Value = ContrastToPercent(temp.ContrastLower);
  415. temprowcel5.Style.Font.Bold = true;
  416. temprowcel5.Style.Font.FontSize = 10;
  417. temprowcel5.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  418. temprowcel6.Value = ContrastToPercent(temp.ContrastTop);
  419. temprowcel6.Style.Font.Bold = true;
  420. temprowcel6.Style.Font.FontSize = 10;
  421. temprowcel6.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  422. temprowcel7.Value = ProductDefects.Pdt.ModelName;
  423. temprowcel7.Style.Font.Bold = true;
  424. temprowcel7.Style.Font.FontSize = 10;
  425. temprowcel7.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  426. temprowcel8.Value = temp.IsOR;
  427. temprowcel8.Style.Font.Bold = true;
  428. temprowcel8.Style.Font.FontSize = 10;
  429. temprowcel8.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  430. temprowcel9.Value = tempItemDefectTotal.Count;
  431. temprowcel9.Style.Font.FontSize = 10;
  432. temprowcel9.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  433. }
  434. //更新行号
  435. rowIndex = rowIndex + ProductDefects.DefectTotal.Count;
  436. }
  437. #endregion
  438. #region 最后
  439. //rowIndex++;
  440. if (ProductDefects.DefectDetail != null && ProductDefects.DefectDetail.Count > 0)
  441. {
  442. List<string> lstRow5str = new List<string>() { "源图", "名称", "X(cm)", "Y(米)", "宽(mm)", "高(mm)", "置信度", "面积(mm^2)", "对比度" };
  443. for (int i = 0; i < lstRow5str.Count; i++)
  444. {
  445. var temp_row5_cell = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + i);
  446. temp_row5_cell.Value = lstRow5str[i];
  447. temp_row5_cell.Style.Font.Bold = true;
  448. temp_row5_cell.Style.Font.FontSize = 11;
  449. temp_row5_cell.Style.Font.FontName = "等线";
  450. temp_row5_cell.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  451. }
  452. rowIndex++;
  453. for (int i = 0; i < ProductDefects.DefectDetail.Count; i++)
  454. for (int j = 0; j < lstRow5str.Count; j++)
  455. {
  456. var temp_row6_cell = wsDefectsDetail.Row(rowIndex + i).Cell(cellIndex + j);
  457. switch (lstRow5str[j])
  458. {
  459. case "源图":
  460. temp_row6_cell.Value = ProductDefects.DefectDetail[i].Index;
  461. break;
  462. case "名称":
  463. temp_row6_cell.Value = ProductDefects.DefectDetail[i].Name;
  464. break;
  465. case "X(cm)":
  466. temp_row6_cell.Value = ProductDefects.DefectDetail[i].X;
  467. break;
  468. case "Y(米)":
  469. temp_row6_cell.Value = ProductDefects.DefectDetail[i].Y;
  470. break;
  471. case "宽(mm)":
  472. temp_row6_cell.Value = ProductDefects.DefectDetail[i].Width;
  473. break;
  474. case "高(mm)":
  475. temp_row6_cell.Value = ProductDefects.DefectDetail[i].Height;
  476. break;
  477. case "置信度":
  478. temp_row6_cell.Value = ProductDefects.DefectDetail[i].ZXD;
  479. break;
  480. case "面积(mm^2)":
  481. temp_row6_cell.Value = ProductDefects.DefectDetail[i].Area;
  482. break;
  483. case "对比度":
  484. temp_row6_cell.Value = ProductDefects.DefectDetail[i].Contrast;
  485. break;
  486. }
  487. temp_row6_cell.Style.Font.FontSize = 11;
  488. temp_row6_cell.Style.Font.FontName = "等线";
  489. temp_row6_cell.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  490. }
  491. // row6_cell1.InsertTable(ProductDefects.DefectDetail);
  492. }
  493. #endregion
  494. var wsDefectsImg = workbook.Worksheets.Add("正面疵点分布图");
  495. wsDefectsImg.AddPicture(new MemoryStream(defectImage), "纵向计算")
  496. .MoveTo(wsDefectsImg.Cell(1, 1));
  497. var wsFaceWidthImg = workbook.Worksheets.Add("门幅曲线");
  498. wsFaceWidthImg.AddPicture(new MemoryStream(faceWidthImage), "幅宽曲线图")
  499. .MoveTo(wsFaceWidthImg.Cell(1, 1));
  500. workbook.SaveAs(savePath);
  501. }
  502. // return true;
  503. //}
  504. //catch (Exception ex)
  505. //{
  506. // return false;
  507. //}
  508. }
  509. private double ContrastLow = 0.8;
  510. private double ContrastTop = 1.2;
  511. private double ContrastToPercent(double val)
  512. {
  513. if (val < ContrastLow)
  514. return 0;
  515. else if (val > ContrastTop)
  516. return 100;
  517. double temp = 100 / (ContrastTop - ContrastLow);
  518. return Math.Round(temp * (val - ContrastLow), 2);
  519. }
  520. //
  521. /// <summary>
  522. /// 重新生成缺陷分布(cm2M在内部转换)
  523. /// </summary>
  524. /// <param name="lstDefectInfo">Records.DefectInfoList</param>
  525. /// <param name="XSizeRange">幅宽</param>
  526. /// <param name="YSizeRange">卷长度</param>
  527. private void reDrawDefectPoints(Records records, double[] XSizeRange, double[] YSizeRange)
  528. {
  529. //AddTextEvent($"绘图", $"缺陷分布, W={string.Join(", ", XSizeRange)},H={string.Join(", ", YSizeRange)}, LastData={JsonConvert.SerializeObject(lstDefectInfo[lstDefectInfo.Count - 1])}");
  530. var lstData = records.DefectInfoList.OrderBy(m => m.Code).ThenBy(m => m.Code).ToList();
  531. if (XSizeRange == null || YSizeRange == null)
  532. return;
  533. else
  534. {
  535. if (YSizeRange[0] == YSizeRange[1])
  536. {
  537. YSizeRange[0] -= YSizeRange[0] / 10f;
  538. YSizeRange[1] += YSizeRange[1] / 10f;
  539. }
  540. YSizeRange[0] /= 100;
  541. YSizeRange[1] /= 100;
  542. lineChartDefect.Series.Clear();
  543. lineChartDefect.ChartAreas[0].AxisX.Title = "宽度(cm)";
  544. lineChartDefect.ChartAreas[0].AxisY.Title = "长度(m)";
  545. //option = new UILineOption();
  546. //option.XAxis.Name = "面宽(cm)";
  547. //option.YAxis.Name = "长度(米)";
  548. //option.Grid.Top = 20;//边距
  549. //option.Grid.Right = 20;//边距
  550. foreach (var rowItem in lstData)
  551. {
  552. string name = SysMgr.Instance.GetDefectName(records.ProductInfo.ModelName, rowItem.Code);
  553. if (lineChartDefect.Series.FindByName(name) == null)//rowItem[3] 为name
  554. {
  555. lineChartDefect.Series.Add(name);
  556. var obj = SysMgr.Instance.GetDefectLabel(records.ProductInfo.ModelName, name);
  557. int labelIndex = obj.Value<int>("id");
  558. lineChartDefect.Series[name].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
  559. lineChartDefect.Series[name].MarkerSize = 5;
  560. lineChartDefect.Series[name].MarkerColor = ChartColor[labelIndex];
  561. lineChartDefect.Series[name].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
  562. }
  563. lineChartDefect.Series[name].Points.AddXY(rowItem.CentreX, rowItem.CentreY /100);
  564. }
  565. lineChartDefect.ChartAreas[0].AxisX.Minimum = XSizeRange[0];
  566. lineChartDefect.ChartAreas[0].AxisX.Maximum = XSizeRange[1];
  567. lineChartDefect.ChartAreas[0].AxisY.Minimum = YSizeRange[0];
  568. lineChartDefect.ChartAreas[0].AxisY.Maximum = YSizeRange[1];
  569. //X轴数据类型
  570. //option.XAxisType = UIAxisType.Value;
  571. //设置X/Y轴显示范围
  572. //option.XAxis.SetRange(XSizeRange[0], XSizeRange[1]);
  573. //option.YAxis.SetRange(YSizeRange[0], YSizeRange[1]);
  574. //坐标轴显示小数位数
  575. //option.XAxis.AxisLabel.DecimalPlaces = option.YAxis.AxisLabel.DecimalPlaces = 1;
  576. //X/Y轴画参考线
  577. //option.YAxisScaleLines.Add(new UIScaleLine("上限", 3.5, Color.Red));
  578. //option.YAxisScaleLines.Add(new UIScaleLine("下限", 2.2, Color.Gold));
  579. //option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(3).DateTimeString(), dt.AddHours(3), Color.Red));
  580. //option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(6).DateTimeString(), dt.AddHours(6), Color.Red));
  581. //option.ToolTip.Visible = true;
  582. //option.ToolTip.Formatter = "怎么自定义X,Y显示名称??{X}";
  583. //option.Title = new UITitle();
  584. //option.Title.Text = "";
  585. //option.Title.SubText = "";
  586. }
  587. //string preCode = "";
  588. //UILineSeries series = null;
  589. //foreach (var item in lstData)
  590. //{
  591. // if (preCode != item.Code)//加一组新类型及样式
  592. // {
  593. // preCode = item.Code;
  594. // var one = Config.getDefectItem(item.Code);
  595. // if (one == null)
  596. // {
  597. // continue;
  598. // }
  599. // JObject objItem = one as JObject;
  600. // Color color = ColorTranslator.FromHtml(objItem.Value<string>("color"));
  601. // series = option.AddSeries(new UILineSeries(objItem.Value<string>("name"), color));//加一组
  602. // series.Symbol = UILinePointSymbol.Star;
  603. // series.SymbolSize = 4;
  604. // series.SymbolLineWidth = 2;
  605. // series.ShowLine = false;
  606. // series.SymbolColor = color;
  607. // //数据点显示小数位数(针对当前UILineSeries)
  608. // series.XAxisDecimalPlaces = 1;
  609. // series.YAxisDecimalPlaces = 2;
  610. // //series.Smooth = false;
  611. // }
  612. // series.Add(item.CentreX, item.CentreY / 100); //cm -> m
  613. //}
  614. //====
  615. //option.GreaterWarningArea = new UILineWarningArea(3.5);
  616. //option.LessWarningArea = new UILineWarningArea(2.2, Color.Gold);
  617. //this.BeginInvoke(new System.Action(() =>
  618. //{
  619. //this.lineChartDefect.SetOption(option);
  620. //series.UpdateYData();//按序号更新Y轴值(可设置值超出范围用于闪烁)
  621. //}));
  622. }
  623. /// <summary>
  624. /// 重新门幅宽度
  625. /// </summary>
  626. /// <param name="points"></param>
  627. /// <param name="XSizeRange">卷长度</param>
  628. /// <param name="YSizeRange">幅宽</param>
  629. private void reDrawFaceWidth(List<float[]> points, double[] XSizeRange, double[] YSizeRange)
  630. {
  631. //AddTextEvent($"绘图", $"门幅宽度, W={string.Join(", ", XSizeRange)},H={string.Join(", ", YSizeRange)}, LastData={JsonConvert.SerializeObject(points[points.Count-1])}");
  632. if (YSizeRange[0] == YSizeRange[1])
  633. {
  634. YSizeRange[0] -= YSizeRange[0] / 10f;
  635. YSizeRange[1] += YSizeRange[1] / 10f;
  636. }
  637. XSizeRange[0] /= 100;
  638. XSizeRange[1] /= 100;
  639. //防止超限
  640. XSizeRange[1] += 0.01;
  641. YSizeRange[1] += 0.1;
  642. lineChartFaceWidth.Series.Clear();
  643. lineChartFaceWidth.Series.Add("面宽");
  644. lineChartFaceWidth.ChartAreas[0].AxisX.Title = "长度(m)";
  645. lineChartFaceWidth.ChartAreas[0].AxisY.Title = "宽度(cm)";
  646. lineChartFaceWidth.Series["面宽"].ChartType = SeriesChartType.Line;
  647. lineChartFaceWidth.Series["面宽"].MarkerSize = 5;
  648. lineChartFaceWidth.Series["面宽"].MarkerColor = Color.Blue;
  649. lineChartFaceWidth.Series["面宽"].MarkerStyle = MarkerStyle.Circle;
  650. //UILineOption option = new UILineOption();
  651. //option.XAxis.Name = "长度(米)";
  652. //option.YAxis.Name = "面宽(cm)";
  653. //option.Grid.Top = 20;
  654. //option.Grid.Right = 20;
  655. //X轴数据类型
  656. //option.XAxisType = UIAxisType.Value;
  657. //设置X/Y轴显示范围
  658. //option.XAxis.SetRange(XSizeRange[0], XSizeRange[1]);
  659. //option.YAxis.SetRange(YSizeRange[0], YSizeRange[1]);
  660. //坐标轴显示小数位数
  661. //option.XAxis.AxisLabel.DecimalPlaces = option.YAxis.AxisLabel.DecimalPlaces = 1;
  662. //X/Y轴画参考线
  663. //option.YAxisScaleLines.Add(new UIScaleLine("上限", 3.5, Color.Red));
  664. //option.YAxisScaleLines.Add(new UIScaleLine("下限", 2.2, Color.Gold));
  665. //option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(3).DateTimeString(), dt.AddHours(3), Color.Red));
  666. //option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(6).DateTimeString(), dt.AddHours(6), Color.Red));
  667. //option.ToolTip.Visible = true;
  668. //option.ToolTip.Formatter = "怎么自定义X,Y显示名称??{X}";
  669. //option.Title = new UITitle();
  670. //option.Title.Text = "";
  671. //option.Title.SubText = "";
  672. //Color color = Color.Blue;
  673. //UILineSeries series = null;
  674. //series = option.AddSeries(new UILineSeries("面宽", color));
  675. //series.Symbol = UILinePointSymbol.Circle;
  676. //series.ShowLine = true;
  677. //series.SymbolSize = 4;
  678. //series.SymbolLineWidth = 2;
  679. //series.SymbolColor = color;
  680. ////数据点显示小数位数(针对当前UILineSeries)
  681. //series.XAxisDecimalPlaces = 2;
  682. //series.YAxisDecimalPlaces = 1;
  683. lineChartFaceWidth.ChartAreas[0].AxisY.Maximum = YSizeRange[1];
  684. lineChartFaceWidth.ChartAreas[0].AxisY.Minimum = YSizeRange[0];
  685. lineChartFaceWidth.ChartAreas[0].AxisX.Maximum = XSizeRange[1];
  686. lineChartFaceWidth.ChartAreas[0].AxisX.Minimum = XSizeRange[0];
  687. float x;
  688. foreach (var item in points)
  689. {
  690. x = item[0] / 100; //cm -> m
  691. //series.Add(x, item[1]);
  692. this.lineChartFaceWidth.Series["面宽"].Points.AddXY((float)Math.Round(x, 2), (float)Math.Round(item[1], 2));
  693. }
  694. //====
  695. //option.GreaterWarningArea = new UILineWarningArea(3.5);
  696. //option.LessWarningArea = new UILineWarningArea(2.2, Color.Gold);
  697. //this.BeginInvoke(new System.Action(() =>
  698. //{
  699. //this.lineChartFaceWidth.SetOption(option);
  700. //}));
  701. }
  702. // 截图操作函数
  703. private byte[] captureControl(System.Windows.Forms.Control control)
  704. {
  705. Bitmap bmp = new Bitmap(control.Width, control.Height);
  706. Graphics graphics = Graphics.FromImage(bmp);
  707. Rectangle rectangle = new Rectangle(0, 0, control.Width, control.Height);
  708. control.DrawToBitmap(bmp, rectangle);
  709. //bmp to jpg
  710. MemoryStream ms = new MemoryStream();
  711. bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//bmp是已经获得的bitmap数据
  712. byte[] bytes = ms.GetBuffer();
  713. ms.Close();
  714. graphics.Dispose();
  715. return bytes;
  716. //bitmap.Save(@"C:\Images\Capture.jpg", ImageFormat.Jpeg);
  717. //return Image.FromStream(new MemoryStream(bytes));
  718. }
  719. public class JsonProductDefects
  720. {
  721. [Description("产品品名")]
  722. public string ProName { get; set; }
  723. [Description("产品批号")]
  724. public string BatchId { get; set; }
  725. [Description("产品卷号")]
  726. public string ReelId { get; set; }
  727. [Description("检验长度")]
  728. public string Len { get; set; }
  729. [Description("平均速度")]
  730. public string Speed { get; set; }
  731. [Description("等级")]
  732. public string Grade { get; set; }
  733. [Description("检验时间")]
  734. public string DateTime { get; set; }
  735. [Description("疵点统计")]
  736. public List<JDefectTotal> DefectTotal = new List<JDefectTotal>();
  737. public List<JDefectDetail> DefectDetail = new List<JDefectDetail>();
  738. [Description("检测设置")]
  739. public Models.Product Pdt { get; set; }
  740. [Description("物面分辨率")]
  741. public string xyPix { get; set; }
  742. }
  743. public class JDefectTotal
  744. {
  745. [Description("疵点名")]
  746. public string Name { get; set; }
  747. [Description("疵点数")]
  748. public int Count { get; set; }
  749. }
  750. public class JDefectDetail
  751. {
  752. [Description("源图")]
  753. public int Index { get; set; }
  754. [Description("名称")]
  755. public string Name { get; set; }
  756. [Description("X(cm)")]
  757. public double X { get; set; }
  758. [Description("Y(米)")]
  759. public double Y { get; set; }
  760. [Description("宽(cm)")]
  761. public double Width { get; set; }
  762. [Description("高(cm)")]
  763. public double Height { get; set; }
  764. [Description("置信度")]
  765. public double ZXD { get; set; }
  766. [Description("面积(cm^2)")]
  767. public double Area { get; set; }
  768. [Description("对比度")]
  769. public double Contrast { get; set; }
  770. }
  771. }
  772. }