CalibrationEditVisionViewModel.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. if (SelectCalibration.IsDistortionCorrection)
  143. {
  144. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationDistortion", param);
  145. }
  146. else
  147. {
  148. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationCameraParams", param);
  149. }
  150. }
  151. catch (Exception ex)
  152. {
  153. LogHelper.WriteLogError("相机校准-视觉工具界面点击上一步时出错!", ex);
  154. MessageBox.Show(ex.Message);
  155. }
  156. }
  157. void ExecuteRunVisionProcessCommand()
  158. {
  159. try
  160. {
  161. SelectCalibration.ToolBlock = ToolBlockEdit.Subject;
  162. ICogImage image = Camera.Grab();
  163. if (SelectCalibration.IsDistortionCorrection && SelectCalibration.CalibCheckerboardTool!=null)
  164. {
  165. SelectCalibration.CalibCheckerboardTool.InputImage= image;
  166. SelectCalibration.CalibCheckerboardTool.Run();
  167. image = SelectCalibration.CalibCheckerboardTool.OutputImage;
  168. }
  169. ToolBlockEdit.Subject.Inputs["InputImage"].Value = image;
  170. ToolBlockEdit.Subject.Run();
  171. }
  172. catch (Exception ex)
  173. {
  174. LogHelper.WriteLogError("相机校准-运行流程时出错时出错", ex);
  175. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  176. }
  177. }
  178. #endregion
  179. #region 继承
  180. /// <summary>
  181. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  182. /// </summary>
  183. /// <param name="navigationContext"></param>
  184. /// <param name="continuationCallback"></param>
  185. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  186. {
  187. continuationCallback(true);
  188. }
  189. /// <summary>
  190. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  191. /// </summary>
  192. /// <param name="navigationContext"></param>
  193. /// <exception cref="NotImplementedException"></exception>
  194. public async void OnNavigatedTo(NavigationContext navigationContext)
  195. {
  196. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  197. if (ToolBlockEdit != null)
  198. {
  199. ToolBlockEdit.Subject = SelectCalibration.ToolBlock;
  200. }
  201. if (SelectCalibration.RobotId != Guid.Empty)
  202. {
  203. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  204. }
  205. Camera = _cameraService.GetCamera(SelectCalibration.CameraID);
  206. }
  207. /// <summary>
  208. /// 是否允许导航到此视图模型。
  209. /// </summary>
  210. /// <param name="navigationContext"></param>
  211. /// <returns></returns>
  212. public bool IsNavigationTarget(NavigationContext navigationContext)
  213. {
  214. return true;
  215. }
  216. /// <summary>
  217. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  218. /// </summary>
  219. /// <param name="navigationContext"></param>
  220. public void OnNavigatedFrom(NavigationContext navigationContext)
  221. {
  222. }
  223. #endregion
  224. private bool _IsAllowEdit = false;
  225. public bool IsAllowEdit
  226. {
  227. get { return _IsAllowEdit; }
  228. set { SetProperty(ref _IsAllowEdit, value); }
  229. }
  230. private void LoginChange(Models.User user)
  231. {
  232. if (user.userPart == Enums.UserPart.Operator)
  233. {
  234. IsAllowEdit = false;
  235. }
  236. else
  237. {
  238. IsAllowEdit = true;
  239. }
  240. }
  241. }
  242. }