本文将介绍利用利用C#脚本实现在VisionPro中显示卡尺极性。
目录
VPP编写
脚本编写
结果展示
VPP编写
Step1 创建Job并双击Image Source输入图像,点击单次运行
<最终在Job的右上角有一绿灯,表示Job运行成功,图像成功导入>
Step2 进入Job并插入工具块 ,将Image Source下的OutputImage输入到CogToolBlock工具中
Step3 进入工具块中实现找线功能,并在找线工具中添加终端输出卡尺极性
Step4 添加工具块输出,并修改输入输出名称
Step5 给工具块添加C#高级脚本
脚本编写
Step1 定义变量
/* 定义变量 */
#region Private Member Variables
private CogToolBlock mToolBlock;
string caliperPolarity;
double textX, textY;
#endregion
Step2 获取参数
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);
/* 实例化工具 */
CogImageConvertTool mImageConvert = (CogImageConvertTool)mToolBlock.Tools["CogImageConvertTool1"];
CogFindLineTool mFindLine = (CogFindLineTool)mToolBlock.Tools["CogFindLineTool1"];
/* 获取参数 */
caliperPolarity = this.mToolBlock.Outputs["CaliperPolarity"].Value.ToString(); //获取工具块中的结果
/// 等价于caliperPolarity = caliperPolarity = mFindLine.RunParams.CaliperRunParams.Edge0Polarity.ToString();
textX = mFindLine.Results[mFindLine.RunParams.NumCalipers - 1].X; //获取找线工具中最后一个点的横坐标X
textY = mFindLine.Results[mFindLine.RunParams.NumCalipers-1].Y; //获取找线工具中最后一个点的纵坐标Y
/// mFindLine.RunParams.NumCalipers为找线工具中的卡尺数量
return false;
}
Step3 显示卡尺极性
/* 显示卡尺极性 */
#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)
{
CogGraphicLabel label = new CogGraphicLabel(); //定义标签对象
string labelStr = string.Format("卡尺极性为:{0}", caliperPolarity); //格式化文本
label.SetXYText(textX,textY,labelStr); //设置标签
/// 参数1:标签的横坐标X,参数2:标签的纵坐标Y,参数3:标签内容
label.Font = new Font(labelStr, 20); //设置标签字体大小
label.Color = CogColorConstants.Red; //设置标签颜色
mToolBlock.AddGraphicToRunRecord(label,lastRecord,"CogImageConvertTool1.OutputImage","script"); //添加标签至图层中显示
/// 参数1:标签名,参数2:lastRecord,参数3:图层名,参数4:"script"
}
#endregion
按照step1-step3完成脚本编写,源码如下:
/* 显示卡尺极性ToolBlock */
#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.Caliper;
using Cognex.VisionPro.ImageProcessing;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private CogToolBlock mToolBlock;
string caliperPolarity;
double textX, textY;
#endregion
/// <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);
/* 实例化工具 */
CogImageConvertTool mImageConvert = (CogImageConvertTool)mToolBlock.Tools["CogImageConvertTool1"];
CogFindLineTool mFindLine = (CogFindLineTool)mToolBlock.Tools["CogFindLineTool1"];
/* 获取参数 */
caliperPolarity = this.mToolBlock.Outputs["CaliperPolarity"].Value.ToString(); //获取工具块中的结果
//等价于caliperPolarity = caliperPolarity = mFindLine.RunParams.CaliperRunParams.Edge0Polarity.ToString();
textX = mFindLine.Results[mFindLine.RunParams.NumCalipers - 1].X; //获取找线工具中最后一个点的横坐标X
textY = mFindLine.Results[mFindLine.RunParams.NumCalipers-1].Y; //获取找线工具中最后一个点的纵坐标Y
//mFindLine.RunParams.NumCalipers为找线工具中的卡尺数量
return false;
}
#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)
{
CogGraphicLabel label = new CogGraphicLabel(); //定义标签对象
string labelStr = string.Format("卡尺极性为:{0}", caliperPolarity); //格式化文本
label.SetXYText(textX,textY,labelStr); //设置标签
/// 参数1:标签的横坐标X,参数2:标签的纵坐标Y,参数3:标签内容
label.Font = new Font(labelStr, 20); //设置标签字体大小
label.Color = CogColorConstants.Red; //设置标签颜色
mToolBlock.AddGraphicToRunRecord(label,lastRecord,"CogImageConvertTool1.OutputImage","script"); //添加标签至图层中显示
/// 参数1:标签名,参数2:lastRecord,参数3:图层名,参数4:"script"
}
#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
}