1.使用C#加载vpp文件并显示结果
需要将需要的结果放入已发送项当中(充当输出)。
勾选需要以便后续显示图形
添加这些引用
可以通过带control的一个dll引用添加到工具箱获取控件。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.QuickBuild;
namespace visionprowindow
{
public partial class Form1 : Form
{
#region 定义visionpro变量字段
CogJobManager _myJobManager;//这个是对vpp文件的管理对象
CogJob myJob;//针对于某一个作业的对象
CogJobIndependent myJobIndependent;//
#endregion
public Form1()
{
InitializeComponent();
//进行vpp文件的加载
_myJobManager = (CogJobManager)CogSerializer.LoadObjectFromFile(@"C:\\Users\\红米\\Desktop\\testblob1.vpp");
//作业的选择
myJob = _myJobManager.Job(0);
myJobIndependent = myJob.OwnedIndependent;
//刷新的工作
_myJobManager.UserQueueFlush();
_myJobManager.FailureQueueFlush();
myJob.ImageQueueFlush();
myJobIndependent.RealTimeQueueFlush();
//结果处理,进行结果产生事件的具体方法的订阅
_myJobManager.UserResultAvailable += _myJobManager_UserResultAvailable;
}
//用户结果可用的时间处理程序
private void _myJobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e)
{
//MessageBox.Show("获取到结果");
ICogRecord temp;
//获得提供给用户的结果对象其中层层嵌套
ICogRecord topRecord = _myJobManager.UserResult();
temp = topRecord.SubRecords[@"Tools.Item[""CogBlobTool1""].CogBlobTool.Results.GetBlobs().Count"];
int count = (int)temp.Content;
if (txt_main.InvokeRequired)
{
Invoke(new Action(() =>
{
this.txt_main.Text = count.ToString();
}));
}
//获得显示的图像
temp = topRecord.SubRecords["ShowLastRunRecordForUserQueue"];
temp = temp.SubRecords["LastRun"];
temp = temp.SubRecords["Image Source.OutputImage"];
cogRecordDisplay1.Record = temp;
cogRecordDisplay1.Fit();
//throw new NotImplementedException();
}
private void btn_runonce_Click(object sender, EventArgs e)
{
try
{
//异步运行,单步一次
_myJobManager.Run();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "连续运行")
{
try
{
_myJobManager.RunContinuous();
}
catch (Exception)
{
throw;
}
button1.Text = "停止运行";
}
else
{
try
{
_myJobManager.Stop();
}
catch (Exception)
{
throw;
}
button1.Text = "连续运行";
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//进行结尾工作的处理
_myJobManager.Shutdown();
_myJobManager.UserResultAvailable -= _myJobManager_UserResultAvailable;
cogRecordDisplay1.Dispose();
}
}
}
运行如图