From 699bbf402e9d6333d4dcb0c75e6d32e4386fd6dc Mon Sep 17 00:00:00 2001 From: xcd <834800634@qq.com> Date: 星期日, 28 六月 2020 18:59:26 +0800 Subject: [PATCH] 主体流程完成 --- src/Bro.Common.Device/Interface/IMotion.cs | 8 src/Bro.Device.Gocator/Bro.Device.Gocator.csproj | 10 src/Bro.M071.Process/packages.config | 1 src/Bro.Process/ProcessControl.cs | 29 + src/Bro.Device.Gocator/GocatorConfig.cs | 12 src/Bro.M071.Process/M071Converters.cs | 16 src/Bro.M071.Process/M071Process.cs | 363 +++++++++++++++++++++++++++ src/Bro.UI.Config/Ctrls/CtrlMethodInvoke.Designer.cs | 9 src/Bro.Device.Gocator/GocatorDriver.cs | 37 ++ src/Bro.M071.Process/Bro.M071.Process.csproj | 11 src/Bro.M071.Process/M071Config.cs | 6 src/Bro.UI.Model.Winform/Helper/MenuFormFactory.cs | 3 src/Bro.Common.Model/Model/Spec.cs | 14 src/Bro.M071.Process/M071Models.cs | 205 ++++++++++++++ src/Bro.Common.Device/DeviceBase/PLCBase.cs | 5 15 files changed, 678 insertions(+), 51 deletions(-) diff --git a/src/Bro.Common.Device/DeviceBase/PLCBase.cs b/src/Bro.Common.Device/DeviceBase/PLCBase.cs index 0273e94..c6f8d6f 100644 --- a/src/Bro.Common.Device/DeviceBase/PLCBase.cs +++ b/src/Bro.Common.Device/DeviceBase/PLCBase.cs @@ -280,6 +280,11 @@ return locations; } + + public bool MoveToPoint(IOperationConfig opConfig) + { + throw new NotImplementedException(); + } #endregion } diff --git a/src/Bro.Common.Device/Interface/IMotion.cs b/src/Bro.Common.Device/Interface/IMotion.cs index e6e55f3..233ed8e 100644 --- a/src/Bro.Common.Device/Interface/IMotion.cs +++ b/src/Bro.Common.Device/Interface/IMotion.cs @@ -1,4 +1,5 @@ 锘縰sing Bro.Common.Helper; +using Bro.Common.Interface; using System; using System.Collections.Generic; using System.Linq; @@ -13,6 +14,13 @@ public interface IMotion { List<AxisInfo> GetCurrentAxisInfo(params string[] axisName); + + /// <summary> + /// 杩愬姩鏈烘瀯鐐瑰埌鐐圭Щ鍔� + /// </summary> + /// <param name="opConfig">杩愬姩閰嶇疆</param> + /// <returns>true锛氭甯哥粨鏉� false锛氬紓甯哥粨鏉�</returns> + bool MoveToPoint(IOperationConfig opConfig); } public class AxisInfo : IComplexDisplay diff --git a/src/Bro.Common.Model/Model/Spec.cs b/src/Bro.Common.Model/Model/Spec.cs index 9eaef91..d3898db 100644 --- a/src/Bro.Common.Model/Model/Spec.cs +++ b/src/Bro.Common.Model/Model/Spec.cs @@ -15,7 +15,7 @@ { [Category("閫氱敤閰嶇疆")] [Description("鏍囧噯浠g爜")] - public string Code { get; set; } + public virtual string Code { get; set; } [Category("閫氱敤閰嶇疆")] [Description("鍚敤鐘舵�侊紝true锛氬惎鐢紱false锛氱鐢�")] @@ -25,22 +25,22 @@ [Category("鏍囧噯閰嶇疆")] [Description("鏍囧噯鍊�")] [DisplayName("鏍囧噯鍊�")] - public float StandardValue { get; set; } + public double StandardValue { get; set; } [Category("鏍囧噯閰嶇疆")] [Description("姝e叕宸�")] [DisplayName("姝e叕宸�")] - public float Tolrenance_Positive { get; set; } + public double Tolrenance_Positive { get; set; } [Category("鏍囧噯閰嶇疆")] [Description("璐熷叕宸�")] [DisplayName("璐熷叕宸�")] - public float Tolrenance_Negative { get; set; } + public double Tolrenance_Negative { get; set; } - protected float? actualValue = null; + protected double? actualValue = null; [Browsable(false)] [JsonIgnore] - public virtual float? ActualValue + public virtual double? ActualValue { get { @@ -67,7 +67,7 @@ [Browsable(false)] [JsonIgnore] - public bool MeasureResult { get; set; } = false; + public bool? MeasureResult { get; set; } = null; public virtual string GetCSVHead() { diff --git a/src/Bro.Device.Gocator/Bro.Device.Gocator.csproj b/src/Bro.Device.Gocator/Bro.Device.Gocator.csproj index 2bdad3e..c3a0b6a 100644 --- a/src/Bro.Device.Gocator/Bro.Device.Gocator.csproj +++ b/src/Bro.Device.Gocator/Bro.Device.Gocator.csproj @@ -45,5 +45,15 @@ <Compile Include="GocatorDriver.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Bro.Common.Device\Bro.Common.Device.csproj"> + <Project>{987308DD-8BAA-463A-94E2-77D62E01A5BF}</Project> + <Name>Bro.Common.Device</Name> + </ProjectReference> + <ProjectReference Include="..\Bro.Common.Model\Bro.Common.Model.csproj"> + <Project>{1a3cbfe7-3f78-42c3-95c5-10360450dbea}</Project> + <Name>Bro.Common.Model</Name> + </ProjectReference> + </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> </Project> \ No newline at end of file diff --git a/src/Bro.Device.Gocator/GocatorConfig.cs b/src/Bro.Device.Gocator/GocatorConfig.cs index 462b025..6dba56f 100644 --- a/src/Bro.Device.Gocator/GocatorConfig.cs +++ b/src/Bro.Device.Gocator/GocatorConfig.cs @@ -1,4 +1,6 @@ -锘縰sing System; +锘縰sing Bro.Common.Base; +using Bro.Common.Helper; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +8,13 @@ namespace Bro.Device.Gocator { - class GocatorConfig + [Device("Gocator", "Gocator婵�鍏夋壂鎻忎华", EnumHelper.DeviceAttributeType.InitialConfig)] + public class GocatorInitialConfig : CameraInitialConfigBase + { + } + + [Device("Gocator", "Gocator婵�鍏夋壂鎻忎华", EnumHelper.DeviceAttributeType.OperationConfig)] + public class GocatorOperationConfig : CameraOprerationConfigBase { } } diff --git a/src/Bro.Device.Gocator/GocatorDriver.cs b/src/Bro.Device.Gocator/GocatorDriver.cs index 678c62c..0290bd1 100644 --- a/src/Bro.Device.Gocator/GocatorDriver.cs +++ b/src/Bro.Device.Gocator/GocatorDriver.cs @@ -1,4 +1,7 @@ -锘縰sing System; +锘縰sing Bro.Common.Base; +using Bro.Common.Helper; +using Bro.Common.Interface; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +9,37 @@ namespace Bro.Device.Gocator { - class GocatorDriver + [Device("Gocator", "Gocator婵�鍏夋壂鎻忎华", EnumHelper.DeviceAttributeType.Device)] + public class GocatorDriver : CameraBase { + public override IOperationConfig GetOperationConfigFromDevice() + { + throw new NotImplementedException(); + } + + public override void Snapshot() + { + throw new NotImplementedException(); + } + + public override void UploadOperationConfig(IOperationConfig config) + { + throw new NotImplementedException(); + } + + protected override void Init() + { + throw new NotImplementedException(); + } + + protected override void Pause() + { + throw new NotImplementedException(); + } + + protected override void Resume() + { + throw new NotImplementedException(); + } } } diff --git a/src/Bro.M071.Process/Bro.M071.Process.csproj b/src/Bro.M071.Process/Bro.M071.Process.csproj index 168848e..e276212 100644 --- a/src/Bro.M071.Process/Bro.M071.Process.csproj +++ b/src/Bro.M071.Process/Bro.M071.Process.csproj @@ -39,6 +39,17 @@ <Reference Include="Autofac, Version=4.9.4.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL"> <HintPath>..\..\packages\Autofac.4.9.4\lib\net45\Autofac.dll</HintPath> </Reference> + <Reference Include="halcondotnet, Version=12.0.0.0, Culture=neutral, PublicKeyToken=4973bed59ddbf2b8, processorArchitecture=MSIL"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\..\libs\halcon12\halcondotnet.dll</HintPath> + </Reference> + <Reference Include="hdevenginedotnet, Version=12.0.0.0, Culture=neutral, PublicKeyToken=4973bed59ddbf2b8, processorArchitecture=MSIL"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\..\libs\halcon12\hdevenginedotnet.dll</HintPath> + </Reference> + <Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> + <HintPath>..\..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath> + </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> diff --git a/src/Bro.M071.Process/M071Config.cs b/src/Bro.M071.Process/M071Config.cs index 1c1d42d..2cdacf0 100644 --- a/src/Bro.M071.Process/M071Config.cs +++ b/src/Bro.M071.Process/M071Config.cs @@ -22,10 +22,10 @@ public List<string> KeyNameCollection { get; set; } = new List<string>(); [Category("鍏叡瀛楀吀閰嶇疆")] - [Description("妫�娴嬫爣鍑嗛泦鍚�")] + [Description("妫�娴嬬被鍨嬪拰鏍囧噯闆嗗悎")] [TypeConverter(typeof(CollectionCountConvert))] - [Editor(typeof(ComplexCollectionEditor<Spec>), typeof(UITypeEditor))] - public List<Spec> MeasureSpecCollection { get; set; } = new List<Spec>(); + [Editor(typeof(ComplexCollectionEditor<MeasureType>), typeof(UITypeEditor))] + public List<MeasureType> MeasureTypeCollection { get; set; } = new List<MeasureType>(); [Category("鍏叡瀛楀吀閰嶇疆")] [Description("鍗曢敭绠楁硶閰嶇疆闆嗗悎")] diff --git a/src/Bro.M071.Process/M071Converters.cs b/src/Bro.M071.Process/M071Converters.cs index da87fd1..fb8274b 100644 --- a/src/Bro.M071.Process/M071Converters.cs +++ b/src/Bro.M071.Process/M071Converters.cs @@ -49,10 +49,10 @@ if (iConfig is M071Config config) { - config.SnapshotPointCollection.ForEach(s => - { - table[s.Id] = s.Name; - }); + config.SnapshotPointCollection.Where(u => u.IsEnabled).ToList().ForEach(s => + { + table[s.Id] = s.Name; + }); } } @@ -141,7 +141,7 @@ if (iConfig is M071Config config) { - var resultIds = config.KeyUnitCollection.Where(u => u.Key == (context.Instance as KeyUnitBind).Key).Select(u => u.KeyResult).ToList(); + var resultIds = config.KeyUnitCollection.Where(u => u.IsEnabled && u.Key == (context.Instance as KeyUnitBind).Key).Select(u => u.KeyResultId).ToList(); var result = config.KeyResultCollection.Where(u => resultIds.Contains(u.Id)).SelectMany(u => u.Results).ToList(); @@ -149,10 +149,10 @@ { result.ForEach(r => { - table[r] = r; + table.Add(r, r); }); - table[""] = ""; + table.Add("All", "All"); } } } @@ -181,7 +181,7 @@ if (iConfig is M071Config config) { - return new StandardValuesCollection(config.MeasureSpecCollection.Select(u => u.Code).ToList()); + return new StandardValuesCollection(config.MeasureTypeCollection.Select(u => u.Code).ToList()); } } diff --git a/src/Bro.M071.Process/M071Models.cs b/src/Bro.M071.Process/M071Models.cs index 16af13a..be596b7 100644 --- a/src/Bro.M071.Process/M071Models.cs +++ b/src/Bro.M071.Process/M071Models.cs @@ -3,6 +3,7 @@ using Bro.Common.Helper; using Bro.Common.Interface; using Bro.Common.Model; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; @@ -34,6 +35,19 @@ } } + 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)] @@ -59,7 +73,7 @@ { public string GetDisplayText() { - return Name + " " + string.Join(" ", Destination.Select(u => u.GetDisplayText())) + " " + CameraOp.GetDisplayText(); + return (IsEnabled ? "" : "绂佺敤 ") + Name + " " + string.Join(" ", Destination.Select(u => u.GetDisplayText())) + " " + CameraOp.GetDisplayText(); } public List<string> GetHalconToolPathList() @@ -81,6 +95,10 @@ [Description("鎷嶇収鐐瑰悕绉�")] public string Name { get; set; } + [Category("鍚敤璁剧疆")] + [Description("true锛氬惎鐢� false锛氱鐢�")] + public bool IsEnabled { get; set; } = true; + [Category("璁惧閰嶇疆")] [Description("杩愬姩璁惧")] [TypeConverter(typeof(DeviceSelectorConverter<IMotion>))] @@ -101,6 +119,7 @@ public class KeyUnit : IComplexDisplay, IHalconToolPath { + #region 閰嶇疆 private string key = ""; [Category("閿悕閰嶇疆")] [Description("鍗曢敭妫�娴嬮敭鍚�")] @@ -118,24 +137,36 @@ [Description("鍒悕")] public string AlignName { 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() { @@ -144,15 +175,68 @@ 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 { get; set; } + + public List<MeasurementUint> Measurements { get; set; } = new List<MeasurementUint>(); + + public event PropertyChangedEventHandler PropertyChanged; + + public void Dispose() + { + Barcode = null; + + Measurements?.ForEach(m => m?.Dispose()); + Measurements = 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("妫�娴嬪拰鏍囧噯绫诲瀷")] @@ -164,24 +248,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("鍗曢敭閰嶇疆")] @@ -193,6 +304,26 @@ [TypeConverter(typeof(KeyUnitResultConverter))] public string KeyResult { get; set; } = ""; + //[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; @@ -201,5 +332,55 @@ return msg; } + + public void Dispose() + { + 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")); + // } + // } + //} } diff --git a/src/Bro.M071.Process/M071Process.cs b/src/Bro.M071.Process/M071Process.cs index 33827af..13fde83 100644 --- a/src/Bro.M071.Process/M071Process.cs +++ b/src/Bro.M071.Process/M071Process.cs @@ -1,8 +1,13 @@ -锘縰sing Bro.Common.Helper; +锘縰sing Bro.Common.Base; +using Bro.Common.Helper; +using Bro.Common.Interface; using Bro.Common.Model; using Bro.Process; +using HalconDotNet; using System; using System.Collections.Generic; +using System.ComponentModel; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -23,5 +28,361 @@ get => IConfig as M071Config; } #endregion + + #region 浜嬩欢 + public event Action OnMeasureStart; + public event Action<string> OnBarcodeChanged; + public event Action<IShapeElement> OnElementUpdated; + #endregion + + public override void Open() + { + InitialSetting(); + + base.Open(); + } + + private void InitialSetting() + { + Config.SnapshotPointCollection.ForEach(u => + { + u.GetHalconToolPathList().ForEach(path => + { + LoadHalconTool(path, u.Id); + }); + }); + + Config.KeyUnitCollection.ForEach(u => + { + if (!u.IsEnabled) + return; + + var snapshotPoint = Config.SnapshotPointCollection.FirstOrDefault(s => s.Id == u.SnapshotPointId && s.IsEnabled); + if (snapshotPoint == null) + throw new ProcessException($"{u.AlignName}鏈缃彲鐢ㄦ媿鐓х偣浣�"); + + if (u.ImageSeq < 1) + throw new ProcessException($"{u.AlignName}鍥剧墖搴忓彿灏忎簬1"); + + var algo = Config.KeyAlgorithemCollection.FirstOrDefault(a => a.Id == u.KeyAlgorithemId); + if (algo == null) + throw new ProcessException($"{u.AlignName}鏈缃娴嬬畻娉�"); + + u.KeyAlgorithemPath = algo.AlgorithemPath; + LoadHalconTool(u.KeyAlgorithemPath, u.AlignName); + + var resultSet = Config.KeyResultCollection.FirstOrDefault(r => r.Id == u.KeyResultId); + if (resultSet == null) + throw new ProcessException($"{u.AlignName}鏈缃娴嬬粨鏋滈厤缃�"); + + u.KeyResultList = new List<string>(resultSet.Results); + }); + + Config.MeasurementUnitCollection.ForEach(u => + { + if (!u.IsEnabled) + return; + + var spec = Config.MeasureTypeCollection.FirstOrDefault(s => s.Code == u.MeasureType); + if (spec == null) + throw new ProcessException($"{u.GetDisplayText()}鏈缃娴嬬被鍨�"); + + u.Spec = new MeasureType(); + u.Spec.DataFrom(spec); + + LoadHalconTool(u.Spec.AlgorithemPath, u.Id); + + u.KeyUnitCollection.ForEach(b => + { + var keyRespone = Config.KeyUnitCollection.Where(ku => ku.IsEnabled && ku.Key == b.Key).ToList(); + if (keyRespone.Count == 0) + throw new ProcessException($"{u.GetDisplayText()}瀵瑰簲鐨勫崟閿笉瀛樺湪鎴栦笉鍙敤"); + + if (b.KeyResult == "All") + { + keyRespone.SelectMany(kr => kr.KeyResultList).ToList().ForEach(r => + { + b.MeasureValueDict[r] = null; + }); + } + else + { + b.MeasureValueDict[b.KeyResult] = null; + } + }); + }); + } + + #region InitialHalconTool + //protected override void InitialHalconTool() + //{ + // base.InitialHalconTool(); + + // Config.SnapshotPointCollection.ForEach(u => + // { + // u.GetHalconToolPathList().ForEach(path => + // { + // if (!string.IsNullOrWhiteSpace(path)) + // { + // string directoryPath = Path.GetDirectoryName(path); + // string fileName = Path.GetFileNameWithoutExtension(path); + + // HDevEngineTool tool = new HDevEngineTool(directoryPath); + // tool.LoadProcedure(fileName); + + // //浣跨敤鈥渱鈥濅綔涓洪棿闅旂 + // _halconToolDict[u.Id + "|" + path] = tool; + // } + // }); + // }); + //} + #endregion + + //Dictionary<string, ProductionMeasurement> MeasureDict = new Dictionary<string, ProductionMeasurement>(); + + private string barCode = ""; + public string BarCode + { + get => barCode; + set + { + if (barCode != value) + { + barCode = value; + OnBarcodeChanged?.Invoke(value); + } + } + } + + [ProcessMethod("", "StartJob", "寮�濮嬫壂鎻�", InvokeType.TestInvoke)] + public ProcessResponse StartJob(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + { + if (string.IsNullOrWhiteSpace(BarCode)) + { + throw new ProcessException("鏈緭鍏ヤ骇鍝佹潯鐮侊紝璇峰嬁寮�濮嬫祴閲�"); + } + + OnMeasureStart?.BeginInvoke(null, null); + + var measurements = Config.MeasurementUnitCollection.Where(u => u.IsEnabled).ToList().DeepSerializeClone(); + measurements.ForEach(m => + { + m.InitialKeyUnitMeasureChanged(); + //m.KeyUnitCollection.ForEach(k => k.InitialMeasureValueDictPropertyChanged()); + }); + + //MeasureDict[BarCode] = new ProductionMeasurement() + //{ + // Barcode = BarCode, + // Measurements = measurements, + //}; + + //MeasureDict[BarCode].InitialMeasurementsPropertyChanged(); + //MeasureDict[BarCode].PropertyChanged += MeasureProduction_PropertyChanged; + + var pMeasure = new ProductionMeasurement() + { + Barcode = BarCode, + Measurements = measurements, + }; + + pMeasure.InitialMeasurementsPropertyChanged(); + pMeasure.PropertyChanged += MeasureProduction_PropertyChanged; + + Config.SnapshotPointCollection.Where(u => u.IsEnabled).ToList().ForEach(s => + { + IDevice device = DeviceCollection.FirstOrDefault(u => u.Id == s.MotionDevice); + if (device == null) + throw new ProcessException($"{s.Name}鎷嶇収鐐逛綅鏈缃繍鍔ㄨ澶�"); + + IMotion motionDevice = device as IMotion; + + if (motionDevice == null) + throw new ProcessException($"{s.Name}鎷嶇収鐐逛綅璁剧疆{device.Name}涓嶆槸杩愬姩璁惧"); + + if (!motionDevice.MoveToPoint(null)) + { + throw new ProcessException("杩愬姩涓", null, ExceptionLevel.Info); + } + + CameraBase camera = DeviceCollection.FirstOrDefault(u => u.Id == s.CameraOp.Device) as CameraBase; + if (camera == null) + return; + + ImageSet imgSet = camera.Snapshot(s.CameraOp.OpConfig); + if (imgSet == null) + return; + + RunImageHandle(camera, s.CameraOp.OpConfig, imgSet, s.Id, s.Name, pMeasure.Measurements); + }); + + BarCode = ""; + return new ProcessResponse(true); + } + + private void MeasureProduction_PropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (sender is ProductionMeasurement pMeasure) + { + //妫�鏌ユ槸鍚﹀叏閮ㄥ畬鎴� + pMeasure.Measurements.ForEach(m => + { + if (m.KeyUnitCollection.All(k => k.IsDone != null)) + { + if (!m.IsUpdated) + { + if (m.KeyUnitCollection.Any(k => k.IsDone == false)) + { + m.Spec.ActualValue = -999; + } + else + { + string toolKey = m.Id + "|" + m.Spec.AlgorithemPath; + if (!_halconToolDict.ContainsKey(toolKey)) + { + LogAsync(DateTime.Now, $"{m.GetDisplayText()}{m.Spec.Code}绠楁硶鏈垵濮嬪寲", ""); + m.Spec.ActualValue = -999; + } + else + { + var array = m.KeyUnitCollection.SelectMany(u => u.MeasureValueDict.Values.ToList().ConvertAll(v => v ?? -999)).ToArray(); + _halconToolDict[toolKey].InputTupleDic["INPUT_Params"] = new HTuple(array); + if (!_halconToolDict[toolKey].RunProcedure(out string error)) + { + LogAsync(DateTime.Now, $"{m.GetDisplayText()}{m.Spec.Code}绠楁硶寮傚父,{error}", ""); + m.Spec.ActualValue = -999; + } + else + { + m.Spec.ActualValue = _halconToolDict[toolKey].GetResultTuple("OUTPUT_Result").D; + LogAsync(DateTime.Now, $"{m.GetDisplayText()}鏁版嵁{m.Spec.ActualValue}锛岀粨鏋渰(m.Spec.MeasureResult == null ? "TBD" : (m.Spec.MeasureResult == true ? "OK" : "NG"))}", ""); + } + } + } + + //杈撳嚭鍥惧舰鍩哄厓鍒扮晫闈� todo + OnElementUpdated?.BeginInvoke(null, null, null); + + m.IsUpdated = true; + } + } + }); + + if (!pMeasure.Measurements.All(m => m.IsUpdated)) + { + return; + } + + //MES杈撳嚭 todo + + //Excel鎶ヨ〃杈撳嚭 todo + + //鏁版嵁搴撲繚瀛� todo + + //MeasureDict.Remove(pMeasure.Barcode); + pMeasure.Dispose(); + } + } + + private async void RunImageHandle(CameraBase camera, IOperationConfig opConfig, ImageSet imgSet, string snapshotId, string snapshotName, List<MeasurementUint> measureList) + { + await Task.Run(() => + { + var keys = Config.KeyUnitCollection.Where(u => u.IsEnabled && u.SnapshotPointId == snapshotId); + 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; + + if (!string.IsNullOrWhiteSpace(toolKey)) + { + toolKey = snapshotId + "|" + toolKey; + if (!_halconToolDict.ContainsKey(toolKey)) + { + LogAsync(DateTime.Now, $"{snapshotName}鏈垵濮嬪寲鍙栧浘绠楁硶閰嶇疆", ""); + keyBindCollection.ForEach(k => k.FillKeyValues(null)); + return; + } + + _halconToolDict[toolKey].InputImageDic["INPUT_Image"] = imgSet.HImage; + if (!_halconToolDict[toolKey].RunProcedure(out string error)) + { + LogAsync(DateTime.Now, $"{snapshotName}鍙栧浘绠楁硶寮傚父锛寋error}", ""); + keyBindCollection.ForEach(k => k.FillKeyValues(null)); + return; + } + + images = _halconToolDict[toolKey].GetResultObject("OUTPUT_Images"); + } + + HOperatorSet.CountObj(images, out HTuple count); + + if (count == 0) + { + LogAsync(DateTime.Now, $"{snapshotName}鍙栧浘绠楁硶鏈緭鍑哄浘鍍�", ""); + keyBindCollection.ForEach(k => k.FillKeyValues(null)); + return; + } + + var excludeKeys = keys.Where(u => u.ImageSeq > count).ToList(); + if (excludeKeys.Count > 0) + { + LogAsync(DateTime.Now, $"{string.Join(" ", excludeKeys.Select(u => u.AlignName))}鏈湪鍥剧墖鑾峰彇搴忓垪涓�", ""); + keyBindCollection.Where(k => excludeKeys.Any(u => u.Key == k.Key)).ToList().ForEach(k => + { + k.FillKeyValues(null); + }); + } + + Parallel.For(1, count.I, (i) => + { + HOperatorSet.SelectObj(images, out HObject image, i); + + keys.Where(u => u.ImageSeq == i).ToList().ForEach(k => + { + Dictionary<string, double> resultDict = null; + + var keyBindList = keyBindCollection.Where(u => u.Key == k.Key).ToList(); + + string keyToolKey = k.AlignName + "|" + k.KeyAlgorithemPath; + if (!_halconToolDict.ContainsKey(keyToolKey)) + { + LogAsync(DateTime.Now, $"{k.AlignName}妫�娴嬬畻娉曟湭鍒濆鍖�", ""); + } + else + { + _halconToolDict[keyToolKey].InputImageDic["INPUT_Image"] = image; + if (!_halconToolDict[keyToolKey].RunProcedure(out string error)) + { + LogAsync(DateTime.Now, $"{k.AlignName}妫�娴嬬畻娉曞紓甯革紝{error}", ""); + } + else + { + var results = _halconToolDict[keyToolKey].GetResultTuple("OUTPUT_Results").HTupleToDouble(); + if (results.Count == 0 || results.Any(u => u < 0)) + { + LogAsync(DateTime.Now, $"{k.AlignName}妫�娴嬬粨鏋滃紓甯�", ""); + } + else + { + resultDict = k.KeyResultList.ToDictionary(u => u, u => + { + int index = k.KeyResultList.IndexOf(u); + return results[index]; + }); + } + } + } + + keyBindList.ForEach(kb => kb.FillKeyValues(resultDict)); + }); + + image.Dispose(); + }); + + if (count.I != 1) + imgSet.HImage.Dispose(); + }); + } } } diff --git a/src/Bro.M071.Process/packages.config b/src/Bro.M071.Process/packages.config index 62a076c..d386c7e 100644 --- a/src/Bro.M071.Process/packages.config +++ b/src/Bro.M071.Process/packages.config @@ -1,4 +1,5 @@ 锘�<?xml version="1.0" encoding="utf-8"?> <packages> <package id="Autofac" version="4.9.4" targetFramework="net452" /> + <package id="Newtonsoft.Json" version="11.0.2" targetFramework="net452" /> </packages> \ No newline at end of file diff --git a/src/Bro.Process/ProcessControl.cs b/src/Bro.Process/ProcessControl.cs index 1c146c5..474d0c0 100644 --- a/src/Bro.Process/ProcessControl.cs +++ b/src/Bro.Process/ProcessControl.cs @@ -398,7 +398,7 @@ #endregion #region Halcon绠楀瓙璁剧疆 - private void InitialHalconTool() + protected virtual void InitialHalconTool() { foreach (HDevEngineTool tool in _halconToolDict.Values) { @@ -413,22 +413,27 @@ { toolPath.GetHalconToolPathList().ForEach(path => { - if (!string.IsNullOrWhiteSpace(path)) - { - string directoryPath = Path.GetDirectoryName(path); - string fileName = Path.GetFileNameWithoutExtension(path); - - HDevEngineTool tool = new HDevEngineTool(directoryPath); - tool.LoadProcedure(fileName); - - //浣跨敤鈥渱鈥濅綔涓洪棿闅旂 - _halconToolDict[monitorSet.Id + "|" + path] = tool; - } + LoadHalconTool(path, monitorSet.Id); }); } }); } + protected void LoadHalconTool(string algorithemPath, string prefix) + { + if (!string.IsNullOrWhiteSpace(algorithemPath)) + { + string directoryPath = Path.GetDirectoryName(algorithemPath); + string fileName = Path.GetFileNameWithoutExtension(algorithemPath); + + HDevEngineTool tool = new HDevEngineTool(directoryPath); + tool.LoadProcedure(fileName); + + //浣跨敤鈥渱鈥濅綔涓洪棿闅旂 + _halconToolDict[prefix + "|" + algorithemPath] = tool; + } + } + /// <summary> /// 鑾峰彇棰勫厛杞藉叆鐨凥alcon绠楁硶 /// </summary> diff --git a/src/Bro.UI.Config/Ctrls/CtrlMethodInvoke.Designer.cs b/src/Bro.UI.Config/Ctrls/CtrlMethodInvoke.Designer.cs index b0fea8d..8c66838 100644 --- a/src/Bro.UI.Config/Ctrls/CtrlMethodInvoke.Designer.cs +++ b/src/Bro.UI.Config/Ctrls/CtrlMethodInvoke.Designer.cs @@ -82,9 +82,9 @@ | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.tableLayoutPanel1.SetColumnSpan(this.propOpConfig, 4); - this.propOpConfig.Location = new System.Drawing.Point(23, 65); + this.propOpConfig.Location = new System.Drawing.Point(23, 64); this.propOpConfig.Name = "propOpConfig"; - this.propOpConfig.Size = new System.Drawing.Size(553, 359); + this.propOpConfig.Size = new System.Drawing.Size(553, 360); this.propOpConfig.TabIndex = 15; this.propOpConfig.ToolbarVisible = false; // @@ -111,7 +111,8 @@ // btnInvoke // this.btnInvoke.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.btnInvoke.Location = new System.Drawing.Point(501, 36); + this.btnInvoke.Location = new System.Drawing.Point(501, 35); + this.btnInvoke.Margin = new System.Windows.Forms.Padding(3, 2, 3, 3); this.btnInvoke.Name = "btnInvoke"; this.btnInvoke.Size = new System.Drawing.Size(75, 23); this.btnInvoke.TabIndex = 19; @@ -129,13 +130,13 @@ this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F)); this.tableLayoutPanel1.Controls.Add(this.propOpConfig, 1, 3); - this.tableLayoutPanel1.Controls.Add(this.btnInvoke, 4, 2); this.tableLayoutPanel1.Controls.Add(this.cboTestMethod, 3, 2); this.tableLayoutPanel1.Controls.Add(this.cboTestExecuteDevice, 2, 2); this.tableLayoutPanel1.Controls.Add(this.cboTestSourceDevices, 1, 2); this.tableLayoutPanel1.Controls.Add(this.label8, 2, 1); this.tableLayoutPanel1.Controls.Add(this.label5, 1, 1); this.tableLayoutPanel1.Controls.Add(this.label2, 3, 1); + this.tableLayoutPanel1.Controls.Add(this.btnInvoke, 4, 2); this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(20); diff --git a/src/Bro.UI.Model.Winform/Helper/MenuFormFactory.cs b/src/Bro.UI.Model.Winform/Helper/MenuFormFactory.cs index e13be11..8c67c34 100644 --- a/src/Bro.UI.Model.Winform/Helper/MenuFormFactory.cs +++ b/src/Bro.UI.Model.Winform/Helper/MenuFormFactory.cs @@ -21,6 +21,9 @@ var assm = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).GetFiles(DLLPATTERN).Select(u => Assembly.LoadFrom(u.FullName)).ToList(); assm.AddRange(AppDomain.CurrentDomain.GetAssemblies()); assm = assm.Distinct().ToList(); + + //var types = assm.SelectMany(a => a.GetTypes()); + menuFrmTypeDict = assm.SelectMany(a => a.GetTypes()) .Where(t => { -- Gitblit v1.8.0