CalibrationEditVisionViewModel.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.ToolBlock;
  3. using Prism.Commands;
  4. using Prism.Events;
  5. using Prism.Ioc;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Documents;
  17. using Team.FFFeederService.Interfaces;
  18. using TeamAAS_VP;
  19. using TeamAAS_VP.Core;
  20. using TeamAAS_VP.Events;
  21. using TeamAAS_VP.Interfaces;
  22. using TeamAAS_VP.Models;
  23. using TeamAAS_VP.Models.Calibration;
  24. using TeamAAS_VP.Resources.Languages;
  25. using static NPOI.HSSF.Util.HSSFColor;
  26. namespace TeamAAS_VP.ViewModels.Calibration
  27. {
  28. public class CalibrationEditVisionViewModel : BindableBase, IConfirmNavigationRequest
  29. {
  30. IRegionManager _regionManager;
  31. IRegionNavigationService _regionNavigationService;
  32. IContainerProvider _container;
  33. IDialogService _dialogService;
  34. IEventAggregator _eventAggregator;
  35. IConfigService _configService;
  36. IRobotService _robotService;
  37. ICameraService _cameraService;
  38. IFeederService _feederService;
  39. ISystemDatabaseService _systemDatabaseService;
  40. #region 属性
  41. private CogToolBlockEditV2 _ToolBlockEdit;
  42. public CogToolBlockEditV2 ToolBlockEdit
  43. {
  44. get { return _ToolBlockEdit; }
  45. set { SetProperty(ref _ToolBlockEdit,value); }
  46. }
  47. private CalibrationInfo _SelectCalibration;
  48. public CalibrationInfo SelectCalibration
  49. {
  50. get { return _SelectCalibration; }
  51. set { SetProperty(ref _SelectCalibration, value); }
  52. }
  53. private IRobot _Robot;
  54. public IRobot Robot
  55. {
  56. get { return _Robot; }
  57. set { SetProperty(ref _Robot,value); }
  58. }
  59. private ICamera _Camera;
  60. public ICamera Camera
  61. {
  62. get { return _Camera; }
  63. set { SetProperty(ref _Camera, value); }
  64. }
  65. #endregion
  66. #region 命令
  67. private DelegateCommand _LoadedCommand;
  68. public DelegateCommand LoadedCommand =>
  69. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  70. private DelegateCommand _NextCommand;
  71. public DelegateCommand NextCommand =>
  72. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  73. private DelegateCommand _BackCommand;
  74. public DelegateCommand BackCommand =>
  75. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  76. private DelegateCommand _RunVisionProcessCommand;
  77. public DelegateCommand RunVisionProcessCommand =>
  78. _RunVisionProcessCommand ?? (_RunVisionProcessCommand = new DelegateCommand(ExecuteRunVisionProcessCommand));
  79. #endregion
  80. #region 事件
  81. #endregion
  82. public CalibrationEditVisionViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
  83. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, ISystemDatabaseService systemDatabaseService)
  84. {
  85. _regionManager = regionManager;
  86. _regionNavigationService = regionNavigationService;
  87. _container = container;
  88. _dialogService = dialogService;
  89. _eventAggregator = ea;
  90. _configService = configService;
  91. _robotService = robotService;
  92. _cameraService = cameraService;
  93. _feederService = feederService;
  94. _systemDatabaseService = systemDatabaseService;
  95. }
  96. #region 方法
  97. void ExecuteLoadedCommand()
  98. {
  99. try
  100. {
  101. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  102. {
  103. IsAllowEdit = false;
  104. }
  105. else
  106. {
  107. IsAllowEdit = true;
  108. }
  109. }
  110. catch (Exception ex)
  111. {
  112. LogHelper.WriteLogError(Lang.相机校准加载视觉工具处理界面时出错, ex);
  113. MessageBox.Show(ex.Message);
  114. }
  115. }
  116. /// <summary>
  117. /// 下一步
  118. /// </summary>
  119. void ExecuteNextCommand()
  120. {
  121. try
  122. {
  123. SelectCalibration.ToolBlock = ToolBlockEdit.Subject;
  124. NavigationParameters param = new NavigationParameters();
  125. param.Add("SelectCalibration", SelectCalibration);
  126. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationTeachCenterPoint", param);
  127. }
  128. catch (Exception ex)
  129. {
  130. LogHelper.WriteLogError(Lang.相机校准视觉工具界面点击下一步时出错, ex);
  131. MessageBox.Show(ex.Message);
  132. }
  133. }
  134. /// <summary>
  135. /// 上一步
  136. /// </summary>
  137. void ExecuteBackCommand()
  138. {
  139. try
  140. {
  141. NavigationParameters param = new NavigationParameters();
  142. param.Add("SelectCalibration", SelectCalibration);
  143. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationCameraParams", param);
  144. }
  145. catch (Exception ex)
  146. {
  147. LogHelper.WriteLogError(Lang.相机校准视觉工具界面点击上一步时出错, ex);
  148. MessageBox.Show(ex.Message);
  149. }
  150. }
  151. void ExecuteRunVisionProcessCommand()
  152. {
  153. try
  154. {
  155. SelectCalibration.ToolBlock = ToolBlockEdit.Subject;
  156. ToolBlockEdit.Subject.Inputs["InputImage"].Value = Camera.Grab();
  157. ToolBlockEdit.Subject.Run();
  158. }
  159. catch (Exception ex)
  160. {
  161. LogHelper.WriteLogError(Lang.相机校准运行流程时出错时出错, ex);
  162. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  163. }
  164. }
  165. #endregion
  166. #region 继承
  167. /// <summary>
  168. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  169. /// </summary>
  170. /// <param name="navigationContext"></param>
  171. /// <param name="continuationCallback"></param>
  172. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  173. {
  174. continuationCallback(true);
  175. }
  176. /// <summary>
  177. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  178. /// </summary>
  179. /// <param name="navigationContext"></param>
  180. /// <exception cref="NotImplementedException"></exception>
  181. public async void OnNavigatedTo(NavigationContext navigationContext)
  182. {
  183. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  184. if (ToolBlockEdit != null)
  185. {
  186. ToolBlockEdit.Subject = SelectCalibration.ToolBlock;
  187. }
  188. if (SelectCalibration.RobotId != Guid.Empty)
  189. {
  190. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  191. }
  192. Camera = _cameraService.GetCamera(SelectCalibration.CameraID);
  193. }
  194. /// <summary>
  195. /// 是否允许导航到此视图模型。
  196. /// </summary>
  197. /// <param name="navigationContext"></param>
  198. /// <returns></returns>
  199. public bool IsNavigationTarget(NavigationContext navigationContext)
  200. {
  201. return true;
  202. }
  203. /// <summary>
  204. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  205. /// </summary>
  206. /// <param name="navigationContext"></param>
  207. public void OnNavigatedFrom(NavigationContext navigationContext)
  208. {
  209. }
  210. #endregion
  211. private bool _IsAllowEdit = false;
  212. public bool IsAllowEdit
  213. {
  214. get { return _IsAllowEdit; }
  215. set { SetProperty(ref _IsAllowEdit, value); }
  216. }
  217. private void LoginChange(Models.User user)
  218. {
  219. if (user.userPart == Enums.UserPart.Operator)
  220. {
  221. IsAllowEdit = false;
  222. }
  223. else
  224. {
  225. IsAllowEdit = true;
  226. }
  227. }
  228. }
  229. }