using System;
using System.Collections.Generic;
using System.Threading;
using HalconDotNet;
using TeamAAS.FlowEditor.Models;
using TeamAAS.FlowEditor.Plugins;
using Plugins.Halcon.Models;
using Plugins.Halcon;
namespace Plugins.Halcon
{
// ─────────────────────────────────────────────────────────────────────
// HALCON 检测识别分类算子(从主程序集迁入分类 DLL)。
// 显示名/类型全名保持不变 —— 工具箱、流程文件、公式绑定全部兼容。
// ─────────────────────────────────────────────────────────────────────
/// H形状筛选:按特征与阈值区间从区域集中保留符合条件的区域。
[Serializable]
[Plugin("H形状筛选", PluginCategory.Halocn模块, typeof(HalconSelectShapeModel), "FilterVariant",
NodeShape = NodeCategory.Normal, IsSubFlowNode = true, VisionCategory = VisionPlugin.检测识别,
Description = "形状筛选:按特征(area/roundness/compactness…)与[min,max]保留区域,输出区域集与数量")]
public class HalconSelectShapePlugin : BasePlugin
{
public override List DeclareOutputs() => new List
{
new OutputField("区域集", typeof(object)),
new OutputField("数量", typeof(int)),
new OutputField("结果", typeof(bool)),
};
public override NodeRunStatus PluginRun(CancellationToken token, out Dictionary results)
{
var res = new Dictionary();
results = res;
NodeRunStatus Fail(string m) { res["Error"] = m; res["结果"] = false; Log(1, $"形状筛选失败: {m}", TeamAAS.LogLevel.Error); return NodeRunStatus.Failed; }
try
{
var src = GetResolve(Model.RegionSource);
if (!(src is HObject region)) return Fail("未获取到输入区域集(请绑定上游「区域集」输出)");
if (string.IsNullOrWhiteSpace(Model.Feature)) return Fail("未指定筛选特征");
var selected = HalconRuntime.SelectShape(region, Model.Feature, "and", Model.Min, Model.Max);
int n = HalconRuntime.CountObj(selected);
res["区域集"] = selected;
res["数量"] = n;
res["结果"] = true;
HalconEditorPreview.Show(Model, null, selected, "cyan"); // 只更新叠加区域,保留底图
Log(2, $"形状筛选[{Model.Feature} ∈ {Model.Min}~{Model.Max}]:命中 {n} 个区域");
return NodeRunStatus.Success;
}
catch (Exception ex) { return Fail(ex.Message); }
}
}
}
namespace Plugins.Halcon.Models
{
using PropertyGridLib.Attributes;
using PropertyGridLib.Controls;
using System.ComponentModel;
using TeamAAS.FlowEditor.Plugins;
using TeamAAS.FlowEngine.FormulaData;
/// 形状筛选:按特征与阈值区间从区域集中保留符合条件的区域。
[Serializable]
public class HalconSelectShapeModel : HalconOperatorModelBase
{
[Category("I.输入")]
[DisplayName("1.输入区域")]
[Description("上游区域集(一般来自连通域的输出「区域集」)")]
[FormulaEditor(typeof(PluginDataProvider))]
public FormulaBound