From 699bbf402e9d6333d4dcb0c75e6d32e4386fd6dc Mon Sep 17 00:00:00 2001 From: xcd <834800634@qq.com> Date: 星期日, 28 六月 2020 18:59:26 +0800 Subject: [PATCH] 主体流程完成 --- src/Bro.M071.Process/M071Process.cs | 363 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 362 insertions(+), 1 deletions(-) diff --git a/src/Bro.M071.Process/M071Process.cs b/src/Bro.M071.Process/M071Process.cs index 33827af..13fde83 100644 --- a/src/Bro.M071.Process/M071Process.cs +++ b/src/Bro.M071.Process/M071Process.cs @@ -1,8 +1,13 @@ -锘縰sing Bro.Common.Helper; +锘縰sing Bro.Common.Base; +using Bro.Common.Helper; +using Bro.Common.Interface; using Bro.Common.Model; using Bro.Process; +using HalconDotNet; using System; using System.Collections.Generic; +using System.ComponentModel; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -23,5 +28,361 @@ get => IConfig as M071Config; } #endregion + + #region 浜嬩欢 + public event Action OnMeasureStart; + public event Action<string> OnBarcodeChanged; + public event Action<IShapeElement> OnElementUpdated; + #endregion + + public override void Open() + { + InitialSetting(); + + base.Open(); + } + + private void InitialSetting() + { + Config.SnapshotPointCollection.ForEach(u => + { + u.GetHalconToolPathList().ForEach(path => + { + LoadHalconTool(path, u.Id); + }); + }); + + Config.KeyUnitCollection.ForEach(u => + { + if (!u.IsEnabled) + return; + + var snapshotPoint = Config.SnapshotPointCollection.FirstOrDefault(s => s.Id == u.SnapshotPointId && s.IsEnabled); + if (snapshotPoint == null) + throw new ProcessException($"{u.AlignName}鏈缃彲鐢ㄦ媿鐓х偣浣�"); + + if (u.ImageSeq < 1) + throw new ProcessException($"{u.AlignName}鍥剧墖搴忓彿灏忎簬1"); + + var algo = Config.KeyAlgorithemCollection.FirstOrDefault(a => a.Id == u.KeyAlgorithemId); + if (algo == null) + throw new ProcessException($"{u.AlignName}鏈缃娴嬬畻娉�"); + + u.KeyAlgorithemPath = algo.AlgorithemPath; + LoadHalconTool(u.KeyAlgorithemPath, u.AlignName); + + var resultSet = Config.KeyResultCollection.FirstOrDefault(r => r.Id == u.KeyResultId); + if (resultSet == null) + throw new ProcessException($"{u.AlignName}鏈缃娴嬬粨鏋滈厤缃�"); + + u.KeyResultList = new List<string>(resultSet.Results); + }); + + Config.MeasurementUnitCollection.ForEach(u => + { + if (!u.IsEnabled) + return; + + var spec = Config.MeasureTypeCollection.FirstOrDefault(s => s.Code == u.MeasureType); + if (spec == null) + throw new ProcessException($"{u.GetDisplayText()}鏈缃娴嬬被鍨�"); + + u.Spec = new MeasureType(); + u.Spec.DataFrom(spec); + + LoadHalconTool(u.Spec.AlgorithemPath, u.Id); + + u.KeyUnitCollection.ForEach(b => + { + var keyRespone = Config.KeyUnitCollection.Where(ku => ku.IsEnabled && ku.Key == b.Key).ToList(); + if (keyRespone.Count == 0) + throw new ProcessException($"{u.GetDisplayText()}瀵瑰簲鐨勫崟閿笉瀛樺湪鎴栦笉鍙敤"); + + if (b.KeyResult == "All") + { + keyRespone.SelectMany(kr => kr.KeyResultList).ToList().ForEach(r => + { + b.MeasureValueDict[r] = null; + }); + } + else + { + b.MeasureValueDict[b.KeyResult] = null; + } + }); + }); + } + + #region InitialHalconTool + //protected override void InitialHalconTool() + //{ + // base.InitialHalconTool(); + + // Config.SnapshotPointCollection.ForEach(u => + // { + // u.GetHalconToolPathList().ForEach(path => + // { + // if (!string.IsNullOrWhiteSpace(path)) + // { + // string directoryPath = Path.GetDirectoryName(path); + // string fileName = Path.GetFileNameWithoutExtension(path); + + // HDevEngineTool tool = new HDevEngineTool(directoryPath); + // tool.LoadProcedure(fileName); + + // //浣跨敤鈥渱鈥濅綔涓洪棿闅旂 + // _halconToolDict[u.Id + "|" + path] = tool; + // } + // }); + // }); + //} + #endregion + + //Dictionary<string, ProductionMeasurement> MeasureDict = new Dictionary<string, ProductionMeasurement>(); + + private string barCode = ""; + public string BarCode + { + get => barCode; + set + { + if (barCode != value) + { + barCode = value; + OnBarcodeChanged?.Invoke(value); + } + } + } + + [ProcessMethod("", "StartJob", "寮�濮嬫壂鎻�", InvokeType.TestInvoke)] + public ProcessResponse StartJob(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + { + if (string.IsNullOrWhiteSpace(BarCode)) + { + throw new ProcessException("鏈緭鍏ヤ骇鍝佹潯鐮侊紝璇峰嬁寮�濮嬫祴閲�"); + } + + OnMeasureStart?.BeginInvoke(null, null); + + var measurements = Config.MeasurementUnitCollection.Where(u => u.IsEnabled).ToList().DeepSerializeClone(); + measurements.ForEach(m => + { + m.InitialKeyUnitMeasureChanged(); + //m.KeyUnitCollection.ForEach(k => k.InitialMeasureValueDictPropertyChanged()); + }); + + //MeasureDict[BarCode] = new ProductionMeasurement() + //{ + // Barcode = BarCode, + // Measurements = measurements, + //}; + + //MeasureDict[BarCode].InitialMeasurementsPropertyChanged(); + //MeasureDict[BarCode].PropertyChanged += MeasureProduction_PropertyChanged; + + var pMeasure = new ProductionMeasurement() + { + Barcode = BarCode, + Measurements = measurements, + }; + + pMeasure.InitialMeasurementsPropertyChanged(); + pMeasure.PropertyChanged += MeasureProduction_PropertyChanged; + + Config.SnapshotPointCollection.Where(u => u.IsEnabled).ToList().ForEach(s => + { + IDevice device = DeviceCollection.FirstOrDefault(u => u.Id == s.MotionDevice); + if (device == null) + throw new ProcessException($"{s.Name}鎷嶇収鐐逛綅鏈缃繍鍔ㄨ澶�"); + + IMotion motionDevice = device as IMotion; + + if (motionDevice == null) + throw new ProcessException($"{s.Name}鎷嶇収鐐逛綅璁剧疆{device.Name}涓嶆槸杩愬姩璁惧"); + + if (!motionDevice.MoveToPoint(null)) + { + throw new ProcessException("杩愬姩涓", null, ExceptionLevel.Info); + } + + CameraBase camera = DeviceCollection.FirstOrDefault(u => u.Id == s.CameraOp.Device) as CameraBase; + if (camera == null) + return; + + ImageSet imgSet = camera.Snapshot(s.CameraOp.OpConfig); + if (imgSet == null) + return; + + RunImageHandle(camera, s.CameraOp.OpConfig, imgSet, s.Id, s.Name, pMeasure.Measurements); + }); + + BarCode = ""; + return new ProcessResponse(true); + } + + private void MeasureProduction_PropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (sender is ProductionMeasurement pMeasure) + { + //妫�鏌ユ槸鍚﹀叏閮ㄥ畬鎴� + pMeasure.Measurements.ForEach(m => + { + if (m.KeyUnitCollection.All(k => k.IsDone != null)) + { + if (!m.IsUpdated) + { + if (m.KeyUnitCollection.Any(k => k.IsDone == false)) + { + m.Spec.ActualValue = -999; + } + else + { + string toolKey = m.Id + "|" + m.Spec.AlgorithemPath; + if (!_halconToolDict.ContainsKey(toolKey)) + { + LogAsync(DateTime.Now, $"{m.GetDisplayText()}{m.Spec.Code}绠楁硶鏈垵濮嬪寲", ""); + m.Spec.ActualValue = -999; + } + else + { + var array = m.KeyUnitCollection.SelectMany(u => u.MeasureValueDict.Values.ToList().ConvertAll(v => v ?? -999)).ToArray(); + _halconToolDict[toolKey].InputTupleDic["INPUT_Params"] = new HTuple(array); + if (!_halconToolDict[toolKey].RunProcedure(out string error)) + { + LogAsync(DateTime.Now, $"{m.GetDisplayText()}{m.Spec.Code}绠楁硶寮傚父,{error}", ""); + m.Spec.ActualValue = -999; + } + else + { + m.Spec.ActualValue = _halconToolDict[toolKey].GetResultTuple("OUTPUT_Result").D; + LogAsync(DateTime.Now, $"{m.GetDisplayText()}鏁版嵁{m.Spec.ActualValue}锛岀粨鏋渰(m.Spec.MeasureResult == null ? "TBD" : (m.Spec.MeasureResult == true ? "OK" : "NG"))}", ""); + } + } + } + + //杈撳嚭鍥惧舰鍩哄厓鍒扮晫闈� todo + OnElementUpdated?.BeginInvoke(null, null, null); + + m.IsUpdated = true; + } + } + }); + + if (!pMeasure.Measurements.All(m => m.IsUpdated)) + { + return; + } + + //MES杈撳嚭 todo + + //Excel鎶ヨ〃杈撳嚭 todo + + //鏁版嵁搴撲繚瀛� todo + + //MeasureDict.Remove(pMeasure.Barcode); + pMeasure.Dispose(); + } + } + + private async void RunImageHandle(CameraBase camera, IOperationConfig opConfig, ImageSet imgSet, string snapshotId, string snapshotName, List<MeasurementUint> measureList) + { + await Task.Run(() => + { + var keys = Config.KeyUnitCollection.Where(u => u.IsEnabled && u.SnapshotPointId == snapshotId); + var keyBindCollection = measureList.SelectMany(u => u.KeyUnitCollection).Where(u => keys.Any(k => k.Key == u.Key)).ToList(); + + string toolKey = (opConfig as CameraOprerationConfigBase).AlgorithemPath; + HObject images = imgSet.HImage; + + if (!string.IsNullOrWhiteSpace(toolKey)) + { + toolKey = snapshotId + "|" + toolKey; + if (!_halconToolDict.ContainsKey(toolKey)) + { + LogAsync(DateTime.Now, $"{snapshotName}鏈垵濮嬪寲鍙栧浘绠楁硶閰嶇疆", ""); + keyBindCollection.ForEach(k => k.FillKeyValues(null)); + return; + } + + _halconToolDict[toolKey].InputImageDic["INPUT_Image"] = imgSet.HImage; + if (!_halconToolDict[toolKey].RunProcedure(out string error)) + { + LogAsync(DateTime.Now, $"{snapshotName}鍙栧浘绠楁硶寮傚父锛寋error}", ""); + keyBindCollection.ForEach(k => k.FillKeyValues(null)); + return; + } + + images = _halconToolDict[toolKey].GetResultObject("OUTPUT_Images"); + } + + HOperatorSet.CountObj(images, out HTuple count); + + if (count == 0) + { + LogAsync(DateTime.Now, $"{snapshotName}鍙栧浘绠楁硶鏈緭鍑哄浘鍍�", ""); + keyBindCollection.ForEach(k => k.FillKeyValues(null)); + return; + } + + var excludeKeys = keys.Where(u => u.ImageSeq > count).ToList(); + if (excludeKeys.Count > 0) + { + LogAsync(DateTime.Now, $"{string.Join(" ", excludeKeys.Select(u => u.AlignName))}鏈湪鍥剧墖鑾峰彇搴忓垪涓�", ""); + keyBindCollection.Where(k => excludeKeys.Any(u => u.Key == k.Key)).ToList().ForEach(k => + { + k.FillKeyValues(null); + }); + } + + Parallel.For(1, count.I, (i) => + { + HOperatorSet.SelectObj(images, out HObject image, i); + + keys.Where(u => u.ImageSeq == i).ToList().ForEach(k => + { + Dictionary<string, double> resultDict = null; + + var keyBindList = keyBindCollection.Where(u => u.Key == k.Key).ToList(); + + string keyToolKey = k.AlignName + "|" + k.KeyAlgorithemPath; + if (!_halconToolDict.ContainsKey(keyToolKey)) + { + LogAsync(DateTime.Now, $"{k.AlignName}妫�娴嬬畻娉曟湭鍒濆鍖�", ""); + } + else + { + _halconToolDict[keyToolKey].InputImageDic["INPUT_Image"] = image; + if (!_halconToolDict[keyToolKey].RunProcedure(out string error)) + { + LogAsync(DateTime.Now, $"{k.AlignName}妫�娴嬬畻娉曞紓甯革紝{error}", ""); + } + else + { + var results = _halconToolDict[keyToolKey].GetResultTuple("OUTPUT_Results").HTupleToDouble(); + if (results.Count == 0 || results.Any(u => u < 0)) + { + LogAsync(DateTime.Now, $"{k.AlignName}妫�娴嬬粨鏋滃紓甯�", ""); + } + else + { + resultDict = k.KeyResultList.ToDictionary(u => u, u => + { + int index = k.KeyResultList.IndexOf(u); + return results[index]; + }); + } + } + } + + keyBindList.ForEach(kb => kb.FillKeyValues(resultDict)); + }); + + image.Dispose(); + }); + + if (count.I != 1) + imgSet.HImage.Dispose(); + }); + } } } -- Gitblit v1.8.0