#regionnamespace importsusing 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.ImageProcessing;using Cognex.VisionPro.PMAlign;using Cognex.VisionPro.CalibFix;using Cognex.VisionPro.Caliper;using Cognex.VisionPro.Dimensioning;using Cognex.VisionPro.PixelMap;using Cognex.VisionPro.Blob;#endregionpublic class CogToolBlockAdvancedScript:CogToolBlockAdvancedScriptBase{#regionPrivate Member Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;Cognex.VisionPro.CogGraphicCollection graphics1=newCogGraphicCollection();Cognex.VisionPro.CogGraphicCollection graphics2=newCogGraphicCollection();Cognex.VisionPro.CogGraphicCollection graphics3=newCogGraphicCollection();//Cognex.VisionPro.CogCircle cogcir = new CogCircle();//Cognex.VisionPro.CogCircularArc circular = new CogCircularArc();CogPMAlignTool[]pma=new CogPMAlignTool[5];CogFixtureTool[]fix=new CogFixtureTool[3];CogFindCircleTool[]findcircle=new CogFindCircleTool[2];CogFitLineTool fitline=newCogFitLineTool();CogFindLineTool findline=newCogFindLineTool();CogFindLineTool findline2=newCogFindLineTool();#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 boolGroupRun(ref string message,ref CogToolResultConstants result){// To let the execution stop in this script when a debugger is attached, uncomment the following lines.// #if DEBUGif(System.Diagnostics.Debugger.IsAttached)System.Diagnostics.Debugger.Break();// #endif// Run each tool using the RunTool function#regionmToolBlock.Outputs["Data"].Value="2";mToolBlock.Outputs["X"].Value=999;mToolBlock.Outputs["Y"].Value=999;mToolBlock.Outputs["R"].Value=999;string tBdata=string.Empty;string data2=string.Empty;string data3=string.Empty;string data4=string.Empty;doublet1=666;graphics1.Clear();graphics2.Clear();graphics3.Clear();intproductnum=0;#endregiontry{//foreach(ICogTool tool in mToolBlock.Tools)// mToolBlock.RunTool(tool, ref message, ref result);CogPMAlignTool pma1=(CogPMAlignTool)mToolBlock.Tools["正平面"];// CogPMAlignTool pma2 = (CogPMAlignTool)mToolBlock.Tools["反斜面"];CogToolBlock tb1=(CogToolBlock)mToolBlock.Tools["正面坐标计算"];CogToolBlock tb2=(CogToolBlock)mToolBlock.Tools["反面坐标计算"];CogBlobTool blob=(CogBlobTool)mToolBlock.Tools["CogBlobTool1"];pma1.Run();blob.Run();// pma2.Run();for(inti=0;i<blob.Results.GetBlobs().Count;i++){productnum++;if(blob.Results.GetBlobs()[i].Area>2000&&blob.Results.GetBlobs()[i].Area<4000){productnum++;}elseif(blob.Results.GetBlobs()[i].Area>4000){intnum=Convert.ToInt32(blob.Results.GetBlobs()[i].Area/2000);for(intj=0;j<num-1;j++){productnum++;}}}if(pma1.Results.Count>0&&productnum>0){tb2.Run();tBdata=(string)tb2.Outputs["Data"].Value;if(tBdata=="4"){mToolBlock.Outputs["X"].Value=(double)tb2.Outputs["X"].Value;mToolBlock.Outputs["Y"].Value=(double)tb2.Outputs["Y"].Value;mToolBlock.Outputs["R"].Value=(double)tb2.Outputs["R"].Value;mToolBlock.Outputs["Graphics"].Value=(CogGraphicCollection)tb2.Outputs["Graphics"].Value;}else{tb1.Run();tBdata=(string)tb1.Outputs["Data"].Value;if(tBdata=="1"){mToolBlock.Outputs["X"].Value=(double)tb1.Outputs["X"].Value;mToolBlock.Outputs["Y"].Value=(double)tb1.Outputs["Y"].Value;mToolBlock.Outputs["R"].Value=(double)tb1.Outputs["R"].Value;mToolBlock.Outputs["Graphics"].Value=(CogGraphicCollection)tb1.Outputs["Graphics"].Value;}}if(productnum>=8){mToolBlock.Outputs["Data"].Value=tBdata;}else{mToolBlock.Outputs["Data"].Value="3";}}elseif(productnum<3){mToolBlock.Outputs["Data"].Value="3";mToolBlock.Outputs["X"].Value=t1;mToolBlock.Outputs["Y"].Value=t1;mToolBlock.Outputs["R"].Value=t1;}else{mToolBlock.Outputs["Data"].Value="3";mToolBlock.Outputs["X"].Value=t1;mToolBlock.Outputs["Y"].Value=t1;mToolBlock.Outputs["R"].Value=t1;}DisplayLabel(100,300,"数量: "+productnum.ToString(),CogColorConstants.Green,1);mToolBlock.Outputs["Graphics2"].Value=graphics2;}catch{DisplayLabel(100,100,"计算ERROR",CogColorConstants.Red,1);mToolBlock.Outputs["Data"].Value="2";mToolBlock.Outputs["X"].Value=t1;mToolBlock.Outputs["Y"].Value=t1;mToolBlock.Outputs["R"].Value=t1;mToolBlock.Outputs["Graphics"].Value=graphics2;}returnfalse;}publicvoidDisplayLabel(doublex,doubley,string text,CogColorConstants color,intindex){CogGraphicLabel displaylabel=newCogGraphicLabel();displaylabel.Alignment=CogGraphicLabelAlignmentConstants.TopLeft;displaylabel.LineWidthInScreenPixels=100;displaylabel.Font=newFont("微软雅黑",12);if(index==2){displaylabel.Font=newFont("微软雅黑",20);}displaylabel.Color=CogColorConstants.Black;displaylabel.SelectedSpaceName="@";displaylabel.SetXYText(x,y,text);displaylabel.BackgroundColor=color;graphics1.Add(displaylabel);}publicvoidDebugLabel(doublex,doubley,string text,CogColorConstants color){CogGraphicLabel debuglabel=newCogGraphicLabel();debuglabel.Alignment=CogGraphicLabelAlignmentConstants.TopLeft;debuglabel.LineWidthInScreenPixels=100;debuglabel.Font=newFont("微软雅黑",15);debuglabel.BackgroundColor=CogColorConstants.Green;debuglabel.SetXYText(x,y,text);debuglabel.Color=color;graphics2.Add(debuglabel);}publicvoidGraph_Circle(doublex,doubley,doubleradius,CogColorConstants color){CogCircle cir=newCogCircle();cir.Color=color;cir.DragLineWidthInScreenPixels=50;cir.LineWidthInScreenPixels=5;cir.CenterX=x;cir.CenterY=y;cir.Radius=radius;graphics3.Add(cir);}#regionWhen 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 overridevoidModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord){}#endregion#regionWhen 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 overridevoidModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord){foreach(ICogGraphic gra in graphics1){mToolBlock.AddGraphicToRunRecord(gra,lastRecord,"正平面.InputImage","");}foreach(ICogGraphic gra in graphics2){mToolBlock.AddGraphicToRunRecord(gra,lastRecord,"CogPMAlignTool1.InputImage","");}}#endregion#regionWhen the Script is Initialized/// <summary>/// Perform any initialization required by your script here/// </summary>/// <param name="host">The host tool</param>public overridevoidInitialize(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}