CalibrationDistortionViewModel.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.CalibFix;
  3. using Cognex.VisionPro.ToolBlock;
  4. using Prism.Commands;
  5. using Prism.Events;
  6. using Prism.Ioc;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using Prism.Services.Dialogs;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Windows;
  14. using Team.FFFeederService.Interfaces;
  15. using TeamAAS_VP.Core;
  16. using TeamAAS_VP.Events;
  17. using TeamAAS_VP.Interfaces;
  18. using TeamAAS_VP.Models.Calibration;
  19. namespace TeamAAS_VP.ViewModels.Calibration
  20. {
  21. public class CalibrationDistortionViewModel : BindableBase, IConfirmNavigationRequest
  22. {
  23. IRegionManager _regionManager;
  24. IRegionNavigationService _regionNavigationService;
  25. IContainerProvider _container;
  26. IDialogService _dialogService;
  27. IEventAggregator _eventAggregator;
  28. IConfigService _configService;
  29. IRobotService _robotService;
  30. ICameraService _cameraService;
  31. IFeederService _feederService;
  32. ISystemDatabaseService _systemDatabaseService;
  33. #region 属性
  34. private CogCalibCheckerboardEditV2 _CalibCheckerboardEdit;
  35. public CogCalibCheckerboardEditV2 CalibCheckerboardEdit
  36. {
  37. get { return _CalibCheckerboardEdit; }
  38. set { SetProperty(ref _CalibCheckerboardEdit, value); }
  39. }
  40. private CalibrationInfo _SelectCalibration;
  41. public CalibrationInfo SelectCalibration
  42. {
  43. get { return _SelectCalibration; }
  44. set { SetProperty(ref _SelectCalibration, value); }
  45. }
  46. private ICamera _Camera;
  47. public ICamera Camera
  48. {
  49. get { return _Camera; }
  50. set { SetProperty(ref _Camera, value); }
  51. }
  52. #endregion
  53. #region 命令
  54. private DelegateCommand _LoadedCommand;
  55. public DelegateCommand LoadedCommand =>
  56. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  57. private DelegateCommand _NextCommand;
  58. public DelegateCommand NextCommand =>
  59. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  60. private DelegateCommand _BackCommand;
  61. public DelegateCommand BackCommand =>
  62. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  63. private DelegateCommand _RunCommand;
  64. public DelegateCommand RunCommand =>
  65. _RunCommand ?? (_RunCommand = new DelegateCommand(ExecuteRunCommand));
  66. #endregion
  67. #region 事件
  68. #endregion
  69. public CalibrationDistortionViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
  70. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, ISystemDatabaseService systemDatabaseService)
  71. {
  72. _regionManager = regionManager;
  73. _regionNavigationService = regionNavigationService;
  74. _container = container;
  75. _dialogService = dialogService;
  76. _eventAggregator = ea;
  77. _configService = configService;
  78. _robotService = robotService;
  79. _cameraService = cameraService;
  80. _feederService = feederService;
  81. _systemDatabaseService = systemDatabaseService;
  82. }
  83. #region 方法
  84. void ExecuteLoadedCommand()
  85. {
  86. try
  87. {
  88. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  89. {
  90. IsAllowEdit = false;
  91. }
  92. else
  93. {
  94. IsAllowEdit = true;
  95. }
  96. }
  97. catch (Exception ex)
  98. {
  99. MessageBox.Show(ex.Message);
  100. }
  101. }
  102. /// <summary>
  103. /// 下一步
  104. /// </summary>
  105. void ExecuteNextCommand()
  106. {
  107. try
  108. {
  109. string calibToolPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + "CalibCheckerboard.vpp";
  110. SelectCalibration.CalibCheckerboardTool = CalibCheckerboardEdit.Subject;
  111. //保存校准工具
  112. CogSerializer.SaveObjectToFile(CalibCheckerboardEdit.Subject, calibToolPath);
  113. NavigationParameters param = new NavigationParameters();
  114. param.Add("SelectCalibration", SelectCalibration);
  115. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationEditVision", param);
  116. }
  117. catch (Exception ex)
  118. {
  119. LogHelper.WriteLogError("相机校准-畸变校准工具界面点击下一步时出错!", ex);
  120. MessageBox.Show(ex.Message);
  121. }
  122. }
  123. /// <summary>
  124. /// 上一步
  125. /// </summary>
  126. void ExecuteBackCommand()
  127. {
  128. try
  129. {
  130. NavigationParameters param = new NavigationParameters();
  131. param.Add("SelectCalibration", SelectCalibration);
  132. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationCameraParams", param);
  133. }
  134. catch (Exception ex)
  135. {
  136. LogHelper.WriteLogError("相机校准-畸变校准工具界面点击上一步时出错!", ex);
  137. MessageBox.Show(ex.Message);
  138. }
  139. }
  140. /// <summary>
  141. /// 运行
  142. /// </summary>
  143. void ExecuteRunCommand()
  144. {
  145. try
  146. {
  147. SelectCalibration.CalibCheckerboardTool = CalibCheckerboardEdit.Subject;
  148. CalibCheckerboardEdit.Subject.InputImage = Camera.Grab();
  149. CalibCheckerboardEdit.Subject.Run();
  150. }
  151. catch (Exception ex)
  152. {
  153. LogHelper.WriteLogError("相机校准-畸变校准时出错", ex);
  154. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  155. }
  156. }
  157. #endregion
  158. #region 继承
  159. /// <summary>
  160. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  161. /// </summary>
  162. /// <param name="navigationContext"></param>
  163. /// <param name="continuationCallback"></param>
  164. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  165. {
  166. continuationCallback(true);
  167. }
  168. /// <summary>
  169. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  170. /// </summary>
  171. /// <param name="navigationContext"></param>
  172. /// <exception cref="NotImplementedException"></exception>
  173. public async void OnNavigatedTo(NavigationContext navigationContext)
  174. {
  175. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  176. if (CalibCheckerboardEdit != null)
  177. {
  178. //先判断校准文件夹下是否存在校准工具VPP文件,存在则加载,不存在则新建一个工具
  179. string calibToolPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + "CalibCheckerboard.vpp";
  180. if (System.IO.File.Exists(calibToolPath))
  181. {
  182. SelectCalibration.CalibCheckerboardTool = CogSerializer.LoadObjectFromFile(calibToolPath) as CogCalibCheckerboardTool;
  183. CalibCheckerboardEdit.Subject = SelectCalibration.CalibCheckerboardTool;
  184. }
  185. else
  186. {
  187. SelectCalibration.CalibCheckerboardTool = new CogCalibCheckerboardTool();
  188. CalibCheckerboardEdit.Subject = SelectCalibration.CalibCheckerboardTool;
  189. }
  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. }