C3204
2025-12-23 b06eaa9b8036b2d54022d211df6871a341b69d3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using LB_VisionProcesses.Alogrithms;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
 
namespace LB_VisionProcesses
{
    public abstract class IProcess : IDisposable, ICloneable
    {
        /// <summary>
        /// 流程集合(指向PluginManager的已加载工具)
        /// </summary>
        public static Dictionary<string, string> dicProcesses
        {
            get
            {
                // 延迟初始化,确保PluginManager已加载
                if (!_initialized)
                {
                    InitializePluginManager();
                }
                return PluginManager.LoadedProcesses;
            }
        }
 
        private static bool _initialized = false;
 
        static IProcess()
        {
            // 延迟初始化PluginManager
            InitializePluginManager();
 
            // 注册插件事件
            PluginManager.PluginLoaded += OnPluginLoaded;
            PluginManager.PluginUnloaded += OnPluginUnloaded;
 
            // 加载所有插件
            PluginManager.LoadAllPlugins();
 
        }
 
        /// <summary>
        /// 初始化插件管理器
        /// </summary>
        private static void InitializePluginManager()
        {
            if (!_initialized)
            {
                // 注册插件事件
                PluginManager.PluginLoaded += OnPluginLoaded;
                PluginManager.PluginUnloaded += OnPluginUnloaded;
 
                // 加载所有插件
                PluginManager.LoadAllPlugins();
 
                _initialized = true;
            }
        }
 
        private static void OnPluginLoaded(object sender, PluginEventArgs e)
        {
            Debug.WriteLine($"插件加载成功: {e.PluginInfo.Name}, 包含 {e.PluginInfo.Processes.Count} 个工具");
        }
 
        private static void OnPluginUnloaded(object sender, PluginEventArgs e)
        {
            Debug.WriteLine($"插件卸载成功: {e.PluginInfo.Name}");
        }
 
        /// <summary>
        /// 动态创建工具实例
        /// </summary>
        public static IProcess CreateProcess(string displayName)
        {
            return PluginManager.CreateProcess(displayName);
        }
 
        /// <summary>
        /// 获取所有工具分类
        /// </summary>
        public static List<string> GetCategories()
        {
            return PluginManager.GetCategories();
        }
 
        /// <summary>
        /// 获取指定分类的工具
        /// </summary>
        public static Dictionary<string, string> GetProcessesByCategory(string category)
        {
            return PluginManager.GetProcessesByCategory(category);
        }
 
        /// <summary>
        /// 重新加载所有插件
        /// </summary>
        public static void ReloadPlugins()
        {
            PluginManager.ReloadPlugins();
        }
 
 
        /// <summary>
        /// 流程集合
        /// </summary>
        public static ConcurrentDictionary<string, Fixture> dicFixtures = new ConcurrentDictionary<string, Fixture>();
 
        /// <summary>
        /// 全局变量集合(Key:变量名,Value:变量值)
        /// </summary>
        public static ConcurrentDictionary<string, object> dicGlobalVars = new ConcurrentDictionary<string, object>();
 
        /// <summary>
        /// 运行日志
        /// </summary>
        public string Msg = "运行成功";
 
        /// <summary>
        /// 运行结果
        /// </summary>
        public bool Result = true;
 
        /// <summary>
        /// 运行时间
        /// </summary>
        public double RunTime = 0;
 
        /// <summary>
        /// 允许运行时间
        /// </summary>
        public double MaxTimeOut = 2000;
 
        /// <summary>
        /// 工具名称
        /// </summary>
        public string strProcessName = string.Empty;
 
        /// <summary>
        /// 工具类名
        /// </summary>
        public string strProcessClass = "LB_VisionProcesses.IProcess";
 
        /// <summary>
        /// 算法参数
        /// </summary>
        public ProcessParams Params = new ProcessParams();
 
        /// <summary>
        /// 输入图片
        /// </summary>
        public object InputImage;
 
        /// <summary>
        /// 输出图片
        /// </summary>
        public object OutputImage;
 
        /// <summary>
        /// 结果区域
        /// </summary>
        public ObjectRecord Record = null;
 
        /// <summary>
        /// 运行完成标记
        /// </summary>
        public bool bCompleted = false;
 
        public abstract void InitRunParams();
 
        public abstract bool Run();
 
        public abstract bool Load(string fullPath);
 
        public abstract bool Save(string filePath);
 
        public static Assembly GetExecutingAssembly()
        {
            return Assembly.GetExecutingAssembly();
        }
 
        public abstract object Clone();
 
        public abstract void Dispose();
    }
 
 
    [Serializable]
    public class Fixture
    {
        /// <summary>
        /// 横坐标X
        /// </summary>
        public double X = 0;
        public double Column
        {
            get { return X; }
            set { X = value; }
        }
 
        /// <summary>
        /// 纵坐标Y
        /// </summary>
        public double Y = 0;
        public double Row
        {
            get { return Y; }
            set { Y = value; }
        }
 
        /// <summary>
        /// 弧度Phi
        /// </summary>
        public double Phi = 0;
        public double Angle
        {
            get { return Phi / Math.PI * 180; }
            set { Phi = value / 180 * Math.PI; }
        }
 
        public string strName = "";
 
        public Fixture() { }
 
        [JsonConstructor]  // 标记为用于反序列化
        public Fixture(double x, double y, double phi, string strName)
        {
            this.X = x; this.Y = y; this.Phi = phi;
            this.strName = strName;
        }
 
        public Fixture(Fixture fixture)
        {
            if (fixture == null)
                return;
            this.X = fixture.X; this.Y = fixture.Y; this.Phi = fixture.Phi;
            this.strName = fixture.strName;
        }
 
        public Fixture(string strName) => this.strName = strName;
 
        public void GetOffset(double x, double y, double phi, out double offsetX, out double offsetY, out double offsetPhi)
        {
            offsetX = X - x; offsetY = Y - y; offsetPhi = Phi - phi;
        }
 
        public object Clone()
        {
            return MemberwiseClone();
        }
    }
 
    public class NaturalStringComparer : IComparer<string>
    {
        [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
        public static extern int StrCmpLogicalW(string psz1, string psz2);
        public int Compare(string x, string y)
        {
            return StrCmpLogicalW(x, y);
        }
    }
}