领胜LDS 键盘AOI检测项目
xcd
2020-06-30 8feea03e4ff2bd4f6db21928e7989f0b72686867
修改流程取图方式。添加M071流程OK/NG/原图保存。
4个文件已修改
169 ■■■■■ 已修改文件
src/Bro.M071.Process/M071Config.cs 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Bro.M071.Process/M071Models.cs 28 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Bro.M071.Process/M071Process.cs 125 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Bro.Process/ProcessControl.cs 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Bro.M071.Process/M071Config.cs
@@ -1,4 +1,5 @@
using Bro.Common.Helper;
using Bro.Common.Base;
using Bro.Common.Helper;
using Bro.Common.Model;
using Bro.Process;
using System;
@@ -62,5 +63,16 @@
        [TypeConverter(typeof(CollectionCountConvert))]
        [Editor(typeof(ComplexCollectionEditor<MeasurementUint>), typeof(UITypeEditor))]
        public List<MeasurementUint> MeasurementUnitCollection { get; set; } = new List<MeasurementUint>();
        [Category("图片保存配置")]
        [Description("单键图片保存配置")]
        [TypeConverter(typeof(ComplexObjectConvert))]
        [Editor(typeof(PropertyObjectEditor), typeof(UITypeEditor))]
        public ImageSaveOption ImageSaveOption { get; set; } = new ImageSaveOption();
        [Category("图片保存配置")]
        [Description("单键图片保存目录路径")]
        [Editor(typeof(FoldDialogEditor),typeof(UITypeEditor))]
        public string ImageSaveFolder { get; set; } = "";
    }
}
src/Bro.M071.Process/M071Models.cs
@@ -3,6 +3,7 @@
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.Common.Model;
using HalconDotNet;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
@@ -11,6 +12,7 @@
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Bro.M071.Process
@@ -195,18 +197,20 @@
    public class ProductionMeasurement : INotifyPropertyChanged, IDisposable
    {
        public string Barcode { get; set; }
        public string Barcode;
        public List<MeasurementUint> Measurements { get; set; } = new List<MeasurementUint>();
        public List<MeasurementUint> Measurements = new List<MeasurementUint>();
        public List<IShapeElement> ElementList = new List<IShapeElement>();
        public event PropertyChangedEventHandler PropertyChanged;
        public void Dispose()
        {
            Barcode = null;
            Measurements?.ForEach(m => m?.Dispose());
            Measurements = null;
            Barcode = null;
            GC.Collect();
        }
@@ -304,6 +308,10 @@
        [TypeConverter(typeof(KeyUnitResultConverter))]
        public string KeyResult { get; set; } = "";
        public List<HImage> KeyImages = new List<HImage>();
        public volatile int ImageSaveStatus = 0;
        //[Browsable(false)]
        //public NoticedDictionary<string, double?> MeasureValueDict { get; set; } = new NoticedDictionary<string, double?>();
@@ -335,6 +343,18 @@
        public void Dispose()
        {
            SpinWait wait = new SpinWait();
            while (ImageSaveStatus != 0)
            {
                wait.SpinOnce();
            }
            KeyImages?.ForEach(i =>
            {
                i?.Dispose();
                i = null;
            });
            KeyImages = null;
            MeasureValueDict = null;
        }
src/Bro.M071.Process/M071Process.cs
@@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
@@ -200,11 +201,17 @@
                      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 = "";
@@ -243,10 +250,13 @@
            _pauseHandle.WaitResult = !_pauseHandle.WaitResult;
            return new ProcessResponse(_pauseHandle.WaitResult);
        }
        #region 私有方法
        private void MeasureProduction_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (sender is ProductionMeasurement pMeasure)
            {
                lock (pMeasure)
            {
                //检查是否全部完成
                pMeasure.Measurements.ForEach(m =>
@@ -287,6 +297,8 @@
                            //输出图形基元到界面 todo
                            OnElementUpdated?.BeginInvoke(null, null, null);
                                SaveKeyImages(pMeasure.Barcode, m);
                            m.IsUpdated = true;
                        }
                    }
@@ -297,18 +309,104 @@
                    return;
                }
                //MES输出 todo
                //Excel报表输出 todo
                //数据库保存 todo
                //MeasureDict.Remove(pMeasure.Barcode);
                    SaveWholeImage(pMeasure);
                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(() =>
             {
@@ -316,7 +414,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))
                 {
@@ -328,7 +426,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}", "");
@@ -398,14 +496,21 @@
                                 }
                             }
                             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
src/Bro.Process/ProcessControl.cs
@@ -628,7 +628,7 @@
                TimeRecordCSV(DateTime.Now, camera.Name, methodCode + "采图", (int)sw.ElapsedMilliseconds);
            }
            imgSetId = set.Id;
            imgSetId = set?.Id;
            return set.HImage;
        }