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 = 200000000000;
|
|
/// <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 = null;
|
|
/// <summary>
|
/// 输出图片
|
/// </summary>
|
public object OutputImage = null;
|
|
/// <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);
|
}
|
}
|
}
|