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.ImageProcessing;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.Caliper;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion

  //创建一个新的数组
  ArrayList labelList = new ArrayList();
  /// <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);
      tool.Run();
    //清除数组中的数据
    labelList.Clear();
    
    //获取到工具
    CogFindCircleTool CT1 = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;
    double CT1_X = CT1.Results.GetCircle().CenterX;
    double CT1_Y = CT1.Results.GetCircle().CenterY;
    double CT1_Z = 1.0;
            
    CogFindCircleTool CT2 = mToolBlock.Tools["CogFindCircleTool2"] as CogFindCircleTool;
    double CT2_X  = CT2.Results.GetCircle().CenterX;
    double CT2_Y = CT2.Results.GetCircle().CenterY;
    double CT2_Z = 2.0;
    
    CogFindCircleTool CT3 = mToolBlock.Tools["CogFindCircleTool3"] as CogFindCircleTool;
    double CT3_X  = CT3.Results.GetCircle().CenterX;
    double CT3_Y = CT3.Results.GetCircle().CenterY;
    double CT3_Z = 3.0;
    
    CogFindCircleTool CT4 = mToolBlock.Tools["CogFindCircleTool4"] as CogFindCircleTool;
    double CT4_X  = CT4.Results.GetCircle().CenterX;
    double CT4_Y = CT4.Results.GetCircle().CenterY;
    double CT4_Z = 4.0;
    
    //给数组中添加数值
     labelList.Add(setLabel(CT1_X, CT1_Y, Convert.ToString(CT1_Z), CogColorConstants.Red));
     labelList.Add(setLabel(CT2_X, CT2_Y, Convert.ToString(CT2_Z), CogColorConstants.Red));
     labelList.Add(setLabel(CT3_X, CT3_Y, Convert.ToString(CT3_Z), CogColorConstants.Red));
     labelList.Add(setLabel(CT4_X, CT4_Y, Convert.ToString(CT4_Z), CogColorConstants.Red));
   // if  CT.Results.GetCircle().CenterX = null
     

    
//CogGraphicLebel 
    
    return false;
  }
  
  //创建一个新方法
 
  CogGraphicLabel setLabel(double x, double y, string mValue, CogColorConstants color)
  {
    CogGraphicLabel label = new CogGraphicLabel();
    label.SetXYText(x, y, mValue);
    label.Alignment = CogGraphicLabelAlignmentConstants.BaselineLeft;
    label.Color = color;
    label.SelectedSpaceName = "#";
    label.Font = new Font("宋体", 12);
    return label;
  }

  #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)
  {
    //显示
    foreach (ICogGraphic g in labelList)
    {
      mToolBlock.AddGraphicToRunRecord(g, lastRecord, lastRecord.SubRecords[1].RecordKey, "");
    }
  }
  #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

}

实现的功能主要是:

1)准确匹配蓝框中四个圆柱,并按从左到右显示编号

2将得到的圆柱结果连点拟合成线L1

3)准确抓取三条小线段,拟合直线L2

上面是代码的内容,下面是实现的效果。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高高呀~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值