在Visionpro中编写C#脚本

在 V i s i o n p r o 中 编 写 C S h a r p 脚 本 在Visionpro中编写CSharp脚本 VisionproCSharp

在这里插入图片描述

using System;
using Cognex.VisionPro;
using Cognex.VisionPro3D;
using Cognex.VisionPro.ToolGroup;
using System.Windows.Forms;
using System.Drawing ;
using Cognex.VisionPro.ImageProcessing ;
using Cognex.VisionPro.Caliper;


public class UserScript : CogToolGroupBaseScript
{
  private double Radius = 0;
  private CogCircle myCircle;
  private CogFindCircleTool myCF;
  
  // The GroupRun function is called when the tool group is run.  The default
  // implementation provided here is equivalent to the normal behavior of the
  // tool group.  Modifying this function will allow you to change the behavior
  // when the tool group is run.
  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 in the tool group using the RunTool function
    for (Int32 toolIdx = 0; toolIdx < toolGroup.Tools.Count; toolIdx++)
      toolGroup.RunTool(toolGroup.Tools[toolIdx], ref message, ref result);
    

    myCF = (CogFindCircleTool)toolGroup.Tools["CogFindCircleTool1"];

    myCircle = myCF.Results.GetCircle();
        
    Radius = double.Parse(myCircle.Radius.ToString("0.00"));
    
    
    // Returning False indicates we ran the tools in script, and they should not be
    // run by VisionPro 
    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"
  // Allows you to add or modify the contents of the last run record when it is
  // created.  For example, you might add custom graphics to the run record here.
  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.Blue;
    ResultLabel.SelectedSpaceName = @"@\Fixture";
    toolGroup.AddGraphicToRunRecord(ResultLabel, lastRecord, "Image Source.OutputImage", "script");
    toolGroup.AddGraphicToRunRecord(myCircle, lastRecord, "Image Source.OutputImage", "script");
    /*
    Should only be called from tool group script. Adds a graphic to a specified run record
    参数一: A CogGraphic object to add
    参数二: A tree of CogRecords
    参数三: The name of the specific record where the graphic should be added
    参数四: A key string used to identify your graphic 
    */
    
    for( int i = 0 ;i < myCF.Results.Count - 1;i++)
    {
      CogCompositeShape myCS = new CogCompositeShape();
      //myCS = myCF.Results.Item(i).CreateResultGraphics(Cognex.VisionPro.Caliper.CogCaliperResultGraphicConstants.All);
      toolGroup.AddGraphicToRunRecord(myCS, lastRecord, "Image Source.OutputImage", "script");
    }
    
    
  }
#endregion

#region "When the Script is Initialized"
  // Perform any initialization required by your script here
  public override void Initialize(CogToolGroup host)
  {
    // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
    myCF = new  CogFindCircleTool();
    myCircle = new CogCircle();
    base.Initialize(host);
  }
#endregion

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值