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<string>))]
|
[Editor(typeof(SimpleCollectionEditor<string>), typeof(UITypeEditor))]
|
public List<string> Results { get; set; } = new List<string>();
|
|
public string GetDisplayText()
|
{
|
return $"{Name} -- {(string.Join(" ", Results))}";
|
}
|
}
|
|
public class SnapshotPoint : IComplexDisplay, IHalconToolPath
|
{
|
public string GetDisplayText()
|
{
|
return (IsEnabled ? "" : "禁用 ") + Name + " " + string.Join(" ", Destination.Select(u => u.GetDisplayText())) + " " + CameraOp.GetDisplayText();
|
}
|
|
public List<string> GetHalconToolPathList()
|
{
|
if (CameraOp.OpConfig is IHalconToolPath path)
|
{
|
return path.GetHalconToolPathList();
|
}
|
else
|
{
|
return new List<string>();
|
}
|
}
|
|
[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<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(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<string, double> KeyValues = null;
|
//public SnapshotPoint SnapshotPoint = null;
|
public string KeyAlgorithemPath = "";
|
public List<string> KeyResultList = null;
|
#endregion
|
|
public string GetDisplayText()
|
{
|
return $"{AlignName}";
|
}
|
|
public List<string> GetHalconToolPathList()
|
{
|
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 ProductionMeasurement : INotifyPropertyChanged, IDisposable
|
{
|
public string Barcode;
|
|
public List<MeasurementUint> Measurements = new List<MeasurementUint>();
|
|
public List<IShapeElement> ElementList = new List<IShapeElement>();
|
|
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<KeyUnitBind>), typeof(UITypeEditor))]
|
public List<KeyUnitBind> KeyUnitCollection { get; set; } = new List<KeyUnitBind>();
|
#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 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?>();
|
|
[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}";
|
|
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"));
|
// }
|
// }
|
//}
|
}
|