using IKapC.NET;
using LeatherApp.Device;
using LeatherApp.Device.CamerUtil;
using LeatherApp.Interface;
using LeatherApp.Utils;
using Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using S7.Net;
using SqlSugar;
using Sunny.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LeatherApp.Page
{
public partial class FHome : UIPage
{
private WarningEnum warningLevel;//警告等级
private CurrentStateEnum currentState;//当前状态
private DevContainer devContainer = new DevContainer();
private Service.ProductService svcProduct = new Service.ProductService();
private Service.RecordsService svcRecord = new Service.RecordsService();
private bool defectPauseForUser = false;
//private Models.Product currProductModel = null;//当前产品
//private Models.Records record = null;
private Stopwatch pStopWatch=new Stopwatch();//计算速度用,暂停时停止计数
private object lockScanPhoto = new object();
private object lockCurrKey = new object();
private int currKey=0;
private Hashtable htTask = new Hashtable();//默认单线程写入不用lock, 多线程安全同步读取用Synchronized
//无产品编码时加载
FProductInfo frmProduct;
public FHome(FProductInfo frm)
{
InitializeComponent();
frmProduct = frm;
if(Config.Camer_Name == CamerDevNameEnum.埃科)
IKapCLib.ItkManInitialize();
#region dataGridView设置
uiDataGridView1.AllowUserToAddRows = uiDataGridView1.AllowUserToDeleteRows = false;//用户添加删除行
uiDataGridView1.AllowUserToResizeRows = false;//用户调整行大小
uiDataGridView1.AllowUserToResizeColumns = false;//用户调整列大小
uiDataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//只可选中整行,不是单元格
//显示行号与列宽度自动调整
uiDataGridView1.RowHeadersVisible = true;
uiDataGridView1.RowHeadersWidth = 60;
uiDataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
uiDataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变
uiDataGridView1.RowPostPaint += (sender, e) =>//序号列头
{
Utils.Util.showRowNum_onDataGrid_RowPostPaint(this.uiDataGridView1, sender, e);
//Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, uiDataGridView1.RowHeadersWidth - 4, e.RowBounds.Height);
//TextRenderer.DrawText(e.Graphics, (e.RowIndex).ToString(), uiDataGridView1.RowHeadersDefaultCellStyle.Font, rectangle, uiDataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
};
for (int i = 0; i < uiDataGridView1.Columns.Count; i++)//禁止点击列头排序
uiDataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
////行列交叉处标题
//if (dataGridView1.RowHeadersVisible) dataGridView1.TopLeftHeaderCell.Value = "SPH/CYL";
//事件
//this.uiDataGridView1.DataBindingComplete += this.uiDataGridView1_DataBindingComplete;//bing data时发生,可修改单元格内容
//this.uiDataGridView1.SelectIndexChange += uiDataGridView1_SelectIndexChange;//选择行时发行
this.uiDataGridView1.CellClick += uiDataGridView1_CellClick; ;
#endregion
this.ucColorListDefect.ColorChanged = (xcode, xcolor) =>
{
try
{
var item=Config.getDefectItem(xcode);
if(item != null)
{
item["color"] = xcolor;
Config.SaveDefectItemList(Config.defectItemList);
}
}
catch (Exception ex)
{
this.AddTextEvent(DateTime.Now,"事件", "缺陷颜色修改后保存失败!");
}
};
}
private void uiDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0 || currKey == 0)
return;
Records record = Hashtable.Synchronized(htTask)[currKey] as Records;
//var defectInfo = record.DefectInfoList[record.DefectInfoList.Count - e.RowIndex-1];//按顺序索引引用
var defectInfo = record.DefectInfoList.FirstOrDefault(m => m.uid == (long)this.uiDataGridView1.CurrentRow.Cells["colUid"].Value);
if(defectInfo == null)
{
UIMessageTip.ShowError("此记录已不存在!", 1000);
return;
}
this.picDefectImage.loadImage(defectInfo.image);
//选中
this.Invoke(new System.Action(() =>
{
if (lineChartDefect.Option.Series != null && lineChartDefect.Option.Series.ContainsKey("SELECT"))
{
lineChartDefect.Option.Series["SELECT"].Clear();
lineChartDefect.Option.Series["SELECT"].Add( defectInfo.CentreX, defectInfo.CentreY / 100);
lineChartDefect.Refresh();
}
}));
}
private void lineChartDefect_Click(object sender, EventArgs e)
{
if (lineChartDefect.Option.Series != null && lineChartDefect.Option.Series.ContainsKey("SELECT") && lineChartDefect.Option.Series["SELECT"].DataCount > 0)
{
lineChartDefect.Option.Series["SELECT"].Clear();
lineChartDefect.Refresh();
}
}
#region 内部方法
private void resetUIValue(bool resetCurrKey=true)
{
firstTest = true;
if(resetCurrKey)
currKey = 0;
pStopWatch.Reset();
this.Invoke(new System.Action(() =>
{
lblLen.Text = "0米";
lblSpeed.Text = "速度:0米/分";
txtBarCodeName.Text = txtBatchId.Text = txtReelId.Text = "";
numErpLen.Text = "0";
this.lineChartDefect.SetOption(new UILineOption());
this.lineChartFaceWidth.SetOption(new UILineOption());
this.uiDataGridView1.DataSource = null;
this.uiDataGridView1.Rows.Clear();
this.lstboxLog.Items.Clear();
this.picDefectImage.clear();
this.picScanner1.Image = this.picScanner2.Image = null;
picScanner1.Refresh();
picScanner2.Refresh();
}));
}
///
/// 全局中断
///
private void globalBreakEvent(int portIndex, byte data)
{
AddTextEvent(DateTime.Now,"I/0状态", $"{portIndex}:{Convert.ToString(data, 2)}");
if (compareIOInput(CMDName.启动按钮) && this.btnStart.Enabled)
{
this.devContainer.devIOCard.writeBitState(0, 0, true);
this.startCommand();
Task.Run(async () =>
{
await Task.Delay(500);
this.devContainer.devIOCard.writeBitState(0, 0, false);
});
}
else if (compareIOInput(CMDName.暂停按钮) && this.btnPause.Enabled)
{
this.devContainer.devIOCard.writeBitState(0, 1, true);
this.pauseCommand();//true 输出暂停不会触发输入暂停
Task.Run(async () =>
{
await Task.Delay(500);
this.devContainer.devIOCard.writeBitState(0, 1, false);
});
}
//else if (compareIOInput(CMDName.复位按钮) && this.tsbtnReset.Enabled)
// resetCommand();
//else if (!this.disableBuzzer && compareIOInput(CMDName.门磁告警输入))
// warning(WarningEnum.Low, true);
//else if (!this.disableBuzzer && compareIOInput(CMDName.喷墨告警输入))
// warning(WarningEnum.Low, true);
}
private bool compareIOInput(CMDName key)
{
JObject joJson = Config.CMDProcess[key];
IODirectionEnum direction = (IODirectionEnum)joJson.Value("Direction");
if (direction == IODirectionEnum.输入 || direction == IODirectionEnum.输入输出)
{
return Util.compareIOInput(
joJson.Value("IN_OP_SHOW").ToObject>().ToArray(),
devContainer.devIOCard.DIData);
}
return false;
}
///
/// 报警,只响应low,high
///
private void warning(WarningEnum level, bool buzzer = true)
{
if (level == WarningEnum.Normal)
return;
//lock (myLock)
warningLevel = level;
//if ((int)level >= (int)WarningEnum.Low)//暂停
{
//currentState = CurrentStateEnum.暂停;
if (!Config.StopPLC)
this.devContainer.devPlc.pauseDev();
else if (!Config.StopIO && devContainer.devIOCard.IsInit)
{
this.devContainer.devIOCard.writeBitState(0, 1, true);
Task.Run(async () =>
{
await Task.Delay(500);
this.devContainer.devIOCard.writeBitState(0, 1, false);
});
}
pauseCommand(buzzer);
}
//else if (level == WarningEnum.High)//急停
//{
// currentState = CurrentStateEnum.急停;
// devContainer.devAxis.stopNow();
// stopNowCommand();
//}
//启用报警消除按钮
//this.Invoke(new System.Action(() =>
//{
// tsbtnWarning.Enabled = true;
//}));
}
///
/// 重新生成缺陷分布(cm2M在内部转换)
///
/// Records.DefectInfoList
/// 幅宽
/// 卷长度
private void reDrawDefectPoints(List lstDefectInfo, double[] XSizeRange=null, double[] YSizeRange=null,bool addSelRect=true)
{
UILineOption option;
//AddTextEvent(DateTime.Now,$"绘图", $"缺陷分布, W={string.Join(", ", XSizeRange)},H={string.Join(", ", YSizeRange)}, LastData={JsonConvert.SerializeObject(lstDefectInfo[lstDefectInfo.Count - 1])}");
var lstData = lstDefectInfo.OrderBy(m => m.Code).ThenBy(m => m.Code).ToList();
if (XSizeRange == null || YSizeRange == null)
option = this.lineChartDefect.Option;
else
{
if (YSizeRange[0] == YSizeRange[1])
{
YSizeRange[0] -= YSizeRange[0] / 10f;
YSizeRange[1] += YSizeRange[1] / 10f;
}
YSizeRange[0] /= 100;
YSizeRange[1] /= 100;
option = new UILineOption();
option.XAxis.Name = "面宽(cm)";
option.YAxis.Name = "长度(米)";
//option.Grid.Top = 20;//边距
option.Grid.Right = 20;//边距
//X轴数据类型
option.XAxisType = UIAxisType.Value;
//设置X/Y轴显示范围
option.XAxis.SetRange(XSizeRange[0], XSizeRange[1]);
option.YAxis.SetRange(YSizeRange[0], YSizeRange[1]);
//坐标轴显示小数位数
option.XAxis.AxisLabel.DecimalPlaces = option.YAxis.AxisLabel.DecimalPlaces = 1;
//X/Y轴画参考线
//option.YAxisScaleLines.Add(new UIScaleLine("上限", 3.5, Color.Red));
//option.YAxisScaleLines.Add(new UIScaleLine("下限", 2.2, Color.Gold));
//option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(3).DateTimeString(), dt.AddHours(3), Color.Red));
//option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(6).DateTimeString(), dt.AddHours(6), Color.Red));
option.ToolTip.Visible = true;
//option.ToolTip.Formatter = "怎么自定义X,Y显示名称??{X}";
option.Title = new UITitle();
option.Title.Text = "";
option.Title.SubText = "";
}
option.Series.Clear();
string preCode = "";
UILineSeries series=null;
foreach (var item in lstData)
{
if (preCode != item.Code)//加一组新类型及样式
{
preCode = item.Code;
var one = Config.getDefectItem(item.Code);
if (one == null)
{
AddTextEvent(DateTime.Now,$"绘图", $"getDefectItem({item.Code}) is null!");
continue;
}
JObject objItem= one as JObject;
Color color = ColorTranslator.FromHtml(objItem.Value("color"));
series = option.AddSeries(new UILineSeries(objItem.Value("name"), color));//加一组
series.Symbol = UILinePointSymbol.Star;
series.SymbolSize = 4;
series.SymbolLineWidth = 2;
series.ShowLine = false;
series.SymbolColor = color;
//数据点显示小数位数(针对当前UILineSeries)
series.XAxisDecimalPlaces = 1;
series.YAxisDecimalPlaces = 2;
//series.Smooth = false;
}
series.Add( item.CentreX, item.CentreY / 100); //cm -> m
}
////加一组框选
if (addSelRect && !option.Series.ContainsKey("SELECT"))
{
series=option.AddSeries(new UILineSeries("SELECT", Color.Red));
series.Symbol = UILinePointSymbol.Circle;
series.SymbolSize = 6;
series.SymbolLineWidth = 3;
series.ShowLine = false;
series.SymbolColor = Color.Red;
//数据点显示小数位数(针对当前UILineSeries)
series.XAxisDecimalPlaces = 1;
series.YAxisDecimalPlaces = 2;
//series.Add(1, 1);
}
//====
//option.GreaterWarningArea = new UILineWarningArea(3.5);
//option.LessWarningArea = new UILineWarningArea(2.2, Color.Gold);
this.BeginInvoke(new System.Action(() =>
{
this.lineChartDefect.SetOption(option);
//series.UpdateYData();//按序号更新Y轴值(可设置值超出范围用于闪烁)
}));
}
///
/// 重新门幅宽度
///
///
/// 卷长度
/// 幅宽
private void reDrawFaceWidth(List points, double[] XSizeRange, double[] YSizeRange)
{
//AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度, W={string.Join(", ", XSizeRange)},H={string.Join(", ", YSizeRange)}, LastData={JsonConvert.SerializeObject(points[points.Count-1])}");
if(YSizeRange[0]== YSizeRange[1])
{
YSizeRange[0] -= YSizeRange[0] / 10f;
YSizeRange[1] += YSizeRange[1] / 10f;
}
XSizeRange[0] /= 100;
XSizeRange[1] /= 100;
//防止超限
XSizeRange[1] += 0.01;
YSizeRange[1] += 0.1;
UILineOption option = new UILineOption();
option.XAxis.Name = "长度(米)";
option.YAxis.Name = "面宽(cm)";
option.Grid.Top = 20;
option.Grid.Right = 20;
//X轴数据类型
option.XAxisType = UIAxisType.Value;
//设置X/Y轴显示范围
option.XAxis.SetRange(XSizeRange[0], XSizeRange[1]);
option.YAxis.SetRange(YSizeRange[0], YSizeRange[1]);
//坐标轴显示小数位数
option.XAxis.AxisLabel.DecimalPlaces = option.YAxis.AxisLabel.DecimalPlaces = 1;
//X/Y轴画参考线
//option.YAxisScaleLines.Add(new UIScaleLine("上限", 3.5, Color.Red));
//option.YAxisScaleLines.Add(new UIScaleLine("下限", 2.2, Color.Gold));
//option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(3).DateTimeString(), dt.AddHours(3), Color.Red));
//option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(6).DateTimeString(), dt.AddHours(6), Color.Red));
option.ToolTip.Visible = true;
//option.ToolTip.Formatter = "怎么自定义X,Y显示名称??{X}";
option.Title = new UITitle();
option.Title.Text = "";
option.Title.SubText = "";
Color color = Color.Blue;
UILineSeries series = null;
series = option.AddSeries(new UILineSeries("面宽", color));
series.Symbol = UILinePointSymbol.Circle;
series.ShowLine = true;
series.SymbolSize = 1;//4
series.SymbolLineWidth = 1;//2
series.SymbolColor = color;
//数据点显示小数位数(针对当前UILineSeries)
series.XAxisDecimalPlaces = 2;
series.YAxisDecimalPlaces = 1;
float x;
foreach (var item in points)
{
x = item[0] / 100; //cm -> m
series.Add(x, item[1]);
if (x < XSizeRange[0]) AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度超限 1!!!! {x}<{XSizeRange[0]}",WarningEnum.High);
if (x > XSizeRange[1]) AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度超限 2!!!! {x}>{XSizeRange[1]}", WarningEnum.High);
if (item[1] < YSizeRange[0]) AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度超限 3!!!! {item[1]}<{YSizeRange[0]}", WarningEnum.High);
if (item[1] > YSizeRange[1]) AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度超限 4!!!! {item[1]}>{YSizeRange[1]}", WarningEnum.High);
}
//====
//option.GreaterWarningArea = new UILineWarningArea(3.5);
//option.LessWarningArea = new UILineWarningArea(2.2, Color.Gold);
this.BeginInvoke(new System.Action(() =>
{
this.lineChartFaceWidth.SetOption(option);
}));
}
private delegate void AddTextDelegate(DateTime time,string tag, string msg, WarningEnum level);
private void AddTextEvent(DateTime now, string tag, string msg, WarningEnum level = WarningEnum.Normal)
{
try
{
if (InvokeRequired)
{
Invoke(new AddTextDelegate(AddTextEvent), new object[]
{
now,
tag,
msg,
level
});
}
else
{
if (tag != null && tag != "")
tag = $" - [{tag}]";
//var now = DateTime.Now;
msg = now.ToString("HH:mm:ss fff") + tag + " - " + msg;
//cont = MyHelper.subString(cont, 300);
//写日志,warn和error日志直接写
writeLog(now, level, msg);
//
//if (type > 0)
// cont = $"{cont}";
//msg = (level == WarningEnum.Normal ? "B" : level == WarningEnum.Low ? "Y" : "R") + msg;
msg = (level == WarningEnum.Normal ? "" : level == WarningEnum.Low ? "Y" : "R") + msg;
//this.Invoke(new System.Action(() =>
//{
if (this.lstboxLog.Items.Count > 1000)
this.lstboxLog.Items.Clear();
lstboxLog.Items.Insert(0, msg);
//}));
//日志滚动
//lstLog.SelectedIndex = lstLog.Items.Count - 1;
}
}
catch (Exception ex)
{
//MessageBox.Show("AddTextEvent ex=(" + ex.Message + ")", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
}
}
private void writeLog(DateTime now, WarningEnum level, string text)
{
string directory = Config.LogPath + "\\" + DateTime.Now.ToString("yyyyMM") + "\\";
//if (type == 0) directory = Application.StartupPath + "\\Log\\Info\\";
//else if (type == 1) directory = Application.StartupPath + "\\Log\\Warn\\";
//else directory = Application.StartupPath + "\\Log\\Error\\";
if (!System.IO.Directory.Exists(directory))
System.IO.Directory.CreateDirectory(directory);
File.AppendAllText(directory + now.ToString("yyyyMMdd") + ".log", text + "\r\n");
}
#endregion
private void FHome_Load(object sender, EventArgs e)
{
ucColorListDefect.initData(Config.defectItemList);
this.lineChartDefect.SetOption(new UILineOption());
this.lineChartFaceWidth.SetOption(new UILineOption());
}
private void FHome_Shown(object sender, EventArgs e)
{
//picScanner1.BackColor = Color.Red;
//picScanner2.BackColor = Color.Green;
picScanner1.Width = picScanner2.Width = pnlScannerImg.ClientSize.Width / 2 - 5;
picScanner2.Left = picScanner1.Width + 5;
}
//开机
private void btnOpen_Click(object sender, EventArgs e)
{
//this.txtNote.Text = (currProductModel == null ? "NO" : currProductModel.Name);
Config.LoadAllConfig();
//设置程序最小/大线程池
// Get the current settings.
int minWorker, minIOC;
ThreadPool.GetMinThreads(out minWorker, out minIOC);
ThreadPool.SetMinThreads(25, minIOC);
//ThreadPool.SetMaxThreads(256, 256);
this.btnOpen.Enabled = false;
this.resetUIValue();
AddTextEvent(DateTime.Now,"设备启动", $"正在启动硬件设备(版本:v{ Assembly.GetExecutingAssembly().GetName().Version.ToString()})。。。");
warningLevel = WarningEnum.Normal;
this.btnStart.Enabled = this.btnEnd.Enabled = this.btnPause.Enabled = false;
//后台线程回调事件
devContainer = new DevContainer();
devContainer.StateChange = (state, msg) =>
{
if (state)
{
currentState = CurrentStateEnum.初始;
AddTextEvent(DateTime.Now,"设备启动", $"启动成功,请点开始验布按钮!");
//全局中断
devContainer.devIOCard.INEvent = globalBreakEvent;
// I/O reset后,输出默认状态
if (devContainer.devIOCard.IsInit)
{
AddTextEvent(DateTime.Now,"复位", $"I/O复位...");
if (!devContainer.devIOCard.reset())
{
AddTextEvent(DateTime.Now,"复位", "I/O板卡复位失败!", WarningEnum.High);
return;
}
if (!devContainer.io_output(CMDName.IO默认输出))
{
//AddTextEvent(DateTime.Now,"复位", "I/O板卡复位默认值失败!", WarningEnum.High);
//return;
}
int portIndex = 0;
foreach(var data in devContainer.devIOCard.DIData)
AddTextEvent(DateTime.Now,"I/0状态", $"DIData: {portIndex++}-{Convert.ToString(data, 2)}");
}
devContainer.OutDebugEvent = (tag, debugInfo) =>
{
AddTextEvent(DateTime.Now,tag, debugInfo);
};
//
this.Invoke(new System.Action(() =>
{
this.btnOpen.Enabled = false;
this.btnClose.Enabled = true;
this.btnStart.Enabled = true;
this.tcbarLightValue.Enabled = true;
tcbarLightValue.Value=devContainer.devLight.getDigitalValue(1);
}));
devContainer.libPhoto.QueueCountEvent = (count) =>
{
this.BeginInvoke(new System.Action(() =>
{
this.lblWaitImageCount.Text = count.ToString();
}));
};
devContainer.libDefect.QueueCountEvent = (type, count) =>
{
this.BeginInvoke(new System.Action(() =>
{
switch (type)
{
case 0: this.lblDefectQueue0.Text = count.ToString(); break;
case 1: this.lblDefectQueue1.Text = count.ToString(); break;
case 2: this.lblDefectQueue2.Text = count.ToString(); break;
}
}));
};
//在【暂停】和【运行中】时都可以扫码,扫码后即开始即结束上一卷(如存在),自动开始下一卷,等待相机照片回调
devContainer.devCodeScanner.ScanerEvent = (barCode) =>
{
int errCode = 0;
try
{
//AddTextEvent(DateTime.Now,"扫码", $"{code}:{currentState.ToString()}");
if (!devContainer.state || string.IsNullOrWhiteSpace(barCode)
|| (currentState != CurrentStateEnum.运行中 && currentState != CurrentStateEnum.暂停))
return;
string barCodeName="",len = "0", batchId = "", reelId = "";
if (!string.IsNullOrWhiteSpace(Config.ErpDBConStr) && !string.IsNullOrWhiteSpace(Config.ErpSql) && !string.IsNullOrWhiteSpace(barCode))
{
AddTextEvent(DateTime.Now,"扫码", $"产品条码({barCode})到ERP查询对应数据...", WarningEnum.Normal);
var rowData = this.loadErpData(barCode);
if (rowData == null)
{
AddTextEvent(DateTime.Now,"扫码", $"产品条码({barCode})无对应ERP数据,不做响应!", WarningEnum.Low);
return;
}
barCodeName = rowData[0].ToString();
if (rowData.ItemArray.Length > 1) len = rowData[1].ToString();
if (rowData.ItemArray.Length > 2) batchId = rowData[2].ToString();
if (rowData.ItemArray.Length > 3) reelId = rowData[3].ToString();
}
else
barCodeName = barCode;//没有ERP对应关系时直接使用条码做为品名
errCode = 1;
//SHNY-PX-6-***
string[] codes = barCodeName.Split(new char[] { '-' });
if (codes.Length < 4)
{
AddTextEvent(DateTime.Now,"扫码", $"产品品名({barCodeName})格式错误,不做响应!", WarningEnum.Low);
return;
}
//新开始
this.resetUIValue(false);
errCode = 2;
//加载新产品
string pcode = "1-" + codes[2];
if (codes[1] == "0" || Config.SuedeList.Contains(codes[1]))
pcode = "0-" + codes[2];
var productInfo = svcProduct.GetModelNav(pcode); //frmProduct.loadProduct(code);
Records record;
lock (lockCurrKey)
{
errCode = 3;
//保存,这里队列图片可能还未检测完
if (currKey > 0)
{
string szBatchId, szReelId;
double ldErpLen;
szBatchId = txtBatchId.Text.Trim();
szReelId = txtReelId.Text.Trim();
ldErpLen = Convert.ToDouble(numErpLen.Text.Trim());
//BatchId = code,//code[2]
//ReelId = "1",//code[3]
int mykey = currKey;
Task.Run(() => { saveCurrRecord(mykey, szBatchId, szReelId, ldErpLen); });
currKey = 0;
}
if (productInfo == null)
{
AddTextEvent(DateTime.Now,"扫码", $"编码({pcode})对应配方不存在,请先添加产品配方设置,暂停设备!", WarningEnum.High);
warning(WarningEnum.Low);//暂停
this.BeginInvoke(new System.Action(() =>
{
frmProduct.loadProduct(pcode);//转到新建编码
}));
return;
}
errCode = 4;
var now = DateTime.Now;
currKey = now.Hour * 10000 + now.Minute * 100 + now.Second;
//var materialItem = codes[0]+"-"+ codes[1];
var colorItem = Config.getColorItem(int.Parse(codes[2]));
record = new Records()
{
currKey = currKey,
ProductId = productInfo.Id,
ProductInfo = productInfo,//后面计算合格时用
Color = (colorItem == null ? "未知" : colorItem["name"].ToString()),
Material = codes[0] + "-" + codes[1],// (materialItem == null ? "未知" : materialItem["name"].ToString()),
BarCode = barCode,
BarCodeName = barCodeName,
ErpLen=double.Parse(len),
BatchId=batchId,
ReelId=reelId,
ModifyUserCode = Config.loginUser.Code,
CreateUserCode = Config.loginUser.Code,
};
htTask.Add(currKey, record);
}
errCode = 8;
//
errCode = 9;
AddTextEvent(DateTime.Now,"扫码", $"品名({barCodeName}),加载产品信息({productInfo.Code})完成,加载配方(光源={productInfo.LightValue},曝光={productInfo.ExposureTime},增益={productInfo.Gain})...");
errCode = 10;
if (productInfo.LightValue > 0)//光源 - 通道0
devContainer.devLight.setDigitalValue(1, productInfo.LightValue);
errCode = 11;
if (productInfo.ExposureTime > 0 || productInfo.Gain > 0)//相机曝光 增益
{
devContainer.devCamer1.setParam((float)(productInfo.ExposureTime > 0 ? productInfo.ExposureTime : -1), (float)(productInfo.Gain > 0 ? productInfo.Gain : -1));
devContainer.devCamer2.setParam((float)(productInfo.ExposureTime > 0 ? productInfo.ExposureTime : -1), (float)(productInfo.Gain > 0 ? productInfo.Gain : -1));
}
errCode = 15;
AddTextEvent(DateTime.Now,"扫码", $"品名({barCodeName}),配方设置完成:光源={productInfo.LightValue},曝光={productInfo.ExposureTime}");
//注意,这里和修改页共享操作(UI操作),注意冲突
this.Invoke(new System.Action(() =>
{
this.txtBarCode.Text = "";
this.txtBarCodeName.Text= barCodeName;
//txtBatchId.Text = record.BatchId;
//txtReelId.Text = record.ReelId;
this.numErpLen.Text = len;
this.txtBatchId.Text = batchId;
this.txtReelId.Text = reelId;
//暂时全部放开
//this.btnStart.Enabled = true;
//this.btnEnd.Enabled = true;
this.btnPause.Enabled = true;
//
this.swcDefectPauseForUser.Active = this.defectPauseForUser = productInfo.DefectPauseForUser;
}));
//
pStopWatch.Restart();
errCode = 19;
}
catch (Exception ex)
{
AddTextEvent(DateTime.Now,"扫码", $"异常({errCode}):{ex.Message}", WarningEnum.High);
}
};
//相机回调出照片
devContainer.devCamer1.ScanEvent = callBackScanMatEvent;
devContainer.devCamer2.ScanEvent = callBackScanMatEvent;
//devContainer.devScannerGentl1.ScanEventPath = callBackScanPathEvent;
//devContainer.devScannerGentl2.ScanEventPath = callBackScanPathEvent;
devContainer.devCamer1.PhotoNumCacheEvent = (num) =>
{
this.BeginInvoke(new System.Action(() =>
{
this.lblScanner1Cache.Text =num.ToString();
}));
};
devContainer.devCamer2.PhotoNumCacheEvent = (num) =>
{
this.BeginInvoke(new System.Action(() =>
{
this.lblScanner2Cache.Text = num.ToString();
}));
};
devContainer.devPlc.RuningStateChangeEvent = (runningState) =>
{
AddTextEvent(DateTime.Now,"PLC", $"当前状态为:{(runningState ? "运行中" : "暂停")}");
if (runningState)
this.startCommand();
else
this.pauseCommand();
};
//devContainer.devIOCard.INEvent = (i,value) =>
//{
// AddTextEvent(DateTime.Now,"I/O", $"INEvent 当前值:{i}-{value}");
// if (compareIOInput(CMDName.启动按钮))
// {
// this.startCommand();
// }
// else if (compareIOInput(CMDName.暂停按钮))
// {
// this.pauseCommand(true);
// }
//};
}
else
{
AddTextEvent(DateTime.Now,"设备启动", $"启动失败,{msg}", WarningEnum.High);
this.Invoke(new System.Action(() =>
{
this.btnOpen.Enabled = true;
this.btnClose.Enabled = false;
}));
}
};
devContainer.WarningEvent = (now,level, msg) =>
{
AddTextEvent(DateTime.Now,$"设备事件{Thread.CurrentThread.ManagedThreadId}", msg, level);
if (level == WarningEnum.High)
Task.Run(() => warning(level, true));
};
devContainer.start(this.picScanner1, this.picScanner2);
//devContainer.start(IntPtr.Zero,IntPtr.Zero);
}
private DataRow loadErpData(string barCode)
{
var paramList = new List() {
new SugarParameter("@code", barCode)
};
Stopwatch stopwatch = Stopwatch.StartNew();
var data = Utils.DBUtils.execSql(Config.ErpSql, paramList);
if (data != null && data.Rows.Count < 1)
{
AddTextEvent(DateTime.Now,"Erp查询结果", $"{barCode}: 时长={stopwatch.ElapsedMilliseconds}ms, 无数据!", WarningEnum.Normal);
return null;
}
AddTextEvent(DateTime.Now,"Erp查询结果", $"{barCode}: 时长={stopwatch.ElapsedMilliseconds}ms, {JsonConvert.SerializeObject(data.Rows[0])}", WarningEnum.Normal);
return data.Rows[0];
//Task.Run(() =>
//{
// try
// {
// var paramList = new List() {
// new SugarParameter("@code", code)
// };
// Stopwatch stopwatch = Stopwatch.StartNew();
// var data = Utils.DBUtils.execSql(Config.ErpSql, paramList);
// AddTextEvent(DateTime.Now,"Erp查询结果", $"{code}: 时长={stopwatch.ElapsedMilliseconds}ms, {JsonConvert.SerializeObject(data.Rows[0])}", WarningEnum.Normal);
// if (data != null && data.Rows.Count > 0)
// {
// this.Invoke(new System.Action(() =>
// {
// this.numErpLen.Text = data.Rows[0][0].ToString();
// if (data.Columns.Count > 1) this.txtBatchId.Text = data.Rows[0][1].ToString();
// if (data.Columns.Count > 2) this.txtReelId.Text = data.Rows[0][2].ToString();
// }));
// }
// }
// catch (Exception ex)
// {
// AddTextEvent(DateTime.Now,"Erp查询异常", $"{code}:{ex.Message}", WarningEnum.Low);
// }
//});
}
private class ScanPhotoInfo
{
///
///
///
///
/// 1-n 第1张会把1改为0
///
public ScanPhotoInfo(int _devIndex,int _photoIndex,string _path)
{
devIndex = _devIndex;
photoIndex = _photoIndex;
path = _path;
}
public ScanPhotoInfo(int _devIndex, int _photoIndex, Mat _mat)
{
devIndex = _devIndex;
photoIndex = _photoIndex;
mat = _mat;
}
public int devIndex { get; set; }
///
/// 0-n
///
public int photoIndex { get; set; }
public string path { get; set; }
public Mat mat { get; set; }
}
//private List> scanPhotoQueue1 = new List>();
///
/// 存放相机1/2的实时图像(3-多容错1帧)
///
private ScanPhotoInfo[] scanPhotos=new ScanPhotoInfo[3];
private void callBackScanPathEvent(int num, string path, int devIndex) //ScanEvent
{
AddTextEvent(DateTime.Now,"拍照", $"相机({devIndex})采集图索到图像({num}):{path}!");
}
bool firstTest = true;
///
///
///
/// 1-n
///
///
private void callBackScanMatEvent(int num, Mat matone, int devIndex) //ScanEvent
{
int errStep = 0;
try
{
if (Config.Camer_Name == CamerDevNameEnum.埃科)
{
AddTextEvent(DateTime.Now, $"拍照{Thread.CurrentThread.ManagedThreadId}", $"采集卡({devIndex}),图像({num})");
Cv2.Flip(matone, matone, FlipMode.XY);//翻转
Bitmap bitmap = BitmapConverter.ToBitmap(matone);
this.BeginInvoke(new Action(() =>
{
//bitmap.Save($"d:\\{devIndex}_{num}.bmp", ImageFormat.Bmp);
//显示图片
if (devIndex == 0)
{
picScanner2.Image = bitmap;
picScanner2.Refresh();
}
else
{
picScanner1.Image = bitmap;
picScanner1.Refresh();
}
}));
}
errStep = 1;
if (!devContainer.state || (currentState != CurrentStateEnum.运行中 && currentState != CurrentStateEnum.暂停))
{
matone.Dispose();
return;
}
errStep = 2;
lock (lockCurrKey)
{
if (currKey == 0 || !htTask.ContainsKey(currKey))
{
lock (lockScanPhoto)
{
if (scanPhotos[0] != null || scanPhotos[1] != null)
scanPhotos[0] = scanPhotos[1] = scanPhotos[2] = null;
}
matone.Dispose();
AddTextEvent(DateTime.Now, $"拍照{Thread.CurrentThread.ManagedThreadId}", $"(图像{num})-未扫码,采集卡({devIndex}),图像丢弃!", WarningEnum.Low);
return;
}
}
errStep = 3;
Records curRecord = Hashtable.Synchronized(htTask)[currKey] as Records;
Device.PhotoLib.PhotoTask task;
Mat myMat = matone;//.Clone();
AddTextEvent(DateTime.Now, $"拍照{Thread.CurrentThread.ManagedThreadId}", $"(图像{num})-采集卡({devIndex}),size:({myMat.Width}*{myMat.Height})({(myMat == null ? "A" : "B")})");
//AddTextEvent(DateTime.Now,"拍照", $"Dev={devIndex},图像{num}-2");
lock (lockScanPhoto)//多线程操作
{
if (scanPhotos[2] != null && scanPhotos[2].photoIndex == num)
{
AddTextEvent(DateTime.Now, $"拍照{Thread.CurrentThread.ManagedThreadId}", $"相机图像不同步,急停中止(需停机重新开始,不可继续)!!!", WarningEnum.High);
warning(WarningEnum.High);
//重置...
}
if (scanPhotos[devIndex] != null) //添加一张容错
{
AddTextEvent(DateTime.Now, $"拍照{Thread.CurrentThread.ManagedThreadId}", $"(图像{num})-采集卡({devIndex}) 容错一张图到队列!", WarningEnum.Low);
scanPhotos[2] = new ScanPhotoInfo(devIndex, num, myMat);
return;
}
errStep = 4;
//--
scanPhotos[devIndex] = new ScanPhotoInfo(devIndex, num, myMat);
if (scanPhotos[0] == null || scanPhotos[1] == null)
return;
//
//if (scanPhotos[0].photoIndex != scanPhotos[1].photoIndex)
//{
// if (scanPhotos[0].photoIndex > scanPhotos[1].photoIndex)
// {
// AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"(图像{num})-索引不一致),丢弃采集卡({scanPhotos[1].devIndex})中较小索引图像({scanPhotos[1].photoIndex})!", WarningEnum.High);
// scanPhotos[1] = null;
// }
// else
// {
// AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"(图像{num})-索引不一致),丢弃采集卡({scanPhotos[0].devIndex})中较小索引图像({scanPhotos[0].photoIndex})!", WarningEnum.High);
// scanPhotos[0] = null;
// }
// if (num > 1)
// {
// AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"相机图像不同步,急停中止(需停机重新开始,不可继续)!!!", WarningEnum.High);
// warning(WarningEnum.High);
// //重置...
// }
// return;
//}
//curRecord置新时,相机里最后一张图还是上次的计数器devIndex累计
if (curRecord.ScannerPhotoCount == 0 && num >= 1)
{
devContainer.devCamer1.resetScanIndex();
devContainer.devCamer2.resetScanIndex();
scanPhotos[0].photoIndex = scanPhotos[1].photoIndex = 0;
if (scanPhotos[2] != null)
scanPhotos[2].photoIndex = 1;
AddTextEvent(DateTime.Now, $"拍照{Thread.CurrentThread.ManagedThreadId}", $"新任务开始,Dev={devIndex},图像({num})重置为图像(0)");
}
task = new Device.PhotoLib.PhotoTask()
{
scanPhotos0 = scanPhotos[0],
scanPhotos1 = scanPhotos[1],
};
scanPhotos[0] = scanPhotos[1] = null;
if (scanPhotos[2] != null)//容错图前移到正常位置
{
AddTextEvent(DateTime.Now, $"拍照{Thread.CurrentThread.ManagedThreadId}", $"(图像{scanPhotos[2].photoIndex})-采集卡({scanPhotos[2].devIndex}) 容错队列图前移。");
scanPhotos[scanPhotos[2].devIndex] = scanPhotos[2];
scanPhotos[2] = null;
}
curRecord.ScannerPhotoCount++;
errStep = 5;
}
//长度剩余提醒
if (Config.residueWarnningLen > 0 && curRecord.ErpLen > 0 && Config.residueWarnningLen >= curRecord.ErpLen - curRecord.Len)
{
AddTextEvent(DateTime.Now, $"告警{Thread.CurrentThread.ManagedThreadId}", $"已达剩余长度不足提醒!({curRecord.ErpLen - curRecord.Len}<={Config.residueWarnningLen})", WarningEnum.High);
}
errStep = 6;
//
AddTextEvent(DateTime.Now, $"拍照{Thread.CurrentThread.ManagedThreadId}", $"待处理图像队列:{curRecord.ScannerPhotoCount - curRecord.ScannerPhotoFinishCount}张");
if (task.scanPhotos0 != null && task.scanPhotos1 != null)
{
var scanPhoto = task.scanPhotos0 as ScanPhotoInfo;
curRecord.dicPhoto_Defect.TryAdd(scanPhoto.photoIndex, false);//加入索引,默认无瑕疵
AddTextEvent(DateTime.Now, $"拍照{Thread.CurrentThread.ManagedThreadId}", $"图像索引:{scanPhoto.photoIndex},标识数:{curRecord.dicPhoto_Defect.Count}");
errStep = 7;
//暂停:瑕疵二次判断
if (this.defectPauseForUser)
{
int liPhotoIndex = scanPhoto.photoIndex - Config.defectPauseSkipPhotoCount;
AddTextEvent(DateTime.Now, $"拍照{Thread.CurrentThread.ManagedThreadId}", $"Dev={devIndex},图像{scanPhoto.photoIndex} {liPhotoIndex}={scanPhoto.photoIndex}-{Config.defectPauseSkipPhotoCount};{JsonConvert.SerializeObject(curRecord.dicPhoto_Defect)}");
if (liPhotoIndex >= 0 && curRecord.dicPhoto_Defect[liPhotoIndex])
{
List lstEditDefect = curRecord.DefectInfoList.Where(m => m.PhotoIndex == liPhotoIndex).ToList();
AddTextEvent(DateTime.Now, $"暂停{Thread.CurrentThread.ManagedThreadId}", $"(图像{liPhotoIndex})已达观察台,瑕疵二次判断=》({string.Join(",", lstEditDefect.Select(m => m.Code).ToArray())})是否包含在({string.Join(",", curRecord.ProductInfo.DefectPauseOption.ToArray())})中。");
//瑕疵选项过滤
if (curRecord.ProductInfo.DefectPauseOption.Count == 0 || lstEditDefect.Where(x => curRecord.ProductInfo.DefectPauseOption.Contains(x.Code)).Count() > 0)
{
AddTextEvent(DateTime.Now, $"暂停{Thread.CurrentThread.ManagedThreadId}", $"(图像{liPhotoIndex})需瑕疵二次判断,已达观察台,进入暂停。");
if (!Config.StopPLC)
this.devContainer.devPlc.pauseDev();
else if (!Config.StopIO && devContainer.devIOCard.IsInit)
{
//只是设备暂停,APP没暂停
devContainer.io_output(CMDName.绿灯输出, false, true, 0);
devContainer.io_output(CMDName.黄灯输出);
devContainer.devIOCard.writeBitState(0, 1, true);
Task.Run(async () =>
{
await Task.Delay(500);
this.devContainer.devIOCard.writeBitState(0, 1, false);
});
}
errStep = 8;
//不能使用同步Invoke方式,会使相机超时丢帧
this.BeginInvoke(new System.Action(() =>
{
int liDefectCount = lstEditDefect.Count;
FHome_Defect frmDefect = new FHome_Defect(lstEditDefect);
if (frmDefect.ShowDialog() == DialogResult.OK)
{
string oldCode;
for (int i = 0; i < this.uiDataGridView1.Rows.Count; i++)
{
if ((int)this.uiDataGridView1.Rows[i].Cells["colIndex"].Value != liPhotoIndex)
continue;
long uid = (long)this.uiDataGridView1.Rows[i].Cells["colUid"].Value;
foreach (var row in lstEditDefect)
{
AddTextEvent(DateTime.Now, $"暂停{Thread.CurrentThread.ManagedThreadId}", $"修改第({i + 1})行瑕疵名称,{uid} {row.uid}");
if (row.uid == uid)
{
oldCode = this.uiDataGridView1.Rows[i].Cells["colCode"].Value.ToString();
AddTextEvent(DateTime.Now, $"暂停{Thread.CurrentThread.ManagedThreadId}", $"修改第({i + 1})行瑕疵名称 ({this.uiDataGridView1.Rows[i].Cells["colDefectName"].Value})->({row.Name})");
this.uiDataGridView1.Rows[i].Cells["colCode"].Value = row.Code;
this.uiDataGridView1.Rows[i].Cells["colDefectName"].Value = row.Name;
//this.uiDataGridView1.Refresh();
if (!string.IsNullOrWhiteSpace(row.TagFilePath))
File.Move(row.TagFilePath, row.TagFilePath.Replace($"_类别{oldCode}", $"_类别{row.Code}"));//
break;
}
}
foreach (var item in frmDefect.lstDel)
{
if (item.uid == uid)
{
this.uiDataGridView1.Rows.RemoveAt(i);
i--;
break;
}
}
}
foreach (var item in frmDefect.lstDel)
{
curRecord.DefectInfoList.Remove(item);
//删除忽略瑕疵小图
//if (!string.IsNullOrWhiteSpace(item.TagFilePath))
// File.Delete(item.TagFilePath);
}
//double len = (double)this.lblLen.Tag;
//this.reDrawDefectPoints(curRecord.DefectInfoList, new double[] { 0, Math.Round(curRecord.FaceWidthMax + 0.005f, 2) }, new double[] { 0, len });
AddTextEvent(DateTime.Now, $"二次检测{Thread.CurrentThread.ManagedThreadId}", $"本次忽略{frmDefect.lstDel.Count}个瑕疵,本张图由{liDefectCount} -> {lstEditDefect.Count},总数{curRecord.DefectInfoList.Count}");
}
this.uiMiniPagination1.TotalCount = curRecord.DefectTotalCount = curRecord.DefectInfoList.Count;
//
//double len = Math.Round((res.photoIndex + 1) * bmpHeight * 1.0d / Config.cm2px_y + 0.005f, 2);
this.reDrawDefectPoints(curRecord.DefectInfoList);
errStep = 9;
//自动继续运行设备(这里临时暂停后不能再急停,否则无法继续)
if (!Config.StopPLC)
this.devContainer.devPlc.runDev();
else if (!Config.StopIO && devContainer.devIOCard.IsInit)
{
if (!compareIOInput(CMDName.暂停按钮))
{
devContainer.io_output(CMDName.绿灯输出);
devContainer.io_output(CMDName.黄灯输出, false, true, 0);
this.devContainer.devIOCard.writeBitState(0, 0, true);
Task.Run(async () =>
{
await Task.Delay(500);
this.devContainer.devIOCard.writeBitState(0, 0, false);
});
}
}
}));
}
}
}
errStep = 10;
task.record = curRecord;
task.finishEvent = callBackPhotoEvent;
devContainer.libPhoto.add(task);
AddTextEvent(DateTime.Now, $"拍照{Thread.CurrentThread.ManagedThreadId}", $"Dev={devIndex},图像{scanPhoto.photoIndex},已加入图像处理队列");
errStep = 11;
}
}
catch (Exception e)
{
AddTextEvent(DateTime.Now, $"拍照{Thread.CurrentThread.ManagedThreadId}", $"errStep:{errStep}-{e.Message}", WarningEnum.High);
}
}
private void callBackPhotoEvent(Device.PhotoLib.PhotoTask task)
{
int errStep = 0;
Records curRecord = task.record;
ScanPhotoInfo scanPhotos0 = task.scanPhotos0 as ScanPhotoInfo;
ScanPhotoInfo scanPhotos1 = task.scanPhotos1 as ScanPhotoInfo;
Stopwatch stopWatch = new Stopwatch();
AddTextEvent(DateTime.Now,$"图像处理{Thread.CurrentThread.ManagedThreadId}", $"图像{scanPhotos0.photoIndex},ThreadId={Thread.CurrentThread.ManagedThreadId}");
string time = "";
stopWatch.Start();
try
{
if (scanPhotos0.mat.Height != scanPhotos1.mat.Height)
{
AddTextEvent(DateTime.Now,$"警告{Thread.CurrentThread.ManagedThreadId}", $"两相机采集图高度不一致({scanPhotos0.photoIndex}),dev1.Height={scanPhotos0.mat.Height},dev2.Height={scanPhotos1.mat.Height},重新resize...", WarningEnum.Low);
if (scanPhotos0.mat.Height > scanPhotos1.mat.Height)
scanPhotos1.mat = OpenCVUtil.resize(scanPhotos1.mat, scanPhotos0.mat.Width, scanPhotos0.mat.Height);
else
scanPhotos0.mat = OpenCVUtil.resize(scanPhotos0.mat, scanPhotos1.mat.Width, scanPhotos1.mat.Height);
}
//saveMatTest(scanPhotos0.mat, 1);
//saveMatTest(scanPhotos1.mat, 2);
//反转+相机索引调换
Mat mat0 = scanPhotos1.mat;
Mat mat1 = scanPhotos0.mat;
if (Config.ScannerReversalX)
{
Cv2.Flip(mat0, mat0, FlipMode.X);
errStep = 1;
Cv2.Flip(mat1, mat1, FlipMode.X);
time += $"X翻转({stopWatch.ElapsedMilliseconds})";
}
if (Config.ScannerReversalY)
{
Cv2.Flip(mat0, mat0, FlipMode.Y);
errStep = 2;
Cv2.Flip(mat1, mat1, FlipMode.Y);
time += $"Y翻转({stopWatch.ElapsedMilliseconds})";
}
firstTest = false;
//记录resize大小
//var resize = new System.Drawing.Size(mat0.Width * 2, mat0.Height);
int resizeWidth = devContainer.libDefect.GetWidthForResize(mat0.Width+ mat1.Width - Config.MiddleSuperposition);
if (resizeWidth == 0)
throw new Exception("GetWidthForResize result 0 失败!");
var resize = new System.Drawing.Size(resizeWidth, 4096);//固定8192*2张*4096
//裁边,两侧和中间重合部分
if (Config.MiddleSuperposition > 0)//中间重合部分
{
errStep = 3;
int width = mat0.Width - Config.MiddleSuperposition;
mat0 = OpenCVUtil.cutImage(mat0, 0, 0, width, mat0.Height);
time += $"->图1去重({stopWatch.ElapsedMilliseconds})";
}
AddTextEvent(DateTime.Now,$"裁边{Thread.CurrentThread.ManagedThreadId}",
$"(图像{scanPhotos0.photoIndex})-左图去重后:{mat0.Width}*{mat0.Height},右图:{mat1.Width}*{mat1.Height}," +
$"重复值:{Config.MiddleSuperposition},孔洞:{Config.MarginHoleWidth},cm2px:{Config.cm2px_x},resizeWidth:{resizeWidth}");
errStep = 4;
//mat0 = OpenCVUtil.getMaxInsetRect(mat0);
int marginWidth0, marginWidth1;
mat0 = OpenCVUtil.getMaxInsetRect2(mat0, true, Config.MarginHoleWidth,out marginWidth0);
//AddTextEvent(DateTime.Now,"裁边", $"(图像{scanPhotos0.photoIndex})-图0裁边后:{mat0.Width}*{mat0.Height}");
errStep = 5;
time += $"->图1裁边({stopWatch.ElapsedMilliseconds})";
//mat1 = OpenCVUtil.getMaxInsetRect(mat1);
mat1 = OpenCVUtil.getMaxInsetRect2(mat1,false, Config.MarginHoleWidth, out marginWidth1);
//AddTextEvent(DateTime.Now,"裁边", $"(图像{scanPhotos0.photoIndex})-图1裁边后:{mat1.Width}*{mat1.Height}");
errStep = 6;
time += $"->图2裁边({stopWatch.ElapsedMilliseconds})";
//水平合并l
Mat mat = OpenCVUtil.mergeImage_sameSize(new Mat[] { mat0, mat1 });//这里相机反装,左右反转下
AddTextEvent(DateTime.Now,$"裁边{Thread.CurrentThread.ManagedThreadId}", $"(图像{scanPhotos0.photoIndex})-边缘宽度:(左图)={marginWidth0},(右图)={marginWidth1}; 裁边去孔洞后:({mat0.Width}+{mat1.Width}={mat0.Width+ mat1.Width});合并后(去孔洞):{mat.Width}*{mat.Height}");
float widthRatio = mat.Width * 1.0f / resize.Width;//宽度比例
time += $"->图1+2合并({stopWatch.ElapsedMilliseconds})";
//门幅更新(含两侧孔洞)x,y cm
float faceWidthX_cm = (float)Math.Round((scanPhotos0.photoIndex+1) * mat.Height * 1.0f / Config.cm2px_y,2);
float faceWidthY_cm = (float)Math.Round((mat.Width + Config.MarginHoleWidth * 2) * 1.0f / Config.cm2px_x, 2);
faceWidthX_cm = (float)Math.Round(faceWidthX_cm, 2);
faceWidthY_cm = (float)Math.Round(faceWidthY_cm, 2);
if (curRecord.FaceWidthMin==0 || curRecord.FaceWidthMin > faceWidthY_cm)
curRecord.FaceWidthMin = faceWidthY_cm;
if (curRecord.FaceWidthMax < faceWidthY_cm)
curRecord.FaceWidthMax = faceWidthY_cm;
var point = new float[] { faceWidthX_cm, faceWidthY_cm };// new System.Drawing.PointF(faceWidthX_cm, faceWidthY_cm);
AddTextEvent(DateTime.Now,$"门幅{Thread.CurrentThread.ManagedThreadId}", $"(图像{scanPhotos0.photoIndex})-({scanPhotos0.photoIndex})位置:{point[0]}; 幅宽:{point[1]}");
curRecord.FacePointList.Add(point);
reDrawFaceWidth(curRecord.FacePointList,
new double[] { 0, Math.Round(point[0] + 0.005f, 2) },
new double[] { curRecord.FaceWidthMin, Math.Round(curRecord.FaceWidthMax + 0.005f, 2) });
errStep = 7;
time += $"->门幅刷新({stopWatch.ElapsedMilliseconds})";
//去除两侧孔洞(门幅计算时不能去除)
//if (Config.MarginHoleWidth > 0)
// mat = OpenCVUtil.cutImage(mat, Config.MarginHoleWidth, 0, mat.Width - Config.MarginHoleWidth * 2, mat.Height);
//计算速度
double lenMi = Math.Round(faceWidthX_cm / 100, 2);
curRecord.Len = lenMi;
curRecord.TimeLen= pStopWatch.ElapsedMilliseconds / 1000.0d / 60.0d;//总时间 分
this.BeginInvoke(new System.Action(() =>
{
this.lblLen.Text = $"{lenMi}米";
this.lblLen.Tag = faceWidthX_cm;
this.lblSpeed.Text = $"速度:{Math.Round(lenMi / curRecord.TimeLen, 2)}米/分";
}));
//
errStep = 9;
time += $"->速度刷新({stopWatch.ElapsedMilliseconds})";
//----缺陷队列
mat = OpenCVUtil.resize(mat, resize.Width, resize.Height);
AddTextEvent(DateTime.Now,$"图像处理{Thread.CurrentThread.ManagedThreadId}", $"(图像{scanPhotos0.photoIndex})-合成图resize后:{mat.Width}*{mat.Height}");
devContainer.libDefect.add(new Device.DefectLib.DefectTask()
{
modelName= curRecord.ProductInfo.ModelName,
record = curRecord,
bmp = mat,
bmpTag = mat.Clone(),
photoIndex = scanPhotos0.photoIndex,//0-n 首张必需为0,因下面计算长度是从0开始
widthRatio = widthRatio,
qualifiedLimitList = curRecord.ProductInfo.QualifiedLimitList,
finishEvent = callBackDefectEvent,
});
errStep = 10;
time += $"->加入瑕疵待检队列({stopWatch.ElapsedMilliseconds})";
}
catch (Exception ex)
{
curRecord.ScannerPhotoFinishCount++;//失败时不能因数量不一致无法保存
AddTextEvent(DateTime.Now,$"图像处理{Thread.CurrentThread.ManagedThreadId}", $"异常({errStep}):(图像{scanPhotos0.photoIndex})-{ex.Message}", WarningEnum.High);
string dirPath = FileUtil.initFolder($"{Config.ImagePath}{curRecord.BatchId}_{curRecord.ReelId}\\Err\\");
OpenCvSharp.Extensions.BitmapConverter.ToBitmap(scanPhotos0.mat).Save($"{dirPath}{scanPhotos0.photoIndex}_0_Step{errStep}.bmp", ImageFormat.Bmp);
OpenCvSharp.Extensions.BitmapConverter.ToBitmap(scanPhotos1.mat).Save($"{dirPath}{scanPhotos1.photoIndex}_1_Step{errStep}.bmp", ImageFormat.Bmp);
}
finally
{
AddTextEvent(DateTime.Now,$"图像处理{Thread.CurrentThread.ManagedThreadId}", $"(图像{scanPhotos0.photoIndex})-进度计时:{time}");
scanPhotos0.mat.Dispose();
scanPhotos1.mat.Dispose();
scanPhotos0 = scanPhotos1 = null;
task = null;
System.GC.Collect();
}
}
private void callBackDefectEvent(Device.DefectLib.DefectTask res)
{
{
int step = 0;
try
{
AddTextEvent(DateTime.Now,$"检测完成{Thread.CurrentThread.ManagedThreadId}", $"图像队列:{res.record.ScannerPhotoFinishCount+1}/{res.record.ScannerPhotoCount} (图像{res.photoIndex})检测结果:{res.isSucceed}");
string dirPath = FileUtil.initFolder($"{Config.ImagePath}{res.record.BatchId}_{res.record.ReelId}\\");
string dirSourcePath = FileUtil.initFolder($"{Config.ImagePath}{res.record.BatchId}_{res.record.ReelId}\\源图\\");
//Cv2.Flip(res.bmp, res.bmp, FlipMode.XY);//翻转
if (Config.IsSaveAllImage)//保存所有原图
OpenCvSharp.Extensions.BitmapConverter.ToBitmap(res.bmp).Save($"{dirSourcePath}{res.photoIndex}.bmp", ImageFormat.Bmp);
if (res.isSucceed)
{
step = 1;
AddTextEvent(DateTime.Now,$"检测完成{Thread.CurrentThread.ManagedThreadId}", $"(图像{res.photoIndex})-瑕疵检测完成,共{res.excelTable.Rows.Count}个瑕疵!各环节用时:{string.Join(",",res.stopwatch)}");
//AddTextEvent(DateTime.Now,$"打标完成", $"第 ({res.photoIndex}) 张照片,计算过程:{res.resultInfo}");
if (!Config.IsSaveAllImage && Config.IsSaveDefectSourceImage)
OpenCvSharp.Extensions.BitmapConverter.ToBitmap(res.bmp).Save($"{dirSourcePath}{res.photoIndex}.bmp", ImageFormat.Bmp);
step = 2;
if (res.excelTable.Rows.Count > 0)
{
res.record.dicPhoto_Defect[res.photoIndex] = true;//改为此图有瑕疵
//有瑕疵打标图必需保存 Jpeg
OpenCvSharp.Extensions.BitmapConverter.ToBitmap(res.bmpTag).Save($"{dirPath}{res.photoIndex}_tag.jpg", ImageFormat.Jpeg);
step = 3;
res.record.DefectTotalCount += res.excelTable.Rows.Count;
if (res.record.DefectInfoList == null)
res.record.DefectInfoList = new List();
step = 4;
JObject defectNameInfo;
DefectInfo defectInfo=null;
List