Visionpro联合C#学习练习笔记

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();
        }
    }
}

运行如图

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值