VisionPro之脚本

一、VisionPro脚本介绍

1.1项目组成

​ 首先介绍一下QuickBuild的项目结构,Job是QuickBuild工程中的基本组成单位,一个QuickBuild至少有一个Job,每个Job之间互不影响。每个Job中默认包含一个toolGroup,在toolGroup中可以添加项目需要的工具和工具块。
​ 其中工具块(toolBlock)和工具组(toolGroup)都是工具的“容器”,通过工具块和工具组对某些功能的工具进行封装,实现项目模块化。但一般都是使用toolBlock较多,是因为toolBlock添加输入输出的方式比较灵活,甚至toolBlock可以在“输入输出”页面查看输入输出数据的当前值、类型等信息,甚至可以手动修改输入输出的值,toolGroup则不支持这些功能。

在这里插入图片描述

1.2 VisionPro脚本简介

VisionPro工具封装了视觉算法与用户交互界面,toolGroup与toolBlock提供了组合工具的容器,但是并非所有的功能都能通过既定交互界面实现。VisionPro本身是无法实现逻辑功能,但可以通过VisionPro中的脚本功能实现逻辑判断。

二、脚本类与方法

VisionPro通过”多态”技术实现脚本功能,VisionPro 的每一Job、toolGroup、toolBlock对象都含有一个接口对象,用户通过重写接口方法实现自定义拓展功能。以toolBlock为例, CogToolBlockAdvancedScriptBase接口中定义了子类中必须实现的函数,当toolBlock执行到某一节点(工具准备运行、工具运行完成等)时会调用相应的接口函数实现用户指定的功能。

2.1 toolBlock脚本类的分析

(1)添加命名空间的区域,需要用到工具什么就进行添加。脚本的流程:1、初始化+工具关联,运行+输出结果,逻辑判断

#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.QuickBuild.Implementation.Internal;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.Caliper;
#endregion

这里会可能出现错误,比如会出现没有程序集的添加,这要手动添加参考程序集,步骤如下。

1)若不知道要添加什么,可以先收索需要添加相关单词,再去程序集中查找添加:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mptk8Nyo-1680602287158)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20230404171515544.png)]

2)在参考程序集中添加需要的程序集。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dncQeE49-1680602287159)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20230404171820826.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cCga3tkP-1680602287159)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20230404172216899.png)]

(2)脚本中添加变量如下:

  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  private double Radius = 0;
  private CogFindCircleTool CogFindCircleToolObject;
  private CogCircle myCircle;
  #endregion

(3)重写GroupRun,主要都是在这里进行脚本的更改和添加,这里可以添加输出结果和逻辑判断。(其中工具关联也可以放在后面的初始化那边,这个代码位置不是硬性要求的,根据具体情况具体分析)

public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);//循环运行工具
    
    CogFindCircleToolObject = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;//工具关联
    
    myCircle = CogFindCircleToolObject.Results.GetCircle();
    
    Radius = double.Parse(myCircle.Radius.ToString("0.00"));
 	
    //可以继续添加逻辑判断等操作
    return false;
  }

(4)重写ModifyCurrentRunRecord方法,这个用于修改CurrentRecord,在toolBlock的CurrentRecord被创建后调用。这个一般用不到。

  public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
  {
  }

(5)重写ModifyLastRunRecord方法,这个用于修改LastRunRecord,在toolBlock的LastRunRecord被创建后调用,例如:在最终生成图像中添加标签、该表颜色、用不同几何图像标记目标区域,例如代码:

  public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
    CogGraphicLabel ResultLabel = new CogGraphicLabel();
    string labelStr = string.Format("Radius={0:F2} pixel", Radius);
    ResultLabel.SetXYText(myCircle.CenterX, myCircle.CenterY, labelStr);
    ResultLabel.Color = Cognex.VisionPro.CogColorConstants.Red;
    mToolBlock.AddGraphicToRunRecord(ResultLabel, lastRecord, "CogImageConvertTool1.OutputImage", "script");
  }

(6)重写Initialize方法,这个用于对toolBlock工具进行初始化,当退出脚本编辑工具时脚本会进行编译并进行初始化,此时该方法会被调用。(这里可以放些工具关联的相关代码)

  public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
  {
    base.Initialize(host); 
    this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock) (host));
  }

VisionPro总脚本:

#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.QuickBuild.Implementation.Internal;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.Caliper;
#endregion
 
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  private double Radius = 0;
  private CogFindCircleTool CogFindCircleToolObject;
  private CogCircle myCircle;
  #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);
    
    CogFindCircleToolObject = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;
    
    myCircle = CogFindCircleToolObject.Results.GetCircle();
    
    Radius = double.Parse(myCircle.Radius.ToString("0.00"));
 
    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 ResultLabel = new CogGraphicLabel();
    string labelStr = string.Format("Radius={0:F2} pixel", Radius);
    ResultLabel.SetXYText(myCircle.CenterX, myCircle.CenterY, labelStr);
    ResultLabel.Color = Cognex.VisionPro.CogColorConstants.Red;
    mToolBlock.AddGraphicToRunRecord(ResultLabel, lastRecord, "CogImageConvertTool1.OutputImage", "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
 
}
 

三、运行结果

找圆,并用脚本写显示输出圆的半径运行结果:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值