CalibrationHomeViewModel.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using MaterialDesignThemes.Wpf;
  2. using NPOI.SS.Formula.Functions;
  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 SixLabors.Fonts.Unicode;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.Linq;
  14. using TeamAAS_VP.Controls;
  15. using TeamAAS_VP.Events;
  16. using TeamAAS_VP.Core;
  17. using TeamAAS_VP.Models;
  18. using TeamAAS_VP;
  19. using System.Windows;
  20. using Cognex.VisionPro.ToolBlock;
  21. using Cognex.VisionPro;
  22. using TeamAAS_VP.Resources.Languages;
  23. namespace TeamAAS_VP.ViewModels.Calibration
  24. {
  25. public class CalibrationHomeViewModel : BindableBase
  26. {
  27. IRegionManager _regionManager;
  28. IRegionNavigationService _regionNavigationService;
  29. IContainerProvider _container;
  30. IDialogService _dialogService;
  31. IEventAggregator _eventAggregator;
  32. #region 属性
  33. private ObservableCollection<RobotInfo> _Robots;
  34. public ObservableCollection<RobotInfo> Robots
  35. {
  36. get { return _Robots; }
  37. set { SetProperty(ref _Robots, value); }
  38. }
  39. private ObservableCollection<FeederInfo> _Feeders;
  40. public ObservableCollection<FeederInfo> Feeders
  41. {
  42. get { return _Feeders; }
  43. set { SetProperty(ref _Feeders, value); }
  44. }
  45. private ObservableCollection<CalibrationInfo> _Calibrations;
  46. public ObservableCollection<CalibrationInfo> Calibrations
  47. {
  48. get { return _Calibrations; }
  49. set { SetProperty(ref _Calibrations, value); }
  50. }
  51. private CalibrationInfo _SelectCalibration;
  52. public CalibrationInfo SelectCalibration
  53. {
  54. get { return _SelectCalibration; }
  55. set
  56. {
  57. if (_SelectCalibration != null && _SelectCalibration.ToolBlock != null)
  58. {
  59. _SelectCalibration.ToolBlock.Dispose();
  60. _SelectCalibration.ToolBlock = null;
  61. }
  62. SetProperty(ref _SelectCalibration, value);
  63. }
  64. }
  65. #endregion
  66. #region 命令
  67. private DelegateCommand _LoadedCommand;
  68. public DelegateCommand LoadedCommand =>
  69. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  70. private DelegateCommand _CreateCalibrationCommand;
  71. public DelegateCommand CreateCalibrationCommand =>
  72. _CreateCalibrationCommand ?? (_CreateCalibrationCommand = new DelegateCommand(ExecuteCreateCalibrationCommand));
  73. private DelegateCommand<CalibrationInfo> _StartCalibrationCommand;
  74. public DelegateCommand<CalibrationInfo> StartCalibrationCommand =>
  75. _StartCalibrationCommand ?? (_StartCalibrationCommand = new DelegateCommand<CalibrationInfo>(ExecuteStartCalibrationCommand));
  76. private DelegateCommand<CalibrationInfo> _RemoveCalibrationCommand;
  77. public DelegateCommand<CalibrationInfo> RemoveCalibrationCommand =>
  78. _RemoveCalibrationCommand ?? (_RemoveCalibrationCommand = new DelegateCommand<CalibrationInfo>(ExecuteRemoveCalibrationCommand));
  79. #endregion
  80. #region 事件
  81. #endregion
  82. public CalibrationHomeViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService)
  83. {
  84. _regionManager = regionManager;
  85. _regionNavigationService = regionNavigationService;
  86. _container = container;
  87. _dialogService = dialogService;
  88. _eventAggregator = ea;
  89. Robots = _container.Resolve<Management>().DeviceConfig.Robots;
  90. Feeders = _container.Resolve<Management>().DeviceConfig.Feeders;
  91. Calibrations = _container.Resolve<Management>().DeviceConfig.Calibrations;
  92. }
  93. #region 方法
  94. void ExecuteLoadedCommand()
  95. {
  96. if (_container.Resolve<Management>().CurrentUser.userPart == Enums.UserPart.Operator)
  97. {
  98. IsAllowEdit = false;
  99. }
  100. else
  101. {
  102. IsAllowEdit = true;
  103. }
  104. }
  105. async void ExecuteCreateCalibrationCommand()
  106. {
  107. try
  108. {
  109. var view = new AddProduct() { Title = $"{Lang.创建校准}:", Item = "Name" };
  110. //show the dialog
  111. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  112. if (result != null && !string.IsNullOrEmpty(result.ToString()))
  113. {
  114. if (!FileHelper.CheckFileName(result.ToString()))
  115. {
  116. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.输入的名称不符合文件名命名规则, Duration = 1 });
  117. return;
  118. }
  119. if (Calibrations.FirstOrDefault(p => p.Name == result.ToString()) != null)
  120. {
  121. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.已存在同名校准, Duration = 1 });
  122. return;
  123. }
  124. if (SelectCalibration != null && SelectCalibration.ToolBlock != null)
  125. {
  126. SelectCalibration.ToolBlock.Dispose();
  127. SelectCalibration.ToolBlock = null;
  128. SelectCalibration = null;
  129. }
  130. SelectCalibration = new CalibrationInfo()
  131. {
  132. Id = Guid.NewGuid(),
  133. Name = result.ToString(),
  134. DateTime = DateTime.Now,
  135. CenterPoint = new RPoint(),
  136. HomePoint = new RPoint(),
  137. Pick = new PickInfo(),
  138. Tool = new RobotTool(),
  139. Arm = new RobotArm(),
  140. CalibPoints = new ObservableCollection<RobotPixelPoint>(),
  141. Speed = 20,
  142. Accel = 20,
  143. Power = true,
  144. Width = 100,
  145. Height = 80,
  146. WaitBlow = 200,
  147. WaitPhoto = 200,
  148. WaitSuction = 200,
  149. IsCreate = true,
  150. ToolBlock= CogSerializer.LoadObjectFromFile(FilePath.CalibrationToolblockPath) as CogToolBlock,
  151. };
  152. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationBasics");
  153. }
  154. }
  155. catch (Exception ex)
  156. {
  157. LogHelper.WriteLogError("相机校准-Home界面创建校准时出错!", ex);
  158. MessageBox.Show(ex.Message);
  159. }
  160. }
  161. void ExecuteStartCalibrationCommand(CalibrationInfo calibration)
  162. {
  163. if (calibration == null)
  164. {
  165. return;
  166. }
  167. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationBasics");
  168. }
  169. void ExecuteRemoveCalibrationCommand(CalibrationInfo calibration)
  170. {
  171. if (calibration == null)
  172. {
  173. return;
  174. }
  175. try
  176. {
  177. _container.Resolve<Management>().RemoveCalibration(calibration);
  178. }
  179. catch (Exception ex)
  180. {
  181. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  182. LogHelper.WriteLogError("移除校准时出错!", ex);
  183. }
  184. }
  185. #endregion
  186. private bool _IsAllowEdit = false;
  187. public bool IsAllowEdit
  188. {
  189. get { return _IsAllowEdit; }
  190. set { SetProperty(ref _IsAllowEdit, value); }
  191. }
  192. private void LoginChange(Models.User user)
  193. {
  194. if (user.userPart == Enums.UserPart.Operator)
  195. {
  196. IsAllowEdit = false;
  197. }
  198. else
  199. {
  200. IsAllowEdit = true;
  201. }
  202. }
  203. }
  204. }