From 5b6ffebeeee53e375cf8f8d5c30c51f03ad1d96e Mon Sep 17 00:00:00 2001
From: xcd <834800634@qq.com>
Date: 星期四, 09 七月 2020 19:12:42 +0800
Subject: [PATCH] Merge branch 'master' of http://gitblit.broconcentric.com:8088/r/M071

---
 src/Bro.M071.Process/M071Models.cs |  277 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 252 insertions(+), 25 deletions(-)

diff --git a/src/Bro.M071.Process/M071Models.cs b/src/Bro.M071.Process/M071Models.cs
index 16af13a..5ccaad2 100644
--- a/src/Bro.M071.Process/M071Models.cs
+++ b/src/Bro.M071.Process/M071Models.cs
@@ -3,6 +3,8 @@
 using Bro.Common.Helper;
 using Bro.Common.Interface;
 using Bro.Common.Model;
+using HalconDotNet;
+using Newtonsoft.Json;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
@@ -10,6 +12,7 @@
 using System.Drawing.Design;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 
 namespace Bro.M071.Process
@@ -32,6 +35,19 @@
         {
             return $"{Name} -- {AlgorithemPath}";
         }
+    }
+
+    public class MeasureType : Spec
+    {
+        [Category("妫�娴嬬被鍨�")]
+        [Description("妫�娴嬬被鍨�")]
+        [DisplayName("妫�娴嬬被鍨�")]
+        public override string Code { get; set; }
+
+        [Category("妫�娴嬬畻娉�")]
+        [Description("妫�娴嬬畻娉曡矾寰�")]
+        [Editor(typeof(FileDialogEditor), typeof(UITypeEditor))]
+        public string AlgorithemPath { get; set; }
     }
 
     public class KeyResult : IComplexDisplay
@@ -59,7 +75,7 @@
     {
         public string GetDisplayText()
         {
-            return Name + " " + string.Join(" ", Destination.Select(u => u.GetDisplayText())) + " " + CameraOp.GetDisplayText();
+            return (IsEnabled ? "" : "绂佺敤 ") + Name + " " + MotionOp.GetDisplayText() + " " + CameraOp.GetDisplayText();
         }
 
         public List<string> GetHalconToolPathList()
@@ -81,16 +97,26 @@
         [Description("鎷嶇収鐐瑰悕绉�")]
         public string Name { get; set; }
 
-        [Category("璁惧閰嶇疆")]
-        [Description("杩愬姩璁惧")]
-        [TypeConverter(typeof(DeviceSelectorConverter<IMotion>))]
-        public string MotionDevice { get; set; }
+        [Category("鍚敤璁剧疆")]
+        [Description("true锛氬惎鐢� false锛氱鐢�")]
+        public bool IsEnabled { get; set; } = true;
 
-        [Category("杩愬姩鐐逛綅")]
-        [Description("杩愬姩鐐逛綅")]
+        //[Category("璁惧閰嶇疆")]
+        //[Description("杩愬姩璁惧")]
+        //[TypeConverter(typeof(DeviceSelectorConverter<IMotion>))]
+        //public string MotionDevice { get; set; }
+
+        //[Category("杩愬姩鐐逛綅")]
+        //[Description("杩愬姩鐐逛綅")]
+        //[TypeConverter(typeof(ComplexObjectConvert))]
+        //[Editor(typeof(ComplexCollectionEditor<AxisInfo>), typeof(UITypeEditor))]
+        //public List<AxisInfo> Destination { get; set; } = new List<AxisInfo>();
+
+        [Category("杩愬姩鏈烘瀯閰嶇疆")]
+        [Description("杩愬姩鏈烘瀯閰嶇疆")]
         [TypeConverter(typeof(ComplexObjectConvert))]
-        [Editor(typeof(ComplexCollectionEditor<AxisInfo>), typeof(UITypeEditor))]
-        public List<AxisInfo> Destination { get; set; } = new List<AxisInfo>();
+        [Editor(typeof(IOperationConfigByDeviceEditor), typeof(UITypeEditor))]
+        public DeviceOpBind MotionOp { get; set; } = new DeviceOpBind();
 
         [Category("鐩告満鍜屾搷浣滈厤缃�")]
         [Description("鐩告満鍜屾搷浣滈厤缃�")]
@@ -101,6 +127,7 @@
 
     public class KeyUnit : IComplexDisplay, IHalconToolPath
     {
+        #region 閰嶇疆
         private string key = "";
         [Category("閿悕閰嶇疆")]
         [Description("鍗曢敭妫�娴嬮敭鍚�")]
@@ -110,49 +137,136 @@
             get => key;
             set
             {
-                AlignName = key = value;
+                AliasName = key = value;
             }
         }
 
         [Category("閿悕閰嶇疆")]
         [Description("鍒悕")]
-        public string AlignName { get; set; }
+        public string AliasName { get; set; }
+
+        [Category("鍚敤璁剧疆")]
+        [Description("true锛氬惎鐢� false锛氱鐢�")]
+        public bool IsEnabled { get; set; } = true;
 
         [Category("鍥惧儚鏉ユ簮")]
         [Description("鎷嶇収鐐逛綅")]
         [TypeConverter(typeof(SnapshotPointConverter))]
-        public string SnapshotPoint { get; set; }
+        public string SnapshotPointId { get; set; }
 
         [Category("鍥惧儚鏉ユ簮")]
-        [Description("鎷嶇収鐐逛綅鑾峰彇鐨勫浘鐗囩储寮�")]
-        public int ImageIndex { get; set; } = 0;
+        [Description("鎷嶇収鐐逛綅鑾峰彇鐨勫浘鐗囧簭鍙凤紝浠�1寮�濮�")]
+        public int ImageSeq { get; set; } = 1;
 
         [Category("绠楁硶閰嶇疆")]
         [Description("鍗曢敭妫�娴嬮厤缃畻娉曠被鍨�")]
         [TypeConverter(typeof(KeyAlgorithemConverter))]
-        public string KeyAlgorithem { get; set; }
+        public string KeyAlgorithemId { get; set; }
 
         [Category("绠楁硶閰嶇疆")]
         [Description("鍗曢敭妫�娴嬮厤缃粨鏋滅被鍨�")]
         [TypeConverter(typeof(KeyResultConverter))]
-        public string KeyResult { get; set; }
+        public string KeyResultId { get; set; }
+        #endregion
+
+        #region 妫�娴嬬浉鍏冲瓧娈�
+        public Dictionary<string, double> KeyValues = null;
+        //public SnapshotPoint SnapshotPoint = null;
+        public string KeyAlgorithemPath = "";
+        public List<string> KeyResultList = null;
+        #endregion
 
         public string GetDisplayText()
         {
-            return $"{AlignName}";
+            string snapShot = "鏈寚瀹�";
+
+            using (var scope = GlobalVar.Container.BeginLifetimeScope())
+            {
+                var config = scope.Resolve<IProcessConfig>();
+                if (config != null)
+                {
+                    var snapshotPoint = (config as M071Config).SnapshotPointCollection.FirstOrDefault(u => u.Id == SnapshotPointId);
+                    if (snapshotPoint != null)
+                    {
+                        snapShot = snapshotPoint.Name;
+                    }
+                }
+            }
+
+            return $"{AliasName}-{snapShot}-{ImageSeq}";
         }
 
         public List<string> GetHalconToolPathList()
         {
-            return null;
+            using (var scope = GlobalVar.Container.BeginLifetimeScope())
+            {
+                IProcessConfig iConfig = scope.Resolve<IProcessConfig>();
+
+                if (iConfig is M071Config config)
+                {
+                    var algo = config.KeyAlgorithemCollection.FirstOrDefault(u => u.Id == KeyAlgorithemId);
+                    if (algo != null)
+                    {
+                        return new List<string>() { algo.AlgorithemPath };
+                    }
+                }
+            }
+
+            return new List<string>();
         }
     }
 
-    public class MeasurementUint : IComplexDisplay
+    public class ProductionMeasurement : INotifyPropertyChanged, IDisposable
     {
+        public string Barcode;
+        public string PResult;
+
+        public List<MeasurementUnit> Measurements = new List<MeasurementUnit>();
+
+        public List<IShapeElement> ElementList = new List<IShapeElement>();
+
+        public DateTime? StartTime = null;
+
+        public DateTime? EndTime = null;
+
+        public event PropertyChangedEventHandler PropertyChanged;
+
+        public void Dispose()
+        {
+            Measurements?.ForEach(m => m?.Dispose());
+            Measurements = null;
+
+            Barcode = null;
+            GC.Collect();
+        }
+
+        public void InitialMeasurementsPropertyChanged()
+        {
+            Measurements.ForEach(m => m.PropertyChanged += M_PropertyChanged);
+        }
+
+        private object pChangedLock = new object();
+        private void M_PropertyChanged(object sender, PropertyChangedEventArgs e)
+        {
+            lock (pChangedLock)
+            {
+                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Measurements"));
+            }
+        }
+    }
+
+    public class MeasurementUnit : IComplexDisplay, INotifyPropertyChanged, IDisposable
+    {
+        public string Id = Guid.NewGuid().ToString();
+
+        #region 閰嶇疆
         [Category("鍚嶇О")]
         [Description("鍚嶇О")]
         public string Name { get; set; } = "";
+
+        [Category("鍚敤璁剧疆")]
+        [Description("true锛氬惎鐢� false锛氱鐢�")]
+        public bool IsEnabled { get; set; } = true;
 
         [Category("绠楁硶閰嶇疆")]
         [Description("妫�娴嬪拰鏍囧噯绫诲瀷")]
@@ -164,24 +278,51 @@
         [TypeConverter(typeof(ComplexObjectConvert))]
         [Editor(typeof(ComplexCollectionEditor<KeyUnitBind>), typeof(UITypeEditor))]
         public List<KeyUnitBind> KeyUnitCollection { get; set; } = new List<KeyUnitBind>();
+        #endregion
 
+        #region 鏄剧ず
         [Browsable(false)]
-        public RectangleF DisplayLocation { get; set; } = new RectangleF();
+        public Rectangle DisplayLocation { get; set; } = new Rectangle();
+        #endregion
+
+        #region 妫�娴嬬粨鏋�
+        [Browsable(false)]
+        public MeasureType Spec { get; set; } = null;
+        #endregion
+
+        public bool IsUpdated = false;
+        public event PropertyChangedEventHandler PropertyChanged;
+
+        public void Dispose()
+        {
+            KeyUnitCollection?.ForEach(k => k?.Dispose());
+            KeyUnitCollection = null;
+        }
 
         public string GetDisplayText()
         {
             if (string.IsNullOrWhiteSpace(Name))
             {
-                return string.Join("-", KeyUnitCollection.Select(u => u.GetDisplayText())) + "-" + MeasureType.ToString();
+                return (IsEnabled ? "" : "绂佺敤 ") + string.Join("-", KeyUnitCollection.Select(u => u.GetDisplayText())) + "-" + MeasureType?.ToString();
             }
             else
             {
-                return Name;
+                return (IsEnabled ? "" : "绂佺敤 ") + Name;
             }
+        }
+
+        public void InitialKeyUnitMeasureChanged()
+        {
+            KeyUnitCollection.ForEach(k => k.PropertyChanged += K_PropertyChanged);
+        }
+
+        private void K_PropertyChanged(object sender, PropertyChangedEventArgs e)
+        {
+            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("KeyUnitCollection"));
         }
     }
 
-    public class KeyUnitBind : IComplexDisplay
+    public class KeyUnitBind : IComplexDisplay, INotifyPropertyChanged, IDisposable
     {
         [Category("鍗曢敭閰嶇疆")]
         [Description("鍗曢敭閰嶇疆")]
@@ -191,15 +332,101 @@
         [Category("鍗曢敭閰嶇疆")]
         [Description("鍗曢敭缁撴灉閰嶇疆")]
         [TypeConverter(typeof(KeyUnitResultConverter))]
-        public string KeyResult { get; set; } = "";
+        public string KeyResultId { 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?>();
+
+        [Browsable(false)]
+        public Dictionary<string, double?> MeasureValueDict { get; set; } = new Dictionary<string, double?>();
+
+        public event PropertyChangedEventHandler PropertyChanged;
+
+        //public void InitialMeasureValueDictPropertyChanged()
+        //{
+        //    MeasureValueDict.PropertyChanged += MeasureValueDict_PropertyChanged;
+        //}
+
+        //private void MeasureValueDict_PropertyChanged(object sender, PropertyChangedEventArgs e)
+        //{
+        //    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("MeasureValueDict"));
+        //}
+
+        public bool? IsDone = null;
 
         public string GetDisplayText()
         {
             string msg = Key;
 
-            msg += string.IsNullOrWhiteSpace(KeyResult) ? "" : $"-{KeyResult}";
+            msg += string.IsNullOrWhiteSpace(KeyResultId) ? "" : $"-{KeyResultId}";
 
             return msg;
         }
+
+        public void Dispose()
+        {
+            SpinWait wait = new SpinWait();
+            while (ImageSaveStatus != 0)
+            {
+                wait.SpinOnce();
+            }
+
+            KeyImages?.ForEach(i =>
+            {
+                i?.Dispose();
+                i = null;
+            });
+            KeyImages = null;
+            MeasureValueDict = null;
+        }
+
+        object valueDictLock = new object();
+        public void FillKeyValues(Dictionary<string, double> valuePairs)
+        {
+            lock (valueDictLock)
+            {
+                if (valuePairs == null || valuePairs.Count == 0)
+                {
+                    IsDone = false;
+                }
+
+                foreach (KeyValuePair<string, double> pair in valuePairs)
+                {
+                    if (MeasureValueDict.ContainsKey(pair.Key))
+                    {
+                        MeasureValueDict[pair.Key] = pair.Value;
+                    }
+                }
+
+                if (MeasureValueDict.Values.All(u => u != null))
+                {
+                    IsDone = true;
+                }
+
+                if (IsDone != null)
+                {
+                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsDone"));
+                }
+            }
+        }
     }
+
+    //public class NoticedDictionary<T1, T2> : Dictionary<T1, T2>, INotifyPropertyChanged
+    //{
+    //    public event PropertyChangedEventHandler PropertyChanged;
+
+    //    public new T2 this[T1 index]
+    //    {
+    //        get => base[index];
+    //        set
+    //        {
+    //            base[index] = value;
+    //            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Index"));
+    //        }
+    //    }
+    //}
 }

--
Gitblit v1.8.0