| | |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.ComponentModel; |
| | | using System.Drawing; |
| | | using System.IO; |
| | | using System.Linq; |
| | | using System.Text; |
| | |
| | | if (camera == null) |
| | | return; |
| | | |
| | | IImageSet imgSet = camera.Snapshot(s.CameraOp.OpConfig); |
| | | if (imgSet == null) |
| | | return; |
| | | //IImageSet imgSet = camera.Snapshot(s.CameraOp.OpConfig); |
| | | //if (imgSet == null) |
| | | // return; |
| | | |
| | | RunImageHandle(camera, s.CameraOp.OpConfig, imgSet, s.Id, s.Name, pMeasure.Measurements); |
| | | HImage hImage = CollectHImage(camera, s.CameraOp.OpConfig, out string imgSetId); |
| | | if (string.IsNullOrWhiteSpace(imgSetId)) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | RunImageHandle(camera, s.CameraOp.OpConfig, hImage, s.Id, s.Name, pMeasure.Measurements); |
| | | }); |
| | | |
| | | BarCode = ""; |
| | |
| | | _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"))}", ""); |
| | | } |
| | | } |
| | | } |
| | | |
| | | //输出图形基元到界面 todo |
| | | OnElementUpdated?.BeginInvoke(null, 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); |
| | | pMeasure.Dispose(); |
| | | } |
| | | |
| | | //MES输出 todo |
| | | |
| | | //Excel报表输出 todo |
| | | |
| | | //数据库保存 todo |
| | | |
| | | //MeasureDict.Remove(pMeasure.Barcode); |
| | | pMeasure.Dispose(); |
| | | } |
| | | } |
| | | |
| | | private async void RunImageHandle(CameraBase camera, IOperationConfig opConfig, IImageSet 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(() => |
| | | { |
| | |
| | | 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)) |
| | | { |
| | |
| | | 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}", ""); |
| | |
| | | } |
| | | } |
| | | |
| | | 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 |