Visionpro获取某个工具的值+添加标签

点击查看代码
#region namespace imports
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.Dimensioning;
using Cognex.VisionPro.Caliper;

#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  private List<CogGraphicLabel> mLabels;//声明标签变量
  #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 void Label(double postion_x,double postion_y,string lab,object TB_result,bool TB_Status)
  {
    string tb_result;
    if(TB_result is string)
    {
      tb_result = TB_result.ToString();
    }
    else if(TB_result is double)
    {
      tb_result = Convert.ToDouble(TB_result).ToString("0.000");
    }
    else
    {
      tb_result = "数据错误";
    }
    CogGraphicLabel tempLabe = new CogGraphicLabel();
    tempLabe.Alignment = CogGraphicLabelAlignmentConstants.BaselineLeft;
    tempLabe.SetXYText(postion_x, postion_y, lab+":" + tb_result);
    tempLabe.Color = CogColorConstants.Green;
    Font f = new Font("", 25);
    tempLabe.Font = f;
    if(TB_Status)
    {
      tempLabe.Color = CogColorConstants.Green;
      
    }
    else
    {
      tempLabe.Color = CogColorConstants.Red;
    }
    //tempLabe.BackgroundColor = CogColorConstants.Blue;
    mLabels.Add(tempLabe);
  }
  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
    //bool Finalresult = true;
    mLabels.Clear();
    // Run each tool using the RunTool function

    double r1,r2,d1,concentricity;
    r1 = r2 = d1 = concentricity = 0;
    foreach(ICogTool tool in mToolBlock.Tools)
    {
      //获取某个工具的值
      mToolBlock.RunTool(tool, ref message, ref result);
      if(tool.Name == "CogFindCircleTool1")
      {
        CogFindCircleTool Circle1 = tool as CogFindCircleTool;
         r1 = Circle1.Results.GetCircle().Radius;
        
      }
      if(tool.Name == "CogFindCircleTool2")
      {
        CogFindCircleTool Circle2 = tool as CogFindCircleTool;
        r2 = Circle2.Results.GetCircle().Radius;
        
      }
      if(tool.Name == "CogDistancePointPointTool1")
      {
        CogDistancePointPointTool Distance1 = tool as CogDistancePointPointTool;
        d1 = Distance1.Distance;
        
      }
      //if(result!=CogToolResultConstants.Accept)
     // {
      //  Finalresult = false;
      //}
      
    }
    //计算
    concentricity = d1 - (r2 - r1);
    
      
    //string X = Convert.ToDouble(mToolBlock.Outputs["deltaX"].Value).ToString("F3");
    //string Y = Convert.ToDouble(mToolBlock.Outputs["deltaY"].Value).ToString("F3");
    //string R = Convert.ToDouble(mToolBlock.Outputs["deltaR"].Value).ToString("F3");

      //if(result == CogToolResultConstants.Accept)
    

      //if(0<1)
      //{
      //方法调用
      Label(1400, 1000, "Result","OK",true);
      Label(1400, 1200, "Concentricity",concentricity,true);
      //Labe(200, 800, "Y",Y,true);
      //Labe(200, 1000, "R",R,true);
      //}
      
      //else
      //{
      //result = CogToolResultConstants.Error;
      //Labe(1400, 1000, "Result","NG",false);
      //Labe(1400, 1200, "Concentricity", C1,false);
      //Labe(200, 800,  "X",Y,false);
      //Labe(200, 1000,  "X",R,false);
      //}
    //输出到Concentricity
    this.mToolBlock.Outputs["Concentricity"].Value = concentricity;

    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)
  {
    foreach(CogGraphicLabel label in mLabels)
    {
      //显示图层
      mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogFindCircleTool1.InputImage", " ");
    }
  }
  #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));
    mLabels = new List<CogGraphicLabel>();//初始化
  }
  #endregion

}


posted @   密西西比弗斯  阅读(118)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示