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 { /// /// 流程集合(指向PluginManager的已加载工具) /// public static Dictionary 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(); } /// /// 初始化插件管理器 /// 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}"); } /// /// 动态创建工具实例 /// public static IProcess CreateProcess(string displayName) { return PluginManager.CreateProcess(displayName); } /// /// 获取所有工具分类 /// public static List GetCategories() { return PluginManager.GetCategories(); } /// /// 获取指定分类的工具 /// public static Dictionary GetProcessesByCategory(string category) { return PluginManager.GetProcessesByCategory(category); } /// /// 重新加载所有插件 /// public static void ReloadPlugins() { PluginManager.ReloadPlugins(); } /// /// 流程集合 /// public static ConcurrentDictionary dicFixtures = new ConcurrentDictionary(); /// /// 全局变量集合(Key:变量名,Value:变量值) /// public static ConcurrentDictionary dicGlobalVars = new ConcurrentDictionary(); /// /// 运行日志 /// public string Msg = "运行成功"; /// /// 运行结果 /// public bool Result = true; /// /// 运行时间 /// public double RunTime = 0; /// /// 允许运行时间 /// public double MaxTimeOut = 200000000000; /// /// 工具名称 /// public string strProcessName = string.Empty; /// /// 工具类名 /// public string strProcessClass = "LB_VisionProcesses.IProcess"; /// /// 算法参数 /// public ProcessParams Params = new ProcessParams(); /// /// 输入图片 /// public object InputImage = null; /// /// 输出图片 /// public object OutputImage = null; /// /// 结果区域 /// public ObjectRecord Record = null; /// /// 运行完成标记 /// 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 { /// /// 横坐标X /// public double X = 0; public double Column { get { return X; } set { X = value; } } /// /// 纵坐标Y /// public double Y = 0; public double Row { get { return Y; } set { Y = value; } } /// /// 弧度Phi /// 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 { [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); } } }