CalibrationEditVisionViewModel.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using Cognex.VisionPro.ToolBlock;
  2. using Prism.Commands;
  3. using Prism.Events;
  4. using Prism.Ioc;
  5. using Prism.Mvvm;
  6. using Prism.Regions;
  7. using Prism.Services.Dialogs;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Documents;
  16. using Team.FFFeederService.Interfaces;
  17. using TeamAAS_VP.Core;
  18. using TeamAAS_VP.Events;
  19. using TeamAAS_VP.Interfaces;
  20. using TeamAAS_VP.Models;
  21. using TeamAAS_VP;
  22. using static NPOI.HSSF.Util.HSSFColor;
  23. namespace TeamAAS_VP.ViewModels.Calibration
  24. {
  25. public class CalibrationEditVisionViewModel : BindableBase
  26. {
  27. IRegionManager _regionManager;
  28. IRegionNavigationService _regionNavigationService;
  29. IContainerProvider _container;
  30. IDialogService _dialogService;
  31. IEventAggregator _eventAggregator;
  32. #region 属性
  33. private CogToolBlockEditV2 _ToolBlockEdit;
  34. public CogToolBlockEditV2 ToolBlockEdit
  35. {
  36. get { return _ToolBlockEdit; }
  37. set { SetProperty(ref _ToolBlockEdit,value); }
  38. }
  39. private CalibrationInfo _SelectCalibration;
  40. public CalibrationInfo SelectCalibration
  41. {
  42. get { return _SelectCalibration; }
  43. set { SetProperty(ref _SelectCalibration, value); }
  44. }
  45. private IRobot _Robot;
  46. public IRobot Robot
  47. {
  48. get { return _Robot; }
  49. set { SetProperty(ref _Robot,value); }
  50. }
  51. private ICamera _Camera;
  52. public ICamera Camera
  53. {
  54. get { return _Camera; }
  55. set { SetProperty(ref _Camera, value); }
  56. }
  57. #endregion
  58. #region 命令
  59. private DelegateCommand _LoadedCommand;
  60. public DelegateCommand LoadedCommand =>
  61. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  62. private DelegateCommand _NextCommand;
  63. public DelegateCommand NextCommand =>
  64. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  65. private DelegateCommand _BackCommand;
  66. public DelegateCommand BackCommand =>
  67. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  68. private DelegateCommand _RunVisionProcessCommand;
  69. public DelegateCommand RunVisionProcessCommand =>
  70. _RunVisionProcessCommand ?? (_RunVisionProcessCommand = new DelegateCommand(ExecuteRunVisionProcessCommand));
  71. #endregion
  72. #region 事件
  73. #endregion
  74. public CalibrationEditVisionViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService)
  75. {
  76. _regionManager = regionManager;
  77. _regionNavigationService = regionNavigationService;
  78. _container = container;
  79. _dialogService = dialogService;
  80. _eventAggregator = ea;
  81. SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
  82. }
  83. #region 方法
  84. void ExecuteLoadedCommand()
  85. {
  86. try
  87. {
  88. if (_container.Resolve<Management>().CurrentUser.userPart == Enums.UserPart.Operator)
  89. {
  90. IsAllowEdit = false;
  91. }
  92. else
  93. {
  94. IsAllowEdit = true;
  95. }
  96. SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
  97. if (ToolBlockEdit != null)
  98. {
  99. ToolBlockEdit.Subject = SelectCalibration.ToolBlock;
  100. }
  101. if (SelectCalibration.RobotId != Guid.Empty)
  102. {
  103. Robot = _container.Resolve<Management>().RobotService.GetRobot(SelectCalibration.RobotId);
  104. }
  105. Camera = _container.Resolve<Management>().CameraService.GetCamera(SelectCalibration.CameraID);
  106. }
  107. catch (Exception ex)
  108. {
  109. LogHelper.WriteLogError("相机校准加载视觉工具处理界面时出错!", ex);
  110. MessageBox.Show(ex.Message);
  111. }
  112. }
  113. /// <summary>
  114. /// 下一步
  115. /// </summary>
  116. void ExecuteNextCommand()
  117. {
  118. try
  119. {
  120. SelectCalibration.ToolBlock = ToolBlockEdit.Subject;
  121. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationTeachCenterPoint");
  122. }
  123. catch (Exception ex)
  124. {
  125. LogHelper.WriteLogError("相机校准-视觉工具界面点击下一步时出错!", ex);
  126. MessageBox.Show(ex.Message);
  127. }
  128. }
  129. /// <summary>
  130. /// 上一步
  131. /// </summary>
  132. void ExecuteBackCommand()
  133. {
  134. try
  135. {
  136. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationCameraParams");
  137. }
  138. catch (Exception ex)
  139. {
  140. LogHelper.WriteLogError("相机校准-视觉工具界面点击上一步时出错!", ex);
  141. MessageBox.Show(ex.Message);
  142. }
  143. }
  144. void ExecuteRunVisionProcessCommand()
  145. {
  146. try
  147. {
  148. SelectCalibration.ToolBlock = ToolBlockEdit.Subject;
  149. ToolBlockEdit.Subject.Inputs["InputImage"].Value = Camera.Grab();
  150. ToolBlockEdit.Subject.Run();
  151. }
  152. catch (Exception ex)
  153. {
  154. LogHelper.WriteLogError("相机校准-运行流程时出错时出错", ex);
  155. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  156. }
  157. }
  158. #endregion
  159. private bool _IsAllowEdit = false;
  160. public bool IsAllowEdit
  161. {
  162. get { return _IsAllowEdit; }
  163. set { SetProperty(ref _IsAllowEdit, value); }
  164. }
  165. private void LoginChange(Models.User user)
  166. {
  167. if (user.userPart == Enums.UserPart.Operator)
  168. {
  169. IsAllowEdit = false;
  170. }
  171. else
  172. {
  173. IsAllowEdit = true;
  174. }
  175. }
  176. }
  177. }