#region namespace importsusingSystem;usingSystem.Collections;usingSystem.Drawing;usingSystem.IO;usingSystem.Windows.Forms;usingCognex.VisionPro;usingCognex.VisionPro.ToolBlock;usingCognex.VisionPro3D;usingCognex.VisionPro.ImageProcessing;usingCognex.VisionPro.Caliper;usingCognex.VisionPro.Dimensioning;#endregionpublicclassCogToolBlockAdvancedScript:CogToolBlockAdvancedScriptBase{#region Private Member VariablesprivateCognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;//1.变量定义CogGraphicCollection my_gc =newCogGraphicCollection();CogGraphicLabel res =newCogGraphicLabel();#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>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 functionforeach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool,ref message,ref result);//===2.1 工具获取、获取参数、初始化等//初始化方向起点位置CogCaliperTool start = mToolBlock.Tools["StartPoint"]asCogCaliperTool;
start.Run();//初始化方向终点位置CogCaliperTool end = mToolBlock.Tools["EndPoint"]asCogCaliperTool;
end.Run();//动态卡尺,不断地利用该卡尺得到下一个点的位置CogCaliperTool autoCaliper = mToolBlock.Tools["AutoPoint"]asCogCaliperTool;//动态卡尺的矩形框CogRectangleAffine rec = autoCaliper.Region asCogRectangleAffine;//线段工具用来计算角度CogCreateSegmentTool segment = mToolBlock.Tools["CogCreateSegmentTool1"]asCogCreateSegmentTool;//获取参数int d = Convert.ToInt32(mToolBlock.Inputs["Distance"].Value);int totalNum = Convert.ToInt32(mToolBlock.Inputs["totalNum"].Value);int range = Convert.ToInt32(mToolBlock.Inputs["range"].Value);//清空图形集合
my_gc.Clear();//实例化一个多变形对象CogPolygon p =newCogPolygon();
res.SetXYText(100,100,"OK");
res.Font =newFont("宋体",20);//===2.2 循环生成指定数量的点for(int i =0; i < totalNum; i++){if(i ==0){//第一个点使用搜索方向的起点
p.AddVertex(start.Results[0].PositionX, start.Results[0].PositionX, i);continue;}elseif(i ==1){//第二个点使用搜索方向的终点
p.AddVertex(start.Results[0].PositionX, end.Results[0].PositionX, i);continue;}else{//第三个以上的路径点//设置线段起点和终点,运行线段工具,最终得到上一个线段的角度
segment.Segment.StartX = p.GetVertexX(i -2);
segment.Segment.StartY = p.GetVertexY(i -2);
segment.Segment.EndX = p.GetVertexX(i -1);
segment.Segment.EndY = p.GetVertexY(i -1);
segment.Run();//通过上一个线段的角度、路径之间的距离、上一个点的坐标,算出下一个点的坐标,并将算出的坐标作为动态卡尺的中心点,卡尺角度与线段垂直
rec.CenterX = p.GetVertexX(i -1)+ Math.Round(Math.Cos(segment.Segment.Rotation),5)* d;
rec.CenterY = p.GetVertexY(i -1)+ Math.Round(Math.Sin(segment.Segment.Rotation),5)* d;//将动态卡尺的角度设为与线段垂直,然后运行
rec.Rotation = CogMisc.DegToRad(90+ Convert.ToInt32(CogMisc.RadToDeg(segment.Segment.Rotation)));
autoCaliper.Run();//自动卡尺运行结果判断if(autoCaliper.Results.Count !=0){//如果动态卡尺找到了,就直接存进多边形
p.AddVertex(autoCaliper.Results[0].PositionX, autoCaliper.Results[0].PositionY, i);}else{//如果没有找到,则以线段为基准,向左旋转一定角度遍历运行//----旋转搜索----//初始角度为上一个线段的角度int angle = Convert.ToInt32(CogMisc.RadToDeg(segment.Segment.Rotation));int j;for(j =0; j < range; j+=5){if(j <= range /2){//右转角度计算
angle = angle + j;}else{//左转角度计算
angle = angle -(j - range /2);}
rec.CenterX = p.GetVertexX(i -1)+ Math.Round(Math.Cos((angle * Math.PI)/180),5)* d;//5是返回值中的小数位数
rec.CenterY = p.GetVertexX(i -1)+ Math.Round(Math.Sin((angle * Math.PI)/180),5)* d;
autoCaliper.Run();if(autoCaliper.Results.Count !=0){
p.AddVertex(autoCaliper.Results[0].PositionX, autoCaliper.Results[0].PositionY, i);break;}//如果还没有找到,退出if(j >= range){
res.SetXYText(100,100,"NG");break;}}}}//生成每一段路径上的指示箭头CogLineSegment LineArrow1 =newCogLineSegment();
LineArrow1.Color = CogColorConstants.Yellow;
LineArrow1.LineWidthInScreenPixels =2;
LineArrow1.SetStartEnd(p.GetVertexX(i -1), p.GetVertexY(i -1), autoCaliper.Results[0].PositionX, autoCaliper.Results[0].PositionY);
LineArrow1.EndPointAdornment = CogLineSegmentAdornmentConstants.SolidArrow;
my_gc.Add(LineArrow1);}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){//3.图形输出foreach(ICogGraphic x in my_gc){
mToolBlock.AddGraphicToRunRecord(x, lastRecord,"CogImageConvertTool1.InputImage","");}
mToolBlock.AddGraphicToRunRecord(res, lastRecord,"CogImageConvertTool1.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>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}