那么多瓶液体,不想用那么多卡尺工具一个卡怎么办?上代码啊
添加CogToolBlock不用多说
我直接一个CogPMAlignTool直接找出全部
再添加一个CogCaliperTool随机卡出一个液位
不想多个卡尺工具,那咱们就用for
卡尺中心点按照CogPMAlignTool的中心原点定位一个一个卡,然后重新运行CogCaliperTool工具,话不多说直接上代码
#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.Caliper;
using Cognex.VisionPro.Dimensioning;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
//声明文本
private CogGraphicLabel Label;
private CogGraphicCollection cc = new CogGraphicCollection();//图形集合
#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
cc.Clear();//图形集合清空
CogPMAlignTool pma = new CogPMAlignTool();//new CogPMAlignTool工具
pma = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;//找他
CogCaliperTool cal = new CogCaliperTool();//卡尺new他
cal = mToolBlock.Tools["CogCaliperTool1"]as CogCaliperTool;//再找他
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
//CogPMAlignTool找到几个循环几次
for (int i = 0; i < pma.Results.Count; i++)
{
cal.Region.CenterX= pma.Results[i].GetPose().TranslationX;//卡尺工具更换中心点
cal.Region.CenterY = pma.Results[i].GetPose().TranslationY;
cal.Run();//运行卡尺工具
CogRectangle z = new CogRectangle();//new一个方形,也可以线段
double x = cal.Results[0].Edge1.PositionX;//找到卡尺第二个线段坐标
double y = cal.Results[0].Edge1.PositionY;
if(cal.Results[0].Width > 110 && cal.Results[0].Width < 130){//制定标准
Label = new CogGraphicLabel();//循环一次new一个文本
Label.SetXYText(x, y, "OK "+cal.Results[0].Width.ToString("f2"));//给文本位置
cc.Add(Label);//文本添加到图形集合
}else{
Label = new CogGraphicLabel();
Label.SetXYText(x, y,"NG " +cal.Results[0].Width.ToString("f2"));
Label.Color = CogColorConstants.Red;//不合格干成红色
cc.Add(Label);
}
z.Color = CogColorConstants.Red;
z.SetXYWidthHeight(x-8,y,15,0.01);//给方形位置微调他
cc.Add(z);//添加方形
}
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)
{
for (int i = 0; i < cc.Count; i++)
{
mToolBlock.AddGraphicToRunRecord(cc[i], lastRecord, "CogPMAlignTool1.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));
}
#endregion
}
写完后有一些不显示或者报错?
调整CogPMAlignTool中心原点位置或者CogCaliperTool大小
然后再来看看最终效果