方法如下:
方法1:通过找圆工具上的点求解隔离纸边缘点-钢壳边缘点的距离。
#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;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.Dimensioning;
using System.Linq;
using System.Threading;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#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>
///
#region
CogGraphicCollection gc;
CogGraphicCollection gc1;
CogGraphicLabel label1;
CogGraphicLabel label2;
CogGraphicLabel label3;
CogGraphicLabel label4;
CogGraphicLabel label5;
CogGraphicLabel label6;
CogRectangleAffine rect;
// int s = 0;
//计算圆心度所需变量
double Min_Distance = 0.0;
double Max_Distance = 0.0;
//double Minus_Value = 0;
//double Point_CenterD = 0;
//double InputRMSCircle = 0;
// int s = 0;
#endregion
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
#region 显示运行
mToolBlock.Outputs["Result"].Value = 1;
//mToolBlock.Outputs["Area"].Value = 0;
mToolBlock.Outputs["bStatus"].Value = false;
//mToolBlock.Outputs["DistanceMax"].Value = 0;
//mToolBlock.Outputs["DistanceMin"].Value = 0;
mToolBlock.Outputs["GraphicCollection"].Value = null;
mToolBlock.Outputs["GraphicCollection1"].Value = null;
//数组下标对应0 1 2 3 4:产品定位NG或OK 两圆距离计算OK或NG 卡尺燕尾判断OK或NG
int[] AllResult = new int[4];
bool DecideResult = false;
CogFindCircleTool Pat1 = (CogFindCircleTool) mToolBlock.Tools["外圆定位"];
CogFixtureTool Fix = (CogFixtureTool) mToolBlock.Tools["矫正坐标系"];
CogFindCircleTool FindCircle = (CogFindCircleTool) mToolBlock.Tools["外圆"];
CogFindCircleTool FindCircle1 = (CogFindCircleTool) mToolBlock.Tools["隔离管外圆"];
CogDistancePointPointTool PointP_Distance = (CogDistancePointPointTool) mToolBlock.Tools["两圆距离"];
CogCaliperTool YanWeiCaliper = (CogCaliperTool) mToolBlock.Tools["卡尺燕尾"];
if (gc == null) gc = new CogGraphicCollection(); gc.Clear();
if (gc1 == null) gc1 = new CogGraphicCollection(); gc1.Clear();
if (label1 == null) label1 = new CogGraphicLabel();
if (label2 == null) label2 = new CogGraphicLabel();
if (label3 == null) label3 = new CogGraphicLabel();
if (label4 == null) label4 = new CogGraphicLabel();
if (label5 == null) label5 = new CogGraphicLabel();
if (label6 == null) label6 = new CogGraphicLabel();
if (rect == null) rect = new CogRectangleAffine();
label1.Font = new Font("宋体", 16);
label1.SelectedSpaceName = "#";
label1.Alignment = CogGraphicLabelAlignmentConstants.BaselineLeft;
label2.Font = new Font("宋体", 16);
label2.SelectedSpaceName = "#";
label2.Alignment = CogGraphicLabelAlignmentConstants.BaselineLeft;
label3.Font = new Font("宋体", 16);
label3.SelectedSpaceName = "#";
label3.Alignment = CogGraphicLabelAlignmentConstants.BaselineLeft;
label4.Font = new Font("宋体", 16);
label4.SelectedSpaceName = "#";
label4.Alignment = CogGraphicLabelAlignmentConstants.BaselineLeft;
label5.Font = new Font("宋体", 16);
label5.SelectedSpaceName = "#";
label5.Alignment = CogGraphicLabelAlignmentConstants.BaselineLeft;
label6.Font = new Font("宋体", 16);
label6.SelectedSpaceName = "#";
label6.Alignment = CogGraphicLabelAlignmentConstants.BaselineLeft;
Pat1.InputImage = (CogImage8Grey) mToolBlock.Inputs["InputImage"].Value;
Pat1.Run();
if (Pat1.Results != null && Pat1.Results.Count > 0)
{
label1.SetXYText(100, 50, "产品定位OK");
label1.Color = CogColorConstants.Green;
gc.Add(label1)