using Bro.Common.Helper;
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Bro.Common.Base
{
public class HDevEngineTool
{
#region 常量
// path of external procedures
string ProcedurePath = Environment.CurrentDirectory + "\\Vision\\";
#endregion
#region 成员变量
///
/// 处理过程名
///
public string ProcedureName;
///
/// hdev程序启动引擎
///
private HDevEngine myEngine;
///
/// 过程载入工具 .hdvp
///
private HDevProcedureCall procedureCall;
///
/// 程序运行是否成功
///
private bool isSuccess = false;
///
/// 控制参数字典
///
public Dictionary InputTupleDic { get; set; }
///
/// 图形参数字典
///
public Dictionary InputImageDic { get; set; }
#endregion
#region 初始化
///
/// 实例化 默认搜索路径为: 启动路径//Vision//
///
public HDevEngineTool()
{
ProcedureName = "";
myEngine = new HalconDotNet.HDevEngine();
myEngine.SetProcedurePath(ProcedurePath);
InputImageDic = new Dictionary();
InputTupleDic = new Dictionary();
}
///
/// 实例化
///
/// 外部函数搜索路径
public HDevEngineTool(string path)
{
myEngine = new HalconDotNet.HDevEngine();
myEngine.SetProcedurePath(path);
InputImageDic = new Dictionary();
InputTupleDic = new Dictionary();
}
#endregion
///
/// 设置函数运行所需参数
///
/// 控制参数
/// 图形参数
public void SetDictionary(Dictionary _tupleDictionary, Dictionary _imageDictionary)
{
this.InputTupleDic = _tupleDictionary;
this.InputImageDic = _imageDictionary;
}
///
/// 载入过程 .hdvp
///
/// 过程名
public void LoadProcedure(string procedureName)
{
ProcedureName = procedureName;
try
{
HDevProcedure procedure = new HDevProcedure(procedureName);
procedureCall = new HDevProcedureCall(procedure);
}
catch (HDevEngineException Ex)
{
Trace.TraceInformation("HDevProgram {0} Load fail ,Error Line : {1}, Line number: {2}, Halcon error number : {3}", Ex.ProcedureName, Ex.LineText, Ex.LineNumber, Ex.HalconError);
return;
}
}
///
/// 执行过程
///
public void RunProcedure()
{
try
{
foreach (KeyValuePair pair in InputTupleDic)
{
procedureCall.SetInputCtrlParamTuple(pair.Key, pair.Value);
}
foreach (KeyValuePair pair in InputImageDic)
{
procedureCall.SetInputIconicParamObject(pair.Key, pair.Value);
}
procedureCall.Execute();
isSuccess = true;
}
catch (HDevEngineException ex)
{
isSuccess = false;
Trace.TraceInformation("HDevProgram {0} Run fail , Line number: {1}, Halcon error number : {2},ex:{3}", ex.ProcedureName, ex.LineNumber, ex.HalconError, ex.Message);
return;
}
}
public HTuple GetResultTuple(string key)
{
if (isSuccess)
{
return procedureCall.GetOutputCtrlParamTuple(key);
}
else
{
return new HTuple();
}
}
public HObject GetResultObject(string key, bool ignoreError = false)
{
if (ignoreError || isSuccess)
{
return procedureCall.GetOutputIconicParamObject(key);
}
else
{
return new HObject();
}
}
}
public static class HalconHelper
{
public static HObject ConvertBitmapToHObject(this Bitmap bmp)
{
HImage himage = new HImage();
try
{
//判断输入图像不为null
if (bmp == null)
{
return null;
}
{
//重绘himage
//HImage curImage = new HImage();
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
himage.GenImage1("byte", bmp.Width, bmp.Height, bmpData.Scan0);
bmp.UnlockBits(bmpData);
//himage = curImage;
}
return himage;
}
catch (Exception e)
{
return null;
}
}
public static List HTupleToDouble(this HTuple tuple)
{
List list = new List();
for (int i = 0; i < tuple.Length; i++)
{
list.Add(tuple[i]);
}
return list;
}
}
}