CalibrationEditVisionViewModel.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.ToolBlock;
  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.IO;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Documents;
  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.Models.Calibration;
  24. using static NPOI.HSSF.Util.HSSFColor;
  25. namespace TeamAAS_VP.ViewModels.Calibration
  26. {
  27. public class CalibrationEditVisionViewModel : BindableBase, IConfirmNavigationRequest
  28. {
  29. IRegionManager _regionManager;
  30. IRegionNavigationService _regionNavigationService;
  31. IContainerProvider _container;
  32. IDialogService _dialogService;
  33. IEventAggregator _eventAggregator;
  34. IConfigService _configService;
  35. IRobotService _robotService;
  36. ICameraService _cameraService;
  37. IFeederService _feederService;
  38. ISystemDatabaseService _systemDatabaseService;
  39. #region 属性
  40. private CogToolBlockEditV2 _ToolBlockEdit;
  41. public CogToolBlockEditV2 ToolBlockEdit
  42. {
  43. get { return _ToolBlockEdit; }
  44. set { SetProperty(ref _ToolBlockEdit,value); }
  45. }
  46. private CalibrationInfo _SelectCalibration;
  47. public CalibrationInfo SelectCalibration
  48. {
  49. get { return _SelectCalibration; }
  50. set { SetProperty(ref _SelectCalibration, value); }
  51. }
  52. private IRobot _Robot;
  53. public IRobot Robot
  54. {
  55. get { return _Robot; }
  56. set { SetProperty(ref _Robot,value); }
  57. }
  58. private ICamera _Camera;
  59. public ICamera Camera
  60. {
  61. get { return _Camera; }
  62. set { SetProperty(ref _Camera, value); }
  63. }
  64. #endregion
  65. #region 命令
  66. private DelegateCommand _LoadedCommand;
  67. public DelegateCommand LoadedCommand =>
  68. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  69. private DelegateCommand _NextCommand;
  70. public DelegateCommand NextCommand =>
  71. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  72. private DelegateCommand _BackCommand;
  73. public DelegateCommand BackCommand =>
  74. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  75. private DelegateCommand _RunVisionProcessCommand;
  76. public DelegateCommand RunVisionProcessCommand =>
  77. _RunVisionProcessCommand ?? (_RunVisionProcessCommand = new DelegateCommand(ExecuteRunVisionProcessCommand));
  78. #endregion
  79. #region 事件
  80. #endregion
  81. public CalibrationEditVisionViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
  82. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, ISystemDatabaseService systemDatabaseService)
  83. {
  84. _regionManager = regionManager;
  85. _regionNavigationService = regionNavigationService;
  86. _container = container;
  87. _dialogService = dialogService;
  88. _eventAggregator = ea;
  89. _configService = configService;
  90. _robotService = robotService;
  91. _cameraService = cameraService;
  92. _feederService = feederService;
  93. _systemDatabaseService = systemDatabaseService;
  94. }
  95. #region 方法
  96. void ExecuteLoadedCommand()
  97. {
  98. try
  99. {
  100. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  101. {
  102. IsAllowEdit = false;
  103. }
  104. else
  105. {
  106. IsAllowEdit = true;
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. LogHelper.WriteLogError("相机校准加载视觉工具处理界面时出错!", ex);
  112. MessageBox.Show(ex.Message);
  113. }
  114. }
  115. /// <summary>
  116. /// 下一步
  117. /// </summary>
  118. void ExecuteNextCommand()
  119. {
  120. try
  121. {
  122. SelectCalibration.ToolBlock = ToolBlockEdit.Subject;
  123. string toolBlockPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + SelectCalibration.Name+".vpp";
  124. if (!Directory.Exists(Path.GetDirectoryName(toolBlockPath)))
  125. {
  126. Directory.CreateDirectory(Path.GetDirectoryName(toolBlockPath));
  127. }
  128. //保存校准工具
  129. CogSerializer.SaveObjectToFile(ToolBlockEdit.Subject, toolBlockPath);
  130. NavigationParameters param = new NavigationParameters();
  131. param.Add("SelectCalibration", SelectCalibration);
  132. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationTeachCenterPoint", param);
  133. }
  134. catch (Exception ex)
  135. {
  136. LogHelper.WriteLogError("相机校准-视觉工具界面点击下一步时出错!", ex);
  137. MessageBox.Show(ex.Message);
  138. }
  139. }
  140. /// <summary>
  141. /// 上一步
  142. /// </summary>
  143. void ExecuteBackCommand()
  144. {
  145. try
  146. {
  147. NavigationParameters param = new NavigationParameters();
  148. param.Add("SelectCalibration", SelectCalibration);
  149. if (SelectCalibration.IsDistortionCorrection)
  150. {
  151. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationDistortion", param);
  152. }
  153. else
  154. {
  155. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationCameraParams", param);
  156. }
  157. }
  158. catch (Exception ex)
  159. {
  160. LogHelper.WriteLogError("相机校准-视觉工具界面点击上一步时出错!", ex);
  161. MessageBox.Show(ex.Message);
  162. }
  163. }
  164. void ExecuteRunVisionProcessCommand()
  165. {
  166. try
  167. {
  168. SelectCalibration.ToolBlock = ToolBlockEdit.Subject;
  169. ICogImage image = Camera.Grab();
  170. if (SelectCalibration.IsDistortionCorrection && SelectCalibration.CalibCheckerboardTool!=null)
  171. {
  172. SelectCalibration.CalibCheckerboardTool.InputImage= image;
  173. SelectCalibration.CalibCheckerboardTool.Run();
  174. image = SelectCalibration.CalibCheckerboardTool.OutputImage;
  175. }
  176. ToolBlockEdit.Subject.Inputs["InputImage"].Value = image;
  177. ToolBlockEdit.Subject.Run();
  178. }
  179. catch (Exception ex)
  180. {
  181. LogHelper.WriteLogError("相机校准-运行流程时出错时出错", ex);
  182. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  183. }
  184. }
  185. #endregion
  186. #region 继承
  187. /// <summary>
  188. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  189. /// </summary>
  190. /// <param name="navigationContext"></param>
  191. /// <param name="continuationCallback"></param>
  192. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  193. {
  194. continuationCallback(true);
  195. }
  196. /// <summary>
  197. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  198. /// </summary>
  199. /// <param name="navigationContext"></param>
  200. /// <exception cref="NotImplementedException"></exception>
  201. public async void OnNavigatedTo(NavigationContext navigationContext)
  202. {
  203. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  204. if (ToolBlockEdit != null)
  205. {
  206. ToolBlockEdit.Subject = SelectCalibration.ToolBlock;
  207. }
  208. if (SelectCalibration.RobotId != Guid.Empty)
  209. {
  210. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  211. }
  212. Camera = _cameraService.GetCamera(SelectCalibration.CameraID);
  213. }
  214. /// <summary>
  215. /// 是否允许导航到此视图模型。
  216. /// </summary>
  217. /// <param name="navigationContext"></param>
  218. /// <returns></returns>
  219. public bool IsNavigationTarget(NavigationContext navigationContext)
  220. {
  221. return true;
  222. }
  223. /// <summary>
  224. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  225. /// </summary>
  226. /// <param name="navigationContext"></param>
  227. public void OnNavigatedFrom(NavigationContext navigationContext)
  228. {
  229. }
  230. #endregion
  231. private bool _IsAllowEdit = false;
  232. public bool IsAllowEdit
  233. {
  234. get { return _IsAllowEdit; }
  235. set { SetProperty(ref _IsAllowEdit, value); }
  236. }
  237. private void LoginChange(Models.User user)
  238. {
  239. if (user.userPart == Enums.UserPart.Operator)
  240. {
  241. IsAllowEdit = false;
  242. }
  243. else
  244. {
  245. IsAllowEdit = true;
  246. }
  247. }
  248. }
  249. }