#region namespace imports
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.Caliper;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//创建一个新的数组
ArrayList labelList = new ArrayList();
/// <summary>
/// Called when the parent tool is run.
/// Add code here to customize or replace the normal run behavior.
/// </summary>
/// <param name="message">Sets the Message in the tool's RunStatus.</param>
/// <param name="result">Sets the Result in the tool's RunStatus</param>
/// <returns>True if the tool should run normally,
/// False if GroupRun customizes run behavior</returns>
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
// To let the execution stop in this script when a debugger is attached, uncomment the following lines.
// #if DEBUG
// if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
// #endif
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
// mToolBlock.RunTool(tool, ref message, ref result);
tool.Run();
//清除数组中的数据
labelList.Clear();
//获取到工具
CogFindCircleTool CT1 = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;
double CT1_X = CT1.Results.GetCircle().CenterX;
double CT1_Y = CT1.Results.GetCircle().CenterY;
double CT1_Z = 1.0;
CogFindCircleTool CT2 = mToolBlock.Tools["CogFindCircleTool2"] as CogFindCircleTool;
double CT2_X = CT2.Results.GetCircle().CenterX;
double CT2_Y = CT2.Results.GetCircle().CenterY;
double CT2_Z = 2.0;
CogFindCircleTool CT3 = mToolBlock.Tools["CogFindCircleTool3"] as CogFindCircleTool;
double CT3_X = CT3.Results.GetCircle().CenterX;
double CT3_Y = CT3.Results.GetCircle().CenterY;
double CT3_Z = 3.0;
CogFindCircleTool CT4 = mToolBlock.Tools["CogFindCircleTool4"] as CogFindCircleTool;
double CT4_X = CT4.Results.GetCircle().CenterX;
double CT4_Y = CT4.Results.GetCircle().CenterY;
double CT4_Z = 4.0;
//给数组中添加数值
labelList.Add(setLabel(CT1_X, CT1_Y, Convert.ToString(CT1_Z), CogColorConstants.Red));
labelList.Add(setLabel(CT2_X, CT2_Y, Convert.ToString(CT2_Z), CogColorConstants.Red));
labelList.Add(setLabel(CT3_X, CT3_Y, Convert.ToString(CT3_Z), CogColorConstants.Red));
labelList.Add(setLabel(CT4_X, CT4_Y, Convert.ToString(CT4_Z), CogColorConstants.Red));
// if CT.Results.GetCircle().CenterX = null
//CogGraphicLebel
return false;
}
//创建一个新方法
CogGraphicLabel setLabel(double x, double y, string mValue, CogColorConstants color)
{
CogGraphicLabel label = new CogGraphicLabel();
label.SetXYText(x, y, mValue);
label.Alignment = CogGraphicLabelAlignmentConstants.BaselineLeft;
label.Color = color;
label.SelectedSpaceName = "#";
label.Font = new Font("宋体", 12);
return label;
}
#region When the Current Run Record is Created
/// <summary>
/// Called when the current record may have changed and is being reconstructed
/// </summary>
/// <param name="currentRecord">
/// The new currentRecord is available to be initialized or customized.</param>
public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
{
}
#endregion
#region When the Last Run Record is Created
/// <summary>
/// Called when the last run record may have changed and is being reconstructed
/// </summary>
/// <param name="lastRecord">
/// The new last run record is available to be initialized or customized.</param>
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
//显示
foreach (ICogGraphic g in labelList)
{
mToolBlock.AddGraphicToRunRecord(g, lastRecord, lastRecord.SubRecords[1].RecordKey, "");
}
}
#endregion
#region When the Script is Initialized
/// <summary>
/// Perform any initialization required by your script here
/// </summary>
/// <param name="host">The host tool</param>
public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
{
// DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
base.Initialize(host);
// Store a local copy of the script host
this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
}
#endregion
}
实现的功能主要是:
(1)准确匹配蓝框中四个圆柱,并按从左到右显示编号
(2)将得到的圆柱结果连点拟合成线L1
(3)准确抓取三条小线段,拟合直线L2
上面是代码的内容,下面是实现的效果。