From ede58adb19d04a796ec00d8f3409b2b362af75b7 Mon Sep 17 00:00:00 2001
From: C3204 <zhengyabo@lanpucloud.cn>
Date: 星期一, 22 十二月 2025 09:11:16 +0800
Subject: [PATCH] 修复运行bug添加项目文件。
---
LB_VisionFlowNode/IFlowContext.cs | 363 +++------------------------------------------------
1 files changed, 21 insertions(+), 342 deletions(-)
diff --git a/LB_VisionFlowNode/IFlowContext.cs b/LB_VisionFlowNode/IFlowContext.cs
index 4ca3882..e0b7147 100644
--- a/LB_VisionFlowNode/IFlowContext.cs
+++ b/LB_VisionFlowNode/IFlowContext.cs
@@ -1,6 +1,7 @@
锘縰sing LB_VisionProcesses;
using System;
using System.Diagnostics;
+using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text.Json.Nodes;
@@ -20,7 +21,7 @@
public string Group { get; set; }
public string Description { get; set; }
}
- public abstract class IFlowContext
+ public class IFlowContext
{
public readonly Dictionary<string, MethodInfo> _nodeMethods
= new Dictionary<string, MethodInfo>();
@@ -31,33 +32,10 @@
public readonly Dictionary<string, Func<FlowNode, bool>> _nodeDelegates
= new Dictionary<string, Func<FlowNode, bool>>();
- // 鑷姩鐢熸垚鐨勬彃浠惰妭鐐规柟娉曚俊鎭�
- public readonly Dictionary<string, PluginNodeMethodInfo> _pluginNodeMethods
- = new Dictionary<string, PluginNodeMethodInfo>();
-
- // 鍔ㄦ�佹柟娉曠殑鍖呰锛堢敤浜庢ā鎷烳ethodInfo锛�
- private readonly Dictionary<string, DynamicMethodWrapper> _dynamicMethodWrappers
- = new Dictionary<string, DynamicMethodWrapper>();
-
- // 鏄惁宸茶嚜鍔ㄧ敓鎴愭彃浠惰妭鐐�
- private bool _pluginNodesGenerated = false;
-
- // 鍔ㄦ�佹柟娉曞寘瑁呯被
- public class DynamicMethodWrapper
- {
- public string DisplayName { get; set; }
- public string Category { get; set; }
- public string Group { get; set; }
- public string Description { get; set; }
- public Func<FlowNode, bool> Execute { get; set; }
- // 妯℃嫙MethodInfo鐨勫繀瑕佸睘鎬�
- public string Name => DisplayName;
- }
-
public void InitializeMethods()
{
- // 1. 鍏堝鐞嗚嚜韬殑Node鐗规�ф柟娉�
var methods = GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);
+
foreach (var method in methods)
{
var nodeAttr = method.GetCustomAttribute<NodeAttribute>();
@@ -74,269 +52,28 @@
_groupedMethods[nodeAttr.Group].Add(method);
}
}
-
- // 2. 鑷姩涓篒Process宸ュ叿鐢熸垚鍔ㄦ�佽妭鐐规柟娉�
- GeneratePluginNodeMethods();
- }
-
- /// <summary>
- /// 鑷姩涓篒Process宸ュ叿鐢熸垚鑺傜偣鏂规硶锛堟牳蹇冩柟娉曪級
- /// </summary>
- private void GeneratePluginNodeMethods()
- {
- if (_pluginNodesGenerated)
- return;
- try
- {
- // 纭繚IProcess鐨勬彃浠剁鐞嗗櫒宸插垵濮嬪寲
- if (IProcess.dicProcesses == null || IProcess.dicProcesses.Count == 0)
- {
- var _ = IProcess.dicProcesses; // 瑙﹀彂鍒濆鍖�
- }
- // 娓呯┖鐜版湁鐨勬彃浠惰妭鐐规柟娉�
- _pluginNodeMethods.Clear();
- _dynamicMethodWrappers.Clear();
- // 閬嶅巻鎵�鏈塈Process宸ュ叿
- foreach (var processKvp in IProcess.dicProcesses)
- {
- string toolName = processKvp.Key;
- string className = processKvp.Value;
- // 鑾峰彇宸ュ叿鐨勫垎绫�
- string category = GetCategoryForTool(toolName);
- string group = GetGroupForCategory(category); // 鏍规嵁鍒嗙被纭畾缁�
- string description = $"{toolName}";
-
- // 鍒涘缓鎻掍欢鑺傜偣鏂规硶淇℃伅
- var pluginNode = new PluginNodeMethodInfo
- {
- DisplayName = toolName,
- FullTypeName = className,
- Category = category,
- Group = group,
- Description = description
- };
- _pluginNodeMethods[toolName] = pluginNode;
-
- // 鍒涘缓鍔ㄦ�佹墽琛屽鎵�
- Func<FlowNode, bool> executeDelegate = (node) =>
- {
- try
- {
- // 鍒涘缓IProcess瀹炰緥
- IProcess process = IProcess.CreateProcess(toolName);
- if (process == null)
- {
- Debug.WriteLine($"鏃犳硶鍒涘缓宸ュ叿瀹炰緥: {toolName}");
- node.Result = false;
- return false;
- }
-
- // 鍒濆鍖栬繍琛屽弬鏁�
- process.InitRunParams();
-
- // 鎵ц宸ュ叿鐨凴un鏂规硶
- bool runResult = process.Run();
-
- // 璁剧疆鑺傜偣缁撴灉
- node.Result = runResult;
- node.BranchIndex = "0"; // 榛樿鍒嗘敮
-
- // 閲婃斁璧勬簮
- process.Dispose();
-
- Debug.WriteLine($"鎻掍欢宸ュ叿 {toolName} 鎵ц瀹屾垚锛岀粨鏋�: {runResult}");
- return runResult;
- }
- catch (Exception ex)
- {
- Debug.WriteLine($"鎻掍欢宸ュ叿 {toolName} 鎵ц澶辫触: {ex.Message}");
- node.Result = false;
- return false;
- }
- };
-
- // 鍒涘缓鍔ㄦ�佹柟娉曞寘瑁�
- var dynamicMethod = new DynamicMethodWrapper
- {
- DisplayName = toolName,
- Category = category,
- Group = group,
- Description = description,
- Execute = executeDelegate
- };
- _dynamicMethodWrappers[description] = dynamicMethod;
-
- // ********** 鍏抽敭锛氬皢鍔ㄦ�佹柟娉曟坊鍔犲埌_nodeMethods鍜宊groupedMethods **********
- // 鏂瑰紡锛氱敓鎴愬姩鎬丮ethodInfo锛堜娇鐢‥mit锛夛紝鎴栬�呮ā鎷燂紙杩欓噷鐢‥mit鐢熸垚鐪熷疄鐨凪ethodInfo锛�
- MethodInfo dynamicMethodInfo = CreateDynamicMethod(toolName, group, description);
-
- // 娣诲姞鍒癬nodeMethods锛堥敭鏄痙escription锛�
- _nodeMethods[description] = dynamicMethodInfo;
-
- // 娣诲姞鍒癬groupedMethods
- if (!_groupedMethods.ContainsKey(group))
- {
- _groupedMethods[group] = new List<MethodInfo>();
- }
- _groupedMethods[group].Add(dynamicMethodInfo);
-
- // 娣诲姞鍒癬nodeDelegates
- _nodeDelegates[description] = executeDelegate;
-
- Debug.WriteLine($"鑷姩鐢熸垚鎻掍欢鑺傜偣: {toolName} (鍒嗙被: {category}, 缁�: {group})");
- }
-
- _pluginNodesGenerated = true;
- }
- catch (Exception ex)
- {
- Debug.WriteLine($"鑷姩鐢熸垚鎻掍欢鑺傜偣鏂规硶澶辫触: {ex.Message}");
- }
- }
-
- /// <summary>
- /// 浣跨敤Reflection.Emit鐢熸垚鍔ㄦ�丮ethodInfo锛屾ā鎷烴ode鐗规�ф柟娉�
- /// </summary>
- /// <param name="toolName">宸ュ叿鍚嶇О</param>
- /// <param name="group">缁勫悕</param>
- /// <param name="description">鎻忚堪</param>
- /// <returns>鍔ㄦ�佺敓鎴愮殑MethodInfo</returns>
- private MethodInfo CreateDynamicMethod(string toolName, string group, string description)
- {
- // 1. 瀹氫箟鍔ㄦ�佹柟娉曠殑鍙傛暟鍜岃繑鍥炵被鍨�
- Type flowNodeType = typeof(FlowNode);
- DynamicMethod dynamicMethod = new DynamicMethod(
- toolName, // 鏂规硶鍚�
- typeof(void), // 杩斿洖绫诲瀷锛堝拰鍘熸湁Node鏂规硶涓�鑷达級
- new[] { typeof(FlowNode) }, // 鍙傛暟绫诲瀷
- GetType(), // 鎵�灞炵被鍨�
- true); // 璺宠繃JIT鍙鎬ф鏌�
-
- // 2. 鐢熸垚IL浠g爜锛岃皟鐢ㄥ姩鎬佸鎵�
- ILGenerator il = dynamicMethod.GetILGenerator();
- // 鍔犺浇鍙傛暟锛團lowNode锛�
- il.Emit(OpCodes.Ldarg_1); // 娉ㄦ剰锛氬疄渚嬫柟娉曠殑绗竴涓弬鏁版槸this锛圠darg_0锛夛紝杩欓噷鍔ㄦ�佹柟娉曟槸闈欐�佺殑锛屾墍浠darg_0鏄疐lowNode
- // 璋冪敤濮旀墭锛堣繖閲岀畝鍖栵紝鐩存帴璁剧疆Result涓簍rue锛�
- il.Emit(OpCodes.Ldflda, flowNodeType.GetProperty("Result").GetSetMethod());
- il.Emit(OpCodes.Ldc_I4_1);
- il.Emit(OpCodes.Callvirt, flowNodeType.GetProperty("Result").GetSetMethod());
- // 璁剧疆BranchIndex
- il.Emit(OpCodes.Ldarg_1);
- il.Emit(OpCodes.Ldflda, flowNodeType.GetProperty("BranchIndex").GetSetMethod());
- il.Emit(OpCodes.Ldstr, "0");
- il.Emit(OpCodes.Callvirt, flowNodeType.GetProperty("BranchIndex").GetSetMethod());
- // 杩斿洖
- il.Emit(OpCodes.Ret);
-
- // 3. 灏咲ynamicMethod鍖呰涓篗ethodInfo锛圖ynamicMethod缁ф壙鑷狹ethodInfo锛�
- return dynamicMethod;
- }
-
- /// <summary>
- /// 鏍规嵁宸ュ叿鍚嶇О鎺ㄦ柇鍒嗙被锛堝畬鍠勯�昏緫锛�
- /// </summary>
- private string GetCategoryForTool(string toolName)
- {
- if (string.IsNullOrWhiteSpace(toolName))
- return "鏈垎绫�";
-
- toolName = toolName.ToLower();
-
- if (toolName.Contains("鐩告満") || toolName.Contains("鍙栧浘"))
- return "鐩告満宸ュ叿";
- else if (toolName.Contains("閫氳") || toolName.Contains("command"))
- return "閫氳宸ュ叿";
- else if (toolName.Contains("opencvsharp"))
- return "OpencvSharp绠楁硶";
- else if (toolName.Contains("halcon"))
- return "Halcon绠楁硶";
- else if (toolName.Contains("yolo") || toolName.Contains("tensorrt") ||
- toolName.Contains("ocr") || toolName.Contains("deeplearning") || toolName.Contains("澶фā鍨�"))
- return "娣卞害瀛︿範";
- else if (toolName.Contains("鍥惧儚澶勭悊") || toolName.Contains("鐐逛簯") ||
- toolName.Contains("绾跨嚎浜ょ偣"))
- return "鍥惧儚澶勭悊宸ュ叿";
- else if (toolName.Contains("娴嬭瘯") || toolName.Contains("妫�鏌�") ||
- toolName.Contains("绔彛") || toolName.Contains("灞忓箷") ||
- toolName.Contains("宸ュ簭") || toolName.Contains("杞骇"))
- return "鑷畾涔夊伐鍏�";
- else if (toolName.Contains("鑴氭湰") || toolName.Contains("鍒嗘敮") ||
- toolName.Contains("寤舵椂") || toolName.Contains("缁樺埗"))
- return "宸ュ叿";
- else if (toolName.Contains("鍥哄畾璺熼殢") || toolName.Contains("fixture"))
- return "宸ュ叿";
- else
- return "鎻掍欢宸ュ叿";
- }
-
- /// <summary>
- /// 鏍规嵁鍒嗙被纭畾缁勫悕
- /// </summary>
- private string GetGroupForCategory(string category)
- {
- switch (category)
- {
- case "閫氳宸ュ叿":
- return "Basic";
- case "绠楁硶":
- return "Basic";
- case "娣卞害瀛︿範":
- return "Basic";
- case "鍥惧儚澶勭悊宸ュ叿":
- return "Basic";
- case "鑷畾涔夊伐鍏�":
- return "Basic";
- case "宸ュ叿":
- return "Basic";
- default:
- return "Plugin";
- }
}
public void InitializeDelegates()
{
- // 1. 澶勭悊鑷韩鐨凬ode鏂规硶
foreach (var kvp in _nodeMethods)
{
var method = kvp.Value;
var parameters = method.GetParameters();
- //濡傛灉鏄姩鎬佹柟娉曪紙DynamicMethod锛夛紝璺宠繃锛堝凡缁忓湪GeneratePluginNodeMethods涓坊鍔犱簡濮旀墭锛�
- if (method is DynamicMethod)
- {
- // 浠庡姩鎬佹柟娉曞寘瑁呬腑鑾峰彇瀵瑰簲鐨勫鎵�
- if (_dynamicMethodWrappers.TryGetValue(kvp.Key, out var wrapper))
- {
- // 鐩存帴浣跨敤鍖呰涓殑Execute濮旀墭
- _nodeDelegates[kvp.Key] = wrapper.Execute;
- }
- else
- {
- // 濡傛灉娌℃湁鎵惧埌鍖呰锛屽垱寤轰竴涓粯璁ょ殑鎵ц濮旀墭
- _nodeDelegates[kvp.Key] = (node) =>
- {
- Debug.WriteLine($"鎵ц鍔ㄦ�佹柟娉�: {kvp.Key}");
- node.Result = true;
- node.BranchIndex = "0";
- return true;
- };
- }
- continue;
- }
-
if (parameters.Length == 0)
{
+ // 鏃犲弬鏁版柟娉�
var action = (Action)Delegate.CreateDelegate(typeof(Action), this, method);
_nodeDelegates[kvp.Key] = (node) => { action(); return node.Result; };
}
else if (parameters.Length == 1 && parameters[0].ParameterType == typeof(FlowNode))
{
+ // 甯lowNode鍙傛暟鐨勬柟娉�
var action = (Action<FlowNode>)Delegate.CreateDelegate(typeof(Action<FlowNode>), this, method);
_nodeDelegates[kvp.Key] = (node) => { action(node); return node.Result; };
}
}
-
- // 2. 纭繚鍔ㄦ�佹柟娉曠殑濮旀墭宸叉坊鍔狅紙GeneratePluginNodeMethods涓凡澶勭悊锛�
}
public virtual bool ExecuteNode(FlowNode node)
@@ -344,7 +81,7 @@
if (string.IsNullOrEmpty(node.Description))
return false;
- // 浼樺厛妫�鏌ュ姩鎬佸鎵�
+ // 浣跨敤濮旀墭缂撳瓨锛岄伩鍏嶅弽灏勫紑閿�
if (_nodeDelegates.TryGetValue(node.Description, out var nodeDelegate))
{
try
@@ -358,7 +95,6 @@
}
}
- // 鍏煎鍘熸湁閫昏緫
return false;
}
@@ -370,7 +106,7 @@
);
}
- // 鑾峰彇鎸夌粍鍒嗙粍鐨勫彲鐢ㄨ妭鐐癸紙鍖呭惈鍔ㄦ�佺敓鎴愮殑鎻掍欢鑺傜偣锛�
+ // 鑾峰彇鎸夌粍鍒嗙粍鐨勫彲鐢ㄨ妭鐐�
public Dictionary<string, List<ToolStripMenuItem>> GetGroupedMenuItems(EventHandler clickHandler)
{
var groupedMenuItems = new Dictionary<string, List<ToolStripMenuItem>>();
@@ -381,30 +117,12 @@
foreach (var method in group.Value)
{
- string menuText;
- string toolTip;
-
- // 鍒ゆ柇鏄潤鎬佹柟娉曡繕鏄姩鎬佹柟娉�
- if (method is DynamicMethod dynamicMethod)
- {
- // 鍔ㄦ�佹柟娉曪紙鎻掍欢宸ュ叿锛�
- var pluginNode = _pluginNodeMethods.Values.FirstOrDefault(p => p.DisplayName == dynamicMethod.Name);
- menuText = pluginNode?.DisplayName ?? dynamicMethod.Name;
- toolTip = pluginNode?.Description ?? $"鍔ㄦ�佸伐鍏�: {dynamicMethod.Name}";
- }
- else
- {
- // 闈欐�佹柟娉曪紙鍘熸湁Node鐗规�ф柟娉曪級
- var nodeAttr = method.GetCustomAttribute<NodeAttribute>();
- menuText = nodeAttr?.Name ?? method.Name;
- toolTip = nodeAttr?.Description ?? method.Name;
- }
-
+ var nodeAttr = method.GetCustomAttribute<NodeAttribute>();
var menuItem = new ToolStripMenuItem
{
- Text = menuText,
- Tag = menuText, // 瀛樺偍鎻忚堪浣滀负鏍囪瘑
- ToolTipText = toolTip
+ Text = nodeAttr.Name,
+ Tag = nodeAttr.Description, // 瀛樺偍鎻忚堪浣滀负鏍囪瘑
+ ToolTipText = nodeAttr.Description
};
if (clickHandler != null)
@@ -421,17 +139,15 @@
return groupedMenuItems;
}
- // 鑾峰彇鎸夌被鍒垎缁勭殑鑿滃崟椤癸紙鍖呭惈鍔ㄦ�佺敓鎴愮殑鎻掍欢鑺傜偣锛�
+
+ // 鑾峰彇鎸夌被鍒垎缁勭殑鑿滃崟椤�
public Dictionary<string, List<ToolStripMenuItem>> GetCategorizedMenuItems(EventHandler clickHandler)
{
var categorizedMethods = new Dictionary<string, List<MethodInfo>>();
- // 1. 澶勭悊鍘熸湁Node鏂规硶
- foreach (var method in _nodeMethods.Values.Where(m => !(m is DynamicMethod)))
+ foreach (var method in _nodeMethods.Values)
{
var nodeAttr = method.GetCustomAttribute<NodeAttribute>();
- if (nodeAttr == null) continue;
-
if (!categorizedMethods.ContainsKey(nodeAttr.Category))
{
categorizedMethods[nodeAttr.Category] = new List<MethodInfo>();
@@ -439,54 +155,20 @@
categorizedMethods[nodeAttr.Category].Add(method);
}
- // 2. 澶勭悊鍔ㄦ�佹彃浠舵柟娉�
- foreach (var pluginNode in _pluginNodeMethods.Values)
- {
- if (!categorizedMethods.ContainsKey(pluginNode.Category))
- {
- categorizedMethods[pluginNode.Category] = new List<MethodInfo>();
- }
-
- // 鎵惧埌瀵瑰簲鐨勫姩鎬丮ethodInfo
- var dynamicMethod = _nodeMethods.Values
- .OfType<DynamicMethod>()
- .FirstOrDefault(m => m.Name == pluginNode.DisplayName);
-
- if (dynamicMethod != null)
- {
- categorizedMethods[pluginNode.Category].Add(dynamicMethod);
- }
- }
-
- // 鐢熸垚鑿滃崟椤�
var categorizedMenuItems = new Dictionary<string, List<ToolStripMenuItem>>();
+
foreach (var category in categorizedMethods)
{
var menuItems = new List<ToolStripMenuItem>();
foreach (var method in category.Value)
{
- string menuText;
- string toolTip;
-
- if (method is DynamicMethod dynamicMethod)
- {
- var pluginNode = _pluginNodeMethods.Values.FirstOrDefault(p => p.DisplayName == dynamicMethod.Name);
- menuText = pluginNode?.DisplayName ?? dynamicMethod.Name;
- toolTip = pluginNode?.Description ?? $"鍔ㄦ�佸伐鍏�: {dynamicMethod.Name}";
- }
- else
- {
- var nodeAttr = method.GetCustomAttribute<NodeAttribute>();
- menuText = nodeAttr?.Name ?? method.Name;
- toolTip = $"{nodeAttr?.Group} - {nodeAttr?.Description}";
- }
-
+ var nodeAttr = method.GetCustomAttribute<NodeAttribute>();
var menuItem = new ToolStripMenuItem
{
- Text = menuText,
- Tag = menuText,
- ToolTipText = toolTip
+ Text = nodeAttr.Name,
+ Tag = nodeAttr.Description,
+ ToolTipText = $"{nodeAttr.Group} - {nodeAttr.Description}"
};
if (clickHandler != null)
@@ -503,7 +185,6 @@
return categorizedMenuItems;
}
- // 鍘熸湁Node鏂规硶
[Node("寮�濮�", "鎺у埗", "Logic", "寮�濮�")]
public virtual void 寮�濮�(FlowNode node) { node.Result = true; }
@@ -538,7 +219,6 @@
[Node("骞惰鍒嗘敮缁撴潫", "鎺у埗", "Logic", "骞惰鍒嗘敮缁撴潫")]
public virtual void 骞惰鍒嗘敮缁撴潫(FlowNode node) { node.Result = true; }
}
-
public class FlowContext : IFlowContext
{
/// <summary>
@@ -573,9 +253,11 @@
public FlowContext()
{
+ //GenerateFixedNodeMethods();
InitializeMethods();
InitializeDelegates();
}
+
// 缁熶竴鐨勫姩鎬佽妭鐐规墽琛岃緟鍔╂柟娉曪紙鍙�夛級
private void RunNodeAsync(FlowNode node)
@@ -672,9 +354,6 @@
}
}
- // 淇濈暀鍘熸湁娴嬭瘯鏂规硶锛堝彲閫夛紝涔熷彲浠ョЩ闄わ紝鐢卞姩鎬佺敓鎴愭浛浠o級
- [Node("娴嬭瘯宸ュ叿", "绠楁硶", "Basic", "娴嬭瘯宸ュ叿")]
- public void 娴嬭瘯宸ュ叿(FlowNode node) { RunNodeAsync(node); }
}
}
--
Gitblit v1.9.3