CalibrationEditVisionViewModel.cs 8.5 KB

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