领胜LDS 键盘AOI检测项目
xcd
2020-07-09 c0b8009ef36746c7a14606267662570e8c59c2c6
src/Bro.M071.Process/M071Process.cs
@@ -5,6 +5,7 @@
using Bro.M071.Model;
using Bro.Process;
using HalconDotNet;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@@ -47,14 +48,19 @@
            base.Open();
            SwitchBeep(false);
            SwitchLightGreen(false);
            SwitchLightRed(false);
            SwitchLightYellow(false);
            Reset(null, null, null);
            FullReset(null);
        }
        private void InitialSetting()
        {
            //数据库迁移检查
            DatabaseInitialize.Initialize();
            ////数据库迁移检查
            //DatabaseInitialize.Initialize();
            MotionCardSettingCheck();
@@ -73,21 +79,21 @@
                var snapshotPoint = Config.SnapshotPointCollection.FirstOrDefault(s => s.Id == u.SnapshotPointId && s.IsEnabled);
                if (snapshotPoint == null)
                    throw new ProcessException($"{u.AlignName}未设置可用拍照点位");
                    throw new ProcessException($"{u.AliasName}未设置可用拍照点位");
                if (u.ImageSeq < 1)
                    throw new ProcessException($"{u.AlignName}图片序号小于1");
                    throw new ProcessException($"{u.AliasName}图片序号小于1");
                var algo = Config.KeyAlgorithemCollection.FirstOrDefault(a => a.Id == u.KeyAlgorithemId);
                if (algo == null)
                    throw new ProcessException($"{u.AlignName}未设置检测算法");
                    throw new ProcessException($"{u.AliasName}未设置检测算法");
                u.KeyAlgorithemPath = algo.AlgorithemPath;
                LoadHalconTool(u.KeyAlgorithemPath, u.AlignName);
                LoadHalconTool(u.KeyAlgorithemPath, u.AliasName);
                var resultSet = Config.KeyResultCollection.FirstOrDefault(r => r.Id == u.KeyResultId);
                if (resultSet == null)
                    throw new ProcessException($"{u.AlignName}未设置检测结果配置");
                    throw new ProcessException($"{u.AliasName}未设置检测结果配置");
                u.KeyResultList = new List<string>(resultSet.Results);
            });
@@ -214,11 +220,10 @@
                      if (camera == null)
                          return;
                      string imgSetId = "";
                      HImage hImage = null;
                      IImageSet set = null;
                      try
                      {
                          hImage = CollectHImage(camera, s.CameraOp.OpConfig, out imgSetId);
                          set = CollectHImage(camera, s.CameraOp.OpConfig);
                      }
                      catch (ProcessException pEx)
                      {
@@ -226,12 +231,12 @@
                          throw pEx;
                      }
                      if (string.IsNullOrWhiteSpace(imgSetId))
                      if (set == null)
                      {
                          return;
                      }
                      RunImageHandle(camera, s.CameraOp.OpConfig, hImage, s.Id, s.Name, pMeasure.Measurements);
                      RunImageHandle(camera, s.CameraOp.OpConfig, set, s.Id, s.Name, pMeasure.Measurements);
                  });
            BarCode = "";
@@ -341,7 +346,7 @@
                                IShapeElement indicator = null;
                                pMeasure.ElementList.Add(indicator);
                                //输出图形基元到界面 todo
                                //输出图形基元到界面
                                OnElementUpdated?.BeginInvoke(indicator, null, null);
                                SaveKeyImages(pMeasure.Barcode, m);
@@ -463,7 +468,7 @@
        }
        #endregion
        private async void RunImageHandle(CameraBase camera, IOperationConfig opConfig, HImage hImage, string snapshotId, string snapshotName, List<MeasurementUnit> measureList)
        private async void RunImageHandle(CameraBase camera, IOperationConfig opConfig, IImageSet imgSet, string snapshotId, string snapshotName, List<MeasurementUnit> measureList)
        {
            await Task.Run(() =>
             {
@@ -471,7 +476,8 @@
                 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 = hImage;
                 HObject images = imgSet.HImage;
                 LaserScanParam scanParam = JsonConvert.DeserializeObject<LaserScanParam>(imgSet.ImageData);
                 if (!string.IsNullOrWhiteSpace(toolKey))
                 {
@@ -483,7 +489,7 @@
                         return;
                     }
                     _halconToolDict[toolKey].InputImageDic["INPUT_Image"] = hImage;
                     _halconToolDict[toolKey].InputImageDic["INPUT_Image"] = imgSet.HImage;
                     if (!_halconToolDict[toolKey].RunProcedure(out string error))
                     {
                         LogAsync(DateTime.Now, $"{snapshotName}取图算法异常,{error}", "");
@@ -506,7 +512,7 @@
                 var excludeKeys = keys.Where(u => u.ImageSeq > count).ToList();
                 if (excludeKeys.Count > 0)
                 {
                     LogAsync(DateTime.Now, $"{string.Join(" ", excludeKeys.Select(u => u.AlignName))}未在图片获取序列中", "");
                     LogAsync(DateTime.Now, $"{string.Join(" ", excludeKeys.Select(u => u.AliasName))}未在图片获取序列中", "");
                     keyBindCollection.Where(k => excludeKeys.Any(u => u.Key == k.Key)).ToList().ForEach(k =>
                          {
                              k.FillKeyValues(null);
@@ -523,24 +529,26 @@
                             var keyBindList = keyBindCollection.Where(u => u.Key == k.Key).ToList();
                             string keyToolKey = k.AlignName + "|" + k.KeyAlgorithemPath;
                             string keyToolKey = k.AliasName + "|" + k.KeyAlgorithemPath;
                             if (!_halconToolDict.ContainsKey(keyToolKey))
                             {
                                 LogAsync(DateTime.Now, $"{k.AlignName}检测算法未初始化", "");
                                 LogAsync(DateTime.Now, $"{k.AliasName}检测算法未初始化", "");
                             }
                             else
                             {
                                 _halconToolDict[keyToolKey].InputImageDic["INPUT_Image"] = image;
                                 _halconToolDict[keyToolKey].InputTupleDic["INPUT_Resolution_X"] = scanParam.Resolution_X;
                                 _halconToolDict[keyToolKey].InputTupleDic["INPUT_Resolution_Z"] = scanParam.Resolution_Z;
                                 if (!_halconToolDict[keyToolKey].RunProcedure(out string error))
                                 {
                                     LogAsync(DateTime.Now, $"{k.AlignName}检测算法异常,{error}", "");
                                     LogAsync(DateTime.Now, $"{k.AliasName}检测算法异常,{error}", "");
                                 }
                                 else
                                 {
                                     var results = _halconToolDict[keyToolKey].GetResultTuple("OUTPUT_Results").HTupleToDouble();
                                     if (results.Count == 0 || results.Any(u => u < 0))
                                     {
                                         LogAsync(DateTime.Now, $"{k.AlignName}检测结果异常", "");
                                         LogAsync(DateTime.Now, $"{k.AliasName}检测结果异常", "");
                                     }
                                     else
                                     {
@@ -563,11 +571,11 @@
                     image.Dispose();
                 });
                 if (count.I != 1)
                 {
                     hImage?.Dispose();
                     hImage = null;
                 }
                 //if (count.I != 1)
                 //{
                 //    hImage?.Dispose();
                 //    hImage = null;
                 //}
             });
        }
        #endregion