using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading;using System.Windows.Forms;using System.IO;using Cognex.VisionPro;using Cognex.VisionPro.ToolBlock;using Cognex.VisionPro.ImageFile;namespace FXD_ScanLine{public partial class Form1 : Form{private CogImageFileTool m_ImageFileTool1;private Thread AcquisionImageThreadObject1; //图像采集线程1private bool AcquisionImageThread1_Stop = true; //图像采集线程1停止标记public Form1(){InitializeComponent();//图像处理线程实例化1AcquisionImageThreadObject1 = new Thread(new ThreadStart(AcquisionImageThread1_Function));//允许跨线程访问界面控件System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//加载算法文件string AlgorithmModalPath = Directory.GetCurrentDirectory() + \"/Scanline1.vpp\";cogToolBlockEdit1.Subject = CogSerializer.LoadObjectFromFile(AlgorithmModalPath) as CogToolBlock;//绑定ToolBlock事件cogToolBlockEdit1.Subject.Ran += new EventHandler(GetResult1_VisionPro);//加载图像m_ImageFileTool1 = new CogImageFileTool();m_ImageFileTool1.Operator.Open(Directory.GetCurrentDirectory() + \"/离线图像/1.bmp\", CogImageFileModeConstants.Read);m_ImageFileTool1.Run();}//cogToolBlockEdit1绑定事件public void GetResult1_VisionPro(object sender, EventArgs e){//获取ToolBlock中输出参数内容int OutPutElementsCount=cogToolBlockEdit1.Subject.Outputs.Count;string[] OutPutElements = cogToolBlockEdit1.Subject.Outputs.GetFormattedTerminalStrings();for (int i = 0; i < cogToolBlockEdit1.Subject.Outputs.Count; i++){int StartPosition = OutPutElements[i].IndexOf(\'|\');int EndPosition = OutPutElements[i].LastIndexOf(\'|\');string OutPutElementsName = OutPutElements[i].Substring(StartPosition + 1, EndPosition - StartPosition - 1);double OutPutElementsValue = (double)cogToolBlockEdit1.Subject.Outputs[OutPutElementsName].Value;this.Result_label.Text = OutPutElementsName.ToString() + \":\" + OutPutElementsValue + \"\\n\";}}//图像测试线程函数1public void AcquisionImageThread1_Function(){while (AcquisionImageThread1_Stop){//等待按下测试按钮}while (!AcquisionImageThread1_Stop){cogToolBlockEdit1.Subject.Inputs[\"Image\"].Value = m_ImageFileTool1.OutputImage as CogImage8Grey;cogToolBlockEdit1.Subject.Run();DisplayInteractiveImage(cogToolBlockEdit1);}//while}//显示图像public void DisplayInteractiveImage(CogToolBlockEditV2 CogToolBlockEdit){cogRecordDisplay.Image = CogToolBlockEdit.Subject.Inputs[\"Image\"].Value as CogImage8Grey;cogRecordDisplay.Record = CogToolBlockEdit.Subject.CreateLastRunRecord();cogRecordDisplay.Fit(true);}//开始测试private void StartTestFunction(object sender, EventArgs e){AcquisionImageThread1_Stop = false; //图像采集线程1停止标记}//开始播放private void StartPlayFunction(object sender, EventArgs e){//开启图像测试线程1if (AcquisionImageThreadObject1.ThreadState == System.Threading.ThreadState.Unstarted){AcquisionImageThreadObject1.Start();}if ((AcquisionImageThreadObject1.ThreadState == System.Threading.ThreadState.Stopped) || (AcquisionImageThreadObject1.ThreadState == System.Threading.ThreadState.Aborted)){AcquisionImageThreadObject1 = new Thread(new ThreadStart(AcquisionImageThread1_Function));AcquisionImageThreadObject1.Start();}}//停止测试private void StopTestFunction(object sender, EventArgs e){AcquisionImageThread1_Stop = true;}//释放资源private void Release(object sender, FormClosingEventArgs e){AcquisionImageThread1_Stop = true;//Application.Exit();//this.Close();System.Environment.Exit(0);}}//类}//命名空间
NETC#调用Visionpro视觉软件ToolBlock工具文件
未经允许不得转载:爱站程序员基地 » NETC#调用Visionpro视觉软件ToolBlock工具文件