158 lines
5.9 KiB
C#
158 lines
5.9 KiB
C#
/********************************************
|
||
* 版本号:V1.2
|
||
* 作者: 孙剑
|
||
* 最后修改时间:2022年5月23日
|
||
*
|
||
* 类功能说明:
|
||
*
|
||
*
|
||
* 第一个方法:
|
||
* public static bool PMARunStatus(CogPMAlignTool pmaTool, int i) 可保证任何情况下使用PMATool的结果是可用的,
|
||
* 不可用时返回false。程序不会报错和崩溃。
|
||
* -------------------------------------------
|
||
* 调用例程:
|
||
* double pmaRotation = 0;
|
||
* double pmaX = 0;
|
||
* double pmaY = 0;
|
||
* if (CPMARunStatus.PMARunStatus(myPMATool, 1))
|
||
{
|
||
pmaRotation = myPMATool.Results[i].GetPose().Rotation;
|
||
pmaX = myPMATool.Results[i].GetPose().TranslationX;
|
||
pmaY = myPMATool.Results[i].GetPose().TranslationY;
|
||
}
|
||
* --------------------------------------------
|
||
* 第二个方法 在cogRecordDisplay_0的控件上画出匹配的外形轮廓和十字标线,
|
||
* 注意,您如果没让PMATool图形显示您所要的轮廓线,有可能不能画出
|
||
* CPMARunStatus.DrawPMA(myPMATool, cogRecordDisplay_0);
|
||
*
|
||
* 第三个方法只是画了一个自定义的十字。
|
||
*
|
||
* 备注:画图的方法是在一个CogRecordDisplay上画出。
|
||
*
|
||
********************************************/
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using Cognex.VisionPro.PMAlign;
|
||
using Cognex.VisionPro;
|
||
|
||
|
||
namespace AVP.CRobot
|
||
{
|
||
/// <summary>
|
||
/// 返回CogPMAlignTool的运行结果,请调用PMARunStatus方法
|
||
/// </summary>
|
||
class CPMARunStatus
|
||
{
|
||
/// <summary>
|
||
/// 返回CogPMAlignTool的运行结果,工具运行成功,并且数量>=i 的时候返回true
|
||
/// </summary>
|
||
/// <param name="pmaTool">PMA工具Tool</param>
|
||
/// <param name="i">预期找到的数量</param>
|
||
/// <returns>数量>=i 的时候返回true</returns>
|
||
public static bool PMARunStatus(CogPMAlignTool pmaTool, int i)
|
||
{
|
||
if (pmaTool.RunStatus.Result == CogToolResultConstants.Accept)
|
||
{
|
||
if (pmaTool.Results.Count >= i)
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
/// <summary>
|
||
/// 利用输入的PMATool,在CogRecordDisplay 上画出找到工件的轮廓
|
||
/// </summary>
|
||
/// <param name="pmaTool">输入的PMATool</param>
|
||
/// <param name="disp">输入要显示的CogRecordDisplay</param>
|
||
public static void DrawPMA(CogPMAlignTool pmaTool, CogRecordDisplay disp)
|
||
{
|
||
if (pmaTool.RunStatus.Result == CogToolResultConstants.Accept)
|
||
{
|
||
if (pmaTool.Results.Count > 0) //判断输入的PMATool是否有结果,才可以画图
|
||
{
|
||
foreach (CogPMAlignResult item in pmaTool.Results)
|
||
{
|
||
disp.InteractiveGraphics.Add(item.CreateResultGraphics(CogPMAlignResultGraphicConstants.MatchFeatures),
|
||
"", false);
|
||
disp.InteractiveGraphics.Add(item.CreateResultGraphics(CogPMAlignResultGraphicConstants.CoordinateAxes), "", false);
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
/// <summary>
|
||
/// 画十字
|
||
/// </summary>
|
||
/// <param name="centerX"></param>
|
||
/// <param name="centerY"></param>
|
||
/// <param name="angle"></param>
|
||
/// <param name="color"></param>
|
||
/// <param name="myDisplay"></param>
|
||
/// <param name="spaceName"></param>
|
||
public static void DrawPointMarker(double centerX, double centerY, double angle, CogColorConstants color, CogRecordDisplay myDisplay, string spaceName)
|
||
{
|
||
CogPointMarker cgMarker = new CogPointMarker
|
||
{
|
||
X = centerX,
|
||
Y = centerY,
|
||
Rotation = angle, //弧度
|
||
Color = color,
|
||
SelectedSpaceName = spaceName,
|
||
Interactive = true,
|
||
LineWidthInScreenPixels = 2,
|
||
SizeInScreenPixels = 50
|
||
};
|
||
myDisplay.InteractiveGraphics.Add(cgMarker, null, false);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 在给定的图像空间中画圆
|
||
/// </summary>
|
||
/// <param name="centerX"></param>
|
||
/// <param name="centerY"></param>
|
||
/// <param name="color"></param>
|
||
/// <param name="myDisplay"></param>
|
||
/// <param name="spaceName"></param>
|
||
public static void DrawCircle(double centerX, double centerY, CogColorConstants color, CogRecordDisplay myDisplay, string spaceName)
|
||
{
|
||
CogCircle cgCirlcle = new CogCircle
|
||
{
|
||
CenterX = centerX,
|
||
CenterY = centerY,
|
||
Color = color,
|
||
SelectedSpaceName = spaceName
|
||
};
|
||
myDisplay.InteractiveGraphics.Add(cgCirlcle, null, false);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 在给定的图像空间中画线段
|
||
/// </summary>
|
||
/// <param name="startX"></param>
|
||
/// <param name="startY"></param>
|
||
/// <param name="endX"></param>
|
||
/// <param name="endY"></param>
|
||
/// <param name="color"></param>
|
||
/// <param name="myDisplay"></param>
|
||
/// <param name="spaceName"></param>
|
||
public static void DrawLineSegment(double startX, double startY, double endX, double endY, CogColorConstants color, CogRecordDisplay myDisplay, string spaceName)
|
||
{
|
||
CogLineSegment cgLineSegment = new CogLineSegment
|
||
{
|
||
StartX = startX,
|
||
StartY = startY,
|
||
EndX = endX,
|
||
EndY = endY,
|
||
Color = color,
|
||
SelectedSpaceName = spaceName,
|
||
StartPointAdornment = CogLineSegmentAdornmentConstants.Arrow,
|
||
EndPointAdornment = CogLineSegmentAdornmentConstants.Arrow
|
||
};
|
||
myDisplay.InteractiveGraphics.Add(cgLineSegment, null, false);
|
||
}
|
||
}
|
||
}
|