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("X轴"); 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; timer2.Enabled = true; if(SysMgr.Instance.QGSts()) button5.Text = "气缸松开"; else button5.Text = "气缸加紧"; this.propertyGrid2.SelectedObject = Defectprop; //Sizeprop.deserialize(""); //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; //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 前部相机 #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); System.GC.Collect(); } 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) { VelocityCurveParams _VelParams = new VelocityCurveParams(); int axisNo = cbAxisName.SelectedIndex + 1; _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) { Button btn = sender as Button; int axisNo = cbAxisName.SelectedIndex + 1; 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 + 1; SysMgr.Instance.AxisDev.DecelStop(axisNo); } private void timer2_Tick(object sender, EventArgs e) { int axisNo = cbAxisName.SelectedIndex + 1; 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 厚度 #endregion #region 张力 #endregion #region 变焦 #endregion private void DebugTestFrm_FormClosing(object sender, FormClosingEventArgs e) { timer2.Enabled = false; timer3.Enabled = false; System.GC.Collect(); } } }