调用halcon引擎有两个直接的好处:
-
避免C# 与halcon代码混编时可能产生的内存泄露问题
-
修改halcon程序时不用重新编译C#
勇哥写了一个示例,详细的应用感受和缺点限制勇哥会持续做相关的总结给大家分享。
对于halcon17来说,要运行下面的程序,你要引用
C#
halcondotnet.dll
hdevenginedonet.dll
C#
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp1
{public partial class Form1 : Form{private HDevEngine engine = new HDevEngine();private HDevProgramCall programCall;private string programPath;private string procedurePath;private HDevOpMultiWindowImpl MyHDevOperatorImpl;public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){var path1 = "drawCircle";if (initialengine(path1)){loadengine(); inspectengine();}}public bool initialengine(string filename){programPath = System.Environment.CurrentDirectory + @"\" + filename + ".hdev";procedurePath = System.Environment.CurrentDirectory + @"\";if(!File.Exists(programPath)){MessageBox.Show("file not exist");return false;}if (!HalconAPI.isWindows){programPath = programPath.Replace("\"", "/");procedurePath = procedurePath.Replace("\"", "/");}engine.SetProcedurePath(procedurePath);// viewPort.HalconWindow.SetLineWidth(4);MyHDevOperatorImpl = new HDevOpMultiWindowImpl(hWindowControl1.Handle);engine.SetHDevOperators(MyHDevOperatorImpl);return true;}public void loadengine(){try{HDevProgram program = new HDevProgram(programPath);programCall = new HDevProgramCall(program);}catch { }}public void disposeengine(){try{programCall.Dispose();}catch { }}public void inspectengine(){try{programCall.Execute();}catch { }}}
}
halcon代码如下,只是简单的画了一个圆。
C#
px:=280
py:=300
r:=200xary:=[]
yary:=[]
step:=2
for Index := 0 to 360/step by stepx:=px+px/2*cos(rad(step*Index+step))y:=py+py/2*sin(rad(step*Index+step))xary[Index/step]:=xyary[Index/step]:=ydisp_polygon(200000, xary, yary)
endfor