#region namespace importsusingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.IO;usingSystem.Windows.Forms;usingCognex.VisionPro;usingCognex.VisionPro.ToolBlock;usingCognex.VisionPro3D;usingCognex.VisionPro.ImageProcessing;usingCognex.VisionPro.PMAlign;usingCognex.VisionPro.CalibFix;usingCognex.VisionPro.Caliper;#endregionpublicclassCogToolBlockAdvancedScript:CogToolBlockAdvancedScriptBase{#region Private Member VariablesprivateCognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;#endregionList<CogRectangle> labels =newList<CogRectangle>();/// <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>publicoverrideboolGroupRun(refstring message,refCogToolResultConstants 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)// mToolBlock.RunTool(tool, ref message, ref result);int cirCount =0;List<double> cirX =newList<double>();List<double> cirY =newList<double>();List<double> cirR =newList<double>();CogImageConvertTool convertT =(CogImageConvertTool) mToolBlock.Tools["CogImageConvertTool1"];CogPMAlignTool alignT =(CogPMAlignTool) mToolBlock.Tools["CogPMAlignTool1"];CogFindCircleTool findCirTool =(CogFindCircleTool) mToolBlock.Tools["CogFindCircleTool1"];
convertT.Run();CogImage8Grey gray =(CogImage8Grey) convertT.OutputImage;
alignT.Run();if(alignT.RunStatus.Result == CogToolResultConstants.Accept){
cirCount = alignT.Results.Count;for(int i =0;i < cirCount;i++){
cirX.Add(alignT.Results[i].GetPose().TranslationX);
cirY.Add(alignT.Results[i].GetPose().TranslationY);
findCirTool.RunParams.ExpectedCircularArc.CenterX = cirX[i];
findCirTool.RunParams.ExpectedCircularArc.CenterY = cirY[i];
findCirTool.Run();// // if(findCirTool.Results.Count != 0){
cirX[i]= findCirTool.Results.GetCircle().CenterX;
cirY[i]= findCirTool.Results.GetCircle().CenterY;
cirR.Add(findCirTool.Results.GetCircle().Radius);
labels.Add(findCirTool.Results.GetCircle().EnclosingRectangle(CogCopyShapeConstants.All));// }//输出终端添加// mToolBlock.Outputs.Add(new CogToolBlockTerminal("cirX", cirX));// mToolBlock.Outputs.Add(new CogToolBlockTerminal("cirY", cirY));// mToolBlock.Outputs.Add(new CogToolBlockTerminal("cirR", cirR));// mToolBlock.Outputs.Add(new CogToolBlockTerminal("count", cirCount));
mToolBlock.Outputs["cirX"].Value = cirX;
mToolBlock.Outputs["cirY"].Value = cirY;
mToolBlock.Outputs["cirR"].Value = cirR;
mToolBlock.Outputs["count"].Value = cirCount;// mToolBlock.Outputs.Add("cirY",cirY);// mToolBlock.Outputs.Add("cirR", cirR);// mToolBlock.Outputs.Add("count", labels.Count);}}returnfalse;}#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>publicoverridevoidModifyCurrentRunRecord(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>publicoverridevoidModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord){//最终结果显示if(labels.Count !=0){for(int i =0;i < labels.Count;i++){ICogGraphic label =(ICogGraphic) labels[i];
label.Color = CogColorConstants.Green;
mToolBlock.AddGraphicToRunRecord(label, lastRecord,"CogIPOneImageTool1.OutputImage","script");}}}#endregion#region When the Script is Initialized/// <summary>/// Perform any initialization required by your script here/// </summary>/// <param name="host">The host tool</param>publicoverridevoidInitialize(Cognex.VisionPro.ToolGroup.CogToolGroup host){// DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVEbase.Initialize(host);// Store a local copy of the script hostthis.mToolBlock =((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));}#endregion}