DHDHSoftware/CanFly/UI/GuidePanel/BaseGuideControl.cs

159 lines
4.3 KiB
C#
Raw Normal View History

2025-03-16 17:32:09 +08:00
using CanFly.Canvas.Shape;
using CanFly.Canvas.UI;
using CanFly.Helper;
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanFly.UI.GuidePanel
{
public class BaseGuideControl : UserControl
{
public Action? OnControlCloseEvent;
public event Action<string,string> OnDataPassed;
private string _currentImageFile;
public string CurrentImageFile;
protected string _hScriptsDir = Path.Combine(Environment.CurrentDirectory, "hscripts");
protected HObject? hImage = null;
protected FlyCanvas _canvas;
private HDevEngineTool? tool = null;
public void DataToTriggerEvent(string input,string output)
{
OnDataPassed?.Invoke(input, output);
}
protected virtual void UpdateShape(FlyShape shape)
{
throw new NotImplementedException();
}
protected virtual string GetScriptFileName()
{
throw new NotImplementedException();
}
/// <summary>
/// 执行Halcon脚本
/// </summary>
/// <param name="inputImg">输入图像</param>
/// <param name="inputDic">输入参数</param>
/// <param name="outputParamKeys">输出参数</param>
protected void ExecuteHScript(
Dictionary<string, HObject> inputImg,
Dictionary<string, HTuple> inputDic,
List<string> outputParamKeys,
Action<Exception>? exceptionHandler = null)
{
string filePath = Path.Combine(_hScriptsDir, GetScriptFileName());
if (!File.Exists(filePath))
{
MessageBox.Show($"文件 {filePath} 不存在");
return;
}
try
{
if (tool == null)
{
tool = new HDevEngineTool(_hScriptsDir);
tool.LoadProcedure(Path.GetFileNameWithoutExtension(GetScriptFileName()));
}
//tool.InputImageDic["INPUT_Image"] = hImage;
//tool.InputTupleDic["XCenter"] = _x;
//tool.InputTupleDic["YCenter"] = _y;
//tool.InputTupleDic["Radius"] = _r;
tool.InputImageDic = inputImg;
tool.InputTupleDic = inputDic;
Dictionary<string, HTuple> outputParams = new Dictionary<string, HTuple>();
if (!tool.RunProcedure(out string error, out int timeElasped))
{
OnExecuteHScriptResult(false, outputParams, timeElasped);
return;
}
for (int i = 0; i < outputParamKeys.Count; i++)
{
string k = outputParamKeys[i];
outputParams[k] = tool.GetResultTuple(k);
}
OnExecuteHScriptResult(true, outputParams, timeElasped);
}
catch (Exception ex)
{
exceptionHandler?.Invoke(ex);
}
finally
{
hImage?.Dispose();
hImage = null;
}
}
/// <summary>
/// Halcon脚本执行结果回调函数重写该方法以自行处理算法执行结果
/// </summary>
/// <param name="success">算法执行是否成功</param>
/// <param name="resultDic">算法输出结果</param>
/// <param name="timeElasped">算法耗时单位ms</param>
protected virtual void OnExecuteHScriptResult(bool success, Dictionary<string, HTuple> resultDic, int timeElasped)
{
throw new NotImplementedException();
}
protected void OpenImageFile(Action<Bitmap> callback)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "图像文件|*.jpg;*.jpeg;*.png";
ofd.Multiselect = false;
if (ofd.ShowDialog() == DialogResult.OK)
{
CurrentImageFile = ofd.FileName;
Bitmap bitmap = (Bitmap)Image.FromFile(CurrentImageFile);
callback?.Invoke(bitmap);
}
}
protected void OnControlClose()
{
OnControlCloseEvent?.Invoke();
}
}
}