CalibrationSelectToolViewModel.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. SelectCalibration.CenterPoint = Robot.GetRobotPos();
  115. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "完成!", Duration = 1 });
  116. }
  117. });
  118. }
  119. catch (Exception ex)
  120. {
  121. LogHelper.WriteLogError("相机校准-选择工具坐标界面跳转至校准工具界面出错!", ex);
  122. MessageBox.Show(ex.Message);
  123. }
  124. }
  125. /// <summary>
  126. /// 下一步
  127. /// </summary>
  128. void ExecuteNextCommand()
  129. {
  130. NavigationParameters param = new NavigationParameters();
  131. param.Add("SelectCalibration", SelectCalibration);
  132. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationAuto", param);
  133. }
  134. /// <summary>
  135. /// 上一步
  136. /// </summary>
  137. void ExecuteBackCommand()
  138. {
  139. NavigationParameters param = new NavigationParameters();
  140. param.Add("SelectCalibration", SelectCalibration);
  141. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationTeachCenterPoint", param);
  142. }
  143. #endregion
  144. #region 继承
  145. /// <summary>
  146. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  147. /// </summary>
  148. /// <param name="navigationContext"></param>
  149. /// <param name="continuationCallback"></param>
  150. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  151. {
  152. continuationCallback(true);
  153. }
  154. /// <summary>
  155. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  156. /// </summary>
  157. /// <param name="navigationContext"></param>
  158. /// <exception cref="NotImplementedException"></exception>
  159. public async void OnNavigatedTo(NavigationContext navigationContext)
  160. {
  161. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  162. if (SelectCalibration != null)
  163. {
  164. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  165. }
  166. }
  167. /// <summary>
  168. /// 是否允许导航到此视图模型。
  169. /// </summary>
  170. /// <param name="navigationContext"></param>
  171. /// <returns></returns>
  172. public bool IsNavigationTarget(NavigationContext navigationContext)
  173. {
  174. return true;
  175. }
  176. /// <summary>
  177. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  178. /// </summary>
  179. /// <param name="navigationContext"></param>
  180. public void OnNavigatedFrom(NavigationContext navigationContext)
  181. {
  182. }
  183. #endregion
  184. private bool _IsAllowEdit = false;
  185. public bool IsAllowEdit
  186. {
  187. get { return _IsAllowEdit; }
  188. set { SetProperty(ref _IsAllowEdit, value); }
  189. }
  190. private void LoginChange(Models.User user)
  191. {
  192. if (user.userPart == Enums.UserPart.Operator)
  193. {
  194. IsAllowEdit = false;
  195. }
  196. else
  197. {
  198. IsAllowEdit = true;
  199. }
  200. }
  201. }
  202. }