123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using Cognex.VisionPro.ToolBlock;
- using Cognex.VisionPro;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using LampInspectionMachine.Views;
- using Prism.Commands;
- using System.Windows;
- using LampInspectionMachine.Core;
- using Prism.Events;
- using Prism.Ioc;
- using Prism.Regions;
- using Prism.Mvvm;
- using System.Configuration;
- using System.Drawing.Design;
- using System.Windows.Forms.Integration;
- using System.Threading;
- using System.Windows.Interop;
- using LampInspectionMachine.Model;
- using System.Globalization;
- namespace LampInspectionMachine.ViewModels
- {
- public class CameraViewModel : BindableBase, IConfirmNavigationRequest
- {
- IContainerProvider _container;
- private Management management;
- private int index;
- public Management Management { get => management; set { SetProperty(ref management, value); } }
- public CameraViewModel(IContainerProvider container, IRegionManager regionManager, IEventAggregator eventAggregator)
- {
- _container = container;
- Management = _container.Resolve<Management>();
- }
- private DelegateCommand _SoftTriggerCommand;
- /// <summary>
- /// 开始采集
- /// </summary>
- public DelegateCommand SoftTriggerCommand =>
- _SoftTriggerCommand ?? ( _SoftTriggerCommand = new DelegateCommand(OnSoftTrigger) );
- private DelegateCommand _SoftTrigger_ContinueCommand;
- /// <summary>
- /// 开始采集
- /// </summary>
- public DelegateCommand SoftTrigger_ContinueCommand =>
- _SoftTrigger_ContinueCommand ?? ( _SoftTrigger_ContinueCommand = new DelegateCommand(OnSoftTrigger__Continue) );
- private DelegateCommand _CameraInifCommand;
- /// <summary>
- /// 相机初始化
- /// </summary>
- public DelegateCommand CameraInifCommand =>
- _CameraInifCommand ?? ( _CameraInifCommand = new DelegateCommand(OnCameraInit) );
- private DelegateCommand _CameraCloseCommand;
- /// <summary>
- /// 相机关闭
- /// </summary>
- public DelegateCommand CameraCloseCommand =>
- _CameraCloseCommand ?? ( _CameraCloseCommand = new DelegateCommand(OnCameraClose) );
- private void OnSoftTrigger()
- {
-
-
- Management.SoftTrigger();
- }
- private void OnSoftTrigger__Continue()
- {
- Management.SoftTrigger__Continue();
- }
- private void OnCameraInit()
- {
- Management.InitCamera();
- }
- /// <summary>
- /// 接收参数
- /// </summary>
- /// <param name="navigationContext"></param>
- /// <exception cref="NotImplementedException"></exception>
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- if ( navigationContext.Parameters.ContainsKey("Index") )
- {
- index= Convert.ToInt32(navigationContext.Parameters.GetValue<string>("Index"));
- if ( Management.VisionProManagers!=null )
- {
- if ( index < Management.VisionProManagers.Count )
- {
- Management.CurrCamera = Management.VisionProManagers[ index ].Camera;
- Management.CurrCamConfig = Management.VisionProManagers[ index ].CameraInfo;
- Management.CurrCameraSn = Management.CurrCamConfig.SerialNumber;
- Management.Currvision=Management.VisionProManagers[ index ];
- }
- }
- }
- }
- private void OnCameraClose()
- {
- Management.CloseCamera();
- }
- public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
- {
- if ( Management.CurrCamConfig != null )
- {
- Management.CurrCamConfig.SerialNumber = Management.CurrCameraSn;
- }
- continuationCallback(true);
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- }
- }
- public static class CogDisplayBinder
- {
- public static readonly DependencyProperty ImageSourceProperty =
- DependencyProperty.RegisterAttached(
- "ImageSource",
- typeof(ICogImage),
- typeof(CogDisplayBinder),
- new FrameworkPropertyMetadata(
- null,
- FrameworkPropertyMetadataOptions.None,
- OnImageSourceChanged));
- public static ICogImage GetImageSource(WindowsFormsHost host) =>
- ( ICogImage ) host.GetValue(ImageSourceProperty);
- public static void SetImageSource(WindowsFormsHost host, ICogImage value) =>
- host.SetValue(ImageSourceProperty, value);
- public static void OnImageSourceChanged(
- DependencyObject d,
- DependencyPropertyChangedEventArgs e)
- {
- if ( d is WindowsFormsHost host &&
- host.Child is CogRecordDisplay display )
- {
- if ( e.NewValue is ICogImage newImage )
- {
- // 跨线程安全更新
- host.Dispatcher.Invoke(() =>
- {
- try
- {
- display.Image = newImage;
- display.Fit(true);
- }
- catch
- {
- }
- });
- }
- else
- {
- host.Dispatcher.Invoke(() => display.Image = null);
- }
- }
- }
- }
- }
|