using HalconDotNet; using ImageToolKits; using MaiMuAOI.SysCtrl; using MaiMuControl.Device; using MaiMuControl.Device.AxisDev; using MaiMuControl.Device.CamDev; using MaiMuControl.Utils; using Newtonsoft.Json; using OpenCvSharp; using ProductionControl.UI; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using ToolKits.EnumTool; using static MaiMuAOI.ImageProcessing.DefectLib; using static MaiMuAOI.ImageProcessing.SizeLib; namespace MaiMuAOI.SysUI.DefectPicShow { public partial class DebugTestFrm : Form { private SizeLibProp Sizeprop = new SizeLibProp(); private DefectLibProp Defectprop = new DefectLibProp(ConfMgr.Instance.SysConfigParams.Defect_CutSize, ConfMgr.Instance.SysConfigParams.Defect_ReSize, ConfMgr.Instance.SysConfigParams.Defect_Thresholds); SynchronizationContext SyncContext1 = null; SynchronizationContext SyncContext2 = null; public DebugTestFrm() { InitializeComponent(); UIStyle.SetUIStyle(this); this.uiTitel1.FatherForm = this; } private void DebugTestFrm_Load(object sender, EventArgs e) { this.Left = 600; //显示tS曲线 cbTS.Items.Clear(); Type VelType = typeof(VelocityCurveType); foreach (object s in Enum.GetValues(VelType)) { string st = EnumExtension.GetEnumDescription((VelocityCurveType)s); cbTS.Items.Add(st); } cbTS.Text = cbTS.Items[0].ToString(); cbTS.SelectedIndex = 0; cbDir.Items.Clear(); cbDir.Items.Add("相对运动"); cbDir.Items.Add("绝对运动"); cbDir.Text = cbDir.Items[0].ToString(); cbDir.SelectedIndex = 0; cbAxisName.Items.Clear(); cbAxisName.Items.Add("X1轴"); cbAxisName.Items.Add("X2轴"); cbAxisName.Items.Add("Y轴"); cbAxisName.Items.Add("Z轴"); cbAxisName.Text = cbAxisName.Items[0].ToString(); cbAxisName.SelectedIndex = 0; cbCH.Items.Clear(); Type lightenum = typeof(LightChannelEnum); foreach (object s in Enum.GetValues(lightenum)) { string st = EnumExtension.GetEnumDescription((LightChannelEnum)s); cbCH.Items.Add(st); } cbCH.Text = cbCH.Items[0].ToString(); cbCH.SelectedIndex = 0; cbLensMotor.SelectedIndex = 8; timer2.Enabled = true; if(SysMgr.Instance.QGSts()) button5.Text = "气缸松开"; else button5.Text = "气缸加紧"; if (SysMgr.Instance.ThicknessSts()) BtnHeightAck.Text = "高度传感器上升"; else BtnHeightAck.Text = "高度传感器下降"; this.propertyGrid1.SelectedObject = Sizeprop; this.propertyGrid2.SelectedObject = Defectprop; //Sizeprop.deserialize(""); this.propertyGrid1.Refresh(); //DATA SysMgr.Instance.SizeLib.finishEvent += new System.Action((task) => { Sizeprop.PT1 = task.PT1; Sizeprop.PT2 = task.PT2; Sizeprop.Shanxian = task.Shanxian; //prop.RowP= task.RowP; Sizeprop.Circle_Ymm = task.Circle_Ymm; Sizeprop.Circle_Xmm = task.Circle_Xmm; Sizeprop.offsetX = task.offsetX; Sizeprop.offsetY = task.offsetY; Sizeprop.MarkPointList = task.MarkPointList; Sizeprop.resultInfo = task.isSucceed ? "成功" : task.resultInfo; this.refreshUI1(); }); //Defectprop.deserialize(""); this.propertyGrid2.Refresh(); //DATA SysMgr.Instance.DefectLib.finishEvent += new System.Action((task) => { DateTime t1 = DateTime.Now; var t2 = t1 - task.nowTime; var t3 = t2.Milliseconds; Defectprop.InformationList = JsonConvert.SerializeObject(task.informationList); Defectprop.resultInfo = task.isSucceed ? "成功" : task.resultInfo; this.refreshUI2(); }); } private void refreshUI1() { SyncContext1.Post(m => { var result = m as string; propertyGrid1.Refresh(); //txtLog.Text = szLog;//不显示 //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog"; //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG); }, "异步操作完成结果"); } private void refreshUI2() { SyncContext2.Post(m => { var result = m as string; propertyGrid2.Refresh(); //txtLog.Text = szLog;//不显示 //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog"; //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG); }, "异步操作完成结果"); } #region 前部相机 private void btnTrg_Click(object sender, EventArgs e) { SysMgr.Instance.CamDevFront.ClearImageQueue(); Acquisition acq = SysMgr.Instance.CamDevFront.Snap(1, 5000); if (acq.GrabStatus != "GrabPass") { ; } //ShowImage(acq.Image.CopyObj(1, -1)); ShowImage1(acq.Image); } private void ShowImage1(HObject image) { lock (this) { Mat img = CamDev.HImageToMat(image); //imageBox1.ColorName = "red"; imageBox1.SetInteractive(false); if (imageBox1.Image == null) imageBox1.RefreshWindow(img, ImageBox.ImageModeEnum.Zoom);//可以不显示区域 else imageBox1.RefreshWindow(img, ImageBox.ImageModeEnum.Part);//可以不显示区域 imageBox1.SetInteractive(true); //imageBox1.ColorName = "green"; } } private void btnGrab_Click(object sender, EventArgs e) { if (this.btnGrab.Text == "连续拍照") { this.btnTrg.Enabled = false; this.btnGrab.Text = "停止拍照"; timer1.Enabled = true; } else { this.btnTrg.Enabled = true; this.btnGrab.Text = "连续拍照"; timer1.Enabled = false; } } private void timer1_Tick(object sender, EventArgs e) { SysMgr.Instance.CamDevFront.ClearImageQueue(); Acquisition acq = SysMgr.Instance.CamDevFront.Snap(1, 100); if (acq.GrabStatus != "GrabPass") { return; //MessageBox.Show( "获取图片失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } //ShowImage(acq.Image.CopyObj(1, -1)); ShowImage1(acq.Image); } private void button2_Click(object sender, EventArgs e) { if (!SysMgr.Instance.CamDevFront.SetExposure((double)numExposure.Value)) { stslog.Text = "曝光设置失败!"; return; } if (!SysMgr.Instance.CamDevFront.SetGain((double)numGain.Value)) { stslog.Text = "增益设置失败!"; return; } stslog.Text = "曝光-增益-帧率设置成功"; } private void button6_Click(object sender, EventArgs e) { SysMgr.Instance.CamDevFront.CloseCamera(); button2.Enabled = false; btnTrg.Enabled = false; btnGrab.Enabled = false; } private void button7_Click(object sender, EventArgs e) { try { Sizeprop.resultInfo = ""; SysMgr.Instance.SizeLib.add(new SizeTask() { engineName = Sizeprop.EngineName, file_path = Sizeprop.BmpPath, //bmp =read2Bmp(prop.BmpPath), index = Sizeprop.Index, //finishEvent = (res) => //{ //} }); } catch (Exception ex) { MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } } #endregion #region 后部相机 private void ShowImage2(HObject image) { lock (this) { Mat img = CamDev.HImageToMat(image); imageBox2.SetInteractive(false); if (imageBox2.Image == null) imageBox2.RefreshWindow(img, ImageBox.ImageModeEnum.Zoom);//可以不显示区域 else imageBox2.RefreshWindow(img, ImageBox.ImageModeEnum.Part);//可以不显示区域 imageBox2.SetInteractive(true); } } private void btnTrg2_Click(object sender, EventArgs e) { SysMgr.Instance.CamDevBack.ClearImageQueue(); Acquisition acq = SysMgr.Instance.CamDevBack.Snap(1, 5000); if (acq.GrabStatus != "GrabPass") { ; } ShowImage2(acq.Image); } private void timer3_Tick(object sender, EventArgs e) { SysMgr.Instance.CamDevBack.ClearImageQueue(); Acquisition acq = SysMgr.Instance.CamDevBack.Snap(1, 100); if (acq.GrabStatus != "GrabPass") { return; } ShowImage2(acq.Image); } private void BtnGrab2_Click(object sender, EventArgs e) { if (this.BtnGrab2.Text == "连续拍照") { this.btnTrg2.Enabled = false; this.BtnGrab2.Text = "停止拍照"; timer3.Enabled = true; } else { this.btnTrg2.Enabled = true; this.BtnGrab2.Text = "连续拍照"; timer3.Enabled = false; } } private void BtnSet2_Click(object sender, EventArgs e) { if (!SysMgr.Instance.CamDevBack.SetExposure((double)numExp2.Value)) { stslog.Text = "曝光设置失败!"; return; } if (!SysMgr.Instance.CamDevBack.SetGain((double)numGain2.Value)) { stslog.Text = "增益设置失败!"; return; } stslog.Text = "曝光-增益-帧率设置成功"; } private void BtnDisCam2_Click(object sender, EventArgs e) { SysMgr.Instance.CamDevBack.CloseCamera(); BtnGrab2.Enabled = false; btnTrg2.Enabled = false; BtnSet2.Enabled = false; } private bool verifyThresholdsCount(string szThresholdsClass, int iThresholdsClassCount) { return szThresholdsClass.Trim().TrimEnd(new char[] { ',', ';' }) .Split(new char[] { ',', ';' }).Count() == iThresholdsClassCount; } private void button8_Click(object sender, EventArgs e) { if (!Util.IsNumber(Defectprop.ThresholdsClass.Replace("-", "").Replace(",", "").Replace(";", "").Replace(".", ""))) { MessageBox.Show("输入种类阀值与数量不一致!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!verifyThresholdsCount(Defectprop.ThresholdsClass, Defectprop.ThresholdsClassCount)) { MessageBox.Show("输入种类阀值与数量不一致!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { SysMgr.Instance.DefectLib.loadModelFile(Defectprop.ModelPath); Defectprop.resultInfo = ""; SysMgr.Instance.DefectLib.add(new DefectTask() { nowTime = DateTime.Now, bmp = new Mat(Defectprop.BmpPath), cut_size = Defectprop.CutSize, resize = Defectprop.Resize, thresholds = Defectprop.Thresholds, thresholdsClass = Defectprop.ThresholdsClass, //finishEvent = (res) => //{ //} }); } catch (Exception ex) { MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } } #endregion #region 轴移动 private void btnMove_Click(object sender, EventArgs e) { if (SysMgr.Instance.AxisDev != null) { //测厚气缸缩回 if(!SysMgr.Instance.ThicknessIsSafe()) { stslog.Text = "高度传感器不在安全位置"; return; } VelocityCurveParams _VelParams = new VelocityCurveParams(); int axisNo = cbAxisName.SelectedIndex; _VelParams.Acc = (double)numAcc.Value; _VelParams.Dec = (double)numDec.Value; _VelParams.StartVel = (double)numST.Value; _VelParams.RunVel = (double)numET.Value; _VelParams.JerkT_S = cbTS.SelectedIndex; _VelParams.MM2PulseNum = SysMgr.Instance.GetMMtoPlus((AxisName)axisNo); if (cbDir.Text == "绝对运动") { stslog.Text = "绝对运动:" + numDist.Value.ToString(); SysMgr.Instance.AxisDev.MoveAbsValue(axisNo, _VelParams, (double)numDist.Value); } else if (cbDir.Text == "相对运动") { stslog.Text = "相对运动:" + numDist.Value.ToString(); SysMgr.Instance.AxisDev.MoveRelValue(axisNo, _VelParams, (double)numDist.Value); } } } private void btnJogP_MouseDown(object sender, MouseEventArgs e) { if (!SysMgr.Instance.ThicknessIsSafe()) { stslog.Text = "高度传感器不在安全位置"; return; } Button btn = sender as Button; int axisNo = cbAxisName.SelectedIndex; double val = 50; VelocityCurveParams velocityCurveParams = new VelocityCurveParams(100, 100, 0, 100, 1, SysMgr.Instance.GetMMtoPlus((AxisName)axisNo)); if (btn.Name == "btnJogP") val = 50; else val = -50; SysMgr.Instance.AxisDev.MoveRelValue(axisNo, velocityCurveParams, val); } private void btnJogP_MouseUp(object sender, MouseEventArgs e) { int axisNo = cbAxisName.SelectedIndex; SysMgr.Instance.AxisDev.DecelStop(axisNo); } private void timer2_Tick(object sender, EventArgs e) { int axisNo = cbAxisName.SelectedIndex; try { numericUpDown1.Value = (decimal)SysMgr.Instance.AxisDev.GetFeedbackPos(axisNo) / SysMgr.Instance.GetMMtoPlus((AxisName)axisNo); } catch { numericUpDown1.Value = 0; } } #endregion #region 光源控制 private void button4_Click(object sender, EventArgs e) { int ch = cbCH.SelectedIndex + 1; SysMgr.Instance.LightDev.SetLightDigitalValue(ch, (int)numericUpDown3.Value); } private void button1_Click(object sender, EventArgs e) { int ch = cbCH.SelectedIndex + 1; SysMgr.Instance.LightDev.OpenLight(ch); } private void button3_Click(object sender, EventArgs e) { int ch = cbCH.SelectedIndex + 1; SysMgr.Instance.LightDev.CloseLight(ch); } #endregion #region 产品加紧 private void button5_Click(object sender, EventArgs e) { if (button5.Text == "气缸加紧") { button5.Text = "气缸松开"; SysMgr.Instance.RunQG(true); } else { button5.Text = "气缸加紧"; SysMgr.Instance.RunQG(false); } } #endregion #region 厚度 private void BtnHeightAck_Click(object sender, EventArgs e) { if (BtnHeightAck.Text == "高度传感器下降") { BtnHeightAck.Text = "高度传感器上升"; SysMgr.Instance.RunThickness(true); } else { BtnHeightAck.Text = "高度传感器下降"; SysMgr.Instance.RunThickness(false); } } private void BtnReadHeight_Click(object sender, EventArgs e) { double val = SysMgr.Instance.ThicknessDev.GetValue(); numHeightValue.Value = (decimal)val; } #endregion #region 张力 private void btnReadTension_Click(object sender, EventArgs e) { double val = SysMgr.Instance.TensionDev.GetValue(); numTensionValue.Value = (decimal)val; } #endregion #region 变焦 private void btnLens_Click(object sender, EventArgs e) { int val = (int)SmallAxCmdPos.倍率0_58X; switch(cbLensMotor.SelectedIndex) { case 0: val = (int)SmallAxCmdPos.倍率0_58X; break; case 1: val = (int)SmallAxCmdPos.倍率1_0X; break; case 2: val = (int)SmallAxCmdPos.倍率1_5X; break; case 3: val = (int)SmallAxCmdPos.倍率2_0X; break; case 4: val = (int)SmallAxCmdPos.倍率2_5X; break; case 5: val = (int)SmallAxCmdPos.倍率3_0X; break; case 6: val = (int)SmallAxCmdPos.倍率3_5X; break; case 7: val = (int)SmallAxCmdPos.倍率4_0X; break; case 8: val = (int)SmallAxCmdPos.倍率4_5X; break; case 9: val = (int)SmallAxCmdPos.倍率5_0X; break; case 10: val = (int)SmallAxCmdPos.倍率5_5X; break; case 11: val = (int)SmallAxCmdPos.倍率6_0X; break; case 12: val = (int)SmallAxCmdPos.倍率6_5X; break; case 13: val = (int)SmallAxCmdPos.倍率7_0X; break; case 14: val = (int)SmallAxCmdPos.倍率7_5X; break; } SysMgr.Instance.LensMotorDev.MoveAbsPulse(0, new VelocityCurveParams(), val); //SysMgr.Instance.LensMotorDev.CheckDone(0, 10000); } private void btnLensM_Click(object sender, EventArgs e) { int val = (int)numLensPlus.Value; SysMgr.Instance.LensMotorDev.MoveAbsPulse(0, new VelocityCurveParams(), val); } #endregion } }