CalibrationSelectToolViewModel.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using MathNet.Numerics.LinearAlgebra;
  2. using OpenCvSharp;
  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.Linq;
  12. using System.Windows;
  13. using Team.FFFeederService.Interfaces;
  14. using TeamAAS_VP;
  15. using TeamAAS_VP.Core;
  16. using TeamAAS_VP.Events;
  17. using TeamAAS_VP.Interfaces;
  18. using TeamAAS_VP.Models;
  19. using TeamAAS_VP.Models.Calibration;
  20. using TeamAAS_VP.Services;
  21. namespace TeamAAS_VP.ViewModels.Calibration
  22. {
  23. public class CalibrationSelectToolViewModel : BindableBase, IConfirmNavigationRequest
  24. {
  25. IRegionManager _regionManager;
  26. IRegionNavigationService _regionNavigationService;
  27. IContainerProvider _container;
  28. IDialogService _dialogService;
  29. IEventAggregator _eventAggregator;
  30. IConfigService _configService;
  31. IRobotService _robotService;
  32. ICameraService _cameraService;
  33. IFeederService _feederService;
  34. ISystemDatabaseService _systemDatabaseService;
  35. #region 属性
  36. private CalibrationInfo _SelectCalibration;
  37. public CalibrationInfo SelectCalibration
  38. {
  39. get { return _SelectCalibration; }
  40. set { SetProperty(ref _SelectCalibration, value); }
  41. }
  42. public Management management { get; set; }
  43. public IRobot Robot { get; set; }
  44. #endregion
  45. #region 命令
  46. private DelegateCommand _LoadedCommand;
  47. public DelegateCommand LoadedCommand =>
  48. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  49. private DelegateCommand _NextCommand;
  50. public DelegateCommand NextCommand =>
  51. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  52. private DelegateCommand _BackCommand;
  53. public DelegateCommand BackCommand =>
  54. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  55. private DelegateCommand _CalibToolCommand;
  56. public DelegateCommand CalibToolCommand =>
  57. _CalibToolCommand ?? (_CalibToolCommand = new DelegateCommand(ExecuteCalibToolCommand));
  58. #endregion
  59. #region 事件
  60. #endregion
  61. public CalibrationSelectToolViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
  62. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, ISystemDatabaseService systemDatabaseService)
  63. {
  64. _regionManager = regionManager;
  65. _regionNavigationService = regionNavigationService;
  66. _container = container;
  67. _dialogService = dialogService;
  68. _eventAggregator = ea;
  69. _configService = configService;
  70. _robotService = robotService;
  71. _cameraService = cameraService;
  72. _feederService = feederService;
  73. _systemDatabaseService = systemDatabaseService;
  74. }
  75. #region 方法
  76. void ExecuteLoadedCommand()
  77. {
  78. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  79. {
  80. IsAllowEdit = false;
  81. }
  82. else
  83. {
  84. IsAllowEdit = true;
  85. }
  86. }
  87. /// <summary>
  88. /// 跳转至校准工具界面
  89. /// </summary>
  90. void ExecuteCalibToolCommand()
  91. {
  92. try
  93. {
  94. IDialogParameters parameters = new DialogParameters();
  95. parameters.Add("SelectCalibration", SelectCalibration);
  96. _dialogService.Show("CalibTool", parameters, rst =>
  97. {
  98. //对话框关闭之后的回调函数,可以在这解析结果。
  99. ButtonResult result1 = rst.Result;
  100. if (result1 == ButtonResult.OK)
  101. {
  102. var param = rst.Parameters.GetValue<RobotTool>("Tool");
  103. var param1 = rst.Parameters.GetValue<Matrix<double>>("RobotCameraRotationMatrix");
  104. var param2 = rst.Parameters.GetValue<double>("Angle");
  105. if (param != null)
  106. {
  107. SelectCalibration.Tool = param;
  108. }
  109. if (param1 != null)
  110. {
  111. SelectCalibration.RobotCameraRotationMatrix = param1.ToArray();
  112. }
  113. SelectCalibration.Angle = param2;
  114. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "完成!", Duration = 1 });
  115. }
  116. });
  117. }
  118. catch (Exception ex)
  119. {
  120. LogHelper.WriteLogError("相机校准-选择工具坐标界面跳转至校准工具界面出错!", ex);
  121. MessageBox.Show(ex.Message);
  122. }
  123. }
  124. /// <summary>
  125. /// 下一步
  126. /// </summary>
  127. void ExecuteNextCommand()
  128. {
  129. NavigationParameters param = new NavigationParameters();
  130. param.Add("SelectCalibration", SelectCalibration);
  131. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationAuto", param);
  132. }
  133. /// <summary>
  134. /// 上一步
  135. /// </summary>
  136. void ExecuteBackCommand()
  137. {
  138. NavigationParameters param = new NavigationParameters();
  139. param.Add("SelectCalibration", SelectCalibration);
  140. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationTeachCenterPoint", param);
  141. }
  142. #endregion
  143. #region 继承
  144. /// <summary>
  145. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  146. /// </summary>
  147. /// <param name="navigationContext"></param>
  148. /// <param name="continuationCallback"></param>
  149. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  150. {
  151. continuationCallback(true);
  152. }
  153. /// <summary>
  154. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  155. /// </summary>
  156. /// <param name="navigationContext"></param>
  157. /// <exception cref="NotImplementedException"></exception>
  158. public async void OnNavigatedTo(NavigationContext navigationContext)
  159. {
  160. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  161. if (SelectCalibration != null)
  162. {
  163. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  164. }
  165. }
  166. /// <summary>
  167. /// 是否允许导航到此视图模型。
  168. /// </summary>
  169. /// <param name="navigationContext"></param>
  170. /// <returns></returns>
  171. public bool IsNavigationTarget(NavigationContext navigationContext)
  172. {
  173. return true;
  174. }
  175. /// <summary>
  176. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  177. /// </summary>
  178. /// <param name="navigationContext"></param>
  179. public void OnNavigatedFrom(NavigationContext navigationContext)
  180. {
  181. }
  182. #endregion
  183. private bool _IsAllowEdit = false;
  184. public bool IsAllowEdit
  185. {
  186. get { return _IsAllowEdit; }
  187. set { SetProperty(ref _IsAllowEdit, value); }
  188. }
  189. private void LoginChange(Models.User user)
  190. {
  191. if (user.userPart == Enums.UserPart.Operator)
  192. {
  193. IsAllowEdit = false;
  194. }
  195. else
  196. {
  197. IsAllowEdit = true;
  198. }
  199. }
  200. }
  201. }