VisionPro QuickBuild工具
VisionPro QuickBuild是康耐视旗下的与相机配套的软件,主要是设计了一些底层算法,免去了开发者要进行底层开发的工作量,而其中的ToolBlock工具相当于一个函数,可以在这个函数里面完成你想完成的功能,整个软件主要是通过拖动工具箱里的工具完成你所想要完成的功能,总的来说是一款不错的软件。
当你从创建了一个VPP文件之后,再往里面拖动一个ToolBlock工具时,此时就可以用两种方式加载该VPP,分别是toolblock工具和整个QuickBuild,通常用得比较多的是加载ToolBlock工具,可以从c#加载图片进入到ToolBlock工具,完成采集到检测的功能。注意保存ToolBlock工具时,需要保存完整的工具。
如何确定一个vpp文件是ToolBlock文件还是QuickBuild文件,在Cognex的安装路径下,直接cmd,在使用VPPVersion工具即可。
ToolBlock脚本
ToolBlock有内置脚本,位于ToolBlock内部,主要分为简单脚本和高级脚本,ToolBlock的脚本被编译之后一般都是加载到内存之中,当你不断修改脚本,调试时,可能会存在脚本占用的内存会有小幅度上升的情况,清除的方法是将整个Job重新打开。
ToolBlock的脚本可以执行一些简单的操作,例如在图像中放入Label的代码如下所示:
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//定义变量
//定义一个图形标签
CogGraphicLabel mylabel = new CogGraphicLabel();
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);
//定义一个卡尺工具
CogCaliperTool cc = mToolBlock.Tools["CogCaliperTool1"] as CogCaliperTool;
//获取结果,获取工具的地方
mylabel.SetXYText(0, -50, "宽度"+cc.Results[0].Width.ToString("F2"));
mylabel.Color = CogColorConstants.Cyan;
mylabel.Font = new Font("楷体", 15);
return false;
}
#region When the Current Run Record is Created
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)
{
//输出图像的地方,在此处进行结果的放置
//第一个参数,要放置的图像,mylabel
//第二个参数,运行记录,固定参数lastRecord
//第三个参数,放置的位置,图像信息
//第四个参数,空字符串
mToolBlock.AddGraphicToRunRecord(mylabel,lastRecord,"CogFixtureTool1.InputImage"," ");
}
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));
}
}
需要写代码的地方主要有三部分,第一部分:在类的开始定义变量,第二部分,在GroupRun的函数内部写代码,那一步主要是通过for函数一次运行内部的工具,在后面在进行获取结果等操作;第三部分,主要是将你想展示出来的结果,放到图片的Record以及工具最后的Output部分,如果不在第三部分进行对应的操作,图片上可能不会出现你期待的结果。
C#加载ToolBlock工具
加载过程中最重要的是声明一个CogToolBlock工具,该工具的Run()方法即是运行ToolBlock,获得检测结果。运行ToolBlock工具以及显示在CogRecordDisplay的代码如下所示:
CogToolBlock tbx = new CogToolBlock(); //ToolBlock工具
CogImageFile cogimage_pic1 = new CogImageFile(); //图像读取工具
CogFixtureTool cft_pic1; //ToolBlock工具的其中的小工具
private void Form1_Load(object sender, EventArgs e)
{
//初始化操作
tbx = CogSerializer.LoadObjectFromFile(vpp1path) as CogToolBlock;
cft_pic1 = tbx.Tools["CogFixtureTool1"] as CogFixtureTool;
//ToolBlock里面会拖入很多的工具,想要使用某种工具的结果,都可以去调用你ToolBlock文件中拖入工具的名称。
path_pic1= Directory.GetFiles(img1path);
}
private void Imageshow_record1()
{
orderpic1 %= path_pic1.Length;
cogimage_pic1.Open(path_pic1[orderpic1], CogImageFileModeConstants.Read);
cft_pic1.InputImage = cogimage_pic1[0]; //给ToolBlock工具中输入图片
tbx.Run(); //让ToolBlock工具运行
//将结果显示在cogRecordDisplay1上
Invoke(new Action(() =>
{
try
{
cogRecordDisplay1.InteractiveGraphics.Clear();
cogRecordDisplay1.StaticGraphics.Clear();
cogRecordDisplay1.Image = cft_pic1.OutputImage;
cogRecordDisplay1.Record = cft_pic1.CreateLastRunRecord();
cogRecordDisplay1.AutoFit = true;//自适应
cogRecordDisplay1.Fit();
}
catch (Exception ex)
{
MessageBox.Show($"Error displaying record: {ex.Message}");
}
}));
var now = DateTime.Now;
//存图
if (saveimg)
{
string fileNameWithoutExtension = "reslt_";
fileNameWithoutExtension += now.ToString("yyyy_MM_dd_HH_mm_ss");
string imagname = ressavePath + fileNameWithoutExtension + ".bmp";
Cognex.VisionPro.ICogImage img = cogRecordDisplay1.Image;
Bitmap bitmap = (System.Drawing.Bitmap)img.ToBitmap();
Bitmap img1 = (System.Drawing.Bitmap)bitmap.Clone();
img1.Save(imagname, System.Drawing.Imaging.ImageFormat.Bmp);
}
orderpic1 += 1;
WriteLog(logContent.ToString());
}