CalibrationDistortionViewModel.cs 9.3 KB

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