这是二次开发的第二个文章,所以不重复说明环境配置相关的内容。如果不懂的可以看本专栏的上一个文章。
海康视觉算法平台VisionMaster 4.3.0 C# 二次开发01 加载方案并获取结果-CSDN博客
- <Window x:Class="VmTestWpf.App.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:vm="clr-namespace:VMControls.WPF.Release;assembly=VMControls.WPF.Release"
- xmlns:local="clr-namespace:VmTestWpf.App"
- mc:Ignorable="d"
- Title="MainWindow" Height="450" Width="900">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition/>
- <RowDefinition Height="45"/>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition/>
- <ColumnDefinition Width="300"/>
- </Grid.ColumnDefinitions>
- <!--显示方案执行结果-->
- <vm:VmRenderControl Grid.Row="0" Grid.Column="0"
- x:Name="vrcRender" />
-
- <!--操作按钮-->
- <WrapPanel Grid.Row="1" Grid.Column="0" >
- <Button x:Name="btnLoad" Click="btnLoad_Click"
- Content="加载方案" Width="100" Height="35" HorizontalAlignment="Left"/>
- <Button Grid.Row="1" Grid.Column="1" Name="btnRun" Click="btnRun_Click"
- Content="运行" Width="80" Height="35" HorizontalAlignment="Left"
- Margin="5"/>
- </WrapPanel>
-
- <!--消息显示-->
- <ListBox Grid.Row="1" Grid.Column="1" x:Name="lbRunResult">
- </ListBox>
- </Grid>
- </Window>
注意命名空间的引用
using VM.Core;
using VM.PlatformSDKCS;
using VMControls.Interface;
using IMVSCircleFindModuCs;
- using IMVSCircleFindModuCs;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using VM.Core;
- using VM.PlatformSDKCS;
- using VMControls.Interface;
-
- namespace VmTestWpf.App
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- Loaded += MainWindow_Loaded;
- }
-
- /// <summary>
- /// 流程对象
- /// </summary>
- VmProcedure _vmProcedure;
-
- private void MainWindow_Loaded(object sender, RoutedEventArgs e)
- {
-
- }
-
- /// <summary>
- /// 加载方案
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnLoad_Click(object sender, RoutedEventArgs e)
- {
- //加载方案
- VmSolution.Load("F:/二期VM视觉学习/标定相机找圆心20240614.sol", string.Empty);
-
- //使用流程名称获取流程对象
- _vmProcedure = (VmProcedure)VmSolution.Instance["流程1"];
- var msg = "方案加载成功";
- lbRunResult.Items.Insert(0, msg);
- }
-
- /// <summary>
- /// 获取方案结果
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnGetResult_Click(object sender, RoutedEventArgs e)
- {
- // 获取应用程序的执行目录
- //string appDirectory = Assembly.GetExecutingAssembly().Location;
- //appDirectory = appDirectory.Substring(0, appDirectory.LastIndexOf("\\") + 1);
- //string imagePathRelative = "TestProCess/01/12345.sol";
-
- //加载方案
- VmSolution.Load("F:/二期VM视觉学习/标定相机找圆心20240614.sol", string.Empty);
-
- //使用流程名称获取流程对象
- VmProcedure vmProcedure = (VmProcedure)VmSolution.Instance["流程1"];
-
- //运行一次流程才能拿到结果
- vmProcedure.SyncRun();
-
- IMVSCircleFindModuTool circleFitResult = (IMVSCircleFindModuTool)VmSolution.Instance["流程1.圆查找1"];
- if (circleFitResult != null)
- {
- MessageBox.Show("获取到的圆心坐标:X:" + circleFitResult.ModuResult.OutputCircle.CenterPoint.X.ToString("0.0000") + ",Y:" + circleFitResult.ModuResult.OutputCircle.CenterPoint.Y.ToString("0.0000"));
- }
- else
- {
- MessageBox.Show("失败");
- }
- }
-
- /// <summary>
- /// 运行
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnRun_Click(object sender, RoutedEventArgs e)
- {
- if (_vmProcedure != null)
- {
- //使用流程名称获取流程对象
- _vmProcedure = (VmProcedure)VmSolution.Instance["流程1"];
-
- //运行一次流程才能拿到结果
- //_vmProcedure.SyncRun();
- _vmProcedure.Run();
- //获取 圆查找1 结果
- IMVSCircleFindModuTool circleFitResult = (IMVSCircleFindModuTool)VmSolution.Instance["流程1.圆查找1"];
- if (circleFitResult != null)
- {
- vrcRender.ModuleSource = circleFitResult;
- var msg = "获取到的圆心坐标:X:" + circleFitResult.ModuResult.OutputCircle.CenterPoint.X.ToString("0.0000") + ",Y:" + circleFitResult.ModuResult.OutputCircle.CenterPoint.Y.ToString("0.0000");
- lbRunResult.Items.Insert(0, msg);
- //MessageBox.Show("获取到的圆心坐标:X:" + circleFitResult.ModuResult.OutputCircle.CenterPoint.X.ToString("0.0000") + ",Y:" + circleFitResult.ModuResult.OutputCircle.CenterPoint.Y.ToString("0.0000"));
- }
- else
- {
- MessageBox.Show("失败");
- }
- }
- else
- {
- MessageBox.Show("流程加载失败");
- }
- }
- }
- }