码迷,mamicode.com
首页 > 其他好文 > 详细

VisionPro脚本1

时间:2019-10-21 17:52:03      阅读:554      评论:0      收藏:0      [点我收藏+]

标签:drawing   each   list()   rem   val   local   csu   array   region   

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

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  CogGraphicLabel myLabel = new CogGraphicLabel();
  ArrayList Label_List = new ArrayList();
  #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

    CogBlobTool CogBlobTool1 = new CogBlobTool(); //创建BlobTool工具
    bool Output_Result = true;
    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
    {
      mToolBlock.RunTool(tool, ref message, ref result);      
      if((tool.Name == "CogBlobTool1") && Output_Result)
      {
        CogBlobTool1 = tool as CogBlobTool; //工具赋上对象
                        
        #region 判断一,判断PIN数量是否正确
        int blobscount = CogBlobTool1.Results.GetBlobs().Count;
        if((int) mToolBlock.Inputs["Input_Counter"].Value != blobscount)
        {
          Output_Result = false;       
        }
        #endregion
        
        #region 判断二,判断PIN的坐标差
        if(Output_Result)
        {
          CogBlobResultCollection mBlobResults = CogBlobTool1.Results.GetBlobs(); 
          //获取坐标差初值
          double XDif = 0.0f,YDif = 0.0f;       //X 差值、Y 差值
          double MaxDIS = 0.0f,LastDIS = 0.0f;  //两个PIN之间距离
          for(int i = 1;i < blobscount;i++)
          {
            XDif = mBlobResults[i].CenterOfMassX - mBlobResults[i - 1].CenterOfMassX; //X 差值
            YDif = mBlobResults[i].CenterOfMassY - mBlobResults[i - 1].CenterOfMassY; //Y 差值
            LastDIS = System.Math.Sqrt(XDif * XDif + YDif * YDif);                    //两个PIN之间距离
            if(LastDIS > MaxDIS){MaxDIS = LastDIS;}                                   //得到两点最大差值         
          }
          //输出最大两个PIN差值
          mToolBlock.Outputs["Output_PinDIS1"].Value = MaxDIS;
        }  
        #endregion        
             
      }
    }
    //显示结果  
    myLabel.Alignment = CogGraphicLabelAlignmentConstants.BaselineLeft;
    myLabel.SelectedSpaceName = "#";
    if(Output_Result)
    { 
      myLabel.SetXYText(2400, 400, "OK");     
      myLabel.Color = CogColorConstants.Green;
    }
    else 
    { 
      myLabel.SetXYText(2400, 400, "NG");
      myLabel.Color = CogColorConstants.Red;
    }
    myLabel.BackgroundColor = CogColorConstants.Yellow;
    myLabel.Font = new Font("Arial", 50, FontStyle.Bold, GraphicsUnit.Pixel);
    Label_List.Add(myLabel);
    
    
    
    //输出结果
    mToolBlock.Outputs["Output_Result"].Value = Output_Result;

    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)
  {
    ICogRecord hold = lastRecord.SubRecords["CogFixtureTool1.OutputImage"];
    lastRecord.SubRecords.Clear();
    lastRecord.SubRecords.Add(hold);
    foreach (CogGraphicLabel Lable in Label_List)
    {
      mToolBlock.AddGraphicToRunRecord(Lable, lastRecord, "CogFixtureTool1.OutputImage", "");
    }
  }
  #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

}
View Code

 

VisionPro脚本1

标签:drawing   each   list()   rem   val   local   csu   array   region   

原文地址:https://www.cnblogs.com/MWM0301/p/11714870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!