CameraViewModel.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using Cognex.VisionPro.ToolBlock;
  2. using Cognex.VisionPro;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using LampInspectionMachine.Views;
  10. using Prism.Commands;
  11. using System.Windows;
  12. using LampInspectionMachine.Core;
  13. using Prism.Events;
  14. using Prism.Ioc;
  15. using Prism.Regions;
  16. using Prism.Mvvm;
  17. using System.Configuration;
  18. using System.Drawing.Design;
  19. using System.Windows.Forms.Integration;
  20. using System.Threading;
  21. using System.Windows.Interop;
  22. using LampInspectionMachine.Model;
  23. namespace LampInspectionMachine.ViewModels
  24. {
  25. public class CameraViewModel : BindableBase, IConfirmNavigationRequest
  26. {
  27. IContainerProvider _container;
  28. private Management management;
  29. private int index;
  30. public Management Management { get => management; set { SetProperty(ref management, value); } }
  31. public CameraViewModel(IContainerProvider container, IRegionManager regionManager, IEventAggregator eventAggregator)
  32. {
  33. _container = container;
  34. Management = _container.Resolve<Management>();
  35. }
  36. private DelegateCommand _SoftTriggerCommand;
  37. /// <summary>
  38. /// 开始采集
  39. /// </summary>
  40. public DelegateCommand SoftTriggerCommand =>
  41. _SoftTriggerCommand ?? ( _SoftTriggerCommand = new DelegateCommand(OnSoftTrigger) );
  42. private DelegateCommand _SoftTrigger_ContinueCommand;
  43. /// <summary>
  44. /// 开始采集
  45. /// </summary>
  46. public DelegateCommand SoftTrigger_ContinueCommand =>
  47. _SoftTrigger_ContinueCommand ?? ( _SoftTrigger_ContinueCommand = new DelegateCommand(OnSoftTrigger__Continue) );
  48. private DelegateCommand _CameraInifCommand;
  49. /// <summary>
  50. /// 相机初始化
  51. /// </summary>
  52. public DelegateCommand CameraInifCommand =>
  53. _CameraInifCommand ?? ( _CameraInifCommand = new DelegateCommand(OnCameraInit) );
  54. private DelegateCommand _CameraCloseCommand;
  55. /// <summary>
  56. /// 相机关闭
  57. /// </summary>
  58. public DelegateCommand CameraCloseCommand =>
  59. _CameraCloseCommand ?? ( _CameraCloseCommand = new DelegateCommand(OnCameraClose) );
  60. private void OnSoftTrigger()
  61. {
  62. Management.SoftTrigger();
  63. }
  64. private void OnSoftTrigger__Continue()
  65. {
  66. Management.SoftTrigger__Continue();
  67. }
  68. private void OnCameraInit()
  69. {
  70. Management.InitCamera(Management.CurrCameraSn);
  71. }
  72. /// <summary>
  73. /// 接收参数
  74. /// </summary>
  75. /// <param name="navigationContext"></param>
  76. /// <exception cref="NotImplementedException"></exception>
  77. public void OnNavigatedTo(NavigationContext navigationContext)
  78. {
  79. if ( navigationContext.Parameters.ContainsKey("Index") )
  80. {
  81. index= Convert.ToInt32(navigationContext.Parameters.GetValue<string>("Index"));
  82. if ( Management.CameraList!=null )
  83. {
  84. if( index < Management.CameraList.Count )
  85. Management.CurrCameraSn = Management.CameraList[ index ];
  86. if ( index < Management.CamConfigs.Count )
  87. Management.CurrCamConfig = Management.CamConfigs[ index ].Copy() ;
  88. }
  89. }
  90. }
  91. private void OnCameraClose()
  92. {
  93. Management.CloseCamera();
  94. }
  95. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  96. {
  97. if ( Management.CurrCamConfig != null )
  98. {
  99. Management.CamConfigs[ index ]= Management.CurrCamConfig.Copy();
  100. }
  101. continuationCallback(true);
  102. }
  103. public bool IsNavigationTarget(NavigationContext navigationContext)
  104. {
  105. return true;
  106. }
  107. public void OnNavigatedFrom(NavigationContext navigationContext)
  108. {
  109. }
  110. }
  111. public static class CogDisplayBinder
  112. {
  113. public static readonly DependencyProperty ImageSourceProperty =
  114. DependencyProperty.RegisterAttached(
  115. "ImageSource",
  116. typeof(ICogImage),
  117. typeof(CogDisplayBinder),
  118. new FrameworkPropertyMetadata(
  119. null,
  120. FrameworkPropertyMetadataOptions.None,
  121. OnImageSourceChanged));
  122. public static ICogImage GetImageSource(WindowsFormsHost host) =>
  123. ( ICogImage ) host.GetValue(ImageSourceProperty);
  124. public static void SetImageSource(WindowsFormsHost host, ICogImage value) =>
  125. host.SetValue(ImageSourceProperty, value);
  126. public static void OnImageSourceChanged(
  127. DependencyObject d,
  128. DependencyPropertyChangedEventArgs e)
  129. {
  130. if ( d is WindowsFormsHost host &&
  131. host.Child is CogRecordDisplay display )
  132. {
  133. if ( e.NewValue is ICogImage newImage )
  134. {
  135. // 跨线程安全更新
  136. host.Dispatcher.Invoke(() =>
  137. {
  138. try
  139. {
  140. display.Image = newImage;
  141. display.Fit(true);
  142. }
  143. catch
  144. {
  145. }
  146. });
  147. }
  148. else
  149. {
  150. host.Dispatcher.Invoke(() => display.Image = null);
  151. }
  152. }
  153. }
  154. }
  155. }