VisionPro自定义工具(c#)

这篇具有很好参考价值的文章主要介绍了VisionPro自定义工具(c#)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、新建Windows类库

1、新建类库

visionpro c#编辑,编辑器,c#,visualstudio

2、类库里面添加类(CogTool)和用户控件(CogToolEditV2)

visionpro c#编辑,编辑器,c#,visualstudio

2.1、对类(CogTool)的代码编写。需要Cognex.VisionPro 和 Cognex.VisionPro.Core 的引用
2.2、代码理解如下:
(1)先定义,但是这不清楚是什么意思
	[Serializable]
    [Editor(typeof(CogToolEditV2), typeof(Control))]   //CogToolEditV2为用户控件名称
    [CogDefaultToolInputTerminal(0, "InputImage", "InputImage")]
    [CogDefaultToolOutputTerminal(0, "OutputImage", "OutputImage")]
    [System.ComponentModel.DesignerCategory("")]
(2)本类继承Cognex.VisionPro.Implementation.CogToolBase,应该是VisionPro的工具基类,并实现重写该基类的方法
public class CogTool : Cognex.VisionPro.Implementation.CogToolBase
(3)新定义私有变量,_InputImage、_OutputImage和_ImageProcessingType,其中_ImageProcessingType为图像算法类型-默认为0不处理图像
#region 私有变量
        // 输入图像(可以不序列化输入图像)
        [CogSerializationOptionsAttribute(CogSerializationOptionsConstants.InputImages)]
        private ICogImage _InputImage;
        // 输出图像(可以不序列化输出图像)
        [CogSerializationOptionsAttribute(CogSerializationOptionsConstants.OutputImages)]
        private ICogImage _OutputImage;
        //------------新增私有变量-------------
        /// <summary>
        /// 图像算法类型-默认为0不处理图像
        /// </summary>
        private int _ImageProcessingType = 0;
        #endregion
(4)构造函数和clone,其中构造函数中有三个无参、一个参数、两个参数,主要是一个参数的构造函数,其内容就是初始化,将输入的图片保持输入图片,输出的图片,如果参数的输入和参数的输出的图片相同则输出参数的输入图片,反之,输出参数的输出图片。

重写clone函数,主要就是实现构造函数(这里是一个参数的构造函数)

	#region 构造函数和clone
        //无参构造函数
        public CogTool()
        {
        }
        public CogTool(CogTool otherTool)
            : base(otherTool)
        {
            // 输入图像
            if (otherTool._InputImage != null)
                _InputImage = otherTool._InputImage;
            // 输出图像
            if (otherTool._OutputImage != null)
            {
                //输入图像和输出图像相同
                if (Object.ReferenceEquals(otherTool._InputImage,
                                           otherTool._OutputImage))
                    _OutputImage = _InputImage;
                //输入图像和输出图像不同
                else
                    _OutputImage = otherTool._OutputImage;
            }
            _ImageProcessingType = otherTool._ImageProcessingType;
        }
        private CogTool(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
        protected override object Clone() { return new CogTool(this); }
        #endregion
(5)状态标志,标志图片输入、图片输出、图片加工(SfImageProcessing)和下一哥的标定。
#region 状态标志
        // 为返回可变数据的每个属性或方法定义一个 64 位状态标志常量。
        // 常量的名称必须是“Sf”,后跟属性或方法的名称。
        // 第一个状态标志应设置为“CogToolBase.SfNextSf”。
        private const long Sf0 = CogToolBase.SfNextSf;//CogToolBase.SfNextSf=32
        public const long SfInputImage = Sf0 << 0;
        public const long SfOutputImage = Sf0 << 1;
        public const long SfImageProcessing = Sf0 << 2;
        protected new const long SfNextSf = Sf0 << 3;
        #endregion
(6)get和set,主要是由InputImage、OutputImage和ImageProcessingType构成的,其中InputImage是获取图片输入,并有值输入时,调用OnChanged更改SfCreateCurrentRecord的状态。OutputImage没有进行set操作,因为本次操作是有不同的输出图片,要进行分类输出。ImageProcessingType,与之后的通过控件转换对应的状态有关,获取值后调用OnChanged更改状态。
#region GetSet
        public ICogImage InputImage
        {
            get { return _InputImage; }
            set
            {
                if (!Object.ReferenceEquals(_InputImage, value))
                {
                    _InputImage = value;
                    // 有值输入时,调用OnChanged更改状态
                    OnChanged(SfInputImage | SfCreateCurrentRecord);
                }
            }
        }

        public ICogImage OutputImage
        {
            get { return _OutputImage; }
        }

        public int ImageProcessingType
        {
            get { return _ImageProcessingType; }
            set
            {
                if (_ImageProcessingType != value)
                {
                    _ImageProcessingType = value;
                    OnChanged(SfImageProcessing);
                }
            }
        }
        #endregion
(7)对InternalRun重写,这个是以编程方式或从编辑控件调用工具时执行的。首先对输入的图片进行检查,在通过之前的_ImageProcessingType的值对图片进行对应的操作。比如值为0时,是输出默认图,将_OutputImage的值等于输入的图片值。值为1时,输出的是灰度图,将_OutputImage的值等于灰度后的图片等等。每次结束后都要将SfCreateLastRunRecord的状态进行更新。最后返回工具成功的状态。
protected override CogToolResultConstants InternalRun(ref string message)
        {
            // 输入图像为空抛异常
            if (_InputImage == null)
                throw new Cognex.VisionPro.Exceptions.CogOperatorNoInputImageException();

            ICogImage cogImage = _InputImage;
            Bitmap bitmap = cogImage.ToBitmap();

            switch (_ImageProcessingType)
            {
                case 0:
                    _OutputImage = cogImage;  //将输入图片给到输出
                    message = "默认";
                    break;
                case 1: //灰度化图像

                    try
                    {
                        for (int i = 0; i < bitmap.Width; i++)
                        {
                            for (int j = 0; j < bitmap.Height; j++)
                            {
                                Color color = bitmap.GetPixel(i, j);  //在此获取指定的像素的颜色
                                int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
                                Color newColor = Color.FromArgb(gray, gray, gray);
                                //设置指定像素的颜色  参数:坐标x,坐标y,颜色
                                bitmap.SetPixel(i, j, newColor);
                            }
                        }
                        //将Bitmap转为8位灰度
                        cogImage = new CogImage8Grey(bitmap);

                        _OutputImage = cogImage;
                        message = "图像灰度化成功";
                    }
                    catch (Exception e)
                    {
                        message = "图像处理失败" + e.Message;
                        // 返回工具运行失败状态
                        return CogToolResultConstants.Error;
                    }
                    break;
                case 2: //反转

                    try
                    {
                        for (int i = 0; i < bitmap.Width - 1; i++)
                        {
                            for (int j = 0; j < bitmap.Height - 1; j++)
                            {
                                Color color = bitmap.GetPixel(i, j);  //在此获取指定的像素的颜色
                                int gray = (int)((255 - color.R + 255 - color.G + 255 - color.B) / 3);
                                Color newColor = Color.FromArgb(gray, gray, gray);
                                bitmap.SetPixel(i, j, newColor);
                            }
                        }
                        //将Bitmap转为8位灰度
                        cogImage = new CogImage8Grey(bitmap);

                        _OutputImage = cogImage;
                        message = "图像反转成功";
                    }
                    catch (Exception e)
                    {
                        message = "图像处理失败" + e.Message;
                        // 返回工具运行失败状态
                        return CogToolResultConstants.Error;
                    }
                    break;
                case 3: //二值化
                    try
                    {
                        for (int i = 0; i < bitmap.Width; i++)
                        {
                            for (int j = 0; j < bitmap.Height; j++)
                            {
                                //获取该点的像素的RGB的颜色
                                Color color = bitmap.GetPixel(i, j);
                                int value = 255 - color.B;
                                Color newColor = value > 125 ? Color.FromArgb(0, 0, 0) : Color.FromArgb(255, 255, 255);
                                bitmap.SetPixel(i, j, newColor);
                            }
                            //将Bitmap转为8位灰度
                            cogImage = new CogImage8Grey(bitmap);

                            _OutputImage = cogImage;
                            message = "图像二值化成功";
                        }
                    }
                    catch (Exception e)
                    {
                        message = "图像处理失败" + e.Message;
                        // 返回工具运行失败状态
                        return CogToolResultConstants.Error;
                    }
                    break;
                default:
                    _OutputImage = cogImage;  //将输入图片给到输出
                    break;
            }


            // 输出图像状态改变
            OnChanged(SfOutputImage | SfCreateLastRunRecord);

            // 返回工具运行成功状态
            return CogToolResultConstants.Accept;
        }
(8)输入图像框的重写,设置要在工具运行前显示在编辑控件中的图像;通常是输入图像。如果工具使用某个区域,通常也会包含该区域。
protected override void InternalCreateCurrentRecord(ICogRecord newRecord,
                                                            int currentRecordEnable)
        {
            // newRecord里添加记录
            newRecord.SubRecords.Add(
              new CogRecord(
                "InputImage",                  // 用于标识记录的键
                typeof(ICogImage),         // 定义类型 
                CogRecordUsageConstants.Input, // 用途输入
                false,                         // 不可以更改记录内容
                _InputImage,                   // 数据
                "InputImage"));                // 注释(用于显示)
        }
(9)输出图像框的重写,设置要在工具运行后显示的图像。通常包括输出图像。如果该工具提供了结果图形,它们也包括在这里。
protected override void InternalCreateLastRunRecord(ICogRecord newRecord,
                                                            int lastRunRecordEnable,
                                                            int lastRunRecordDiagEnable)
        {
            // newRecord里添加记录
            newRecord.SubRecords.Add(
              new CogRecord(
                "OutputImage",                  // 用于标识记录的键
                typeof(ICogImage),          // 定义类型 
                CogRecordUsageConstants.Result, // 用途输出结果
                false,                          // 不可以更改记录内容
                _OutputImage,                   // 数据
                "OutputImage"));                // 注释(用于显示)
        }
(10)整体代码
using Cognex.VisionPro.Implementation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Windows.Forms;
using Cognex.VisionPro;
using System.Drawing;

namespace CogToolDemo
{
    //需要Cognex.VisionPro 和 Cognex.VisionPro.Core 的引用

    [Serializable]
    [Editor(typeof(CogToolEditV2), typeof(Control))]   //CogToolEditV2为用户控件名称
    [CogDefaultToolInputTerminal(0, "InputImage", "InputImage")]
    [CogDefaultToolOutputTerminal(0, "OutputImage", "OutputImage")]
    [System.ComponentModel.DesignerCategory("")]
    public class CogTool : Cognex.VisionPro.Implementation.CogToolBase
    {
        #region 私有变量
        // 输入图像(可以不序列化输入图像)
        [CogSerializationOptionsAttribute(CogSerializationOptionsConstants.InputImages)]
        private ICogImage _InputImage;
        // 输出图像(可以不序列化输出图像)
        [CogSerializationOptionsAttribute(CogSerializationOptionsConstants.OutputImages)]
        private ICogImage _OutputImage;
        //------------新增私有变量-------------
        /// <summary>
        /// 图像算法类型-默认为0不处理图像
        /// </summary>
        private int _ImageProcessingType = 0;
        #endregion

        #region 构造函数和clone
        //无参构造函数
        public CogTool()
        {
        }
        public CogTool(CogTool otherTool)
            : base(otherTool)
        {
            // 输入图像
            if (otherTool._InputImage != null)
                _InputImage = otherTool._InputImage;
            // 输出图像
            if (otherTool._OutputImage != null)
            {
                //输入图像和输出图像相同
                if (Object.ReferenceEquals(otherTool._InputImage,
                                           otherTool._OutputImage))
                    _OutputImage = _InputImage;
                //输入图像和输出图像不同
                else
                    _OutputImage = otherTool._OutputImage;
            }
            _ImageProcessingType = otherTool._ImageProcessingType;
        }
        private CogTool(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
        protected override object Clone() { return new CogTool(this); }
        #endregion

        #region 状态标志
        // 为返回可变数据的每个属性或方法定义一个 64 位状态标志常量。
        // 常量的名称必须是“Sf”,后跟属性或方法的名称。
        // 第一个状态标志应设置为“CogToolBase.SfNextSf”。
        private const long Sf0 = CogToolBase.SfNextSf;
        public const long SfInputImage = Sf0 << 0;
        public const long SfOutputImage = Sf0 << 1;
        public const long SfImageProcessing = Sf0 << 2;
        protected new const long SfNextSf = Sf0 << 3;  
        #endregion

        #region GetSet
        public ICogImage InputImage
        {
            get { return _InputImage; }
            set
            {
                if (!Object.ReferenceEquals(_InputImage, value))
                {
                    _InputImage = value;
                    // 有值输入时,调用OnChanged更改状态
                    OnChanged(SfInputImage | SfCreateCurrentRecord);
                }
            }
        }
        public ICogImage OutputImage
        {
            get { return _OutputImage; }
        }
        public int ImageProcessingType
        {
            get { return _ImageProcessingType; }
            set
            {
                if (_ImageProcessingType != value)
                {
                    _ImageProcessingType = value;
                    OnChanged(SfImageProcessing);
                }
            }
        }
        #endregion

        #region CogToolBase overrides
        /// <summary>
        /// 以编程方式或从编辑控件调用工具时执行。
        /// </summary>
        /// <param name="message">
        /// 返回的信息(异常、自定义信息)
        /// </param>
        /// <returns></returns>
        protected override CogToolResultConstants InternalRun(ref string message)
        {
            // 输入图像为空抛异常
            if (_InputImage == null)
                throw new Cognex.VisionPro.Exceptions.CogOperatorNoInputImageException();

            ICogImage cogImage = _InputImage;
            Bitmap bitmap = cogImage.ToBitmap();

            switch (_ImageProcessingType)
            {
                case 0:
                    _OutputImage = cogImage;  //将输入图片给到输出
                    message = "默认";
                    break;
                case 1: //灰度化图像

                    try
                    {
                        for (int i = 0; i < bitmap.Width; i++)
                        {
                            for (int j = 0; j < bitmap.Height; j++)
                            {
                                Color color = bitmap.GetPixel(i, j);  //在此获取指定的像素的颜色
                                int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
                                Color newColor = Color.FromArgb(gray, gray, gray);
                                //设置指定像素的颜色  参数:坐标x,坐标y,颜色
                                bitmap.SetPixel(i, j, newColor);
                            }
                        }
                        //将Bitmap转为8位灰度
                        cogImage = new CogImage8Grey(bitmap); 

                        _OutputImage = cogImage;
                        message = "图像灰度化成功";
                    }
                    catch (Exception e)
                    {
                        message = "图像处理失败" +e.Message;
                        // 返回工具运行失败状态
                        return CogToolResultConstants.Error;
                    }
                    break;
                case 2: //反转

                    try
                    {
                        for (int i = 0; i < bitmap.Width - 1; i++)
                        {
                            for (int j = 0; j < bitmap.Height - 1; j++)
                            {
                                Color color = bitmap.GetPixel(i, j);  //在此获取指定的像素的颜色
                                int gray = (int)((255 - color.R + 255 - color.G + 255 - color.B) / 3);
                                Color newColor = Color.FromArgb(gray, gray, gray);
                                bitmap.SetPixel(i, j, newColor);
                            }
                        }
                        //将Bitmap转为8位灰度
                        cogImage = new CogImage8Grey(bitmap);

                        _OutputImage = cogImage;
                        message = "图像反转成功";
                    }
                    catch (Exception e)
                    {
                        message = "图像处理失败" + e.Message;
                        // 返回工具运行失败状态
                        return CogToolResultConstants.Error;
                    }
                    break;
                case 3: //二值化
                    try
                    {
                        for (int i = 0; i < bitmap.Width; i++)
                        {
                            for (int j = 0; j < bitmap.Height; j++)
                            {
                                //获取该点的像素的RGB的颜色
                                Color color = bitmap.GetPixel(i, j);
                                int value = 255 - color.B;
                                Color newColor = value > 125 ? Color.FromArgb(0, 0, 0) : Color.FromArgb(255, 255, 255);
                                bitmap.SetPixel(i, j, newColor);
                            }
                            //将Bitmap转为8位灰度
                            cogImage = new CogImage8Grey(bitmap);

                            _OutputImage = cogImage;
                            message = "图像二值化成功";
                        }
                    }
                    catch (Exception e)
                    {
                        message = "图像处理失败" + e.Message;
                        // 返回工具运行失败状态
                        return CogToolResultConstants.Error;
                    }
                    break;
                default:
                    _OutputImage = cogImage;  //将输入图片给到输出
                    break;
            }
            // 输出图像状态改变
            OnChanged(SfOutputImage | SfCreateLastRunRecord);
            // 返回工具运行成功状态
            return CogToolResultConstants.Accept;
        }


        /// <summary>
        /// 设置要在工具运行前显示在编辑控件中的图像;通常是输入图像。如果工具使用某个区域,通常也会包含该区域。
        /// </summary>
        /// <param name="newRecord">
        /// A newly-created record to which image sub-records may be added.
        /// </param>
        /// <param name="currentRecordEnable">
        /// An integer bit pattern indicating what images and graphics to add.
        /// </param>
        protected override void InternalCreateCurrentRecord(ICogRecord newRecord,
                                                            int currentRecordEnable)
        {
            // Add a new image record to the top-level "newRecord".
            newRecord.SubRecords.Add(
              new CogRecord(
                "InputImage",                  // Subrecord key for this record,
                typeof(ICogImage),         // Type of its content, 
                CogRecordUsageConstants.Input, // Record represents an input,
                false,                         // Record content is unchanging,
                _InputImage,                   // The record's content,
                "InputImage"));                // Annotation (for display)
        }

        /// <summary>
        /// 设置要在工具运行后显示的图像。通常包括输出图像。如果该工具提供了结果图形,它们也包括在这里。
        /// </summary>
        /// <param name="newRecord">
        /// A newly-created record to which image sub-records may be added.
        /// </param>
        /// <param name="lastRunRecordEnable">
        /// An integer bit pattern indicating what images and graphics to add.
        /// </param>
        /// <param name="lastRunRecordDiagEnable">
        /// An integer bit pattern indicating what 'Diag' images and graphics
        /// to add.  'Diag' images and graphics are specified using a separate
        /// set of bits because they make the tool run significantly slower
        /// (or use significantly more memory).  Users must re-run the tool
        /// to see these items.
        /// </param>
        protected override void InternalCreateLastRunRecord(ICogRecord newRecord,
                                                            int lastRunRecordEnable,
                                                            int lastRunRecordDiagEnable)
        {
            // Add a new image record to the top-level "newRecord".
            newRecord.SubRecords.Add(
              new CogRecord(
                "OutputImage",                  // Subrecord key for this record,
                typeof(ICogImage),          // Type of its content, 
                CogRecordUsageConstants.Result, // Record represents a result,
                false,                          // Record content is unchanging,
                _OutputImage,                   // The record's content,
                "OutputImage"));                // Annotation (for display)
        }
        #endregion
    }
}

3、CogToolEditV2用户控件

3.1先引用Cognex.VisionPro.Controls,并且继承CogToolEditControlBaseV2,它为控件提供了通用框架。
3.2、添加控件(按钮)如图:

visionpro c#编辑,编辑器,c#,visualstudio

3.3、代码理解:
(1)为编辑控件类创建构造函数,首次按钮是第一个。
 public CogToolEditV2()
          : base(false)
        {
            InitializeComponent();
            tbbElectric.Visible = false;
            //选中默认项
            this.radioButton1.Checked = true;
        }
(2)创建主题属性,包括getter和setter。对象是vision工具的实例,其状态将通过此控件公开
[Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public CogTool Subject
        {
            get { return base.GetSubject() as CogTool; }

            set { base.SetSubject(value); }

        }
(3)重写基类的InitializeFromSubject方法。在主题被替换后的第一个线程安全机会调用此方法。控件应使用此方法使用来自新主题的值(重新)初始化自身,并调用SetupSettingsTab方法重新获取控件状态,和控件的交互响应重新更新状态。
protected override void InitializeFromSubject()
        {
            base.InitializeFromSubject();

            SetupSettingsTab();

        }
(4)重写基类的SubjectValuesChanged方法。每当主题引发已更改的事件时,都会调用此方法。此函数始终在GUI线程上执行。在这里,调用SetupSettingsTab方法(用途同上),如所提供的已更改事件参数的状态标志所示。
protected override void SubjectValuesChanged(
          object sender,
          CogChangedEventArgs e)
        {
            base.SubjectValuesChanged(sender, e);

            if ((e.StateFlags & CogTool.SfImageProcessing) != 0)
            {
                SetupSettingsTab();

            }

        }
(5)每当SubjectInUse属性发生更改时,就会调用此方法。这是启用或禁用子控制的好地方,并判断是否有主题属性,若有这将按钮的属性更改。
protected override void SubjectInUseChanged()
        {
            base.SubjectInUseChanged();

            bool bEnabled = false;
            if (Subject != null)  //Subject不为空
                if (!SubjectInUse)   //Subject没有运行
                    bEnabled = true;

            //控制控件Enabled属性
            radioButton1.Enabled = bEnabled;
            radioButton2.Enabled = bEnabled;
            radioButton3.Enabled = bEnabled;
            radioButton4.Enabled = bEnabled;

        }
(6)SetupSettingsTab方法的理解:AssertSafe确保SubjectInUse是false。重新获取控件状态,并更新控件属性。
private void SetupSettingsTab()
        {
            AssertSafe();//确保SubjectInUse是false

            bool bChecked = false;
            if (Subject != null)
                bChecked = true;

            if (bChecked)
            {
                //工具窗体关闭后,重新获取控件状态
                switch (Subject.ImageProcessingType)
                {
                    case 0:
                        radioButton1.Checked = true;
                        radioButton2.Checked = false;
                        radioButton3.Checked = false;
                        radioButton4.Checked = false;
                        break;
                    case 1:
                        radioButton1.Checked = false;
                        radioButton2.Checked = true;
                        radioButton3.Checked = false;
                        radioButton4.Checked = false;
                        break;
                    case 2:
                        radioButton1.Checked = false;
                        radioButton2.Checked = false;
                        radioButton3.Checked = true;
                        radioButton4.Checked = false;
                        break;
                    case 3:
                        radioButton1.Checked = false;
                        radioButton2.Checked = false;
                        radioButton3.Checked = false;
                        radioButton4.Checked = true;
                        break;
                    default:
                        radioButton1.Checked = true;
                        radioButton2.Checked = false;
                        radioButton3.Checked = false;
                        radioButton4.Checked = false;
                        break;
                }
            }


            bool bEnabled = false;
            if (Subject != null)
                bEnabled = true;
            //控制控件Enabled属性
            radioButton1.Enabled = bEnabled;
            radioButton2.Enabled = bEnabled;
            radioButton3.Enabled = bEnabled;
            radioButton4.Enabled = bEnabled;

        }
(7)按钮的功能。先判断主题是否为空或是否运行,再进行对ImageProcessingType的赋值,通过Subject.ImageProcessingType对图片进行相对于的操作。如之前的ImageProcessingType为0时,是原图;1时是灰度图等。
private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;
            Subject.ImageProcessingType = 0;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;
            Subject.ImageProcessingType = 1;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;
            Subject.ImageProcessingType = 2;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;
            Subject.ImageProcessingType = 3;
        }
(8)总代码:
using Cognex.VisionPro;
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;

namespace CogToolDemo
{
    //需要Cognex.VisionPro.Controls的引用
    [System.ComponentModel.ToolboxItem(true)]
    public partial class CogToolEditV2 : CogToolEditControlBaseV2  //继承后用户控件会改变
    {
        //构造函数
        public CogToolEditV2()
          : base(false)
        {
            InitializeComponent();
            tbbElectric.Visible = false;
            //选中默认项
            this.radioButton1.Checked = true;
        }

        //创建Subject属性,包括getter和setter。对象是vision工具的实例,其状态将通过此控件公开
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public CogTool Subject
        {
            get { return base.GetSubject() as CogTool; }

            set { base.SetSubject(value); }

        }

        //在Subject被替换后的第一个线程安全机会调用此方法。控件应使用此方法使用来自新Subject的值(重新)初始化自身。
        protected override void InitializeFromSubject()
        {
            base.InitializeFromSubject();

            SetupSettingsTab();

        }

        //当Subject引发已更改的事件时,都会调用此方法。此函数始终在GUI线程上执行。
        //在这里,你可以更新其关联工具属性值可能已更改的任何子控件,如所提供的已更改事件参数的状态标志所示。
        protected override void SubjectValuesChanged(
          object sender,
          CogChangedEventArgs e)
        {
            base.SubjectValuesChanged(sender, e);

            if ((e.StateFlags & CogTool.SfImageProcessing) != 0)
            {
                SetupSettingsTab();

            }

        }


        // 每当SubjectInUse属性发生更改时,就会调用此方法。这是启用或禁用子控制的好地方。
        protected override void SubjectInUseChanged()
        {
            base.SubjectInUseChanged();

            bool bEnabled = false;
            if (Subject != null)  //Subject不为空
                if (!SubjectInUse)   //Subject没有运行
                    bEnabled = true;

            //控制控件Enabled属性
            radioButton1.Enabled = bEnabled;
            radioButton2.Enabled = bEnabled;
            radioButton3.Enabled = bEnabled;
            radioButton4.Enabled = bEnabled;

        } // protected override void SubjectInUseChanged()


        // SetupSettingsTab() 访问 Subject.xxx,
        // 因此只能在安全时间调用(例如,当 SubjectInUse 为 false 时)。
        private void SetupSettingsTab()
        {
            AssertSafe();

            bool bChecked = false;
            if (Subject != null)
                bChecked = true;

            if (bChecked)
            {
                //工具窗体关闭后,重新获取控件状态
                switch (Subject.ImageProcessingType)
                {
                    case 0:
                        radioButton1.Checked = true;
                        radioButton2.Checked = false;
                        radioButton3.Checked = false;
                        radioButton4.Checked = false;
                        break;
                    case 1:
                        radioButton1.Checked = false;
                        radioButton2.Checked = true;
                        radioButton3.Checked = false;
                        radioButton4.Checked = false;
                        break;
                    case 2:
                        radioButton1.Checked = false;
                        radioButton2.Checked = false;
                        radioButton3.Checked = true;
                        radioButton4.Checked = false;
                        break;
                    case 3:
                        radioButton1.Checked = false;
                        radioButton2.Checked = false;
                        radioButton3.Checked = false;
                        radioButton4.Checked = true;
                        break;
                    default:
                        radioButton1.Checked = true;
                        radioButton2.Checked = false;
                        radioButton3.Checked = false;
                        radioButton4.Checked = false;
                        break;
                }
            }


            bool bEnabled = false;
            if (Subject != null)
                bEnabled = true;
            //控制控件Enabled属性
            radioButton1.Enabled = bEnabled;
            radioButton2.Enabled = bEnabled;
            radioButton3.Enabled = bEnabled;
            radioButton4.Enabled = bEnabled;

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;

            Subject.ImageProcessingType = 0;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;

            Subject.ImageProcessingType = 1;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;

            Subject.ImageProcessingType = 2;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;

            Subject.ImageProcessingType = 3;
        }
    }
}

4、测试结果(这要新建窗口应用,用于测试),和保存生成vtt。

visionpro c#编辑,编辑器,c#,visualstudio
Form代码如下:

public Form1()
        {
            InitializeComponent();
            cogImageFileEditV21.Subject.Ran += new EventHandler(Subject_Ran);
            //生成VTT
            CogSerializer.SaveObjectToFile(cogTool1, Directory.GetCurrentDirectory() + "\\CogTool.vtt");

        }

        void Subject_Ran(object sender, EventArgs e)
        {
            //将cogImageFile里的图像输入进cogToolEdit里
            cogToolEditV21.Subject.InputImage = cogImageFileEditV21.Subject.OutputImage;
        }

测试结果:

visionpro c#编辑,编辑器,c#,visualstudio

5、将自定义工具加入到Vision pro中

5.1创建16*16的ico图标由于辨别区分
5.2将ico、dll:放在vision pro文件所在位置

visionpro c#编辑,编辑器,c#,visualstudio

5.3vtt:放在vision pro文件所在位置下的Templates \ Tools\自定义工具

visionpro c#编辑,编辑器,c#,visualstudio

5.4将自定义的工具添加输入输出图像的终端

visionpro c#编辑,编辑器,c#,visualstudio

5.5展示

visionpro c#编辑,编辑器,c#,visualstudio文章来源地址https://www.toymoban.com/news/detail-784402.html

到了这里,关于VisionPro自定义工具(c#)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【技巧】Latex在线工具:公式编辑器、表格编辑器

    找到什么再补充 目录 表格编辑器 公式编辑器 https://www.tablesgenerator.com/   https://www.latexlive.com/

    2024年02月15日
    浏览(44)
  • 康耐视visionpro-CogAcqFifoTool工具详细说明

    CogAcqFifoTool操作说明: ① 打开工具栏,双击或点击鼠标拖拽 添加CogAcqFifoTool ②.从图片采集设备/图像采集卡列表里选择对应的相机,视频格式选择图像格式。 Mono表示黑白图像,RGB表示彩色相机。点击初始化取相初始化相机。 ③. 设置相机的曝光时间、亮度及对比度等参数。

    2024年04月14日
    浏览(36)
  • 如何将c#在线编辑器嵌入自己项目

    首先我们需要介绍一下这个在线编辑器,当前的在线编辑器支持c#的代码编译运行,并且无需后台服务,基于 WebAssembly 实现的在浏览器端去执行我们的 c# 代码,基于 Roslyn 提供的 Api 封装一套简单的编译,并且封装了 js 的支持,让嵌入的方式更简单。 最简单的嵌入方式是使用

    2024年02月03日
    浏览(34)
  • Unity 扩展自定义编辑器窗口

    在Assets文件夹路径下任意位置创建Editor文件夹,将扩展编辑器的代码放在Editor文件夹下 代码中首先引用命名空间 然后将创建的类继承自EditorWindow 然后通过扩展编辑器菜单功能调用创建窗口的方法 要注意方法中泛型参数需要传入的是自己代码的类,这个功能是根据后面OnGUI方

    2024年04月27日
    浏览(44)
  • FairyGUI编辑器自定义菜单扩展插件

    本文涉及到的软件有:FairyGUI,VSCode 代码环境涉及到了:Lua VSCode插件:EmmyLua 在编写FairyGUI编辑器菜单前,了解一下FairyGUIEditor的API会有效的帮助我们解决很多问题。FairyGUI的扩展是通过编辑器自带的插件功能实现的,插件中我使用的是lua环境模板。导入编辑器的LuaAPI,文件可

    2024年02月12日
    浏览(35)
  • GLTF编辑器:在线模型材质编辑工具

      GLTF 编辑器 是一个功能强大、易于使用的在线3D模型编辑和查看工具,它支持多种格式的3D模型导入并将模型导出为GLB格式,除了可以对3D模型进行基本属性的修改之外,还支持对模型原点重置以及模型材质纹理修改。对于3D开发者和设计师来说,GLTF 编辑器 是一个非常有

    2024年02月07日
    浏览(44)
  • C#时间轴曲线图形编辑器开发1-基本功能

    目录 一、前言 1、简介 2、开发过程 3、工程下载链接 二、基本功能实现 1、绘图面板创建 (1)界面布置 (2)显示面板代码 (3) 面板水平方向、竖直方向移动功能实现 (4)面板放大、缩小、恢复正常显示功能实现 (5)鼠标当前位置坐标值和界面显示 (6)面板实现效果

    2024年02月15日
    浏览(30)
  • C#时间轴曲线图形编辑器开发2-核心功能实现

    目录 三、关键帧编辑 1、新建Winform工程 (1)界面布局  (2)全局变量 2、关键帧添加和删除 (1)鼠标在曲线上识别 (2)键盘按键按下捕捉 (3)关键帧添加、删除  (4)修改关键帧值 3、曲线插值 (1)三次样条插值 (2)工程代码下载链接 四、曲线数据导出和读取 1、数

    2024年02月15日
    浏览(32)
  • UE4自定义资产类型编辑器实现

    在虚幻引擎中,资产是具有持久属性的对象,可以在编辑器中进行操作。 Unreal 附带多种资源类型,从 UStaticMesh 到 UMetasoundSources 等等。 自定义资源类型是实现专门对象的好方法,这些对象需要专门构建的编辑器来进行高效操作。 通过在插件中实现这些类型,它们可以在项目

    2024年02月11日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包