From 3f6a707f2373cfcee7e96c080a01ac1a4964a419 Mon Sep 17 00:00:00 2001 From: wells.liu <wells.liu@broconcentric.com> Date: 星期四, 02 七月 2020 20:54:55 +0800 Subject: [PATCH] 解决 生成新配置问题 --- src/Bro.M071.Process/M071Process.cs | 270 ++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 210 insertions(+), 60 deletions(-) diff --git a/src/Bro.M071.Process/M071Process.cs b/src/Bro.M071.Process/M071Process.cs index 13fde83..a6a58a3 100644 --- a/src/Bro.M071.Process/M071Process.cs +++ b/src/Bro.M071.Process/M071Process.cs @@ -7,9 +7,11 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Drawing; using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace Bro.M071.Process @@ -138,8 +140,6 @@ //} #endregion - //Dictionary<string, ProductionMeasurement> MeasureDict = new Dictionary<string, ProductionMeasurement>(); - private string barCode = ""; public string BarCode { @@ -154,6 +154,8 @@ } } + List<ProductionMeasurement> productionList = new List<ProductionMeasurement>(); + [ProcessMethod("", "StartJob", "寮�濮嬫壂鎻�", InvokeType.TestInvoke)] public ProcessResponse StartJob(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) { @@ -162,23 +164,19 @@ throw new ProcessException("鏈緭鍏ヤ骇鍝佹潯鐮侊紝璇峰嬁寮�濮嬫祴閲�"); } + //妫�鏌ヤ骇鍝佹斁缃甇K + + //姘旂几鎺ㄥ姩鍘嬫澘绉诲姩鍒颁骇鍝佷笂鏂� + + //鍘嬫澘鍘嬬揣浜у搧 + 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() { @@ -186,105 +184,249 @@ Measurements = measurements, }; + var existedProduction = productionList.FirstOrDefault(u => u.Barcode == pMeasure.Barcode); + if (existedProduction != null) + { + productionList.Remove(existedProduction); + existedProduction.Dispose(); + existedProduction = null; + } + + productionList.Add(pMeasure); + pMeasure.InitialMeasurementsPropertyChanged(); pMeasure.PropertyChanged += MeasureProduction_PropertyChanged; Config.SnapshotPointCollection.Where(u => u.IsEnabled).ToList().ForEach(s => { - IDevice device = DeviceCollection.FirstOrDefault(u => u.Id == s.MotionDevice); + _pauseHandle.WaitHandle.WaitOne(); + + IDevice device = DeviceCollection.FirstOrDefault(u => u.Id == s.MotionOp.Device); if (device == null) throw new ProcessException($"{s.Name}鎷嶇収鐐逛綅鏈缃繍鍔ㄨ澶�"); - IMotion motionDevice = device as IMotion; + IMotionCard motionDevice = device as IMotionCard; if (motionDevice == null) throw new ProcessException($"{s.Name}鎷嶇収鐐逛綅璁剧疆{device.Name}涓嶆槸杩愬姩璁惧"); - if (!motionDevice.MoveToPoint(null)) + var response = motionDevice.Run(s.MotionOp.OpConfig); + if (!response.Result) { - throw new ProcessException("杩愬姩涓", null, ExceptionLevel.Info); + throw new ProcessException($"{device.Name}寮傚父锛寋response.Message}", 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) + HImage hImage = CollectHImage(camera, s.CameraOp.OpConfig, out string imgSetId); + if (string.IsNullOrWhiteSpace(imgSetId)) + { return; + } - RunImageHandle(camera, s.CameraOp.OpConfig, imgSet, s.Id, s.Name, pMeasure.Measurements); + RunImageHandle(camera, s.CameraOp.OpConfig, hImage, s.Id, s.Name, pMeasure.Measurements); }); BarCode = ""; + return new ProcessResponse(true); } + /// <summary> + /// 鏆傚仠鏍囧織 + /// WaitHandle 鏆傚仠鍙ユ焺 榛樿涓洪潪闃诲 鍙墽琛� + /// WaitResult 鏆傚仠鏍囧織 true 姝e父鎵ц false 鏆傚仠涓� + /// </summary> + ManualWaitConfirm _pauseHandle = new ManualWaitConfirm() + { + WaitHandle = new ManualResetEvent(true), + WaitResult = true, + }; + + [ProcessMethod("", "PauseJob", "鏆傚仠娴佺▼", InvokeType.TestInvoke)] + public ProcessResponse PauseJob(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + { + if (_pauseHandle.WaitResult) + { + #region 鏉垮崱鏆傚仠鍔ㄤ綔 + #endregion + + _pauseHandle.WaitHandle.Reset(); + } + else + { + #region 鏉垮崱鎭㈠鍔ㄤ綔 + #endregion + + _pauseHandle.WaitHandle.Set(); + } + + _pauseHandle.WaitResult = !_pauseHandle.WaitResult; + return new ProcessResponse(_pauseHandle.WaitResult); + } + + #region 绉佹湁鏂规硶 private void MeasureProduction_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (sender is ProductionMeasurement pMeasure) { - //妫�鏌ユ槸鍚﹀叏閮ㄥ畬鎴� - pMeasure.Measurements.ForEach(m => + lock (pMeasure) { - if (m.KeyUnitCollection.All(k => k.IsDone != null)) + //妫�鏌ユ槸鍚﹀叏閮ㄥ畬鎴� + pMeasure.Measurements.ForEach(m => { - if (!m.IsUpdated) + if (m.KeyUnitCollection.All(k => k.IsDone != null)) { - if (m.KeyUnitCollection.Any(k => k.IsDone == false)) + if (!m.IsUpdated) { - m.Spec.ActualValue = -999; - } - else - { - string toolKey = m.Id + "|" + m.Spec.AlgorithemPath; - if (!_halconToolDict.ContainsKey(toolKey)) + if (m.KeyUnitCollection.Any(k => k.IsDone == false)) { - 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)) + string toolKey = m.Id + "|" + m.Spec.AlgorithemPath; + if (!_halconToolDict.ContainsKey(toolKey)) { - LogAsync(DateTime.Now, $"{m.GetDisplayText()}{m.Spec.Code}绠楁硶寮傚父,{error}", ""); + LogAsync(DateTime.Now, $"{m.GetDisplayText()}{m.Spec.Code}绠楁硶鏈垵濮嬪寲", ""); 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"))}", ""); + 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"))}", ""); + } } } + + LogAsync(DateTime.Now, $"{m.GetDisplayText()}妫�娴嬬粨鏋�", $"{((m.Spec.MeasureResult ?? false) ? "OK" : "NG")}"); + + IShapeElement indicator = null; + pMeasure.ElementList.Add(indicator); + //杈撳嚭鍥惧舰鍩哄厓鍒扮晫闈� todo + OnElementUpdated?.BeginInvoke(indicator, null, null); + + SaveKeyImages(pMeasure.Barcode, m); + + m.IsUpdated = true; } - - //杈撳嚭鍥惧舰鍩哄厓鍒扮晫闈� todo - OnElementUpdated?.BeginInvoke(null, null, null); - - m.IsUpdated = true; } + }); + + if (!pMeasure.Measurements.All(m => m.IsUpdated)) + { + return; } - }); - if (!pMeasure.Measurements.All(m => m.IsUpdated)) - { - return; + //MES杈撳嚭 todo + + //Excel鎶ヨ〃杈撳嚭 todo + + //鏁版嵁搴撲繚瀛� todo + + SaveWholeImage(pMeasure); + + productionList.RemoveAll(p => p.Barcode == pMeasure.Barcode); + pMeasure.Dispose(); } - - //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) + #region 鍥惧儚淇濆瓨 + private void SaveWholeImage(ProductionMeasurement pMeasure) + { + try + { + Bitmap backImage = (Bitmap)Bitmap.FromFile(Config.BackgroundImagePath); + + Bitmap map = new Bitmap(backImage.Width, backImage.Height); + using (Graphics g = Graphics.FromImage(map)) + { + g.DrawImage(backImage, new PointF(0, 0)); + + pMeasure.ElementList.ForEach(e => + { + e.Draw(g); + }); + } + + string dir = Path.Combine(Config.ImageSaveFolder, "TopView", DateTime.Now.ToString("yyyyMMdd")); + if (!Directory.Exists(dir)) + { + Directory.CreateDirectory(dir); + } + + map.Save(Path.Combine(dir, $"{pMeasure.Barcode}_{DateTime.Now.ToString("HHmmss")}.bmp")); + } + catch (Exception ex) + { + LogAsync(DateTime.Now, "鏁翠綋鍥剧墖淇濆瓨寮傚父", ex.GetExceptionMessage()); + } + } + + private void SaveKeyImages(string barCode, MeasurementUint measureUnit) + { + string measureName = measureUnit.GetDisplayText(); + if (Config.ImageSaveOption.IsSaveOriginImage) + { + measureUnit.KeyUnitCollection.ForEach(u => u.ImageSaveStatus++); + + string dir = Path.Combine(Config.ImageSaveFolder, "Origin", DateTime.Now.ToString("yyyyMMdd"), barCode, measureUnit.MeasureType); + if (!Directory.Exists(dir)) + { + Directory.CreateDirectory(dir); + } + + SaveKeyImages(measureUnit, measureName, dir); + } + + string result = (measureUnit.Spec.MeasureResult ?? false) ? "OK" : "NG"; + if (Config.ImageSaveOption.AddtionalSaveType.ToUpper().Contains(result)) + { + measureUnit.KeyUnitCollection.ForEach(u => u.ImageSaveStatus++); + + string dir = Path.Combine(Config.ImageSaveFolder, result, DateTime.Now.ToString("yyyyMMdd"), barCode, measureUnit.MeasureType); + if (!Directory.Exists(dir)) + { + Directory.CreateDirectory(dir); + } + + SaveKeyImages(measureUnit, measureName, dir); + } + } + + private async void SaveKeyImages(MeasurementUint measureUnit, string measureName, string dir) + { + await Task.Run(() => + { + measureUnit.KeyUnitCollection.ForEach(u => + { + int i = 0; + u.KeyImages?.ForEach(image => + { + string fileName = Path.Combine(dir, $"{measureName}_{u.Key}{(i == 0 ? "" : $"-{i}")}_{DateTime.Now.ToString("HHmmss")}.tiff"); + image.WriteImage("tiff", 0, fileName); + i++; + }); + + u.ImageSaveStatus--; + }); + }); + } + #endregion + + private async void RunImageHandle(CameraBase camera, IOperationConfig opConfig, HImage hImage, string snapshotId, string snapshotName, List<MeasurementUint> measureList) { await Task.Run(() => { @@ -292,7 +434,7 @@ 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; + HObject images = hImage; if (!string.IsNullOrWhiteSpace(toolKey)) { @@ -304,7 +446,7 @@ return; } - _halconToolDict[toolKey].InputImageDic["INPUT_Image"] = imgSet.HImage; + _halconToolDict[toolKey].InputImageDic["INPUT_Image"] = hImage; if (!_halconToolDict[toolKey].RunProcedure(out string error)) { LogAsync(DateTime.Now, $"{snapshotName}鍙栧浘绠楁硶寮傚父锛寋error}", ""); @@ -374,15 +516,23 @@ } } - keyBindList.ForEach(kb => kb.FillKeyValues(resultDict)); + keyBindList.ForEach(kb => + { + kb.KeyImages.Add(image.Clone() as HImage); + kb.FillKeyValues(resultDict); + }); }); image.Dispose(); }); if (count.I != 1) - imgSet.HImage.Dispose(); + { + hImage?.Dispose(); + hImage = null; + } }); } + #endregion } } -- Gitblit v1.8.0