AdvancedProcedureViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using Cognex.VisionPro.ToolBlock;
  2. using MathNet.Numerics.LinearAlgebra;
  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.Collections.ObjectModel;
  12. using System.Linq;
  13. using System.Web.Hosting;
  14. using System.Windows;
  15. using System.Windows.Media.Media3D;
  16. using Team.FFFeederService;
  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.Resources.Languages;
  24. namespace TeamAAS_VP.ViewModels.Product
  25. {
  26. public class AdvancedProcedureViewModel : BindableBase, IConfirmNavigationRequest
  27. {
  28. IRegionManager _regionManager;
  29. IEventAggregator _eventAggregator;
  30. IContainerProvider _container;
  31. IDialogService _dialogService;
  32. IFeederService _feederService;
  33. IRobotService _robotService;
  34. ICameraService _cameraService;
  35. ISystemDatabaseService _systemDatabaseService;
  36. #region 属性
  37. private ProductModel _SelectProduct;
  38. /// <summary>
  39. /// 选中的产品
  40. /// </summary>
  41. public ProductModel SelectProduct
  42. {
  43. get { return _SelectProduct; }
  44. set { SetProperty(ref _SelectProduct, value); }
  45. }
  46. private ProcedureModel _SelectProcedure;
  47. /// <summary>
  48. /// 选中的流程
  49. /// </summary>
  50. public ProcedureModel SelectProcedure
  51. {
  52. get { return _SelectProcedure; }
  53. set
  54. {
  55. SetProperty(ref _SelectProcedure, value);
  56. }
  57. }
  58. public CogToolBlockEditV2 ToolBlockEdit { get; set; }
  59. private IRobot _Robot;
  60. public IRobot Robot
  61. {
  62. get { return _Robot; }
  63. set { SetProperty(ref _Robot, value); }
  64. }
  65. private ICamera _Camera;
  66. public ICamera Camera
  67. {
  68. get { return _Camera; }
  69. set { SetProperty(ref _Camera, value); }
  70. }
  71. private string _Message;
  72. public string Message
  73. {
  74. get { return _Message; }
  75. set { SetProperty(ref _Message, value); }
  76. }
  77. #endregion
  78. #region 命令
  79. private DelegateCommand _NextCommand;
  80. public DelegateCommand NextCommand =>
  81. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  82. private DelegateCommand _BackCommand;
  83. public DelegateCommand BackCommand =>
  84. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  85. private DelegateCommand _LoadedCommand;
  86. public DelegateCommand LoadedCommand =>
  87. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  88. private DelegateCommand _RunVisionProcessCommand;
  89. public DelegateCommand RunVisionProcessCommand =>
  90. _RunVisionProcessCommand ?? (_RunVisionProcessCommand = new DelegateCommand(ExecuteRunVisionProcessCommand));
  91. private DelegateCommand _VisionStaticAccuracyAnalyzerCommand;
  92. public DelegateCommand VisionStaticAccuracyAnalyzerCommand =>
  93. _VisionStaticAccuracyAnalyzerCommand ?? (_VisionStaticAccuracyAnalyzerCommand = new DelegateCommand(ExecuteVisionStaticAccuracyAnalyzerCommand));
  94. #endregion
  95. #region 事件
  96. #endregion
  97. public AdvancedProcedureViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
  98. IFeederService feederService, IRobotService robotService, ICameraService cameraService, ISystemDatabaseService systemDatabaseService)
  99. {
  100. _regionManager = regionManager;
  101. _eventAggregator = ea;
  102. _container = container;
  103. _dialogService = dialogService;
  104. _feederService = feederService;
  105. _robotService = robotService;
  106. _cameraService = cameraService;
  107. _systemDatabaseService = systemDatabaseService;
  108. //订阅权限登录事件
  109. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  110. }
  111. #region 方法
  112. /// <summary>
  113. /// 下一步
  114. /// </summary>
  115. void ExecuteNextCommand()
  116. {
  117. try
  118. {
  119. NavigationParameters param = new NavigationParameters();
  120. param.Add("SelectProduct", SelectProduct);
  121. param.Add("SelectedProcedure", SelectProcedure);
  122. SelectProcedure.ToolBlock = ToolBlockEdit.Subject;
  123. if (SelectProcedure.FeederId == Guid.Empty)
  124. {
  125. _regionManager.RequestNavigate("ProductRegionContext", "ProcedureVisionOutput", param);
  126. }
  127. else
  128. {
  129. var management = _container.Resolve<Management>();
  130. var Feeder = _feederService.GetFeeder(SelectProcedure.FeederId);
  131. if (Feeder.FeederBrand == FeederBrand.AFAG)
  132. {
  133. _regionManager.RequestNavigate("ProductRegionContext", "AfagFeederParams", param);
  134. }
  135. else
  136. {
  137. _regionManager.RequestNavigate("ProductRegionContext", "FeederParams", param);
  138. }
  139. }
  140. }
  141. catch (Exception ex)
  142. {
  143. LogHelper.WriteLogError(ex.Message, ex);
  144. }
  145. }
  146. /// <summary>
  147. /// 上一步
  148. /// </summary>
  149. void ExecuteBackCommand()
  150. {
  151. try
  152. {
  153. NavigationParameters param = new NavigationParameters();
  154. param.Add("SelectProduct", SelectProduct);
  155. param.Add("SelectedProcedure", SelectProcedure);
  156. _regionManager.RequestNavigate("ProductRegionContext", "CameraParams", param);
  157. }
  158. catch (Exception ex)
  159. {
  160. LogHelper.WriteLogError(ex.Message, ex);
  161. }
  162. }
  163. void ExecuteLoadedCommand()
  164. {
  165. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  166. {
  167. IsAllowEdit = false;
  168. }
  169. else
  170. {
  171. IsAllowEdit = true;
  172. }
  173. }
  174. void ExecuteRunVisionProcessCommand()
  175. {
  176. try
  177. {
  178. SelectProcedure.ToolBlock = ToolBlockEdit.Subject;
  179. var image = Camera.Grab();
  180. if (image != null)
  181. {
  182. ToolBlockEdit.Subject.Inputs["InputImage"].Value = image;
  183. ToolBlockEdit.Subject.Run();
  184. Message = $"{Lang.图像采集耗时}:{Camera.TotalTime.TotalMilliseconds.ToString("F2")} ms {Lang.工具运行耗时}:{ToolBlockEdit.Subject.RunStatus.TotalTime.ToString("F2")} ms";
  185. }
  186. else
  187. {
  188. Message = Lang.图像采集失败耗时.Replace("{0}", Camera.TotalTime.TotalMilliseconds.ToString("F2")).Replace("{1}", Camera.ErrorMessage);
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. LogHelper.WriteLogError(Lang.高级模式运行流程时出错时出错, ex);
  194. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  195. }
  196. }
  197. /// <summary>
  198. /// 执行视觉静态精度分析仪命令
  199. /// </summary>
  200. void ExecuteVisionStaticAccuracyAnalyzerCommand()
  201. {
  202. try
  203. {
  204. IDialogParameters parameters = new DialogParameters();
  205. parameters.Add("ToolBlock", ToolBlockEdit.Subject);
  206. parameters.Add("SelectedProcedure", SelectProcedure);
  207. _dialogService.Show("VisionStaticAccuracyAnalyzer", parameters, rst =>
  208. {
  209. //对话框关闭之后的回调函数,可以在这解析结果。
  210. ButtonResult result1 = rst.Result;
  211. if (result1 == ButtonResult.OK)
  212. {
  213. //var param = rst.Parameters.GetValue<RobotTool>("Tool");
  214. //var param1 = rst.Parameters.GetValue<Matrix<double>>("RobotCameraRotationMatrix");
  215. //var param2 = rst.Parameters.GetValue<double>("Angle");
  216. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  217. }
  218. });
  219. }
  220. catch (Exception ex)
  221. {
  222. LogHelper.WriteLogError("执行视觉静态精度分析仪命令时出错", ex);
  223. MessageBox.Show(ex.Message);
  224. }
  225. }
  226. #endregion
  227. #region 继承
  228. /// <summary>
  229. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  230. /// </summary>
  231. /// <param name="navigationContext"></param>
  232. /// <param name="continuationCallback"></param>
  233. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  234. {
  235. continuationCallback(true);
  236. }
  237. /// <summary>
  238. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  239. /// </summary>
  240. /// <param name="navigationContext"></param>
  241. /// <exception cref="NotImplementedException"></exception>
  242. public async void OnNavigatedTo(NavigationContext navigationContext)
  243. {
  244. try
  245. {
  246. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  247. SelectProcedure = navigationContext.Parameters.GetValue<ProcedureModel>("SelectedProcedure");
  248. if (SelectProcedure.RobotId != Guid.Empty)
  249. {
  250. Robot = _robotService.GetRobot(SelectProcedure.RobotId);
  251. if (Robot != null)
  252. {
  253. Robot.EnterDebugMode = true;
  254. }
  255. }
  256. Camera = _cameraService.GetCamera(SelectProcedure.CameraId);
  257. ToolBlockEdit.Subject = SelectProcedure.ToolBlock;
  258. }
  259. catch (Exception ex)
  260. {
  261. LogHelper.WriteLogError(Lang.高级视觉界面进入时出错, ex);
  262. }
  263. }
  264. /// <summary>
  265. /// 是否允许导航到此视图模型。
  266. /// </summary>
  267. /// <param name="navigationContext"></param>
  268. /// <returns></returns>
  269. public bool IsNavigationTarget(NavigationContext navigationContext)
  270. {
  271. return true;
  272. }
  273. /// <summary>
  274. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  275. /// </summary>
  276. /// <param name="navigationContext"></param>
  277. public void OnNavigatedFrom(NavigationContext navigationContext)
  278. {
  279. if (Robot != null)
  280. {
  281. Robot.EnterDebugMode = false;
  282. }
  283. }
  284. #endregion
  285. private bool _IsAllowEdit = false;
  286. public bool IsAllowEdit
  287. {
  288. get { return _IsAllowEdit; }
  289. set { SetProperty(ref _IsAllowEdit, value); }
  290. }
  291. private void LoginChange(Models.User user)
  292. {
  293. if (user.userPart == Enums.UserPart.Operator)
  294. {
  295. IsAllowEdit = false;
  296. }
  297. else
  298. {
  299. IsAllowEdit = true;
  300. }
  301. }
  302. }
  303. }