CalibrationHomeViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.ToolBlock;
  3. using MaterialDesignThemes.Wpf;
  4. using NPOI.SS.Formula.Functions;
  5. using Prism.Commands;
  6. using Prism.Events;
  7. using Prism.Ioc;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using Prism.Services.Dialogs;
  11. using SixLabors.Fonts.Unicode;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Collections.ObjectModel;
  15. using System.Linq;
  16. using System.Windows;
  17. using Team.FFFeederService.Interfaces;
  18. using TeamAAS_VP;
  19. using TeamAAS_VP.Controls;
  20. using TeamAAS_VP.Core;
  21. using TeamAAS_VP.Events;
  22. using TeamAAS_VP.Interfaces;
  23. using TeamAAS_VP.Models;
  24. using TeamAAS_VP.Models.Calibration;
  25. using TeamAAS_VP.Models.Robot;
  26. using TeamAAS_VP.Resources.Languages;
  27. using TeamAAS_VP.Services;
  28. namespace TeamAAS_VP.ViewModels.Calibration
  29. {
  30. public class CalibrationHomeViewModel : BindableBase, IConfirmNavigationRequest
  31. {
  32. IRegionManager _regionManager;
  33. IRegionNavigationService _regionNavigationService;
  34. IContainerProvider _container;
  35. IDialogService _dialogService;
  36. IEventAggregator _eventAggregator;
  37. IConfigService _configService;
  38. IFeederService _feederService;
  39. IRobotService _robotService;
  40. ICameraService _cameraService;
  41. ICalibrationService _calibrationService;
  42. ISystemDatabaseService _systemDatabaseService;
  43. #region 属性
  44. private ObservableCollection<RobotInfo> _Robots;
  45. public ObservableCollection<RobotInfo> Robots
  46. {
  47. get { return _Robots; }
  48. set { SetProperty(ref _Robots, value); }
  49. }
  50. private ObservableCollection<FeederInfo> _Feeders;
  51. public ObservableCollection<FeederInfo> Feeders
  52. {
  53. get { return _Feeders; }
  54. set { SetProperty(ref _Feeders, value); }
  55. }
  56. private ObservableCollection<CalibrationInfo> _Calibrations;
  57. public ObservableCollection<CalibrationInfo> Calibrations
  58. {
  59. get { return _Calibrations; }
  60. set { SetProperty(ref _Calibrations, value); }
  61. }
  62. private CalibrationInfo _SelectCalibration;
  63. public CalibrationInfo SelectCalibration
  64. {
  65. get { return _SelectCalibration; }
  66. set
  67. {
  68. if (_SelectCalibration != null && _SelectCalibration.ToolBlock != null)
  69. {
  70. _SelectCalibration.ToolBlock.Dispose();
  71. _SelectCalibration.ToolBlock = null;
  72. }
  73. SetProperty(ref _SelectCalibration, value);
  74. }
  75. }
  76. #endregion
  77. #region 命令
  78. private DelegateCommand _LoadedCommand;
  79. public DelegateCommand LoadedCommand =>
  80. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  81. private DelegateCommand _CreateCalibrationCommand;
  82. public DelegateCommand CreateCalibrationCommand =>
  83. _CreateCalibrationCommand ?? (_CreateCalibrationCommand = new DelegateCommand(ExecuteCreateCalibrationCommand));
  84. private DelegateCommand<CalibrationInfo> _StartCalibrationCommand;
  85. public DelegateCommand<CalibrationInfo> StartCalibrationCommand =>
  86. _StartCalibrationCommand ?? (_StartCalibrationCommand = new DelegateCommand<CalibrationInfo>(ExecuteStartCalibrationCommand));
  87. private DelegateCommand<CalibrationInfo> _RemoveCalibrationCommand;
  88. public DelegateCommand<CalibrationInfo> RemoveCalibrationCommand =>
  89. _RemoveCalibrationCommand ?? (_RemoveCalibrationCommand = new DelegateCommand<CalibrationInfo>(ExecuteRemoveCalibrationCommand));
  90. #endregion
  91. #region 事件
  92. #endregion
  93. public CalibrationHomeViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
  94. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, ICalibrationService calibrationService, ISystemDatabaseService systemDatabaseService)
  95. {
  96. _regionManager = regionManager;
  97. _regionNavigationService = regionNavigationService;
  98. _container = container;
  99. _dialogService = dialogService;
  100. _eventAggregator = ea;
  101. _configService = configService;
  102. _robotService = robotService;
  103. _cameraService = cameraService;
  104. _feederService = feederService;
  105. _calibrationService = calibrationService;
  106. _systemDatabaseService = systemDatabaseService;
  107. }
  108. #region 方法
  109. void ExecuteLoadedCommand()
  110. {
  111. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  112. {
  113. IsAllowEdit = false;
  114. }
  115. else
  116. {
  117. IsAllowEdit = true;
  118. }
  119. }
  120. async void ExecuteCreateCalibrationCommand()
  121. {
  122. try
  123. {
  124. var view = new AddProduct() { Title = $"{Lang.创建校准}:", Item = "Name" };
  125. //show the dialog
  126. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  127. if (result != null && !string.IsNullOrEmpty(result.ToString()))
  128. {
  129. if (!FileHelper.CheckFileName(result.ToString()))
  130. {
  131. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.输入的名称不符合文件名命名规则, Duration = 1 });
  132. return;
  133. }
  134. if (Calibrations.FirstOrDefault(p => p.Name == result.ToString()) != null)
  135. {
  136. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.已存在同名校准, Duration = 1 });
  137. return;
  138. }
  139. if (SelectCalibration != null && SelectCalibration.ToolBlock != null)
  140. {
  141. SelectCalibration.ToolBlock.Dispose();
  142. SelectCalibration.ToolBlock = null;
  143. SelectCalibration = null;
  144. }
  145. SelectCalibration = new CalibrationInfo()
  146. {
  147. Id = Guid.NewGuid(),
  148. Name = result.ToString(),
  149. DateTime = DateTime.Now,
  150. CenterPoint = new RPoint(),
  151. HomePoint = new RPoint(),
  152. Pick = new PickInfo(),
  153. Tool = new RobotTool(),
  154. Arm = new RobotArm(),
  155. CalibPoints = new ObservableCollection<RobotPixelPoint>(),
  156. Speed = 20,
  157. Accel = 20,
  158. Power = true,
  159. Width = 100,
  160. Height = 80,
  161. WaitBlow = 200,
  162. WaitPhoto = 200,
  163. WaitSuction = 200,
  164. IsCreate = true,
  165. ToolBlock = CogSerializer.LoadObjectFromFile(FilePath.CalibrationToolblockPath) as CogToolBlock,
  166. };
  167. NavigationParameters param= new NavigationParameters();
  168. param.Add("SelectCalibration", SelectCalibration);
  169. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationBasics", param);
  170. }
  171. }
  172. catch (Exception ex)
  173. {
  174. LogHelper.WriteLogError("相机校准-Home界面创建校准时出错!", ex);
  175. MessageBox.Show(ex.Message);
  176. }
  177. }
  178. void ExecuteStartCalibrationCommand(CalibrationInfo calibration)
  179. {
  180. if (calibration == null)
  181. {
  182. return;
  183. }
  184. NavigationParameters param = new NavigationParameters();
  185. param.Add("SelectCalibration", SelectCalibration);
  186. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationBasics", param);
  187. }
  188. void ExecuteRemoveCalibrationCommand(CalibrationInfo calibration)
  189. {
  190. if (calibration == null)
  191. {
  192. return;
  193. }
  194. try
  195. {
  196. var calib = Calibrations.FirstOrDefault(x => x.Id == calibration.Id);
  197. Calibrations.Remove(calibration);
  198. _calibrationService.RemoveCalibration(calib.Id);
  199. }
  200. catch (Exception ex)
  201. {
  202. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  203. LogHelper.WriteLogError("移除校准时出错!", ex);
  204. }
  205. }
  206. #endregion
  207. #region 继承
  208. /// <summary>
  209. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  210. /// </summary>
  211. /// <param name="navigationContext"></param>
  212. /// <param name="continuationCallback"></param>
  213. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  214. {
  215. continuationCallback(true);
  216. }
  217. /// <summary>
  218. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  219. /// </summary>
  220. /// <param name="navigationContext"></param>
  221. /// <exception cref="NotImplementedException"></exception>
  222. public async void OnNavigatedTo(NavigationContext navigationContext)
  223. {
  224. Robots = new ObservableCollection<RobotInfo>();
  225. var robots = _configService.GetAllRobots();
  226. foreach (var item in robots)
  227. {
  228. Robots.Add(item);
  229. }
  230. Feeders = new ObservableCollection<FeederInfo>();
  231. var feeders = _configService.GetAllFeeders();
  232. foreach (var item in feeders)
  233. {
  234. Feeders.Add(item);
  235. }
  236. Calibrations = new ObservableCollection<CalibrationInfo>();
  237. var calibs = _calibrationService.GetAllCalibrations();
  238. foreach (var item in calibs)
  239. {
  240. Calibrations.Add(item);
  241. }
  242. try
  243. {
  244. //SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
  245. if (SelectCalibration != null)
  246. {
  247. if (SelectCalibration.RobotId != Guid.Empty)
  248. {
  249. IRobot Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  250. Robot.EnterDebugMode = false;
  251. }
  252. }
  253. }
  254. catch (Exception)
  255. {
  256. }
  257. }
  258. /// <summary>
  259. /// 是否允许导航到此视图模型。
  260. /// </summary>
  261. /// <param name="navigationContext"></param>
  262. /// <returns></returns>
  263. public bool IsNavigationTarget(NavigationContext navigationContext)
  264. {
  265. return true;
  266. }
  267. /// <summary>
  268. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  269. /// </summary>
  270. /// <param name="navigationContext"></param>
  271. public void OnNavigatedFrom(NavigationContext navigationContext)
  272. {
  273. }
  274. #endregion
  275. private bool _IsAllowEdit = false;
  276. public bool IsAllowEdit
  277. {
  278. get { return _IsAllowEdit; }
  279. set { SetProperty(ref _IsAllowEdit, value); }
  280. }
  281. private void LoginChange(Models.User user)
  282. {
  283. if (user.userPart == Enums.UserPart.Operator)
  284. {
  285. IsAllowEdit = false;
  286. }
  287. else
  288. {
  289. IsAllowEdit = true;
  290. }
  291. }
  292. }
  293. }