使用Visual Studio调试VisonPro脚本
- 使用断点进行单步调试。
- 检查变量值和程序流程是否符合预期。
- 使用日志记录关键信息,便于问题追踪。
配合VS调试功能使用断点,便于调试观察内部运行过程。常用分为两种方式:
方式一:
1.VP创建项目配合脚本程序,开启脚本调试功能保存并退出。2.VS创建控制台程序,使用文本修改csproj,在Debug下添加VP打开程序入口路径,VS加载修改并执行。
方式二:
1.VP创建项目配合脚本程序,开启脚本调试功能保存并退出。2.VS创建控制台程序,添加附加,VS加载并运行VP项目。
项目准备
1.VisonPro创建工程
2.VisonPro脚本编辑
#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.PMAlign;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.CalibFix;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
//1.定义标签变量
//定义一个图形ConGraphicLabel数据类型 myLabel变量名 对象名 new创建
CogGraphicLabel myLabel = new CogGraphicLabel();
#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);
//2.获取工具以及工具运行结果
//获取工具 例如卡尺
CogCaliperTool c = mToolBlock.Tools["CogCaliperTool1"] as CogCaliperTool;
//设置标签 三个参数的函数 第一个x坐标 第二个y坐标 第三个要显示的文字
myLabel.SetXYText(10, -80, "宽度:" + c.Results[0].Width.ToString("F2"));
myLabel.Font = new Font ("微软黑体", 20);
myLabel.Color = Cognex.VisionPro.CogColorConstants.Red;
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)
{
//3.把结果放置到图形上
//AddGraphicToRunRecord 四个参数
// 第一个:运行记录 固定的 lastRecord
// 第二个:放置的位置 图像信息
// 第三个:内容可以是空字符
mToolBlock.AddGraphicToRunRecord(myLabel , lastRecord,"CogFixtureTool1.OutputImage","");
}
#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
}
3.Visual Studio项目创建
方式一: 修改项目文件 csproj
//需要添加部分
<StartAction>Program</StartAction>
<StartProgram>D:\program files\Cognex\VisionPro\VisionPro\bin\Cognex.VisionPro.QuickBuild.exe</StartProgram>