如何将静态或交互式图形添加到CogDisplay中
CogDisplay支持两种类型的图形:静态图形和交互式图形。静态图形一旦添加到显示中,就无法移动或更改。静态图形不能添加提示文本(TipText)。
交互式图形在启用图形的交互属性时,可以通过程序或用户进行移动或更改。可以在交互式图形中添加提示文本。以下示例展示了如何添加提示文本。
以下步骤展示了如何将静态或交互式图形添加到CogDisplay中。
界面设计如下,添加CogDisplay控件用于显示图像,添加几个按钮用来添加图形:
具体步骤如下:
1. 添加引用
using Cognex.VisionPro;
using Cognex.VisionPro.Display;
2. 声明枚举
public enum GraphicInteractivityLevel {
eNonInteractive=0,
eNonSelectable=1,
eFullyInteractive=2
}
3. 加载默认图像
private void Form9_Load(object sender, EventArgs e)
{
cogDisplay1.Image = SyntheticImage();
}
private CogImage8Grey SyntheticImage()
{
CogImage8Grey blackImage = new CogImage8Grey();
blackImage.Allocate(200,200);
for (int i = 0; i < blackImage.Width; i++)
{
for (int j = 0; j < blackImage.Height; j++)
{
blackImage.SetPixel(i,j,128);
}
}
return blackImage;
}
4. 添加交互式图形
1. 创建一个形状。本示例使用了CogEllipse。
2. 从交互形状中检索CogGraphicInteractive接口。
3. 设置图形的自由度属性。
4. 将形状添加到交互图形集合中以显示它。
private void AddInteractive(CogColorConstants color, GraphicInteractivityLevel interactivityLevel) {
// 1. 创建一个CogEllipse形状
CogEllipse cogEllipse = new CogEllipse();
if (cogDisplay1.InteractiveGraphics.Count==0)
{
//第一个交互图形在坐标(0,0)
cogEllipse.CenterX = 0;
cogEllipse.CenterY = 0;
}
else
{
//后续图形在前一个图形偏移(50,50)处
CogEllipse previousEllipse = (CogEllipse)cogDisplay1.InteractiveGraphics[cogDisplay1.InteractiveGraphics.Count - 1];
cogEllipse.CenterX = previousEllipse.CenterX + 50;
cogEllipse.CenterY = previousEllipse.CenterY + 50;
}
// 添加提示文本。当鼠标悬停在图形上时,将显示此提示文本
cogEllipse.TipText = $"交互式图形{cogEllipse.CenterX/50}";
// 设置图形颜色
cogEllipse.Color = color;
// 2. 从交互形状中检索CogGraphicInteractive接口
// 一些图形属性仅通过通用接口访问
ICogGraphicInteractive graphicInteractive = cogEllipse;
// 3. 设置图形的自由度(DOF)属性。
// 没有机制可以使交互图形不可选择,而不使其非交互式
// 如果所选颜色与未选颜色相同,则它们都显示为不可选择
cogEllipse.Interactive = interactivityLevel > GraphicInteractivityLevel.eNonInteractive;
if (interactivityLevel>GraphicInteractivityLevel.eNonSelectable)
{
cogEllipse.GraphicDOFEnable = CogEllipseDOFConstants.All;
}
else
{
graphicInteractive.SelectedColor = cogEllipse.Color;
graphicInteractive.GraphicDOFEnableBase = CogGraphicDOFConstants.None;
}
// 根据其交互级别绘制图形的线样式和鼠标光标
// 请参见CogStandardCursorConstants以获取所有支持的光标类型
// 请参见CogGraphicLineStyleConstants以获取所有支持的线样式
switch (interactivityLevel)
{
case GraphicInteractivityLevel.eNonInteractive:
//没有特殊光标
cogEllipse.LineStyle = CogGraphicLineStyleConstants.Dot;
break;
case GraphicInteractivityLevel.eNonSelectable:
graphicInteractive.MouseCursor = CogStandardCursorConstants.TipTextGraphic;
cogEllipse.LineStyle = CogGraphicLineStyleConstants.Dash;
graphicInteractive.SelectedLineStyle = CogGraphicLineStyleConstants.Dash;
break;
case GraphicInteractivityLevel.eFullyInteractive:
graphicInteractive.MouseCursor = CogStandardCursorConstants.ManipulableGraphic;
cogEllipse.LineStyle = CogGraphicLineStyleConstants.Solid;
graphicInteractive.DragLineStyle = CogGraphicLineStyleConstants.Solid;
break;
default:
MessageBox.Show("Unrecognized interactivity level.");
break;
}
// 4. 将形状添加到交互图形集合以进行显示
cogDisplay1.InteractiveGraphics.Add(cogEllipse,"test",false);
}
private void AddNonInteractive_Click(object sender, EventArgs e)
{
AddInteractive(CogColorConstants.Red,GraphicInteractivityLevel.eNonInteractive);
}
private void AddNonSelectable_Click(object sender, EventArgs e)
{
AddInteractive(CogColorConstants.Yellow,GraphicInteractivityLevel.eNonSelectable);
}
private void AddFullyInteractive_Click(object sender, EventArgs e)
{
AddInteractive(CogColorConstants.Green,GraphicInteractivityLevel.eFullyInteractive);
}
5. 添加静态图形
- 创建一个形状。本示例使用了CogRectangleAffine
- 将形状添加到静态图形集合中以显示它
double staticPositionX = 0, staticPositionY = 0;
private void AddStatic_Click(object sender, EventArgs e)
{
// 初始化坐标
if (cogDisplay1.StaticGraphics.ZOrderGroups.Count == 0)
{
staticPositionX = 0;
staticPositionY = 0;
}
//创建静态图形
CogRectangleAffine staticGraphic = new CogRectangleAffine();
//设置图形属性
staticGraphic.CenterX = staticPositionX;
staticGraphic.CenterY = staticPositionY;
staticGraphic.TipText = "Static graphics don't support tiptext!!!";
staticGraphic.Color = CogColorConstants.Orange;
staticGraphic.LineWidthInScreenPixels *= 3;
//添加图形到显示控件
cogDisplay1.StaticGraphics.Add(staticGraphic,"test");
//更新坐标
staticPositionX += 50;
staticPositionY += 50;
}
6. 删除所有图形
private void RemoveGraphics_Click(object sender, EventArgs e)
{
cogDisplay1.StaticGraphics.Clear();
while (cogDisplay1.InteractiveGraphics.Count>0)
{
cogDisplay1.InteractiveGraphics.Remove(0);
}
}