using Autofac; using Bro.Common.Base; 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; using System.Drawing; using System.Drawing.Design; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Bro.M071.Process { public class KeyAlgorithem : IComplexDisplay { [Browsable(false)] public string Id { get; set; } = Guid.NewGuid().ToString(); [Category("单键算法配置")] [Description("算法名称")] public string Name { get; set; } [Category("单键算法配置")] [Description("算法路径")] [Editor(typeof(FileDialogEditor), typeof(UITypeEditor))] public string AlgorithemPath { get; set; } public string GetDisplayText() { 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 { [Browsable(false)] public string Id { get; set; } = Guid.NewGuid().ToString(); [Category("单键结果配置")] [Description("结果配置名称")] public string Name { get; set; } [Category("单键结果配置")] [Description("结果配置集合")] [TypeConverter(typeof(SimpleCollectionConvert))] [Editor(typeof(SimpleCollectionEditor), typeof(UITypeEditor))] public List Results { get; set; } = new List(); public string GetDisplayText() { return $"{Name} -- {(string.Join(" ", Results))}"; } } public class SnapshotPoint : IComplexDisplay, IHalconToolPath { public string GetDisplayText() { return (IsEnabled ? "" : "禁用 ") + Name + " " + MotionOp.GetDisplayText() + " " + CameraOp.GetDisplayText(); } public List GetHalconToolPathList() { if (CameraOp.OpConfig is IHalconToolPath path) { return path.GetHalconToolPathList(); } else { return new List(); } } [Browsable(false)] public string Id { get; set; } = Guid.NewGuid().ToString(); [Category("拍照点名称")] [Description("拍照点名称")] public string Name { get; set; } [Category("启用设置")] [Description("true:启用 false:禁用")] public bool IsEnabled { get; set; } = true; //[Category("设备配置")] //[Description("运动设备")] //[TypeConverter(typeof(DeviceSelectorConverter))] //public string MotionDevice { get; set; } //[Category("运动点位")] //[Description("运动点位")] //[TypeConverter(typeof(ComplexObjectConvert))] //[Editor(typeof(ComplexCollectionEditor), typeof(UITypeEditor))] //public List Destination { get; set; } = new List(); [Category("运动机构配置")] [Description("运动机构配置")] [TypeConverter(typeof(ComplexObjectConvert))] [Editor(typeof(IOperationConfigByDeviceEditor), typeof(UITypeEditor))] public DeviceOpBind MotionOp { get; set; } = new DeviceOpBind(); [Category("相机和操作配置")] [Description("相机和操作配置")] [TypeConverter(typeof(ComplexObjectConvert))] [Editor(typeof(IOperationConfigByDeviceEditor), typeof(UITypeEditor))] public DeviceOpBind CameraOp { get; set; } = new DeviceOpBind(); } public class KeyUnit : IComplexDisplay, IHalconToolPath { #region 配置 private string key = ""; [Category("键名配置")] [Description("单键检测键名")] [TypeConverter(typeof(KeyNameDictConverter))] public string Key { get => key; set { AlignName = key = value; } } [Category("键名配置")] [Description("别名")] public string AlignName { get; set; } [Category("启用设置")] [Description("true:启用 false:禁用")] public bool IsEnabled { get; set; } = true; [Category("图像来源")] [Description("拍照点位")] [TypeConverter(typeof(SnapshotPointConverter))] public string SnapshotPointId { get; set; } [Category("图像来源")] [Description("拍照点位获取的图片序号,从1开始")] public int ImageSeq { get; set; } = 1; [Category("算法配置")] [Description("单键检测配置算法类型")] [TypeConverter(typeof(KeyAlgorithemConverter))] public string KeyAlgorithemId { get; set; } [Category("算法配置")] [Description("单键检测配置结果类型")] [TypeConverter(typeof(KeyResultConverter))] public string KeyResultId { get; set; } #endregion #region 检测相关字段 public Dictionary KeyValues = null; //public SnapshotPoint SnapshotPoint = null; public string KeyAlgorithemPath = ""; public List KeyResultList = null; #endregion public string GetDisplayText() { return $"{AlignName}"; } public List GetHalconToolPathList() { using (var scope = GlobalVar.Container.BeginLifetimeScope()) { IProcessConfig iConfig = scope.Resolve(); if (iConfig is M071Config config) { var algo = config.KeyAlgorithemCollection.FirstOrDefault(u => u.Id == KeyAlgorithemId); if (algo != null) { return new List() { algo.AlgorithemPath }; } } } return new List(); } } public class ProductionMeasurement : INotifyPropertyChanged, IDisposable { public string Barcode; public List Measurements = new List(); public List ElementList = new List(); 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 MeasurementUint : 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("检测和标准类型")] [TypeConverter(typeof(MeasureTypeConverter))] public string MeasureType { get; set; } [Category("算法配置")] [Description("涉及单键集合")] [TypeConverter(typeof(ComplexObjectConvert))] [Editor(typeof(ComplexCollectionEditor), typeof(UITypeEditor))] public List KeyUnitCollection { get; set; } = new List(); #endregion #region 显示 [Browsable(false)] 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 (IsEnabled ? "" : "禁用 ") + string.Join("-", KeyUnitCollection.Select(u => u.GetDisplayText())) + "-" + MeasureType?.ToString(); } else { 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, INotifyPropertyChanged, IDisposable { [Category("单键配置")] [Description("单键配置")] [TypeConverter(typeof(KeyNameDictConverter))] public string Key { get; set; } [Category("单键配置")] [Description("单键结果配置")] [TypeConverter(typeof(KeyUnitResultConverter))] public string KeyResultId { get; set; } = ""; public List KeyImages = new List(); public volatile int ImageSaveStatus = 0; //[Browsable(false)] //public NoticedDictionary MeasureValueDict { get; set; } = new NoticedDictionary(); [Browsable(false)] public Dictionary MeasureValueDict { get; set; } = new Dictionary(); 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(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 valuePairs) { lock (valueDictLock) { if (valuePairs == null || valuePairs.Count == 0) { IsDone = false; } foreach (KeyValuePair 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 : Dictionary, INotifyPropertyChanged //{ // public event PropertyChangedEventHandler PropertyChanged; // public new T2 this[T1 index] // { // get => base[index]; // set // { // base[index] = value; // PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Index")); // } // } //} }