#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.Caliper;
using Cognex.VisionPro.Dimensioning; //测量工具
using System.Collections.Generic;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
CogGraphicCollection gc = new CogGraphicCollection();
CogGraphicLabel ResultLabel = new CogGraphicLabel();
List<CogGraphicLabel> labels = new List<CogGraphicLabel>();
#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
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
{
if( tool.Name != "CogCaliperTool2")
{
mToolBlock.RunTool(tool, ref message, ref result);
}
} /**/
CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
CogCaliperTool c = mToolBlock.Tools["CogCaliperTool2"] as CogCaliperTool;
gc.Clear();
CogDistanceSegmentLineTool dis = mToolBlock.Tools["CogDistanceSegmentLineTool1"] as CogDistanceSegmentLineTool;
labels.Clear();
foreach(CogPMAlignResult r in pma.Results)
{
c.Region.CenterX = r.GetPose().TranslationX ;
c.Region.CenterY = r.GetPose().TranslationY ;
mToolBlock.RunTool(c, ref message, ref result);
CogLineSegment s = new CogLineSegment();
s.StartX = c.Results.Edges[0].PositionX;
s.StartY= c.Results.Edges[0].PositionY;
s.EndX = c.Results.Edges[0].PositionX+20;
s.EndY = c.Results.Edges[0].PositionY;
s.Color = CogColorConstants.Red;
s.LineWidthInScreenPixels = 5;
gc.Add(s);
dis.Segment = s;
mToolBlock.RunTool(dis, ref message, ref result);
CogGraphicLabel label = new CogGraphicLabel();
//label.SetXYText(s.StartX, s.StartY, dis.Distance.ToString("F4"));
if(dis.Distance > 20)
{
label.SetXYText(s.StartX, s.StartY, "NG");
}
else
{
label.SetXYText(s.StartX, s.StartY, "OK");
}
labels.Add(label);
}
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
/// <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 t in gc)
{
//mToolBlock.AddGraphicToRunRecord( t,lastRecord ,"CogPMAlignTool1.InputImage" ,"");
mToolBlock.AddGraphicToRunRecord( t,lastRecord ,"CogIPOneImageTool1.OutputImage" ,"");
//ResultLabel.Color = Cognex.VisionPro.CogColorConstants.Blue;
//mToolBlock.AddGraphicToRunRecord(ResultLabel, lastRecord, "CogIPOneImageTool1.OutputImage", "script");
}
foreach(CogGraphicLabel label in labels)
{
mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogIPOneImageTool1.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
}