halcon例程解析一add_metrology_object_generic转C#实现在winform中检测圆,矩形,圆弧,线

本文不赘述halcon如何导出为C#程序,只写出对导出C#程序进行修改成功的程序,如有不会的问题可以私信我。
本文提供两种方法,一种将程序写下form1.cs中,另一种写在用户控件中。
在这里插入图片描述

  1. 方法一
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace add_metrology_object_generic1
{
    public partial class Form1 : Form
    {
        public HTuple hv_ExpDefaultWinHandle;
        public Form1()
        {
            InitializeComponent();
            hv_ExpDefaultWinHandle = hWindowControl1.HalconWindow;       
        }

        private void button1_Click(object sender, EventArgs e)
        {

            // Local iconic variables 

            HObject ho_EmptyObject, ho_Image, ho_ModelContour;
            HObject ho_MeasureContour, ho_Region = null, ho_ModelRegion = null;
            HObject ho_ImageReduced = null, ho_ShapeModelContours = null;
            HObject ho_CurrentImage = null, ho_Contour = null, ho_UsedEdges = null;
            HObject ho_ResultContours = null;

            // Local control variables 

            HTuple hv_AlignmentMode = null, hv_Width = null;
            HTuple hv_Height = null, hv_WindowHandle = new HTuple();
            HTuple hv_CameraParam = null, hv_MeasurementPlane = null;
            HTuple hv_CalibPlateThickness = null, hv_PartHeight = null;
            HTuple hv_AdjustThickness = null, hv_MeasurementPlaneAdjusted = null;
            HTuple hv_MetrologyHandle = null, hv_CircleParam = null;
            HTuple hv_Row3 = null, hv_Column3 = null, hv_Radius = null;
            HTuple hv_Row4 = null, hv_Column4 = null, hv_Radius1 = null;
            HTuple hv_Row5 = null, hv_Column5 = null, hv_Radius2 = null;
            HTuple hv_Row6 = null, hv_Column6 = null, hv_Radius3 = null;
            HTuple hv_CircleIndices1 = null, hv_Row8 = null, hv_Column8 = null;
            HTuple hv_Radius5 = null, hv_Row7 = null, hv_Column7 = null;
            HTuple hv_Radius4 = null, hv_CircleIndices2 = null, hv_Index3 = null;
            HTuple hv_RectangleParam = null, hv_Row9 = null, hv_Column9 = null;
            HTuple hv_Phi = null, hv_Length1 = null, hv_Length2 = null;
            HTuple hv_RectIndices = null, hv_Line1 = null, hv_Line2 = null;
            HTuple hv_Row11 = null, hv_Column11 = null, hv_Row21 = null;
            HTuple hv_Column21 = null, hv_Row12 = null, hv_Column12 = null;
            HTuple hv_Row22 = null, hv_Column22 = null, hv_LineIndices = null;
            HTuple hv_Row = null, hv_Column = null, hv_Message = null;
            HTuple hv_BorderShapeModel = new HTuple(), hv_ShapeModelID = new HTuple();
            HTuple hv_Area = new HTuple(), hv_RowModel = new HTuple();
            HTuple hv_ColumnModel = new HTuple(), hv_I = null, hv_RowAlign = new HTuple();
            HTuple hv_ColumnAlign = new HTuple(), hv_AngleAlign = new HTuple();
            HTuple hv_Score = new HTuple(), hv_UsedRow = new HTuple();
            HTuple hv_UsedColumn = new HTuple(), hv_RadiusC1 = new HTuple();
            HTuple hv_RadiusC2 = new HTuple(), hv_Length1R = new HTuple();
            HTuple hv_Length2R = new HTuple(), hv_ParamLine1 = new HTuple();
            HTuple hv_ParamLine2 = new HTuple(), hv_XC1 = new HTuple();
            HTuple hv_YC1 = new HTuple(), hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
            HTuple hv_XC2 = new HTuple(), hv_YC2 = new HTuple(), hv_Row2 = new HTuple();
            HTuple hv_Column2 = new HTuple(), hv_J = new HTuple();
            HTuple hv_XRectangle = new HTuple(), hv_YRectangle = new HTuple();
            HTuple hv_RowR = new HTuple(), hv_ColumnR = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_EmptyObject);
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_ModelContour);
            HOperatorSet.GenEmptyObj(out ho_MeasureContour);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ModelRegion);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_ShapeModelContours);
            HOperatorSet.GenEmptyObj(out ho_CurrentImage);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_UsedEdges);
            HOperatorSet.GenEmptyObj(out ho_ResultContours);
            try
            {
                //这个程序展示了如何使用计量模型来获得平面上各种几何形状的校准测量值。
                //几何形态参数由模型图像导出。
                //要在新图像中找到要测量的对象,三个不同的对齐方法如下:
                //a)使用基于形状的匹配
                //b)使用区域处理
                //c)使用点对应的刚性变换
                //测量结果以等高线形式返回
                //作为给定几何对象的调整参数
                //
                //对象通过激活所需的线来打开对齐方法
                hv_AlignmentMode = "shape-based matching";
                //AlignmentMode := 'region processing'
                //AlignmentMode := 'rigid transformation'
                //AlignmentMode:=“基于形状的匹配”
                //AlignmentMode:=“区域处理”
                //AlignmentMode:=“刚性变换”
                //
                //初始化
                dev_update_off();
                //dev_close_window(...);
                HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin");
                ho_EmptyObject.Dispose();
                HOperatorSet.GenEmptyObj(out ho_EmptyObject);
                ho_Image.Dispose();
                HOperatorSet.ReadImage(out ho_Image, "metal-parts/circle_plate_01");
                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle);
                set_display_font(hv_ExpDefaultWinHandle, 16, "mono", "true", "false");
                //设备更新关闭()
                //开发关闭窗口()
                //设置绘图边缘
                //生成空对象(空对象)
                //读取图像(图像,“ 地址 ”)
                //获取图像大小(图像、宽度、高度)
                //按指定图像的大小来打开一个窗口(目标图片,新窗口左上角列标,新窗口宽度高度限制,新图形窗口的窗口句柄)
                //设置显示字体(窗口句柄,字体大小,字体,粗体,斜体)

                //Part I:
                //
                //**初始化计量模型
                //定义相机参数
                hv_CameraParam = new HTuple();
                hv_CameraParam[0] = 0.0128649;
                hv_CameraParam[1] = -661.434;
                hv_CameraParam[2] = 5.30004e-006;
                hv_CameraParam[3] = 5.3e-006;
                hv_CameraParam[4] = 620.043;
                hv_CameraParam[5] = 497.402;
                hv_CameraParam = hv_CameraParam.TupleConcat(hv_Width);
                hv_CameraParam = hv_CameraParam.TupleConcat(hv_Height);
                //测量平面的姿态是通过在被测物体的平面上放置一个校准板并通过确定其姿态来获得的
                hv_MeasurementPlane = new HTuple();
                hv_MeasurementPlane[0] = 0.00940956;
                hv_MeasurementPlane[1] = -0.00481017;
                hv_MeasurementPlane[2] = 0.29128;
                hv_MeasurementPlane[3] = 0.478648;
                hv_MeasurementPlane[4] = 359.65;
                hv_MeasurementPlane[5] = 0.785;
                hv_MeasurementPlane[6] = 0;
                //测量模板

                //*根据零件高度和校准板高度调整测量平面的姿态
                //校准板的厚度
                hv_CalibPlateThickness = 0.006;
                //校准板厚度:=0.006

                //被测物体的厚度
                hv_PartHeight = 0.005;
                hv_AdjustThickness = hv_CalibPlateThickness - hv_PartHeight;
                //零件高度:=0.005
                //调整厚度:=校准板厚度-零件高度
                HOperatorSet.SetOriginPose(hv_MeasurementPlane, 0, 0, hv_AdjustThickness, out hv_MeasurementPlaneAdjusted);
                //设置原点姿态(测量平面,0,0,调整厚度,测量平面调整)

                //创建计量模型并为校准测量做好准备
                HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);

                //提前设置图像大小,以加速第一次调用apply_metrology_model.
                HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);
                //设置相机参数
                HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "camera_param", hv_CameraParam);
                //设置被测物体所在的平面
                HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "plane_pose", hv_MeasurementPlaneAdjusted);
                //
                //将待测对象添加到计量模型中
                //
                //添加几个完整圆
                //CircleParam := [354,274,53]
                //CircleParam := [CircleParam,350,519,53]
                //CircleParam := [CircleParam,345,764,52]
                //CircleParam := [CircleParam,596,523,53]
                HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
                HOperatorSet.DrawCircle(hv_ExpDefaultWinHandle, out hv_Row3, out hv_Column3,
                    out hv_Radius);
                HOperatorSet.DrawCircle(hv_ExpDefaultWinHandle, out hv_Row4, out hv_Column4,
                    out hv_Radius1);
                HOperatorSet.DrawCircle(hv_ExpDefaultWinHandle, out hv_Row5, out hv_Column5,
                    out hv_Radius2);
                HOperatorSet.DrawCircle(hv_ExpDefaultWinHandle, out hv_Row6, out hv_Column6,
                    out hv_Radius3);
                hv_CircleParam = new HTuple();
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Row3);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Column3);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Radius);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Row4);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Column4);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Radius1);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Row5);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Column5);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Radius2);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Row6);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Column6);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Radius3);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "circle", hv_CircleParam,
                    20, 5, 1, 30, new HTuple(), new HTuple(), out hv_CircleIndices1);
                //
                //添加两个部分圆
                //CircleParam1 := [583,1010,79]
                //CircleParam2 := [336,1005,77]
                HOperatorSet.DrawCircle(hv_ExpDefaultWinHandle, out hv_Row8, out hv_Column8,
                    out hv_Radius5);
                HOperatorSet.DrawCircle(hv_ExpDefaultWinHandle, out hv_Row7, out hv_Column7,
                    out hv_Radius4);
                hv_CircleParam = new HTuple();
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Row7);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Column7);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Radius4);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Row8);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Column8);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Radius5);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "circle", hv_CircleParam,
                    20, 5, 1, 30, (new HTuple("start_phi")).TupleConcat("end_phi"), (new HTuple(0)).TupleConcat(
                    (new HTuple(185)).TupleRad()), out hv_CircleIndices2);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "circle", hv_CircleParam,
                    20, 5, 1, 30, (new HTuple("start_phi")).TupleConcat("end_phi"), (new HTuple((new HTuple(45)).TupleRad()
                    )).TupleConcat((new HTuple(185)).TupleRad()), out hv_Index3);
                hv_CircleIndices2 = hv_CircleIndices2.TupleConcat(hv_Index3);
                //
                //添加一个矩形
                hv_RectangleParam = new HTuple();
                hv_RectangleParam[0] = 599;
                hv_RectangleParam[1] = 279;
                hv_RectangleParam = hv_RectangleParam.TupleConcat((new HTuple(90)).TupleRad()
                    );
                hv_RectangleParam = hv_RectangleParam.TupleConcat(62);
                hv_RectangleParam = hv_RectangleParam.TupleConcat(51);
                HOperatorSet.DrawRectangle2(hv_ExpDefaultWinHandle, out hv_Row9, out hv_Column9,
                    out hv_Phi, out hv_Length1, out hv_Length2);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "rectangle2", hv_RectangleParam,
                    20, 5, 1, 30, new HTuple(), new HTuple(), out hv_RectIndices);
                //添加两条线
                hv_Line1 = new HTuple();
                hv_Line1[0] = 143;
                hv_Line1[1] = 1122;
                hv_Line1[2] = 709;
                hv_Line1[3] = 1132;
                hv_Line2 = new HTuple();
                hv_Line2[0] = 151;
                hv_Line2[1] = 153;
                hv_Line2[2] = 136;
                hv_Line2[3] = 1115;
                HOperatorSet.DrawLine(hv_ExpDefaultWinHandle, out hv_Row11, out hv_Column11,
                    out hv_Row21, out hv_Column21);
                HOperatorSet.DrawLine(hv_ExpDefaultWinHandle, out hv_Row12, out hv_Column12,
                    out hv_Row22, out hv_Column22);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "line", hv_Line1.TupleConcat(
                    hv_Line2), 20, 5, 1, 30, new HTuple(), new HTuple(), out hv_LineIndices);
                //检查已添加到计量模型的形状
                ho_ModelContour.Dispose();
                HOperatorSet.GetMetrologyObjectModelContour(out ho_ModelContour, hv_MetrologyHandle,
                    "all", 1.5);
                ho_MeasureContour.Dispose();
                HOperatorSet.GetMetrologyObjectMeasures(out ho_MeasureContour, hv_MetrologyHandle,
                    "all", "all", out hv_Row, out hv_Column);
                hv_Message = "This example shows how to measure geometric shapes using a";
                if (hv_Message == null)
                    hv_Message = new HTuple();
                hv_Message[1] = "metrology model. As preparation, their roughly known ";
                if (hv_Message == null)
                    hv_Message = new HTuple();
                hv_Message[2] = "dimensions and tolerances are specified by the user.";
                show_contours(ho_Image, ho_ModelContour, ho_MeasureContour, ho_EmptyObject,
                    hv_WindowHandle, hv_Message);
                HDevelopStop();
                //
                //Part 2:
                //
                //准备对准
                //
                //a)基于形状的匹配
                if ((int)(new HTuple(hv_AlignmentMode.TupleEqual("shape-based matching"))) != 0)
                {
                    HOperatorSet.SetPart(hv_ExpDefaultWinHandle, ((-hv_Height) / 2) - 100, (-hv_Width) / 2,
                        (1.5 * hv_Height) - 100, 1.5 * hv_Width);
                    //
                    //创建用于在图像中校准该计量模型的形状模型,
                    //其中对象发生的位置和方向与用于创建该模型的图像中的位置和方向不同
                    HOperatorSet.GetSystem("border_shape_models", out hv_BorderShapeModel);
                    HOperatorSet.SetSystem("border_shape_models", "true");
                    ho_Region.Dispose();
                    HOperatorSet.Threshold(ho_Image, out ho_Region, 0, 50);
                    ho_ModelRegion.Dispose();
                    HOperatorSet.DilationRectangle1(ho_Region, out ho_ModelRegion, 5, 5);
                    ho_ImageReduced.Dispose();
                    HOperatorSet.ReduceDomain(ho_Image, ho_ModelRegion, out ho_ImageReduced);
                    HOperatorSet.CreateShapeModel(ho_ImageReduced, 6, 0, (new HTuple(360)).TupleRad()
                        , "auto", "auto", "use_polarity", "auto", 20, out hv_ShapeModelID);
                    //形状模型的原点是create_shape_model中使用的输入区域的中心
                    HOperatorSet.AreaCenter(ho_ModelRegion, out hv_Area, out hv_RowModel, out hv_ColumnModel);
                    ho_ShapeModelContours.Dispose();
                    HOperatorSet.GetShapeModelContours(out ho_ShapeModelContours, hv_ShapeModelID,
                        1);
                    hv_Message = "A shape model will be used for the alignment of the metrology";
                    if (hv_Message == null)
                        hv_Message = new HTuple();
                    hv_Message[1] = "model. The contours of the shape model (white) and of the";
                    if (hv_Message == null)
                        hv_Message = new HTuple();
                    hv_Message[2] = "metrology model (blue) are shown.";
                    show_contours(ho_Image, ho_ModelContour, ho_EmptyObject, ho_ShapeModelContours,
                        hv_WindowHandle, hv_Message);
                    //
                    //改变定义计量模型的参考系,使其与形状模型所使用的参考系相对应。
                    //为了能够直接使用find_shape_model的结果来校准计量模型,这是必要的
                    HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "reference_system",
                        ((hv_RowModel.TupleConcat(hv_ColumnModel))).TupleConcat(0));
                    ho_ModelContour.Dispose();
                    HOperatorSet.GetMetrologyObjectModelContour(out ho_ModelContour, hv_MetrologyHandle,
                        "all", 1.5);
                    hv_Message = "To prepare the alignment, the origin of the shape model";
                    if (hv_Message == null)
                        hv_Message = new HTuple();
                    hv_Message[1] = "is set as the reference system of the metrology model.";
                    show_contours(ho_Image, ho_ModelContour, ho_EmptyObject, ho_ShapeModelContours,
                        hv_WindowHandle, hv_Message);
                    HDevelopStop();
                    HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_Height - 1, hv_Width - 1);
                }
                //
                //
                //在线阶段
                for (hv_I = 2; (int)hv_I <= 5; hv_I = (int)hv_I + 1)
                {
                    ho_CurrentImage.Dispose();
                    HOperatorSet.ReadImage(out ho_CurrentImage, "metal-parts/circle_plate_" + (hv_I.TupleString(
                        "02d")));
                    HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 1);
                    HOperatorSet.DispObj(ho_CurrentImage, hv_ExpDefaultWinHandle);
                    //
                    //a) Shape-based匹配
                    if ((int)(new HTuple(hv_AlignmentMode.TupleEqual("shape-based matching"))) != 0)
                    {
                        //
                        //使用形状模型测量当前图像中物体的位置和方向
                        HOperatorSet.FindShapeModel(ho_CurrentImage, hv_ShapeModelID, 0, (new HTuple(360)).TupleRad()
                            , 0.5, 1, 0, "least_squares", 5, 0.9, out hv_RowAlign, out hv_ColumnAlign,
                            out hv_AngleAlign, out hv_Score);
                        dev_display_shape_matching_results(hv_ShapeModelID, "white", hv_RowAlign,
                            hv_ColumnAlign, hv_AngleAlign, 1, 1, 0);
                    }
                    //

                    //
                    //使用计算的位置和方向将计量模型与其当前引用对齐
                    HOperatorSet.AlignMetrologyModel(hv_MetrologyHandle, hv_RowAlign, hv_ColumnAlign,
                        hv_AngleAlign);
                    //显示校准的计量模型
                    if ((int)(new HTuple(hv_I.TupleLessEqual(2))) != 0)
                    {
                        ho_ModelContour.Dispose();
                        HOperatorSet.GetMetrologyObjectModelContour(out ho_ModelContour, hv_MetrologyHandle,
                            "all", 1.5);
                        HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "blue");
                        HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 2);
                        HOperatorSet.DispObj(ho_ModelContour, hv_ExpDefaultWinHandle);
                        hv_Message = "In each image, the object is matched and aligned";
                        if (hv_Message == null)
                            hv_Message = new HTuple();
                        hv_Message[1] = "before the metrology measurement.";
                        disp_message(hv_ExpDefaultWinHandle, hv_Message, "window", 12, 12, "black",
                            "true");
                        disp_continue_message(hv_ExpDefaultWinHandle, "black", "true");
                        HDevelopStop();
                    }
                    //
                    //在一次命令中对所有计量对象进行测量
                    HOperatorSet.ApplyMetrologyModel(ho_CurrentImage, hv_MetrologyHandle);
                    //获取用于可视化的度量区域
                    ho_Contour.Dispose();
                    HOperatorSet.GetMetrologyObjectMeasures(out ho_Contour, hv_MetrologyHandle,
                        "all", "all", out hv_Row, out hv_Column);
                    //得到实际用于拟合几何图形的边缘点
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "used_edges",
                        "row", out hv_UsedRow);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "used_edges",
                        "column", out hv_UsedColumn);
                    ho_UsedEdges.Dispose();
                    HOperatorSet.GenCrossContourXld(out ho_UsedEdges, hv_UsedRow, hv_UsedColumn,
                        10, (new HTuple(45)).TupleRad());
                    //
                    //获取自摄像机参数设定后的测量结果,
                    //所有结果均以公制坐标系给出,由测量平面定义坐标系,得到被测几何图形的可视化
                    ho_ResultContours.Dispose();
                    HOperatorSet.GetMetrologyObjectResultContour(out ho_ResultContours, hv_MetrologyHandle,
                        "all", "all", 1.5);
                    //提取较小圆的半径
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_CircleIndices1,
                        "all", "result_type", "radius", out hv_RadiusC1);
                    //提取较大的不完整圆的半径
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_CircleIndices2,
                        "all", "result_type", "radius", out hv_RadiusC2);
                    //提取矩形边的长度
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_RectIndices,
                        "all", "result_type", "length1", out hv_Length1R);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_RectIndices,
                        "all", "result_type", "length2", out hv_Length2R);
                    //得到每一条被测线的起点和终点
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_LineIndices.TupleSelect(
                        0), "all", "result_type", "all_param", out hv_ParamLine1);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_LineIndices.TupleSelect(
                        1), "all", "result_type", "all_param", out hv_ParamLine2);
                    //显示结果
                    HOperatorSet.DispObj(ho_CurrentImage, hv_ExpDefaultWinHandle);
                    HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 1);
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "light gray");
                    HOperatorSet.DispObj(ho_Contour, hv_ExpDefaultWinHandle);
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green");
                    HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 2);
                    HOperatorSet.DispObj(ho_ResultContours, hv_ExpDefaultWinHandle);
                    HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 1);
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "white");
                    HOperatorSet.DispObj(ho_UsedEdges, hv_ExpDefaultWinHandle);
                    //
                    //显示圆心处每个圆的半径获取圆心的公制坐标
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_CircleIndices1,
                        "all", "result_type", "x", out hv_XC1);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_CircleIndices1,
                        "all", "result_type", "y", out hv_YC1);
                    //将圆心的度量坐标投影到图像中,得到圆心的图像坐标
                    project_xy_to_image(hv_XC1, hv_YC1, hv_MeasurementPlaneAdjusted, hv_CameraParam,
                        out hv_Row1, out hv_Column1);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_CircleIndices2,
                        "all", "result_type", "x", out hv_XC2);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_CircleIndices2,
                        "all", "result_type", "y", out hv_YC2);
                    project_xy_to_image(hv_XC2, hv_YC2, hv_MeasurementPlaneAdjusted, hv_CameraParam,
                        out hv_Row2, out hv_Column2);
                    for (hv_J = 0; (int)hv_J <= (int)((new HTuple(hv_RadiusC1.TupleLength())) - 1); hv_J = (int)hv_J + 1)
                    {
                        disp_message(hv_ExpDefaultWinHandle, "r=" + ((((hv_RadiusC1.TupleSelect(hv_J)) * 1000)).TupleString(
                            ".2f")), "image", hv_Row1.TupleSelect(hv_J), (hv_Column1.TupleSelect(
                            hv_J)) - 80, "black", "true");
                    }
                    for (hv_J = 0; (int)hv_J <= (int)((new HTuple(hv_RadiusC2.TupleLength())) - 1); hv_J = (int)hv_J + 1)
                    {
                        disp_message(hv_ExpDefaultWinHandle, "r=" + ((((hv_RadiusC2.TupleSelect(hv_J)) * 1000)).TupleString(
                            ".2f")), "image", hv_Row2.TupleSelect(hv_J), (hv_Column2.TupleSelect(
                            hv_J)) - 80, "black", "true");
                    }
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_RectIndices,
                        "all", "result_type", "x", out hv_XRectangle);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_RectIndices,
                        "all", "result_type", "y", out hv_YRectangle);
                    project_xy_to_image(hv_XRectangle, hv_YRectangle, hv_MeasurementPlaneAdjusted,
                        hv_CameraParam, out hv_RowR, out hv_ColumnR);
                    hv_Area = (((hv_Length1R * hv_Length2R) * 4) * 1000) * 1000;
                    disp_message(hv_ExpDefaultWinHandle, "area=" + (hv_Area.TupleString(".2f")),
                        "image", hv_RowR, hv_ColumnR - 120, "black", "true");
                    hv_Message = "Measured metric results after alignment (r in mm, area in mm^2):";
                    //
                    disp_message(hv_ExpDefaultWinHandle, hv_Message, "window", 12, 12, "black",
                        "true");
                    if ((int)(new HTuple(hv_I.TupleLess(5))) != 0)
                    {
                        disp_continue_message(hv_ExpDefaultWinHandle, "black", "true");
                    }
                    HDevelopStop();
                }
                //清理内存
                HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle);
                if ((int)(new HTuple(hv_AlignmentMode.TupleEqual("shape-based matching"))) != 0)
                {
                    HOperatorSet.ClearShapeModel(hv_ShapeModelID);
                    HOperatorSet.SetSystem("border_shape_models", hv_BorderShapeModel);
                }
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_EmptyObject.Dispose();
                ho_Image.Dispose();
                ho_ModelContour.Dispose();
                ho_MeasureContour.Dispose();
                ho_Region.Dispose();
                ho_ModelRegion.Dispose();
                ho_ImageReduced.Dispose();
                ho_ShapeModelContours.Dispose();
                ho_CurrentImage.Dispose();
                ho_Contour.Dispose();
                ho_UsedEdges.Dispose();
                ho_ResultContours.Dispose();

                throw HDevExpDefaultException;
            }
            ho_EmptyObject.Dispose();
            ho_Image.Dispose();
            ho_ModelContour.Dispose();
            ho_MeasureContour.Dispose();
            ho_Region.Dispose();
            ho_ModelRegion.Dispose();
            ho_ImageReduced.Dispose();
            ho_ShapeModelContours.Dispose();
            ho_CurrentImage.Dispose();
            ho_Contour.Dispose();
            ho_UsedEdges.Dispose();
            ho_ResultContours.Dispose();
        }


        public void HDevelopStop()
        {
            MessageBox.Show("Press button to continue", "Program stop");
        }

        // Procedures 
        // External procedures 
        // Chapter: Matching / Shape-Based
        // Short Description: Display the results of Shape-Based Matching. 
        public void dev_display_shape_matching_results(HTuple hv_ModelID, HTuple hv_Color,
            HTuple hv_Row, HTuple hv_Column, HTuple hv_Angle, HTuple hv_ScaleR, HTuple hv_ScaleC,
            HTuple hv_Model)
        {



            // Local iconic variables 

            HObject ho_ModelContours = null, ho_ContoursAffinTrans = null;

            // Local control variables 

            HTuple hv_NumMatches = null, hv_Index = new HTuple();
            HTuple hv_Match = new HTuple(), hv_HomMat2DIdentity = new HTuple();
            HTuple hv_HomMat2DScale = new HTuple(), hv_HomMat2DRotate = new HTuple();
            HTuple hv_HomMat2DTranslate = new HTuple();
            HTuple hv_Model_COPY_INP_TMP = hv_Model.Clone();
            HTuple hv_ScaleC_COPY_INP_TMP = hv_ScaleC.Clone();
            HTuple hv_ScaleR_COPY_INP_TMP = hv_ScaleR.Clone();

            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_ModelContours);
            HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans);
            try
            {
                //This procedure displays the results of Shape-Based Matching.
                //
                hv_NumMatches = new HTuple(hv_Row.TupleLength());
                if ((int)(new HTuple(hv_NumMatches.TupleGreater(0))) != 0)
                {
                    if ((int)(new HTuple((new HTuple(hv_ScaleR_COPY_INP_TMP.TupleLength())).TupleEqual(
                        1))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, hv_ScaleR_COPY_INP_TMP, out hv_ScaleR_COPY_INP_TMP);
                    }
                    if ((int)(new HTuple((new HTuple(hv_ScaleC_COPY_INP_TMP.TupleLength())).TupleEqual(
                        1))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, hv_ScaleC_COPY_INP_TMP, out hv_ScaleC_COPY_INP_TMP);
                    }
                    if ((int)(new HTuple((new HTuple(hv_Model_COPY_INP_TMP.TupleLength())).TupleEqual(
                        0))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, 0, out hv_Model_COPY_INP_TMP);
                    }
                    else if ((int)(new HTuple((new HTuple(hv_Model_COPY_INP_TMP.TupleLength()
                        )).TupleEqual(1))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, hv_Model_COPY_INP_TMP, out hv_Model_COPY_INP_TMP);
                    }
                    for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_ModelID.TupleLength()
                        )) - 1); hv_Index = (int)hv_Index + 1)
                    {
                        ho_ModelContours.Dispose();
                        HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID.TupleSelect(
                            hv_Index), 1);
                        HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Color.TupleSelect(hv_Index % (new HTuple(hv_Color.TupleLength()
                            ))));
                        HTuple end_val18 = hv_NumMatches - 1;
                        HTuple step_val18 = 1;
                        for (hv_Match = 0; hv_Match.Continue(end_val18, step_val18); hv_Match = hv_Match.TupleAdd(step_val18))
                        {
                            if ((int)(new HTuple(hv_Index.TupleEqual(hv_Model_COPY_INP_TMP.TupleSelect(
                                hv_Match)))) != 0)
                            {
                                HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                                HOperatorSet.HomMat2dScale(hv_HomMat2DIdentity, hv_ScaleR_COPY_INP_TMP.TupleSelect(
                                    hv_Match), hv_ScaleC_COPY_INP_TMP.TupleSelect(hv_Match), 0, 0,
                                    out hv_HomMat2DScale);
                                HOperatorSet.HomMat2dRotate(hv_HomMat2DScale, hv_Angle.TupleSelect(
                                    hv_Match), 0, 0, out hv_HomMat2DRotate);
                                HOperatorSet.HomMat2dTranslate(hv_HomMat2DRotate, hv_Row.TupleSelect(
                                    hv_Match), hv_Column.TupleSelect(hv_Match), out hv_HomMat2DTranslate);
                                ho_ContoursAffinTrans.Dispose();
                                HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ContoursAffinTrans,
                                    hv_HomMat2DTranslate);
                                HOperatorSet.DispObj(ho_ContoursAffinTrans, hv_ExpDefaultWinHandle);
                            }
                        }
                    }
                }
                ho_ModelContours.Dispose();
                ho_ContoursAffinTrans.Dispose();

                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_ModelContours.Dispose();
                ho_ContoursAffinTrans.Dispose();

                throw HDevExpDefaultException;
            }
        }

        // Chapter: Develop
        // Short Description: Open a new graphics window that preserves the aspect ratio of the given image. 
        public void dev_open_window_fit_image(HObject ho_Image, HTuple hv_Row, HTuple hv_Column,
            HTuple hv_WidthLimit, HTuple hv_HeightLimit, out HTuple hv_WindowHandle)
        {




            // Local iconic variables 

            // Local control variables 

            HTuple hv_MinWidth = new HTuple(), hv_MaxWidth = new HTuple();
            HTuple hv_MinHeight = new HTuple(), hv_MaxHeight = new HTuple();
            HTuple hv_ResizeFactor = null, hv_ImageWidth = null, hv_ImageHeight = null;
            HTuple hv_TempWidth = null, hv_TempHeight = null, hv_WindowWidth = new HTuple();
            HTuple hv_WindowHeight = null;
            // Initialize local and output iconic variables 
            hv_WindowHandle = new HTuple();
            //This procedure opens a new graphics window and adjusts the size
            //such that it fits into the limits specified by WidthLimit
            //and HeightLimit, but also maintains the correct image aspect ratio.
            //
            //If it is impossible to match the minimum and maximum extent requirements
            //at the same time (f.e. if the image is very long but narrow),
            //the maximum value gets a higher priority,
            //
            //Parse input tuple WidthLimit
            if ((int)((new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual(0))).TupleOr(
                new HTuple(hv_WidthLimit.TupleLess(0)))) != 0)
            {
                hv_MinWidth = 500;
                hv_MaxWidth = 800;
            }
            else if ((int)(new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual(
                1))) != 0)
            {
                hv_MinWidth = 0;
                hv_MaxWidth = hv_WidthLimit.Clone();
            }
            else
            {
                hv_MinWidth = hv_WidthLimit[0];
                hv_MaxWidth = hv_WidthLimit[1];
            }
            //Parse input tuple HeightLimit
            if ((int)((new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual(0))).TupleOr(
                new HTuple(hv_HeightLimit.TupleLess(0)))) != 0)
            {
                hv_MinHeight = 400;
                hv_MaxHeight = 600;
            }
            else if ((int)(new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual(
                1))) != 0)
            {
                hv_MinHeight = 0;
                hv_MaxHeight = hv_HeightLimit.Clone();
            }
            else
            {
                hv_MinHeight = hv_HeightLimit[0];
                hv_MaxHeight = hv_HeightLimit[1];
            }
            //
            //Test, if window size has to be changed.
            hv_ResizeFactor = 1;
            HOperatorSet.GetImageSize(ho_Image, out hv_ImageWidth, out hv_ImageHeight);
            //First, expand window to the minimum extents (if necessary).
            if ((int)((new HTuple(hv_MinWidth.TupleGreater(hv_ImageWidth))).TupleOr(new HTuple(hv_MinHeight.TupleGreater(
                hv_ImageHeight)))) != 0)
            {
                hv_ResizeFactor = (((((hv_MinWidth.TupleReal()) / hv_ImageWidth)).TupleConcat(
                    (hv_MinHeight.TupleReal()) / hv_ImageHeight))).TupleMax();
            }
            hv_TempWidth = hv_ImageWidth * hv_ResizeFactor;
            hv_TempHeight = hv_ImageHeight * hv_ResizeFactor;
            //Then, shrink window to maximum extents (if necessary).
            if ((int)((new HTuple(hv_MaxWidth.TupleLess(hv_TempWidth))).TupleOr(new HTuple(hv_MaxHeight.TupleLess(
                hv_TempHeight)))) != 0)
            {
                hv_ResizeFactor = hv_ResizeFactor * ((((((hv_MaxWidth.TupleReal()) / hv_TempWidth)).TupleConcat(
                    (hv_MaxHeight.TupleReal()) / hv_TempHeight))).TupleMin());
            }
            hv_WindowWidth = hv_ImageWidth * hv_ResizeFactor;
            hv_WindowHeight = hv_ImageHeight * hv_ResizeFactor;
            //Resize window
            //dev_open_window(...);
            HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_ImageHeight - 1, hv_ImageWidth - 1);

            return;
        }

        // Chapter: Develop
        // Short Description: Switch dev_update_pc, dev_update_var and dev_update_window to 'off'. 
        public void dev_update_off()
        {

            // Initialize local and output iconic variables 
            //This procedure sets different update settings to 'off'.
            //This is useful to get the best performance and reduce overhead.
            //
            // dev_update_pc(...); only in hdevelop
            // dev_update_var(...); only in hdevelop
            // dev_update_window(...); only in hdevelop

            return;
        }

        // Chapter: Graphics / Text
        // Short Description: This procedure displays 'Click 'Run' to continue' in the lower right corner of the screen. 
        public void disp_continue_message(HTuple hv_WindowHandle, HTuple hv_Color, HTuple hv_Box)
        {



            // Local iconic variables 

            // Local control variables 

            HTuple hv_ContinueMessage = null, hv_Row = null;
            HTuple hv_Column = null, hv_Width = null, hv_Height = null;
            HTuple hv_Ascent = null, hv_Descent = null, hv_TextWidth = null;
            HTuple hv_TextHeight = null;
            // Initialize local and output iconic variables 
            //This procedure displays 'Press Run (F5) to continue' in the
            //lower right corner of the screen.
            //It uses the procedure disp_message.
            //
            //Input parameters:
            //WindowHandle: The window, where the text shall be displayed
            //Color: defines the text color.
            //   If set to '' or 'auto', the currently set color is used.
            //Box: If set to 'true', the text is displayed in a box.
            //
            hv_ContinueMessage = "Press Run (F5) to continue";
            HOperatorSet.GetWindowExtents(hv_ExpDefaultWinHandle, out hv_Row, out hv_Column,
                out hv_Width, out hv_Height);
            HOperatorSet.GetStringExtents(hv_ExpDefaultWinHandle, (" " + hv_ContinueMessage) + " ",
                out hv_Ascent, out hv_Descent, out hv_TextWidth, out hv_TextHeight);
            disp_message(hv_ExpDefaultWinHandle, hv_ContinueMessage, "window", (hv_Height - hv_TextHeight) - 12,
                (hv_Width - hv_TextWidth) - 12, hv_Color, hv_Box);

            return;
        }

        // Chapter: Graphics / Text
        // Short Description: This procedure writes a text message. 
        public void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
            HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
        {



            // Local iconic variables 

            // Local control variables 

            HTuple hv_Red = null, hv_Green = null, hv_Blue = null;
            HTuple hv_Row1Part = null, hv_Column1Part = null, hv_Row2Part = null;
            HTuple hv_Column2Part = null, hv_RowWin = null, hv_ColumnWin = null;
            HTuple hv_WidthWin = new HTuple(), hv_HeightWin = null;
            HTuple hv_MaxAscent = null, hv_MaxDescent = null, hv_MaxWidth = null;
            HTuple hv_MaxHeight = null, hv_R1 = new HTuple(), hv_C1 = new HTuple();
            HTuple hv_FactorRow = new HTuple(), hv_FactorColumn = new HTuple();
            HTuple hv_UseShadow = null, hv_ShadowColor = null, hv_Exception = new HTuple();
            HTuple hv_Width = new HTuple(), hv_Index = new HTuple();
            HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
            HTuple hv_W = new HTuple(), hv_H = new HTuple(), hv_FrameHeight = new HTuple();
            HTuple hv_FrameWidth = new HTuple(), hv_R2 = new HTuple();
            HTuple hv_C2 = new HTuple(), hv_DrawMode = new HTuple();
            HTuple hv_CurrentColor = new HTuple();
            HTuple hv_Box_COPY_INP_TMP = hv_Box.Clone();
            HTuple hv_Color_COPY_INP_TMP = hv_Color.Clone();
            HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone();
            HTuple hv_Row_COPY_INP_TMP = hv_Row.Clone();
            HTuple hv_String_COPY_INP_TMP = hv_String.Clone();

            // Initialize local and output iconic variables 
            //This procedure displays text in a graphics window.
            //
            //Input parameters:
            //WindowHandle: The WindowHandle of the graphics window, where
            //   the message should be displayed
            //String: A tuple of strings containing the text message to be displayed
            //CoordSystem: If set to 'window', the text position is given
            //   with respect to the window coordinate system.
            //   If set to 'image', image coordinates are used.
            //   (This may be useful in zoomed images.)
            //Row: The row coordinate of the desired text position
            //   If set to -1, a default value of 12 is used.
            //Column: The column coordinate of the desired text position
            //   If set to -1, a default value of 12 is used.
            //Color: defines the color of the text as string.
            //   If set to [], '' or 'auto' the currently set color is used.
            //   If a tuple of strings is passed, the colors are used cyclically
            //   for each new textline.
            //Box: If Box[0] is set to 'true', the text is written within an orange box.
            //     If set to' false', no box is displayed.
            //     If set to a color string (e.g. 'white', '#FF00CC', etc.),
            //       the text is written in a box of that color.
            //     An optional second value for Box (Box[1]) controls if a shadow is displayed:
            //       'true' -> display a shadow in a default color
            //       'false' -> display no shadow (same as if no second value is given)
            //       otherwise -> use given string as color string for the shadow color
            //
            //Prepare window
            HOperatorSet.GetRgb(hv_ExpDefaultWinHandle, out hv_Red, out hv_Green, out hv_Blue);
            HOperatorSet.GetPart(hv_ExpDefaultWinHandle, out hv_Row1Part, out hv_Column1Part,
                out hv_Row2Part, out hv_Column2Part);
            HOperatorSet.GetWindowExtents(hv_ExpDefaultWinHandle, out hv_RowWin, out hv_ColumnWin,
                out hv_WidthWin, out hv_HeightWin);
            HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1);
            //
            //default settings
            if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0)
            {
                hv_Row_COPY_INP_TMP = 12;
            }
            if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0)
            {
                hv_Column_COPY_INP_TMP = 12;
            }
            if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
            {
                hv_Color_COPY_INP_TMP = "";
            }
            //
            hv_String_COPY_INP_TMP = ((("" + hv_String_COPY_INP_TMP) + "")).TupleSplit("\n");
            //
            //Estimate extentions of text depending on font size.
            HOperatorSet.GetFontExtents(hv_ExpDefaultWinHandle, out hv_MaxAscent, out hv_MaxDescent,
                out hv_MaxWidth, out hv_MaxHeight);
            if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0)
            {
                hv_R1 = hv_Row_COPY_INP_TMP.Clone();
                hv_C1 = hv_Column_COPY_INP_TMP.Clone();
            }
            else
            {
                //Transform image to window coordinates
                hv_FactorRow = (1.0 * hv_HeightWin) / ((hv_Row2Part - hv_Row1Part) + 1);
                hv_FactorColumn = (1.0 * hv_WidthWin) / ((hv_Column2Part - hv_Column1Part) + 1);
                hv_R1 = ((hv_Row_COPY_INP_TMP - hv_Row1Part) + 0.5) * hv_FactorRow;
                hv_C1 = ((hv_Column_COPY_INP_TMP - hv_Column1Part) + 0.5) * hv_FactorColumn;
            }
            //
            //Display text box depending on text size
            hv_UseShadow = 1;
            hv_ShadowColor = "gray";
            if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleEqual("true"))) != 0)
            {
                if (hv_Box_COPY_INP_TMP == null)
                    hv_Box_COPY_INP_TMP = new HTuple();
                hv_Box_COPY_INP_TMP[0] = "#fce9d4";
                hv_ShadowColor = "#f28d26";
            }
            if ((int)(new HTuple((new HTuple(hv_Box_COPY_INP_TMP.TupleLength())).TupleGreater(
                1))) != 0)
            {
                if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual("true"))) != 0)
                {
                    //Use default ShadowColor set above
                }
                else if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual(
                    "false"))) != 0)
                {
                    hv_UseShadow = 0;
                }
                else
                {
                    hv_ShadowColor = hv_Box_COPY_INP_TMP[1];
                    //Valid color?
                    try
                    {
                        HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Box_COPY_INP_TMP.TupleSelect(
                            1));
                    }
                    // catch (Exception) 
                    catch (HalconException HDevExpDefaultException1)
                    {
                        HDevExpDefaultException1.ToHTuple(out hv_Exception);
                        hv_Exception = "Wrong value of control parameter Box[1] (must be a 'true', 'false', or a valid color string)";
                        throw new HalconException(hv_Exception);
                    }
                }
            }
            if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleNotEqual("false"))) != 0)
            {
                //Valid color?
                try
                {
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Box_COPY_INP_TMP.TupleSelect(
                        0));
                }
                // catch (Exception) 
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    hv_Exception = "Wrong value of control parameter Box[0] (must be a 'true', 'false', or a valid color string)";
                    throw new HalconException(hv_Exception);
                }
                //Calculate box extents
                hv_String_COPY_INP_TMP = (" " + hv_String_COPY_INP_TMP) + " ";
                hv_Width = new HTuple();
                for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                    )) - 1); hv_Index = (int)hv_Index + 1)
                {
                    HOperatorSet.GetStringExtents(hv_ExpDefaultWinHandle, hv_String_COPY_INP_TMP.TupleSelect(
                        hv_Index), out hv_Ascent, out hv_Descent, out hv_W, out hv_H);
                    hv_Width = hv_Width.TupleConcat(hv_W);
                }
                hv_FrameHeight = hv_MaxHeight * (new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                    ));
                hv_FrameWidth = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax();
                hv_R2 = hv_R1 + hv_FrameHeight;
                hv_C2 = hv_C1 + hv_FrameWidth;
                //Display rectangles
                HOperatorSet.GetDraw(hv_ExpDefaultWinHandle, out hv_DrawMode);
                HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "fill");
                //Set shadow color
                HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_ShadowColor);
                if ((int)(hv_UseShadow) != 0)
                {
                    HOperatorSet.DispRectangle1(hv_ExpDefaultWinHandle, hv_R1 + 1, hv_C1 + 1, hv_R2 + 1,
                        hv_C2 + 1);
                }
                //Set box color
                HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Box_COPY_INP_TMP.TupleSelect(
                    0));
                HOperatorSet.DispRectangle1(hv_ExpDefaultWinHandle, hv_R1, hv_C1, hv_R2, hv_C2);
                HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, hv_DrawMode);
            }
            //Write text.
            for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                )) - 1); hv_Index = (int)hv_Index + 1)
            {
                hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
                    )));
                if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual(
                    "auto")))) != 0)
                {
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_CurrentColor);
                }
                else
                {
                    HOperatorSet.SetRgb(hv_ExpDefaultWinHandle, hv_Red, hv_Green, hv_Blue);
                }
                hv_Row_COPY_INP_TMP = hv_R1 + (hv_MaxHeight * hv_Index);
                HOperatorSet.SetTposition(hv_ExpDefaultWinHandle, hv_Row_COPY_INP_TMP, hv_C1);
                HOperatorSet.WriteString(hv_ExpDefaultWinHandle, hv_String_COPY_INP_TMP.TupleSelect(
                    hv_Index));
            }
            //Reset changed window settings
            HOperatorSet.SetRgb(hv_ExpDefaultWinHandle, hv_Red, hv_Green, hv_Blue);
            HOperatorSet.SetPart(hv_ExpDefaultWinHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part,
                hv_Column2Part);

            return;
        }

        // Chapter: Graphics / Text
        // Short Description: Set font independent of OS 
        public void set_display_font(HTuple hv_WindowHandle, HTuple hv_Size, HTuple hv_Font,
            HTuple hv_Bold, HTuple hv_Slant)
        {



            // Local iconic variables 

            // Local control variables 

            HTuple hv_OS = null, hv_BufferWindowHandle = new HTuple();
            HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_Scale = new HTuple(), hv_Exception = new HTuple();
            HTuple hv_SubFamily = new HTuple(), hv_Fonts = new HTuple();
            HTuple hv_SystemFonts = new HTuple(), hv_Guess = new HTuple();
            HTuple hv_I = new HTuple(), hv_Index = new HTuple(), hv_AllowedFontSizes = new HTuple();
            HTuple hv_Distances = new HTuple(), hv_Indices = new HTuple();
            HTuple hv_FontSelRegexp = new HTuple(), hv_FontsCourier = new HTuple();
            HTuple hv_Bold_COPY_INP_TMP = hv_Bold.Clone();
            HTuple hv_Font_COPY_INP_TMP = hv_Font.Clone();
            HTuple hv_Size_COPY_INP_TMP = hv_Size.Clone();
            HTuple hv_Slant_COPY_INP_TMP = hv_Slant.Clone();

            // Initialize local and output iconic variables 
            //This procedure sets the text font of the current window with
            //the specified attributes.
            //It is assumed that following fonts are installed on the system:
            //Windows: Courier New, Arial Times New Roman
            //Mac OS X: CourierNewPS, Arial, TimesNewRomanPS
            //Linux: courier, helvetica, times
            //Because fonts are displayed smaller on Linux than on Windows,
            //a scaling factor of 1.25 is used the get comparable results.
            //For Linux, only a limited number of font sizes is supported,
            //to get comparable results, it is recommended to use one of the
            //following sizes: 9, 11, 14, 16, 20, 27
            //(which will be mapped internally on Linux systems to 11, 14, 17, 20, 25, 34)
            //
            //Input parameters:
            //WindowHandle: The graphics window for which the font will be set
            //Size: The font size. If Size=-1, the default of 16 is used.
            //Bold: If set to 'true', a bold font is used
            //Slant: If set to 'true', a slanted font is used
            //
            HOperatorSet.GetSystem("operating_system", out hv_OS);
            // dev_get_preferences(...); only in hdevelop
            // dev_set_preferences(...); only in hdevelop
            if ((int)((new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
                new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(-1)))) != 0)
            {
                hv_Size_COPY_INP_TMP = 16;
            }
            if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Win"))) != 0)
            {
                //Set font on Windows systems
                try
                {
                    //Check, if font scaling is switched on
                    //open_window(...);
                    HOperatorSet.SetFont(hv_ExpDefaultWinHandle, "-Consolas-16-*-0-*-*-1-");
                    HOperatorSet.GetStringExtents(hv_ExpDefaultWinHandle, "test_string", out hv_Ascent,
                        out hv_Descent, out hv_Width, out hv_Height);
                    //Expected width is 110
                    hv_Scale = 110.0 / hv_Width;
                    hv_Size_COPY_INP_TMP = ((hv_Size_COPY_INP_TMP * hv_Scale)).TupleInt();
                    //close_window(...);
                }
                // catch (Exception) 
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    //throw (Exception)
                }
                if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))).TupleOr(
                    new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("courier")))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "Courier New";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "Consolas";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "Arial";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "Times New Roman";
                }
                if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    hv_Bold_COPY_INP_TMP = 1;
                }
                else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0)
                {
                    hv_Bold_COPY_INP_TMP = 0;
                }
                else
                {
                    hv_Exception = "Wrong value of control parameter Bold";
                    throw new HalconException(hv_Exception);
                }
                if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    hv_Slant_COPY_INP_TMP = 1;
                }
                else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0)
                {
                    hv_Slant_COPY_INP_TMP = 0;
                }
                else
                {
                    hv_Exception = "Wrong value of control parameter Slant";
                    throw new HalconException(hv_Exception);
                }
                try
                {
                    HOperatorSet.SetFont(hv_ExpDefaultWinHandle, ((((((("-" + hv_Font_COPY_INP_TMP) + "-") + hv_Size_COPY_INP_TMP) + "-*-") + hv_Slant_COPY_INP_TMP) + "-*-*-") + hv_Bold_COPY_INP_TMP) + "-");
                }
                // catch (Exception) 
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    //throw (Exception)
                }
            }
            else if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Dar"))) != 0)
            {
                //Set font on Mac OS X systems. Since OS X does not have a strict naming
                //scheme for font attributes, we use tables to determine the correct font
                //name.
                hv_SubFamily = 0;
                if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    hv_SubFamily = hv_SubFamily.TupleBor(1);
                }
                else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleNotEqual("false"))) != 0)
                {
                    hv_Exception = "Wrong value of control parameter Slant";
                    throw new HalconException(hv_Exception);
                }
                if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    hv_SubFamily = hv_SubFamily.TupleBor(2);
                }
                else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleNotEqual("false"))) != 0)
                {
                    hv_Exception = "Wrong value of control parameter Bold";
                    throw new HalconException(hv_Exception);
                }
                if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))) != 0)
                {
                    hv_Fonts = new HTuple();
                    hv_Fonts[0] = "Menlo-Regular";
                    hv_Fonts[1] = "Menlo-Italic";
                    hv_Fonts[2] = "Menlo-Bold";
                    hv_Fonts[3] = "Menlo-BoldItalic";
                }
                else if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))).TupleOr(
                    new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("courier")))) != 0)
                {
                    hv_Fonts = new HTuple();
                    hv_Fonts[0] = "CourierNewPSMT";
                    hv_Fonts[1] = "CourierNewPS-ItalicMT";
                    hv_Fonts[2] = "CourierNewPS-BoldMT";
                    hv_Fonts[3] = "CourierNewPS-BoldItalicMT";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
                {
                    hv_Fonts = new HTuple();
                    hv_Fonts[0] = "ArialMT";
                    hv_Fonts[1] = "Arial-ItalicMT";
                    hv_Fonts[2] = "Arial-BoldMT";
                    hv_Fonts[3] = "Arial-BoldItalicMT";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
                {
                    hv_Fonts = new HTuple();
                    hv_Fonts[0] = "TimesNewRomanPSMT";
                    hv_Fonts[1] = "TimesNewRomanPS-ItalicMT";
                    hv_Fonts[2] = "TimesNewRomanPS-BoldMT";
                    hv_Fonts[3] = "TimesNewRomanPS-BoldItalicMT";
                }
                else
                {
                    //Attempt to figure out which of the fonts installed on the system
                    //the user could have meant.
                    HOperatorSet.QueryFont(hv_ExpDefaultWinHandle, out hv_SystemFonts);
                    hv_Fonts = new HTuple();
                    hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP);
                    hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP);
                    hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP);
                    hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP);
                    hv_Guess = new HTuple();
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP);
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Regular");
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "MT");
                    for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1)
                    {
                        HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index);
                        if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0)
                        {
                            if (hv_Fonts == null)
                                hv_Fonts = new HTuple();
                            hv_Fonts[0] = hv_Guess.TupleSelect(hv_I);
                            break;
                        }
                    }
                    //Guess name of slanted font
                    hv_Guess = new HTuple();
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Italic");
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-ItalicMT");
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Oblique");
                    for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1)
                    {
                        HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index);
                        if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0)
                        {
                            if (hv_Fonts == null)
                                hv_Fonts = new HTuple();
                            hv_Fonts[1] = hv_Guess.TupleSelect(hv_I);
                            break;
                        }
                    }
                    //Guess name of bold font
                    hv_Guess = new HTuple();
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Bold");
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldMT");
                    for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1)
                    {
                        HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index);
                        if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0)
                        {
                            if (hv_Fonts == null)
                                hv_Fonts = new HTuple();
                            hv_Fonts[2] = hv_Guess.TupleSelect(hv_I);
                            break;
                        }
                    }
                    //Guess name of bold slanted font
                    hv_Guess = new HTuple();
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldItalic");
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldItalicMT");
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldOblique");
                    for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1)
                    {
                        HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index);
                        if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0)
                        {
                            if (hv_Fonts == null)
                                hv_Fonts = new HTuple();
                            hv_Fonts[3] = hv_Guess.TupleSelect(hv_I);
                            break;
                        }
                    }
                }
                hv_Font_COPY_INP_TMP = hv_Fonts.TupleSelect(hv_SubFamily);
                try
                {
                    HOperatorSet.SetFont(hv_ExpDefaultWinHandle, (hv_Font_COPY_INP_TMP + "-") + hv_Size_COPY_INP_TMP);
                }
                // catch (Exception) 
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    //throw (Exception)
                }
            }
            else
            {
                //Set font for UNIX systems
                hv_Size_COPY_INP_TMP = hv_Size_COPY_INP_TMP * 1.25;
                hv_AllowedFontSizes = new HTuple();
                hv_AllowedFontSizes[0] = 11;
                hv_AllowedFontSizes[1] = 14;
                hv_AllowedFontSizes[2] = 17;
                hv_AllowedFontSizes[3] = 20;
                hv_AllowedFontSizes[4] = 25;
                hv_AllowedFontSizes[5] = 34;
                if ((int)(new HTuple(((hv_AllowedFontSizes.TupleFind(hv_Size_COPY_INP_TMP))).TupleEqual(
                    -1))) != 0)
                {
                    hv_Distances = ((hv_AllowedFontSizes - hv_Size_COPY_INP_TMP)).TupleAbs();
                    HOperatorSet.TupleSortIndex(hv_Distances, out hv_Indices);
                    hv_Size_COPY_INP_TMP = hv_AllowedFontSizes.TupleSelect(hv_Indices.TupleSelect(
                        0));
                }
                if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))).TupleOr(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual(
                    "Courier")))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "courier";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "helvetica";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "times";
                }
                if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    hv_Bold_COPY_INP_TMP = "bold";
                }
                else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0)
                {
                    hv_Bold_COPY_INP_TMP = "medium";
                }
                else
                {
                    hv_Exception = "Wrong value of control parameter Bold";
                    throw new HalconException(hv_Exception);
                }
                if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("times"))) != 0)
                    {
                        hv_Slant_COPY_INP_TMP = "i";
                    }
                    else
                    {
                        hv_Slant_COPY_INP_TMP = "o";
                    }
                }
                else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0)
                {
                    hv_Slant_COPY_INP_TMP = "r";
                }
                else
                {
                    hv_Exception = "Wrong value of control parameter Slant";
                    throw new HalconException(hv_Exception);
                }
                try
                {
                    HOperatorSet.SetFont(hv_ExpDefaultWinHandle, ((((((("-adobe-" + hv_Font_COPY_INP_TMP) + "-") + hv_Bold_COPY_INP_TMP) + "-") + hv_Slant_COPY_INP_TMP) + "-normal-*-") + hv_Size_COPY_INP_TMP) + "-*-*-*-*-*-*-*");
                }
                // catch (Exception) 
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    if ((int)((new HTuple(((hv_OS.TupleSubstr(0, 4))).TupleEqual("Linux"))).TupleAnd(
                        new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("courier")))) != 0)
                    {
                        HOperatorSet.QueryFont(hv_ExpDefaultWinHandle, out hv_Fonts);
                        hv_FontSelRegexp = (("^-[^-]*-[^-]*[Cc]ourier[^-]*-" + hv_Bold_COPY_INP_TMP) + "-") + hv_Slant_COPY_INP_TMP;
                        hv_FontsCourier = ((hv_Fonts.TupleRegexpSelect(hv_FontSelRegexp))).TupleRegexpMatch(
                            hv_FontSelRegexp);
                        if ((int)(new HTuple((new HTuple(hv_FontsCourier.TupleLength())).TupleEqual(
                            0))) != 0)
                        {
                            hv_Exception = "Wrong font name";
                            //throw (Exception)
                        }
                        else
                        {
                            try
                            {
                                HOperatorSet.SetFont(hv_ExpDefaultWinHandle, (((hv_FontsCourier.TupleSelect(
                                    0)) + "-normal-*-") + hv_Size_COPY_INP_TMP) + "-*-*-*-*-*-*-*");
                            }
                            // catch (Exception) 
                            catch (HalconException HDevExpDefaultException2)
                            {
                                HDevExpDefaultException2.ToHTuple(out hv_Exception);
                                //throw (Exception)
                            }
                        }
                    }
                    //throw (Exception)
                }
            }
            // dev_set_preferences(...); only in hdevelop

            return;
        }

        // Local procedures 
        public void project_xy_to_image(HTuple hv_XC1, HTuple hv_YC1, HTuple hv_MeasurementPlaneAdjusted,
            HTuple hv_CameraParam, out HTuple hv_Row1, out HTuple hv_Column1)
        {



            // Local control variables 

            HTuple hv_HomMat3D = null, hv_Qx = null, hv_Qy = null;
            HTuple hv_Qz = null;
            // Initialize local and output iconic variables 
            HOperatorSet.PoseToHomMat3d(hv_MeasurementPlaneAdjusted, out hv_HomMat3D);
            HOperatorSet.AffineTransPoint3d(hv_HomMat3D, hv_XC1, hv_YC1, 0 * hv_XC1, out hv_Qx,
                out hv_Qy, out hv_Qz);
            HOperatorSet.Project3dPoint(hv_Qx, hv_Qy, hv_Qz, hv_CameraParam, out hv_Row1,
                out hv_Column1);

            return;
        }

        public void show_contours(HObject ho_Image, HObject ho_ModelContour, HObject ho_MeasureContour,
            HObject ho_ShapeModelContours, HTuple hv_WindowHandle, HTuple hv_Message)
        {




            // Local control variables 

            HTuple hv_Number = null;
            // Initialize local and output iconic variables 
            HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle);
            HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
            HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 2);
            HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "blue");
            HOperatorSet.DispObj(ho_ModelContour, hv_ExpDefaultWinHandle);
            HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 1);
            HOperatorSet.CountObj(ho_MeasureContour, out hv_Number);
            //Determine if the measure contours should be shown
            if ((int)(new HTuple(hv_Number.TupleGreater(0))) != 0)
            {
                HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "gray");
                HOperatorSet.DispObj(ho_MeasureContour, hv_ExpDefaultWinHandle);
            }
            HOperatorSet.CountObj(ho_ShapeModelContours, out hv_Number);
            if ((int)(new HTuple(hv_Number.TupleGreater(0))) != 0)
            {
                HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "white");
                HOperatorSet.DispObj(ho_ShapeModelContours, hv_ExpDefaultWinHandle);
            }
            //
            disp_message(hv_ExpDefaultWinHandle, hv_Message, "window", 12, 12, "black", "true");
            disp_continue_message(hv_ExpDefaultWinHandle, "black", "true");

            return;
        }

        public void extract_reference_points(HObject ho_Image, out HTuple hv_Rows, out HTuple hv_Columns)
        {



            // Local iconic variables 

            HObject ho_Region, ho_ConnectedRegions, ho_Rectangle;
            HObject ho_Snake, ho_QuarterCircles, ho_QuarterCirclesUnion;
            HObject ho_Star;

            // Local control variables 

            HTuple hv_Area1 = null, hv_Row1 = null, hv_Column1 = null;
            HTuple hv_Area2 = null, hv_Row2 = null, hv_Column2 = null;
            HTuple hv_Area3 = null, hv_Row3 = null, hv_Column3 = null;
            HTuple hv_Area4 = null, hv_Row4 = null, hv_Column4 = null;
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_Snake);
            HOperatorSet.GenEmptyObj(out ho_QuarterCircles);
            HOperatorSet.GenEmptyObj(out ho_QuarterCirclesUnion);
            HOperatorSet.GenEmptyObj(out ho_Star);
            try
            {
                //Extract reference points as center points of uniquely
                //identifiable regions.
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_Image, out ho_Region, 50, 255);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);
                ho_Rectangle.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_Rectangle, "num_sides",
                    "and", 3.5, 4.5);
                ho_Snake.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_Snake, "area", "and",
                    3000, 5000);
                ho_QuarterCircles.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_QuarterCircles, "area",
                    "and", 1000, 3000);
                ho_QuarterCirclesUnion.Dispose();
                HOperatorSet.Union1(ho_QuarterCircles, out ho_QuarterCirclesUnion);
                ho_Star.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_Star, ((new HTuple("area")).TupleConcat(
                    "outer_radius")).TupleConcat("num_sides"), "and", ((new HTuple(7500)).TupleConcat(
                    70)).TupleConcat(3), ((new HTuple(10000)).TupleConcat(90)).TupleConcat(
                    5));
                HOperatorSet.AreaCenter(ho_Star, out hv_Area1, out hv_Row1, out hv_Column1);
                HOperatorSet.AreaCenter(ho_Rectangle, out hv_Area2, out hv_Row2, out hv_Column2);
                HOperatorSet.AreaCenter(ho_Snake, out hv_Area3, out hv_Row3, out hv_Column3);
                HOperatorSet.AreaCenter(ho_QuarterCirclesUnion, out hv_Area4, out hv_Row4,
                    out hv_Column4);
                hv_Rows = new HTuple();
                hv_Rows = hv_Rows.TupleConcat(hv_Row1);
                hv_Rows = hv_Rows.TupleConcat(hv_Row2);
                hv_Rows = hv_Rows.TupleConcat(hv_Row3);
                hv_Rows = hv_Rows.TupleConcat(hv_Row4);
                hv_Columns = new HTuple();
                hv_Columns = hv_Columns.TupleConcat(hv_Column1);
                hv_Columns = hv_Columns.TupleConcat(hv_Column2);
                hv_Columns = hv_Columns.TupleConcat(hv_Column3);
                hv_Columns = hv_Columns.TupleConcat(hv_Column4);
                ho_Region.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_Rectangle.Dispose();
                ho_Snake.Dispose();
                ho_QuarterCircles.Dispose();
                ho_QuarterCirclesUnion.Dispose();
                ho_Star.Dispose();

                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_Region.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_Rectangle.Dispose();
                ho_Snake.Dispose();
                ho_QuarterCircles.Dispose();
                ho_QuarterCirclesUnion.Dispose();
                ho_Star.Dispose();

                throw HDevExpDefaultException;
            }
        }

    }
}

方法二

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace add_metrology_object_generic
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        

        private void button1_Click_1(object sender, EventArgs e)
        {
            uchalcon1.Draw();
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;

namespace add_metrology_object_generic
{

    
    public partial class uchalcon : UserControl
    {
        HTuple hv_ExpDefaultWinHandle;
        public uchalcon()
        {
            
            InitializeComponent();
            hv_ExpDefaultWinHandle = hWindowControl1.HalconWindow;
            
        }
        public void Draw()
        {


            // Local iconic variables 

            HObject ho_EmptyObject, ho_Image, ho_ModelContour;
            HObject ho_MeasureContour, ho_Region = null, ho_ModelRegion = null;
            HObject ho_ImageReduced = null, ho_ShapeModelContours = null;
            HObject ho_CurrentImage = null, ho_Contour = null, ho_UsedEdges = null;
            HObject ho_ResultContours = null;

            // Local control variables 

            HTuple hv_AlignmentMode = null, hv_Width = null;
            HTuple hv_Height = null, hv_WindowHandle = new HTuple();
            HTuple hv_CameraParam = null, hv_MeasurementPlane = null;
            HTuple hv_CalibPlateThickness = null, hv_PartHeight = null;
            HTuple hv_AdjustThickness = null, hv_MeasurementPlaneAdjusted = null;
            HTuple hv_MetrologyHandle = null, hv_CircleParam = null;
            HTuple hv_Row3 = null, hv_Column3 = null, hv_Radius = null;
            HTuple hv_Row4 = null, hv_Column4 = null, hv_Radius1 = null;
            HTuple hv_Row5 = null, hv_Column5 = null, hv_Radius2 = null;
            HTuple hv_Row6 = null, hv_Column6 = null, hv_Radius3 = null;
            HTuple hv_CircleIndices1 = null, hv_Row8 = null, hv_Column8 = null;
            HTuple hv_Radius5 = null, hv_Row7 = null, hv_Column7 = null;
            HTuple hv_Radius4 = null, hv_CircleIndices2 = null, hv_Index3 = null;
            HTuple hv_RectangleParam = null, hv_Row9 = null, hv_Column9 = null;
            HTuple hv_Phi = null, hv_Length1 = null, hv_Length2 = null;
            HTuple hv_RectIndices = null, hv_Line1 = null, hv_Line2 = null;
            HTuple hv_Row11 = null, hv_Column11 = null, hv_Row21 = null;
            HTuple hv_Column21 = null, hv_Row12 = null, hv_Column12 = null;
            HTuple hv_Row22 = null, hv_Column22 = null, hv_LineIndices = null;
            HTuple hv_Row = null, hv_Column = null, hv_Message = null;
            HTuple hv_BorderShapeModel = new HTuple(), hv_ShapeModelID = new HTuple();
            HTuple hv_Area = new HTuple(), hv_RowModel = new HTuple();
            HTuple hv_ColumnModel = new HTuple(), hv_I = null, hv_RowAlign = new HTuple();
            HTuple hv_ColumnAlign = new HTuple(), hv_AngleAlign = new HTuple();
            HTuple hv_Score = new HTuple(), hv_UsedRow = new HTuple();
            HTuple hv_UsedColumn = new HTuple(), hv_RadiusC1 = new HTuple();
            HTuple hv_RadiusC2 = new HTuple(), hv_Length1R = new HTuple();
            HTuple hv_Length2R = new HTuple(), hv_ParamLine1 = new HTuple();
            HTuple hv_ParamLine2 = new HTuple(), hv_XC1 = new HTuple();
            HTuple hv_YC1 = new HTuple(), hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
            HTuple hv_XC2 = new HTuple(), hv_YC2 = new HTuple(), hv_Row2 = new HTuple();
            HTuple hv_Column2 = new HTuple(), hv_J = new HTuple();
            HTuple hv_XRectangle = new HTuple(), hv_YRectangle = new HTuple();
            HTuple hv_RowR = new HTuple(), hv_ColumnR = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_EmptyObject);
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_ModelContour);
            HOperatorSet.GenEmptyObj(out ho_MeasureContour);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ModelRegion);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_ShapeModelContours);
            HOperatorSet.GenEmptyObj(out ho_CurrentImage);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_UsedEdges);
            HOperatorSet.GenEmptyObj(out ho_ResultContours);
            try
            {
                //这个程序展示了如何使用计量模型来获得平面上各种几何形状的校准测量值。
                //几何形态参数由模型图像导出。
                //要在新图像中找到要测量的对象,三个不同的对齐方法如下:
                //a)使用基于形状的匹配
                //b)使用区域处理
                //c)使用点对应的刚性变换
                //测量结果以等高线形式返回
                //作为给定几何对象的调整参数
                //
                //对象通过激活所需的线来打开对齐方法
                hv_AlignmentMode = "shape-based matching";
                //AlignmentMode := 'region processing'
                //AlignmentMode := 'rigid transformation'
                //AlignmentMode:=“基于形状的匹配”
                //AlignmentMode:=“区域处理”
                //AlignmentMode:=“刚性变换”
                //
                //初始化
                dev_update_off();
                //dev_close_window(...);
                HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin");
                ho_EmptyObject.Dispose();
                HOperatorSet.GenEmptyObj(out ho_EmptyObject);
                ho_Image.Dispose();
                HOperatorSet.ReadImage(out ho_Image, "metal-parts/circle_plate_01");
                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle);
                set_display_font(hv_ExpDefaultWinHandle, 16, "mono", "true", "false");
                //设备更新关闭()
                //开发关闭窗口()
                //设置绘图边缘
                //生成空对象(空对象)
                //读取图像(图像,“ 地址 ”)
                //获取图像大小(图像、宽度、高度)
                //按指定图像的大小来打开一个窗口(目标图片,新窗口左上角列标,新窗口宽度高度限制,新图形窗口的窗口句柄)
                //设置显示字体(窗口句柄,字体大小,字体,粗体,斜体)

                //Part I:
                //
                //**初始化计量模型
                //定义相机参数
                hv_CameraParam = new HTuple();
                hv_CameraParam[0] = 0.0128649;
                hv_CameraParam[1] = -661.434;
                hv_CameraParam[2] = 5.30004e-006;
                hv_CameraParam[3] = 5.3e-006;
                hv_CameraParam[4] = 620.043;
                hv_CameraParam[5] = 497.402;
                hv_CameraParam = hv_CameraParam.TupleConcat(hv_Width);
                hv_CameraParam = hv_CameraParam.TupleConcat(hv_Height);
                //测量平面的姿态是通过在被测物体的平面上放置一个校准板并通过确定其姿态来获得的
                hv_MeasurementPlane = new HTuple();
                hv_MeasurementPlane[0] = 0.00940956;
                hv_MeasurementPlane[1] = -0.00481017;
                hv_MeasurementPlane[2] = 0.29128;
                hv_MeasurementPlane[3] = 0.478648;
                hv_MeasurementPlane[4] = 359.65;
                hv_MeasurementPlane[5] = 0.785;
                hv_MeasurementPlane[6] = 0;
                //测量模板

                //*根据零件高度和校准板高度调整测量平面的姿态
                //校准板的厚度
                hv_CalibPlateThickness = 0.006;
                //校准板厚度:=0.006

                //被测物体的厚度
                hv_PartHeight = 0.005;
                hv_AdjustThickness = hv_CalibPlateThickness - hv_PartHeight;
                //零件高度:=0.005
                //调整厚度:=校准板厚度-零件高度
                HOperatorSet.SetOriginPose(hv_MeasurementPlane, 0, 0, hv_AdjustThickness, out hv_MeasurementPlaneAdjusted);
                //设置原点姿态(测量平面,0,0,调整厚度,测量平面调整)

                //创建计量模型并为校准测量做好准备
                HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);

                //提前设置图像大小,以加速第一次调用apply_metrology_model.
                HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);
                //设置相机参数
                HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "camera_param", hv_CameraParam);
                //设置被测物体所在的平面
                HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "plane_pose", hv_MeasurementPlaneAdjusted);
                //
                //将待测对象添加到计量模型中
                //
                //添加几个完整圆
                //CircleParam := [354,274,53]
                //CircleParam := [CircleParam,350,519,53]
                //CircleParam := [CircleParam,345,764,52]
                //CircleParam := [CircleParam,596,523,53]
                HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
                HOperatorSet.DrawCircle(hv_ExpDefaultWinHandle, out hv_Row3, out hv_Column3,
                    out hv_Radius);
                HOperatorSet.DrawCircle(hv_ExpDefaultWinHandle, out hv_Row4, out hv_Column4,
                    out hv_Radius1);
                HOperatorSet.DrawCircle(hv_ExpDefaultWinHandle, out hv_Row5, out hv_Column5,
                    out hv_Radius2);
                HOperatorSet.DrawCircle(hv_ExpDefaultWinHandle, out hv_Row6, out hv_Column6,
                    out hv_Radius3);
                hv_CircleParam = new HTuple();
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Row3);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Column3);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Radius);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Row4);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Column4);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Radius1);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Row5);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Column5);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Radius2);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Row6);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Column6);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Radius3);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "circle", hv_CircleParam,
                    20, 5, 1, 30, new HTuple(), new HTuple(), out hv_CircleIndices1);
                //
                //添加两个部分圆
                //CircleParam1 := [583,1010,79]
                //CircleParam2 := [336,1005,77]
                HOperatorSet.DrawCircle(hv_ExpDefaultWinHandle, out hv_Row8, out hv_Column8,
                    out hv_Radius5);
                HOperatorSet.DrawCircle(hv_ExpDefaultWinHandle, out hv_Row7, out hv_Column7,
                    out hv_Radius4);
                hv_CircleParam = new HTuple();
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Row7);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Column7);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Radius4);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Row8);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Column8);
                hv_CircleParam = hv_CircleParam.TupleConcat(hv_Radius5);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "circle", hv_CircleParam,
                    20, 5, 1, 30, (new HTuple("start_phi")).TupleConcat("end_phi"), (new HTuple(0)).TupleConcat(
                    (new HTuple(185)).TupleRad()), out hv_CircleIndices2);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "circle", hv_CircleParam,
                    20, 5, 1, 30, (new HTuple("start_phi")).TupleConcat("end_phi"), (new HTuple((new HTuple(45)).TupleRad()
                    )).TupleConcat((new HTuple(185)).TupleRad()), out hv_Index3);
                hv_CircleIndices2 = hv_CircleIndices2.TupleConcat(hv_Index3);
                //
                //添加一个矩形
                hv_RectangleParam = new HTuple();
                hv_RectangleParam[0] = 599;
                hv_RectangleParam[1] = 279;
                hv_RectangleParam = hv_RectangleParam.TupleConcat((new HTuple(90)).TupleRad()
                    );
                hv_RectangleParam = hv_RectangleParam.TupleConcat(62);
                hv_RectangleParam = hv_RectangleParam.TupleConcat(51);
                HOperatorSet.DrawRectangle2(hv_ExpDefaultWinHandle, out hv_Row9, out hv_Column9,
                    out hv_Phi, out hv_Length1, out hv_Length2);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "rectangle2", hv_RectangleParam,
                    20, 5, 1, 30, new HTuple(), new HTuple(), out hv_RectIndices);
                //添加两条线
                hv_Line1 = new HTuple();
                hv_Line1[0] = 143;
                hv_Line1[1] = 1122;
                hv_Line1[2] = 709;
                hv_Line1[3] = 1132;
                hv_Line2 = new HTuple();
                hv_Line2[0] = 151;
                hv_Line2[1] = 153;
                hv_Line2[2] = 136;
                hv_Line2[3] = 1115;
                HOperatorSet.DrawLine(hv_ExpDefaultWinHandle, out hv_Row11, out hv_Column11,
                    out hv_Row21, out hv_Column21);
                HOperatorSet.DrawLine(hv_ExpDefaultWinHandle, out hv_Row12, out hv_Column12,
                    out hv_Row22, out hv_Column22);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "line", hv_Line1.TupleConcat(
                    hv_Line2), 20, 5, 1, 30, new HTuple(), new HTuple(), out hv_LineIndices);
                //检查已添加到计量模型的形状
                ho_ModelContour.Dispose();
                HOperatorSet.GetMetrologyObjectModelContour(out ho_ModelContour, hv_MetrologyHandle,
                    "all", 1.5);
                ho_MeasureContour.Dispose();
                HOperatorSet.GetMetrologyObjectMeasures(out ho_MeasureContour, hv_MetrologyHandle,
                    "all", "all", out hv_Row, out hv_Column);
                hv_Message = "This example shows how to measure geometric shapes using a";
                if (hv_Message == null)
                    hv_Message = new HTuple();
                hv_Message[1] = "metrology model. As preparation, their roughly known ";
                if (hv_Message == null)
                    hv_Message = new HTuple();
                hv_Message[2] = "dimensions and tolerances are specified by the user.";
                show_contours(ho_Image, ho_ModelContour, ho_MeasureContour, ho_EmptyObject,
                    hv_WindowHandle, hv_Message);
                HDevelopStop();
                //
                //Part 2:
                //
                //准备对准
                //
                //a)基于形状的匹配
                if ((int)(new HTuple(hv_AlignmentMode.TupleEqual("shape-based matching"))) != 0)
                {
                    HOperatorSet.SetPart(hv_ExpDefaultWinHandle, ((-hv_Height) / 2) - 100, (-hv_Width) / 2,
                        (1.5 * hv_Height) - 100, 1.5 * hv_Width);
                    //
                    //创建用于在图像中校准该计量模型的形状模型,
                    //其中对象发生的位置和方向与用于创建该模型的图像中的位置和方向不同
                    HOperatorSet.GetSystem("border_shape_models", out hv_BorderShapeModel);
                    HOperatorSet.SetSystem("border_shape_models", "true");
                    ho_Region.Dispose();
                    HOperatorSet.Threshold(ho_Image, out ho_Region, 0, 50);
                    ho_ModelRegion.Dispose();
                    HOperatorSet.DilationRectangle1(ho_Region, out ho_ModelRegion, 5, 5);
                    ho_ImageReduced.Dispose();
                    HOperatorSet.ReduceDomain(ho_Image, ho_ModelRegion, out ho_ImageReduced);
                    HOperatorSet.CreateShapeModel(ho_ImageReduced, 6, 0, (new HTuple(360)).TupleRad()
                        , "auto", "auto", "use_polarity", "auto", 20, out hv_ShapeModelID);
                    //形状模型的原点是create_shape_model中使用的输入区域的中心
                    HOperatorSet.AreaCenter(ho_ModelRegion, out hv_Area, out hv_RowModel, out hv_ColumnModel);
                    ho_ShapeModelContours.Dispose();
                    HOperatorSet.GetShapeModelContours(out ho_ShapeModelContours, hv_ShapeModelID,
                        1);
                    hv_Message = "A shape model will be used for the alignment of the metrology";
                    if (hv_Message == null)
                        hv_Message = new HTuple();
                    hv_Message[1] = "model. The contours of the shape model (white) and of the";
                    if (hv_Message == null)
                        hv_Message = new HTuple();
                    hv_Message[2] = "metrology model (blue) are shown.";
                    show_contours(ho_Image, ho_ModelContour, ho_EmptyObject, ho_ShapeModelContours,
                        hv_WindowHandle, hv_Message);
                    //
                    //改变定义计量模型的参考系,使其与形状模型所使用的参考系相对应。
                    //为了能够直接使用find_shape_model的结果来校准计量模型,这是必要的
                    HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "reference_system",
                        ((hv_RowModel.TupleConcat(hv_ColumnModel))).TupleConcat(0));
                    ho_ModelContour.Dispose();
                    HOperatorSet.GetMetrologyObjectModelContour(out ho_ModelContour, hv_MetrologyHandle,
                        "all", 1.5);
                    hv_Message = "To prepare the alignment, the origin of the shape model";
                    if (hv_Message == null)
                        hv_Message = new HTuple();
                    hv_Message[1] = "is set as the reference system of the metrology model.";
                    show_contours(ho_Image, ho_ModelContour, ho_EmptyObject, ho_ShapeModelContours,
                        hv_WindowHandle, hv_Message);
                    HDevelopStop();
                    HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_Height - 1, hv_Width - 1);
                }
                //
                //
                //在线阶段
                for (hv_I = 2; (int)hv_I <= 5; hv_I = (int)hv_I + 1)
                {
                    ho_CurrentImage.Dispose();
                    HOperatorSet.ReadImage(out ho_CurrentImage, "metal-parts/circle_plate_" + (hv_I.TupleString(
                        "02d")));
                    HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 1);
                    HOperatorSet.DispObj(ho_CurrentImage, hv_ExpDefaultWinHandle);
                    //
                    //a) Shape-based匹配
                    if ((int)(new HTuple(hv_AlignmentMode.TupleEqual("shape-based matching"))) != 0)
                    {
                        //
                        //使用形状模型测量当前图像中物体的位置和方向
                        HOperatorSet.FindShapeModel(ho_CurrentImage, hv_ShapeModelID, 0, (new HTuple(360)).TupleRad()
                            , 0.5, 1, 0, "least_squares", 5, 0.9, out hv_RowAlign, out hv_ColumnAlign,
                            out hv_AngleAlign, out hv_Score);
                        dev_display_shape_matching_results(hv_ShapeModelID, "white", hv_RowAlign,
                            hv_ColumnAlign, hv_AngleAlign, 1, 1, 0);
                    }
                    //

                    //
                    //使用计算的位置和方向将计量模型与其当前引用对齐
                    HOperatorSet.AlignMetrologyModel(hv_MetrologyHandle, hv_RowAlign, hv_ColumnAlign,
                        hv_AngleAlign);
                    //显示校准的计量模型
                    if ((int)(new HTuple(hv_I.TupleLessEqual(2))) != 0)
                    {
                        ho_ModelContour.Dispose();
                        HOperatorSet.GetMetrologyObjectModelContour(out ho_ModelContour, hv_MetrologyHandle,
                            "all", 1.5);
                        HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "blue");
                        HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 2);
                        HOperatorSet.DispObj(ho_ModelContour, hv_ExpDefaultWinHandle);
                        hv_Message = "In each image, the object is matched and aligned";
                        if (hv_Message == null)
                            hv_Message = new HTuple();
                        hv_Message[1] = "before the metrology measurement.";
                        disp_message(hv_ExpDefaultWinHandle, hv_Message, "window", 12, 12, "black",
                            "true");
                        disp_continue_message(hv_ExpDefaultWinHandle, "black", "true");
                        HDevelopStop();
                    }
                    //
                    //在一次命令中对所有计量对象进行测量
                    HOperatorSet.ApplyMetrologyModel(ho_CurrentImage, hv_MetrologyHandle);
                    //获取用于可视化的度量区域
                    ho_Contour.Dispose();
                    HOperatorSet.GetMetrologyObjectMeasures(out ho_Contour, hv_MetrologyHandle,
                        "all", "all", out hv_Row, out hv_Column);
                    //得到实际用于拟合几何图形的边缘点
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "used_edges",
                        "row", out hv_UsedRow);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "used_edges",
                        "column", out hv_UsedColumn);
                    ho_UsedEdges.Dispose();
                    HOperatorSet.GenCrossContourXld(out ho_UsedEdges, hv_UsedRow, hv_UsedColumn,
                        10, (new HTuple(45)).TupleRad());
                    //
                    //获取自摄像机参数设定后的测量结果,
                    //所有结果均以公制坐标系给出,由测量平面定义坐标系,得到被测几何图形的可视化
                    ho_ResultContours.Dispose();
                    HOperatorSet.GetMetrologyObjectResultContour(out ho_ResultContours, hv_MetrologyHandle,
                        "all", "all", 1.5);
                    //提取较小圆的半径
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_CircleIndices1,
                        "all", "result_type", "radius", out hv_RadiusC1);
                    //提取较大的不完整圆的半径
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_CircleIndices2,
                        "all", "result_type", "radius", out hv_RadiusC2);
                    //提取矩形边的长度
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_RectIndices,
                        "all", "result_type", "length1", out hv_Length1R);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_RectIndices,
                        "all", "result_type", "length2", out hv_Length2R);
                    //得到每一条被测线的起点和终点
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_LineIndices.TupleSelect(
                        0), "all", "result_type", "all_param", out hv_ParamLine1);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_LineIndices.TupleSelect(
                        1), "all", "result_type", "all_param", out hv_ParamLine2);
                    //显示结果
                    HOperatorSet.DispObj(ho_CurrentImage, hv_ExpDefaultWinHandle);
                    HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 1);
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "light gray");
                    HOperatorSet.DispObj(ho_Contour, hv_ExpDefaultWinHandle);
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green");
                    HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 2);
                    HOperatorSet.DispObj(ho_ResultContours, hv_ExpDefaultWinHandle);
                    HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 1);
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "white");
                    HOperatorSet.DispObj(ho_UsedEdges, hv_ExpDefaultWinHandle);
                    //
                    //显示圆心处每个圆的半径获取圆心的公制坐标
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_CircleIndices1,
                        "all", "result_type", "x", out hv_XC1);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_CircleIndices1,
                        "all", "result_type", "y", out hv_YC1);
                    //将圆心的度量坐标投影到图像中,得到圆心的图像坐标
                    project_xy_to_image(hv_XC1, hv_YC1, hv_MeasurementPlaneAdjusted, hv_CameraParam,
                        out hv_Row1, out hv_Column1);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_CircleIndices2,
                        "all", "result_type", "x", out hv_XC2);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_CircleIndices2,
                        "all", "result_type", "y", out hv_YC2);
                    project_xy_to_image(hv_XC2, hv_YC2, hv_MeasurementPlaneAdjusted, hv_CameraParam,
                        out hv_Row2, out hv_Column2);
                    for (hv_J = 0; (int)hv_J <= (int)((new HTuple(hv_RadiusC1.TupleLength())) - 1); hv_J = (int)hv_J + 1)
                    {
                        disp_message(hv_ExpDefaultWinHandle, "r=" + ((((hv_RadiusC1.TupleSelect(hv_J)) * 1000)).TupleString(
                            ".2f")), "image", hv_Row1.TupleSelect(hv_J), (hv_Column1.TupleSelect(
                            hv_J)) - 80, "black", "true");
                    }
                    for (hv_J = 0; (int)hv_J <= (int)((new HTuple(hv_RadiusC2.TupleLength())) - 1); hv_J = (int)hv_J + 1)
                    {
                        disp_message(hv_ExpDefaultWinHandle, "r=" + ((((hv_RadiusC2.TupleSelect(hv_J)) * 1000)).TupleString(
                            ".2f")), "image", hv_Row2.TupleSelect(hv_J), (hv_Column2.TupleSelect(
                            hv_J)) - 80, "black", "true");
                    }
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_RectIndices,
                        "all", "result_type", "x", out hv_XRectangle);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_RectIndices,
                        "all", "result_type", "y", out hv_YRectangle);
                    project_xy_to_image(hv_XRectangle, hv_YRectangle, hv_MeasurementPlaneAdjusted,
                        hv_CameraParam, out hv_RowR, out hv_ColumnR);
                    hv_Area = (((hv_Length1R * hv_Length2R) * 4) * 1000) * 1000;
                    disp_message(hv_ExpDefaultWinHandle, "area=" + (hv_Area.TupleString(".2f")),
                        "image", hv_RowR, hv_ColumnR - 120, "black", "true");
                    hv_Message = "Measured metric results after alignment (r in mm, area in mm^2):";
                    //
                    disp_message(hv_ExpDefaultWinHandle, hv_Message, "window", 12, 12, "black",
                        "true");
                    if ((int)(new HTuple(hv_I.TupleLess(5))) != 0)
                    {
                        disp_continue_message(hv_ExpDefaultWinHandle, "black", "true");
                    }
                    HDevelopStop();
                }
                //清理内存
                HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle);
                if ((int)(new HTuple(hv_AlignmentMode.TupleEqual("shape-based matching"))) != 0)
                {
                    HOperatorSet.ClearShapeModel(hv_ShapeModelID);
                    HOperatorSet.SetSystem("border_shape_models", hv_BorderShapeModel);
                }
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_EmptyObject.Dispose();
                ho_Image.Dispose();
                ho_ModelContour.Dispose();
                ho_MeasureContour.Dispose();
                ho_Region.Dispose();
                ho_ModelRegion.Dispose();
                ho_ImageReduced.Dispose();
                ho_ShapeModelContours.Dispose();
                ho_CurrentImage.Dispose();
                ho_Contour.Dispose();
                ho_UsedEdges.Dispose();
                ho_ResultContours.Dispose();

                throw HDevExpDefaultException;
            }
            ho_EmptyObject.Dispose();
            ho_Image.Dispose();
            ho_ModelContour.Dispose();
            ho_MeasureContour.Dispose();
            ho_Region.Dispose();
            ho_ModelRegion.Dispose();
            ho_ImageReduced.Dispose();
            ho_ShapeModelContours.Dispose();
            ho_CurrentImage.Dispose();
            ho_Contour.Dispose();
            ho_UsedEdges.Dispose();
            ho_ResultContours.Dispose();

        }

        public void HDevelopStop()
        {
            MessageBox.Show("Press button to continue", "Program stop");
        }

        // Procedures 
        // External procedures 
        // Chapter: Matching / Shape-Based
        // Short Description: Display the results of Shape-Based Matching. 
        public void dev_display_shape_matching_results(HTuple hv_ModelID, HTuple hv_Color,
            HTuple hv_Row, HTuple hv_Column, HTuple hv_Angle, HTuple hv_ScaleR, HTuple hv_ScaleC,
            HTuple hv_Model)
        {



            // Local iconic variables 

            HObject ho_ModelContours = null, ho_ContoursAffinTrans = null;

            // Local control variables 

            HTuple hv_NumMatches = null, hv_Index = new HTuple();
            HTuple hv_Match = new HTuple(), hv_HomMat2DIdentity = new HTuple();
            HTuple hv_HomMat2DScale = new HTuple(), hv_HomMat2DRotate = new HTuple();
            HTuple hv_HomMat2DTranslate = new HTuple();
            HTuple hv_Model_COPY_INP_TMP = hv_Model.Clone();
            HTuple hv_ScaleC_COPY_INP_TMP = hv_ScaleC.Clone();
            HTuple hv_ScaleR_COPY_INP_TMP = hv_ScaleR.Clone();

            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_ModelContours);
            HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans);
            try
            {
                //This procedure displays the results of Shape-Based Matching.
                //
                hv_NumMatches = new HTuple(hv_Row.TupleLength());
                if ((int)(new HTuple(hv_NumMatches.TupleGreater(0))) != 0)
                {
                    if ((int)(new HTuple((new HTuple(hv_ScaleR_COPY_INP_TMP.TupleLength())).TupleEqual(
                        1))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, hv_ScaleR_COPY_INP_TMP, out hv_ScaleR_COPY_INP_TMP);
                    }
                    if ((int)(new HTuple((new HTuple(hv_ScaleC_COPY_INP_TMP.TupleLength())).TupleEqual(
                        1))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, hv_ScaleC_COPY_INP_TMP, out hv_ScaleC_COPY_INP_TMP);
                    }
                    if ((int)(new HTuple((new HTuple(hv_Model_COPY_INP_TMP.TupleLength())).TupleEqual(
                        0))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, 0, out hv_Model_COPY_INP_TMP);
                    }
                    else if ((int)(new HTuple((new HTuple(hv_Model_COPY_INP_TMP.TupleLength()
                        )).TupleEqual(1))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, hv_Model_COPY_INP_TMP, out hv_Model_COPY_INP_TMP);
                    }
                    for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_ModelID.TupleLength()
                        )) - 1); hv_Index = (int)hv_Index + 1)
                    {
                        ho_ModelContours.Dispose();
                        HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID.TupleSelect(
                            hv_Index), 1);
                        HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Color.TupleSelect(hv_Index % (new HTuple(hv_Color.TupleLength()
                            ))));
                        HTuple end_val18 = hv_NumMatches - 1;
                        HTuple step_val18 = 1;
                        for (hv_Match = 0; hv_Match.Continue(end_val18, step_val18); hv_Match = hv_Match.TupleAdd(step_val18))
                        {
                            if ((int)(new HTuple(hv_Index.TupleEqual(hv_Model_COPY_INP_TMP.TupleSelect(
                                hv_Match)))) != 0)
                            {
                                HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                                HOperatorSet.HomMat2dScale(hv_HomMat2DIdentity, hv_ScaleR_COPY_INP_TMP.TupleSelect(
                                    hv_Match), hv_ScaleC_COPY_INP_TMP.TupleSelect(hv_Match), 0, 0,
                                    out hv_HomMat2DScale);
                                HOperatorSet.HomMat2dRotate(hv_HomMat2DScale, hv_Angle.TupleSelect(
                                    hv_Match), 0, 0, out hv_HomMat2DRotate);
                                HOperatorSet.HomMat2dTranslate(hv_HomMat2DRotate, hv_Row.TupleSelect(
                                    hv_Match), hv_Column.TupleSelect(hv_Match), out hv_HomMat2DTranslate);
                                ho_ContoursAffinTrans.Dispose();
                                HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ContoursAffinTrans,
                                    hv_HomMat2DTranslate);
                                HOperatorSet.DispObj(ho_ContoursAffinTrans, hv_ExpDefaultWinHandle);
                            }
                        }
                    }
                }
                ho_ModelContours.Dispose();
                ho_ContoursAffinTrans.Dispose();

                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_ModelContours.Dispose();
                ho_ContoursAffinTrans.Dispose();

                throw HDevExpDefaultException;
            }
        }

        // Chapter: Develop
        // Short Description: Open a new graphics window that preserves the aspect ratio of the given image. 
        public void dev_open_window_fit_image(HObject ho_Image, HTuple hv_Row, HTuple hv_Column,
            HTuple hv_WidthLimit, HTuple hv_HeightLimit, out HTuple hv_WindowHandle)
        {




            // Local iconic variables 

            // Local control variables 

            HTuple hv_MinWidth = new HTuple(), hv_MaxWidth = new HTuple();
            HTuple hv_MinHeight = new HTuple(), hv_MaxHeight = new HTuple();
            HTuple hv_ResizeFactor = null, hv_ImageWidth = null, hv_ImageHeight = null;
            HTuple hv_TempWidth = null, hv_TempHeight = null, hv_WindowWidth = new HTuple();
            HTuple hv_WindowHeight = null;
            // Initialize local and output iconic variables 
            hv_WindowHandle = new HTuple();
            //This procedure opens a new graphics window and adjusts the size
            //such that it fits into the limits specified by WidthLimit
            //and HeightLimit, but also maintains the correct image aspect ratio.
            //
            //If it is impossible to match the minimum and maximum extent requirements
            //at the same time (f.e. if the image is very long but narrow),
            //the maximum value gets a higher priority,
            //
            //Parse input tuple WidthLimit
            if ((int)((new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual(0))).TupleOr(
                new HTuple(hv_WidthLimit.TupleLess(0)))) != 0)
            {
                hv_MinWidth = 500;
                hv_MaxWidth = 800;
            }
            else if ((int)(new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual(
                1))) != 0)
            {
                hv_MinWidth = 0;
                hv_MaxWidth = hv_WidthLimit.Clone();
            }
            else
            {
                hv_MinWidth = hv_WidthLimit[0];
                hv_MaxWidth = hv_WidthLimit[1];
            }
            //Parse input tuple HeightLimit
            if ((int)((new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual(0))).TupleOr(
                new HTuple(hv_HeightLimit.TupleLess(0)))) != 0)
            {
                hv_MinHeight = 400;
                hv_MaxHeight = 600;
            }
            else if ((int)(new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual(
                1))) != 0)
            {
                hv_MinHeight = 0;
                hv_MaxHeight = hv_HeightLimit.Clone();
            }
            else
            {
                hv_MinHeight = hv_HeightLimit[0];
                hv_MaxHeight = hv_HeightLimit[1];
            }
            //
            //Test, if window size has to be changed.
            hv_ResizeFactor = 1;
            HOperatorSet.GetImageSize(ho_Image, out hv_ImageWidth, out hv_ImageHeight);
            //First, expand window to the minimum extents (if necessary).
            if ((int)((new HTuple(hv_MinWidth.TupleGreater(hv_ImageWidth))).TupleOr(new HTuple(hv_MinHeight.TupleGreater(
                hv_ImageHeight)))) != 0)
            {
                hv_ResizeFactor = (((((hv_MinWidth.TupleReal()) / hv_ImageWidth)).TupleConcat(
                    (hv_MinHeight.TupleReal()) / hv_ImageHeight))).TupleMax();
            }
            hv_TempWidth = hv_ImageWidth * hv_ResizeFactor;
            hv_TempHeight = hv_ImageHeight * hv_ResizeFactor;
            //Then, shrink window to maximum extents (if necessary).
            if ((int)((new HTuple(hv_MaxWidth.TupleLess(hv_TempWidth))).TupleOr(new HTuple(hv_MaxHeight.TupleLess(
                hv_TempHeight)))) != 0)
            {
                hv_ResizeFactor = hv_ResizeFactor * ((((((hv_MaxWidth.TupleReal()) / hv_TempWidth)).TupleConcat(
                    (hv_MaxHeight.TupleReal()) / hv_TempHeight))).TupleMin());
            }
            hv_WindowWidth = hv_ImageWidth * hv_ResizeFactor;
            hv_WindowHeight = hv_ImageHeight * hv_ResizeFactor;
            //Resize window
            //dev_open_window(...);
            HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_ImageHeight - 1, hv_ImageWidth - 1);

            return;
        }

        // Chapter: Develop
        // Short Description: Switch dev_update_pc, dev_update_var and dev_update_window to 'off'. 
        public void dev_update_off()
        {

            // Initialize local and output iconic variables 
            //This procedure sets different update settings to 'off'.
            //This is useful to get the best performance and reduce overhead.
            //
            // dev_update_pc(...); only in hdevelop
            // dev_update_var(...); only in hdevelop
            // dev_update_window(...); only in hdevelop

            return;
        }

        // Chapter: Graphics / Text
        // Short Description: This procedure displays 'Click 'Run' to continue' in the lower right corner of the screen. 
        public void disp_continue_message(HTuple hv_WindowHandle, HTuple hv_Color, HTuple hv_Box)
        {



            // Local iconic variables 

            // Local control variables 

            HTuple hv_ContinueMessage = null, hv_Row = null;
            HTuple hv_Column = null, hv_Width = null, hv_Height = null;
            HTuple hv_Ascent = null, hv_Descent = null, hv_TextWidth = null;
            HTuple hv_TextHeight = null;
            // Initialize local and output iconic variables 
            //This procedure displays 'Press Run (F5) to continue' in the
            //lower right corner of the screen.
            //It uses the procedure disp_message.
            //
            //Input parameters:
            //WindowHandle: The window, where the text shall be displayed
            //Color: defines the text color.
            //   If set to '' or 'auto', the currently set color is used.
            //Box: If set to 'true', the text is displayed in a box.
            //
            hv_ContinueMessage = "Press Run (F5) to continue";
            HOperatorSet.GetWindowExtents(hv_ExpDefaultWinHandle, out hv_Row, out hv_Column,
                out hv_Width, out hv_Height);
            HOperatorSet.GetStringExtents(hv_ExpDefaultWinHandle, (" " + hv_ContinueMessage) + " ",
                out hv_Ascent, out hv_Descent, out hv_TextWidth, out hv_TextHeight);
            disp_message(hv_ExpDefaultWinHandle, hv_ContinueMessage, "window", (hv_Height - hv_TextHeight) - 12,
                (hv_Width - hv_TextWidth) - 12, hv_Color, hv_Box);

            return;
        }

        // Chapter: Graphics / Text
        // Short Description: This procedure writes a text message. 
        public void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
            HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
        {



            // Local iconic variables 

            // Local control variables 

            HTuple hv_Red = null, hv_Green = null, hv_Blue = null;
            HTuple hv_Row1Part = null, hv_Column1Part = null, hv_Row2Part = null;
            HTuple hv_Column2Part = null, hv_RowWin = null, hv_ColumnWin = null;
            HTuple hv_WidthWin = new HTuple(), hv_HeightWin = null;
            HTuple hv_MaxAscent = null, hv_MaxDescent = null, hv_MaxWidth = null;
            HTuple hv_MaxHeight = null, hv_R1 = new HTuple(), hv_C1 = new HTuple();
            HTuple hv_FactorRow = new HTuple(), hv_FactorColumn = new HTuple();
            HTuple hv_UseShadow = null, hv_ShadowColor = null, hv_Exception = new HTuple();
            HTuple hv_Width = new HTuple(), hv_Index = new HTuple();
            HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
            HTuple hv_W = new HTuple(), hv_H = new HTuple(), hv_FrameHeight = new HTuple();
            HTuple hv_FrameWidth = new HTuple(), hv_R2 = new HTuple();
            HTuple hv_C2 = new HTuple(), hv_DrawMode = new HTuple();
            HTuple hv_CurrentColor = new HTuple();
            HTuple hv_Box_COPY_INP_TMP = hv_Box.Clone();
            HTuple hv_Color_COPY_INP_TMP = hv_Color.Clone();
            HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone();
            HTuple hv_Row_COPY_INP_TMP = hv_Row.Clone();
            HTuple hv_String_COPY_INP_TMP = hv_String.Clone();

            // Initialize local and output iconic variables 
            //This procedure displays text in a graphics window.
            //
            //Input parameters:
            //WindowHandle: The WindowHandle of the graphics window, where
            //   the message should be displayed
            //String: A tuple of strings containing the text message to be displayed
            //CoordSystem: If set to 'window', the text position is given
            //   with respect to the window coordinate system.
            //   If set to 'image', image coordinates are used.
            //   (This may be useful in zoomed images.)
            //Row: The row coordinate of the desired text position
            //   If set to -1, a default value of 12 is used.
            //Column: The column coordinate of the desired text position
            //   If set to -1, a default value of 12 is used.
            //Color: defines the color of the text as string.
            //   If set to [], '' or 'auto' the currently set color is used.
            //   If a tuple of strings is passed, the colors are used cyclically
            //   for each new textline.
            //Box: If Box[0] is set to 'true', the text is written within an orange box.
            //     If set to' false', no box is displayed.
            //     If set to a color string (e.g. 'white', '#FF00CC', etc.),
            //       the text is written in a box of that color.
            //     An optional second value for Box (Box[1]) controls if a shadow is displayed:
            //       'true' -> display a shadow in a default color
            //       'false' -> display no shadow (same as if no second value is given)
            //       otherwise -> use given string as color string for the shadow color
            //
            //Prepare window
            HOperatorSet.GetRgb(hv_ExpDefaultWinHandle, out hv_Red, out hv_Green, out hv_Blue);
            HOperatorSet.GetPart(hv_ExpDefaultWinHandle, out hv_Row1Part, out hv_Column1Part,
                out hv_Row2Part, out hv_Column2Part);
            HOperatorSet.GetWindowExtents(hv_ExpDefaultWinHandle, out hv_RowWin, out hv_ColumnWin,
                out hv_WidthWin, out hv_HeightWin);
            HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1);
            //
            //default settings
            if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0)
            {
                hv_Row_COPY_INP_TMP = 12;
            }
            if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0)
            {
                hv_Column_COPY_INP_TMP = 12;
            }
            if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
            {
                hv_Color_COPY_INP_TMP = "";
            }
            //
            hv_String_COPY_INP_TMP = ((("" + hv_String_COPY_INP_TMP) + "")).TupleSplit("\n");
            //
            //Estimate extentions of text depending on font size.
            HOperatorSet.GetFontExtents(hv_ExpDefaultWinHandle, out hv_MaxAscent, out hv_MaxDescent,
                out hv_MaxWidth, out hv_MaxHeight);
            if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0)
            {
                hv_R1 = hv_Row_COPY_INP_TMP.Clone();
                hv_C1 = hv_Column_COPY_INP_TMP.Clone();
            }
            else
            {
                //Transform image to window coordinates
                hv_FactorRow = (1.0 * hv_HeightWin) / ((hv_Row2Part - hv_Row1Part) + 1);
                hv_FactorColumn = (1.0 * hv_WidthWin) / ((hv_Column2Part - hv_Column1Part) + 1);
                hv_R1 = ((hv_Row_COPY_INP_TMP - hv_Row1Part) + 0.5) * hv_FactorRow;
                hv_C1 = ((hv_Column_COPY_INP_TMP - hv_Column1Part) + 0.5) * hv_FactorColumn;
            }
            //
            //Display text box depending on text size
            hv_UseShadow = 1;
            hv_ShadowColor = "gray";
            if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleEqual("true"))) != 0)
            {
                if (hv_Box_COPY_INP_TMP == null)
                    hv_Box_COPY_INP_TMP = new HTuple();
                hv_Box_COPY_INP_TMP[0] = "#fce9d4";
                hv_ShadowColor = "#f28d26";
            }
            if ((int)(new HTuple((new HTuple(hv_Box_COPY_INP_TMP.TupleLength())).TupleGreater(
                1))) != 0)
            {
                if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual("true"))) != 0)
                {
                    //Use default ShadowColor set above
                }
                else if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual(
                    "false"))) != 0)
                {
                    hv_UseShadow = 0;
                }
                else
                {
                    hv_ShadowColor = hv_Box_COPY_INP_TMP[1];
                    //Valid color?
                    try
                    {
                        HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Box_COPY_INP_TMP.TupleSelect(
                            1));
                    }
                    // catch (Exception) 
                    catch (HalconException HDevExpDefaultException1)
                    {
                        HDevExpDefaultException1.ToHTuple(out hv_Exception);
                        hv_Exception = "Wrong value of control parameter Box[1] (must be a 'true', 'false', or a valid color string)";
                        throw new HalconException(hv_Exception);
                    }
                }
            }
            if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleNotEqual("false"))) != 0)
            {
                //Valid color?
                try
                {
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Box_COPY_INP_TMP.TupleSelect(
                        0));
                }
                // catch (Exception) 
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    hv_Exception = "Wrong value of control parameter Box[0] (must be a 'true', 'false', or a valid color string)";
                    throw new HalconException(hv_Exception);
                }
                //Calculate box extents
                hv_String_COPY_INP_TMP = (" " + hv_String_COPY_INP_TMP) + " ";
                hv_Width = new HTuple();
                for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                    )) - 1); hv_Index = (int)hv_Index + 1)
                {
                    HOperatorSet.GetStringExtents(hv_ExpDefaultWinHandle, hv_String_COPY_INP_TMP.TupleSelect(
                        hv_Index), out hv_Ascent, out hv_Descent, out hv_W, out hv_H);
                    hv_Width = hv_Width.TupleConcat(hv_W);
                }
                hv_FrameHeight = hv_MaxHeight * (new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                    ));
                hv_FrameWidth = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax();
                hv_R2 = hv_R1 + hv_FrameHeight;
                hv_C2 = hv_C1 + hv_FrameWidth;
                //Display rectangles
                HOperatorSet.GetDraw(hv_ExpDefaultWinHandle, out hv_DrawMode);
                HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "fill");
                //Set shadow color
                HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_ShadowColor);
                if ((int)(hv_UseShadow) != 0)
                {
                    HOperatorSet.DispRectangle1(hv_ExpDefaultWinHandle, hv_R1 + 1, hv_C1 + 1, hv_R2 + 1,
                        hv_C2 + 1);
                }
                //Set box color
                HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Box_COPY_INP_TMP.TupleSelect(
                    0));
                HOperatorSet.DispRectangle1(hv_ExpDefaultWinHandle, hv_R1, hv_C1, hv_R2, hv_C2);
                HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, hv_DrawMode);
            }
            //Write text.
            for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                )) - 1); hv_Index = (int)hv_Index + 1)
            {
                hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
                    )));
                if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual(
                    "auto")))) != 0)
                {
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_CurrentColor);
                }
                else
                {
                    HOperatorSet.SetRgb(hv_ExpDefaultWinHandle, hv_Red, hv_Green, hv_Blue);
                }
                hv_Row_COPY_INP_TMP = hv_R1 + (hv_MaxHeight * hv_Index);
                HOperatorSet.SetTposition(hv_ExpDefaultWinHandle, hv_Row_COPY_INP_TMP, hv_C1);
                HOperatorSet.WriteString(hv_ExpDefaultWinHandle, hv_String_COPY_INP_TMP.TupleSelect(
                    hv_Index));
            }
            //Reset changed window settings
            HOperatorSet.SetRgb(hv_ExpDefaultWinHandle, hv_Red, hv_Green, hv_Blue);
            HOperatorSet.SetPart(hv_ExpDefaultWinHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part,
                hv_Column2Part);

            return;
        }

        // Chapter: Graphics / Text
        // Short Description: Set font independent of OS 
        public void set_display_font(HTuple hv_WindowHandle, HTuple hv_Size, HTuple hv_Font,
            HTuple hv_Bold, HTuple hv_Slant)
        {



            // Local iconic variables 

            // Local control variables 

            HTuple hv_OS = null, hv_BufferWindowHandle = new HTuple();
            HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_Scale = new HTuple(), hv_Exception = new HTuple();
            HTuple hv_SubFamily = new HTuple(), hv_Fonts = new HTuple();
            HTuple hv_SystemFonts = new HTuple(), hv_Guess = new HTuple();
            HTuple hv_I = new HTuple(), hv_Index = new HTuple(), hv_AllowedFontSizes = new HTuple();
            HTuple hv_Distances = new HTuple(), hv_Indices = new HTuple();
            HTuple hv_FontSelRegexp = new HTuple(), hv_FontsCourier = new HTuple();
            HTuple hv_Bold_COPY_INP_TMP = hv_Bold.Clone();
            HTuple hv_Font_COPY_INP_TMP = hv_Font.Clone();
            HTuple hv_Size_COPY_INP_TMP = hv_Size.Clone();
            HTuple hv_Slant_COPY_INP_TMP = hv_Slant.Clone();

            // Initialize local and output iconic variables 
            //This procedure sets the text font of the current window with
            //the specified attributes.
            //It is assumed that following fonts are installed on the system:
            //Windows: Courier New, Arial Times New Roman
            //Mac OS X: CourierNewPS, Arial, TimesNewRomanPS
            //Linux: courier, helvetica, times
            //Because fonts are displayed smaller on Linux than on Windows,
            //a scaling factor of 1.25 is used the get comparable results.
            //For Linux, only a limited number of font sizes is supported,
            //to get comparable results, it is recommended to use one of the
            //following sizes: 9, 11, 14, 16, 20, 27
            //(which will be mapped internally on Linux systems to 11, 14, 17, 20, 25, 34)
            //
            //Input parameters:
            //WindowHandle: The graphics window for which the font will be set
            //Size: The font size. If Size=-1, the default of 16 is used.
            //Bold: If set to 'true', a bold font is used
            //Slant: If set to 'true', a slanted font is used
            //
            HOperatorSet.GetSystem("operating_system", out hv_OS);
            // dev_get_preferences(...); only in hdevelop
            // dev_set_preferences(...); only in hdevelop
            if ((int)((new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
                new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(-1)))) != 0)
            {
                hv_Size_COPY_INP_TMP = 16;
            }
            if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Win"))) != 0)
            {
                //Set font on Windows systems
                try
                {
                    //Check, if font scaling is switched on
                    //open_window(...);
                    HOperatorSet.SetFont(hv_ExpDefaultWinHandle, "-Consolas-16-*-0-*-*-1-");
                    HOperatorSet.GetStringExtents(hv_ExpDefaultWinHandle, "test_string", out hv_Ascent,
                        out hv_Descent, out hv_Width, out hv_Height);
                    //Expected width is 110
                    hv_Scale = 110.0 / hv_Width;
                    hv_Size_COPY_INP_TMP = ((hv_Size_COPY_INP_TMP * hv_Scale)).TupleInt();
                    //close_window(...);
                }
                // catch (Exception) 
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    //throw (Exception)
                }
                if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))).TupleOr(
                    new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("courier")))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "Courier New";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "Consolas";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "Arial";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "Times New Roman";
                }
                if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    hv_Bold_COPY_INP_TMP = 1;
                }
                else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0)
                {
                    hv_Bold_COPY_INP_TMP = 0;
                }
                else
                {
                    hv_Exception = "Wrong value of control parameter Bold";
                    throw new HalconException(hv_Exception);
                }
                if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    hv_Slant_COPY_INP_TMP = 1;
                }
                else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0)
                {
                    hv_Slant_COPY_INP_TMP = 0;
                }
                else
                {
                    hv_Exception = "Wrong value of control parameter Slant";
                    throw new HalconException(hv_Exception);
                }
                try
                {
                    HOperatorSet.SetFont(hv_ExpDefaultWinHandle, ((((((("-" + hv_Font_COPY_INP_TMP) + "-") + hv_Size_COPY_INP_TMP) + "-*-") + hv_Slant_COPY_INP_TMP) + "-*-*-") + hv_Bold_COPY_INP_TMP) + "-");
                }
                // catch (Exception) 
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    //throw (Exception)
                }
            }
            else if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Dar"))) != 0)
            {
                //Set font on Mac OS X systems. Since OS X does not have a strict naming
                //scheme for font attributes, we use tables to determine the correct font
                //name.
                hv_SubFamily = 0;
                if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    hv_SubFamily = hv_SubFamily.TupleBor(1);
                }
                else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleNotEqual("false"))) != 0)
                {
                    hv_Exception = "Wrong value of control parameter Slant";
                    throw new HalconException(hv_Exception);
                }
                if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    hv_SubFamily = hv_SubFamily.TupleBor(2);
                }
                else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleNotEqual("false"))) != 0)
                {
                    hv_Exception = "Wrong value of control parameter Bold";
                    throw new HalconException(hv_Exception);
                }
                if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))) != 0)
                {
                    hv_Fonts = new HTuple();
                    hv_Fonts[0] = "Menlo-Regular";
                    hv_Fonts[1] = "Menlo-Italic";
                    hv_Fonts[2] = "Menlo-Bold";
                    hv_Fonts[3] = "Menlo-BoldItalic";
                }
                else if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))).TupleOr(
                    new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("courier")))) != 0)
                {
                    hv_Fonts = new HTuple();
                    hv_Fonts[0] = "CourierNewPSMT";
                    hv_Fonts[1] = "CourierNewPS-ItalicMT";
                    hv_Fonts[2] = "CourierNewPS-BoldMT";
                    hv_Fonts[3] = "CourierNewPS-BoldItalicMT";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
                {
                    hv_Fonts = new HTuple();
                    hv_Fonts[0] = "ArialMT";
                    hv_Fonts[1] = "Arial-ItalicMT";
                    hv_Fonts[2] = "Arial-BoldMT";
                    hv_Fonts[3] = "Arial-BoldItalicMT";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
                {
                    hv_Fonts = new HTuple();
                    hv_Fonts[0] = "TimesNewRomanPSMT";
                    hv_Fonts[1] = "TimesNewRomanPS-ItalicMT";
                    hv_Fonts[2] = "TimesNewRomanPS-BoldMT";
                    hv_Fonts[3] = "TimesNewRomanPS-BoldItalicMT";
                }
                else
                {
                    //Attempt to figure out which of the fonts installed on the system
                    //the user could have meant.
                    HOperatorSet.QueryFont(hv_ExpDefaultWinHandle, out hv_SystemFonts);
                    hv_Fonts = new HTuple();
                    hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP);
                    hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP);
                    hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP);
                    hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP);
                    hv_Guess = new HTuple();
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP);
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Regular");
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "MT");
                    for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1)
                    {
                        HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index);
                        if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0)
                        {
                            if (hv_Fonts == null)
                                hv_Fonts = new HTuple();
                            hv_Fonts[0] = hv_Guess.TupleSelect(hv_I);
                            break;
                        }
                    }
                    //Guess name of slanted font
                    hv_Guess = new HTuple();
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Italic");
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-ItalicMT");
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Oblique");
                    for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1)
                    {
                        HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index);
                        if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0)
                        {
                            if (hv_Fonts == null)
                                hv_Fonts = new HTuple();
                            hv_Fonts[1] = hv_Guess.TupleSelect(hv_I);
                            break;
                        }
                    }
                    //Guess name of bold font
                    hv_Guess = new HTuple();
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Bold");
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldMT");
                    for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1)
                    {
                        HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index);
                        if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0)
                        {
                            if (hv_Fonts == null)
                                hv_Fonts = new HTuple();
                            hv_Fonts[2] = hv_Guess.TupleSelect(hv_I);
                            break;
                        }
                    }
                    //Guess name of bold slanted font
                    hv_Guess = new HTuple();
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldItalic");
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldItalicMT");
                    hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldOblique");
                    for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1)
                    {
                        HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index);
                        if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0)
                        {
                            if (hv_Fonts == null)
                                hv_Fonts = new HTuple();
                            hv_Fonts[3] = hv_Guess.TupleSelect(hv_I);
                            break;
                        }
                    }
                }
                hv_Font_COPY_INP_TMP = hv_Fonts.TupleSelect(hv_SubFamily);
                try
                {
                    HOperatorSet.SetFont(hv_ExpDefaultWinHandle, (hv_Font_COPY_INP_TMP + "-") + hv_Size_COPY_INP_TMP);
                }
                // catch (Exception) 
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    //throw (Exception)
                }
            }
            else
            {
                //Set font for UNIX systems
                hv_Size_COPY_INP_TMP = hv_Size_COPY_INP_TMP * 1.25;
                hv_AllowedFontSizes = new HTuple();
                hv_AllowedFontSizes[0] = 11;
                hv_AllowedFontSizes[1] = 14;
                hv_AllowedFontSizes[2] = 17;
                hv_AllowedFontSizes[3] = 20;
                hv_AllowedFontSizes[4] = 25;
                hv_AllowedFontSizes[5] = 34;
                if ((int)(new HTuple(((hv_AllowedFontSizes.TupleFind(hv_Size_COPY_INP_TMP))).TupleEqual(
                    -1))) != 0)
                {
                    hv_Distances = ((hv_AllowedFontSizes - hv_Size_COPY_INP_TMP)).TupleAbs();
                    HOperatorSet.TupleSortIndex(hv_Distances, out hv_Indices);
                    hv_Size_COPY_INP_TMP = hv_AllowedFontSizes.TupleSelect(hv_Indices.TupleSelect(
                        0));
                }
                if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))).TupleOr(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual(
                    "Courier")))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "courier";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "helvetica";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "times";
                }
                if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    hv_Bold_COPY_INP_TMP = "bold";
                }
                else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0)
                {
                    hv_Bold_COPY_INP_TMP = "medium";
                }
                else
                {
                    hv_Exception = "Wrong value of control parameter Bold";
                    throw new HalconException(hv_Exception);
                }
                if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("times"))) != 0)
                    {
                        hv_Slant_COPY_INP_TMP = "i";
                    }
                    else
                    {
                        hv_Slant_COPY_INP_TMP = "o";
                    }
                }
                else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0)
                {
                    hv_Slant_COPY_INP_TMP = "r";
                }
                else
                {
                    hv_Exception = "Wrong value of control parameter Slant";
                    throw new HalconException(hv_Exception);
                }
                try
                {
                    HOperatorSet.SetFont(hv_ExpDefaultWinHandle, ((((((("-adobe-" + hv_Font_COPY_INP_TMP) + "-") + hv_Bold_COPY_INP_TMP) + "-") + hv_Slant_COPY_INP_TMP) + "-normal-*-") + hv_Size_COPY_INP_TMP) + "-*-*-*-*-*-*-*");
                }
                // catch (Exception) 
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    if ((int)((new HTuple(((hv_OS.TupleSubstr(0, 4))).TupleEqual("Linux"))).TupleAnd(
                        new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("courier")))) != 0)
                    {
                        HOperatorSet.QueryFont(hv_ExpDefaultWinHandle, out hv_Fonts);
                        hv_FontSelRegexp = (("^-[^-]*-[^-]*[Cc]ourier[^-]*-" + hv_Bold_COPY_INP_TMP) + "-") + hv_Slant_COPY_INP_TMP;
                        hv_FontsCourier = ((hv_Fonts.TupleRegexpSelect(hv_FontSelRegexp))).TupleRegexpMatch(
                            hv_FontSelRegexp);
                        if ((int)(new HTuple((new HTuple(hv_FontsCourier.TupleLength())).TupleEqual(
                            0))) != 0)
                        {
                            hv_Exception = "Wrong font name";
                            //throw (Exception)
                        }
                        else
                        {
                            try
                            {
                                HOperatorSet.SetFont(hv_ExpDefaultWinHandle, (((hv_FontsCourier.TupleSelect(
                                    0)) + "-normal-*-") + hv_Size_COPY_INP_TMP) + "-*-*-*-*-*-*-*");
                            }
                            // catch (Exception) 
                            catch (HalconException HDevExpDefaultException2)
                            {
                                HDevExpDefaultException2.ToHTuple(out hv_Exception);
                                //throw (Exception)
                            }
                        }
                    }
                    //throw (Exception)
                }
            }
            // dev_set_preferences(...); only in hdevelop

            return;
        }

        // Local procedures 
        public void project_xy_to_image(HTuple hv_XC1, HTuple hv_YC1, HTuple hv_MeasurementPlaneAdjusted,
            HTuple hv_CameraParam, out HTuple hv_Row1, out HTuple hv_Column1)
        {



            // Local control variables 

            HTuple hv_HomMat3D = null, hv_Qx = null, hv_Qy = null;
            HTuple hv_Qz = null;
            // Initialize local and output iconic variables 
            HOperatorSet.PoseToHomMat3d(hv_MeasurementPlaneAdjusted, out hv_HomMat3D);
            HOperatorSet.AffineTransPoint3d(hv_HomMat3D, hv_XC1, hv_YC1, 0 * hv_XC1, out hv_Qx,
                out hv_Qy, out hv_Qz);
            HOperatorSet.Project3dPoint(hv_Qx, hv_Qy, hv_Qz, hv_CameraParam, out hv_Row1,
                out hv_Column1);

            return;
        }

        public void show_contours(HObject ho_Image, HObject ho_ModelContour, HObject ho_MeasureContour,
            HObject ho_ShapeModelContours, HTuple hv_WindowHandle, HTuple hv_Message)
        {




            // Local control variables 

            HTuple hv_Number = null;
            // Initialize local and output iconic variables 
            HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle);
            HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
            HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 2);
            HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "blue");
            HOperatorSet.DispObj(ho_ModelContour, hv_ExpDefaultWinHandle);
            HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 1);
            HOperatorSet.CountObj(ho_MeasureContour, out hv_Number);
            //Determine if the measure contours should be shown
            if ((int)(new HTuple(hv_Number.TupleGreater(0))) != 0)
            {
                HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "gray");
                HOperatorSet.DispObj(ho_MeasureContour, hv_ExpDefaultWinHandle);
            }
            HOperatorSet.CountObj(ho_ShapeModelContours, out hv_Number);
            if ((int)(new HTuple(hv_Number.TupleGreater(0))) != 0)
            {
                HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "white");
                HOperatorSet.DispObj(ho_ShapeModelContours, hv_ExpDefaultWinHandle);
            }
            //
            disp_message(hv_ExpDefaultWinHandle, hv_Message, "window", 12, 12, "black", "true");
            disp_continue_message(hv_ExpDefaultWinHandle, "black", "true");

            return;
        }

        public void extract_reference_points(HObject ho_Image, out HTuple hv_Rows, out HTuple hv_Columns)
        {



            // Local iconic variables 

            HObject ho_Region, ho_ConnectedRegions, ho_Rectangle;
            HObject ho_Snake, ho_QuarterCircles, ho_QuarterCirclesUnion;
            HObject ho_Star;

            // Local control variables 

            HTuple hv_Area1 = null, hv_Row1 = null, hv_Column1 = null;
            HTuple hv_Area2 = null, hv_Row2 = null, hv_Column2 = null;
            HTuple hv_Area3 = null, hv_Row3 = null, hv_Column3 = null;
            HTuple hv_Area4 = null, hv_Row4 = null, hv_Column4 = null;
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_Snake);
            HOperatorSet.GenEmptyObj(out ho_QuarterCircles);
            HOperatorSet.GenEmptyObj(out ho_QuarterCirclesUnion);
            HOperatorSet.GenEmptyObj(out ho_Star);
            try
            {
                //Extract reference points as center points of uniquely
                //identifiable regions.
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_Image, out ho_Region, 50, 255);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);
                ho_Rectangle.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_Rectangle, "num_sides",
                    "and", 3.5, 4.5);
                ho_Snake.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_Snake, "area", "and",
                    3000, 5000);
                ho_QuarterCircles.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_QuarterCircles, "area",
                    "and", 1000, 3000);
                ho_QuarterCirclesUnion.Dispose();
                HOperatorSet.Union1(ho_QuarterCircles, out ho_QuarterCirclesUnion);
                ho_Star.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_Star, ((new HTuple("area")).TupleConcat(
                    "outer_radius")).TupleConcat("num_sides"), "and", ((new HTuple(7500)).TupleConcat(
                    70)).TupleConcat(3), ((new HTuple(10000)).TupleConcat(90)).TupleConcat(
                    5));
                HOperatorSet.AreaCenter(ho_Star, out hv_Area1, out hv_Row1, out hv_Column1);
                HOperatorSet.AreaCenter(ho_Rectangle, out hv_Area2, out hv_Row2, out hv_Column2);
                HOperatorSet.AreaCenter(ho_Snake, out hv_Area3, out hv_Row3, out hv_Column3);
                HOperatorSet.AreaCenter(ho_QuarterCirclesUnion, out hv_Area4, out hv_Row4,
                    out hv_Column4);
                hv_Rows = new HTuple();
                hv_Rows = hv_Rows.TupleConcat(hv_Row1);
                hv_Rows = hv_Rows.TupleConcat(hv_Row2);
                hv_Rows = hv_Rows.TupleConcat(hv_Row3);
                hv_Rows = hv_Rows.TupleConcat(hv_Row4);
                hv_Columns = new HTuple();
                hv_Columns = hv_Columns.TupleConcat(hv_Column1);
                hv_Columns = hv_Columns.TupleConcat(hv_Column2);
                hv_Columns = hv_Columns.TupleConcat(hv_Column3);
                hv_Columns = hv_Columns.TupleConcat(hv_Column4);
                ho_Region.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_Rectangle.Dispose();
                ho_Snake.Dispose();
                ho_QuarterCircles.Dispose();
                ho_QuarterCirclesUnion.Dispose();
                ho_Star.Dispose();

                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_Region.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_Rectangle.Dispose();
                ho_Snake.Dispose();
                ho_QuarterCircles.Dispose();
                ho_QuarterCirclesUnion.Dispose();
                ho_Star.Dispose();

                throw HDevExpDefaultException;
            }
        }



    }



}


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值