ProcessBasicsViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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.Collections.ObjectModel;
  12. using System.ComponentModel;
  13. using System.Diagnostics.Eventing.Reader;
  14. using System.IO;
  15. using System.Linq;
  16. using TeamAAS_VP;
  17. using TeamAAS_VP.Core;
  18. using TeamAAS_VP.Events;
  19. using TeamAAS_VP.Interfaces;
  20. using TeamAAS_VP.Models;
  21. using TeamAAS_VP.Models.Calibration;
  22. using TeamAAS_VP.Models.Product;
  23. namespace TeamAAS_VP.ViewModels.Product
  24. {
  25. public class ProcessBasicsViewModel : BindableBase, IConfirmNavigationRequest
  26. {
  27. IRegionManager _regionManager;
  28. IEventAggregator _eventAggregator;
  29. IContainerProvider _container;
  30. IDialogService _dialogService;
  31. IConfigService _configService;
  32. ICalibrationService _calibrationService;
  33. ISystemDatabaseService _systemDatabaseService;
  34. #region 属性
  35. private ProductModel _SelectProduct;
  36. /// <summary>
  37. /// 选中的产品
  38. /// </summary>
  39. public ProductModel SelectProduct
  40. {
  41. get { return _SelectProduct; }
  42. set { SetProperty(ref _SelectProduct, value); }
  43. }
  44. private ProcedureModel _SelectProcedure;
  45. /// <summary>
  46. /// 选中的流程
  47. /// </summary>
  48. public ProcedureModel SelectProcedure
  49. {
  50. get { return _SelectProcedure; }
  51. set { SetProperty(ref _SelectProcedure, value);
  52. if (value == null)
  53. {
  54. SelectRobot = null;
  55. SelectFeeder = null;
  56. SelectCalibration = null;
  57. }
  58. else
  59. {
  60. SelectRobot = _configService.GetAllRobots().FirstOrDefault(p => p.Id == value.RobotId);
  61. SelectFeeder = _configService.GetAllFeeders().FirstOrDefault(p => p.Id == value.FeederId);
  62. SelectCalibration = _calibrationService.GetAllCalibrations().FirstOrDefault(p => p.Id == value.CalibrationId);
  63. }
  64. }
  65. }
  66. private ObservableCollection<ProcedureModel> _ProcedureModels;
  67. /// <summary>
  68. /// 流程集合
  69. /// </summary>
  70. public ObservableCollection<ProcedureModel> ProcedureModels
  71. {
  72. get { return _ProcedureModels; }
  73. set { SetProperty(ref _ProcedureModels,value); }
  74. }
  75. private RobotInfo _SelectRobot;
  76. public RobotInfo SelectRobot
  77. {
  78. get {
  79. if (SelectProcedure != null)
  80. {
  81. if (SelectProcedure.RobotId == Guid.Empty)
  82. {
  83. return null;
  84. }
  85. else
  86. {
  87. return _configService.GetAllRobots().FirstOrDefault(p => p.Id == SelectProcedure.RobotId);
  88. }
  89. }
  90. else
  91. {
  92. return null;
  93. }
  94. }
  95. set { SetProperty(ref _SelectRobot, value);
  96. if (value == null)
  97. {
  98. if (SelectProcedure!=null)
  99. {
  100. SelectProcedure.RobotId = Guid.Empty;
  101. }
  102. return;
  103. }
  104. if (SelectProcedure != null)
  105. SelectProcedure.RobotId = value.Id;
  106. }
  107. }
  108. private FeederInfo _SelectFeeder;
  109. public FeederInfo SelectFeeder
  110. {
  111. get
  112. {
  113. if (SelectProcedure != null)
  114. {
  115. if (SelectProcedure.FeederId==Guid.Empty)
  116. {
  117. return null;
  118. }
  119. else
  120. {
  121. return _configService.GetAllFeeders().FirstOrDefault(p => p.Id == SelectProcedure.FeederId);
  122. }
  123. }
  124. else
  125. {
  126. return null;
  127. }
  128. }
  129. set
  130. {
  131. SetProperty(ref _SelectFeeder, value);
  132. if (value == null)
  133. {
  134. if (SelectProcedure != null)
  135. {
  136. SelectProcedure.FeederId = Guid.Empty;
  137. }
  138. return;
  139. }
  140. if (SelectProcedure != null)
  141. SelectProcedure.FeederId = value.Id;
  142. }
  143. }
  144. private CalibrationInfo _SelectCalibration;
  145. public CalibrationInfo SelectCalibration
  146. {
  147. get
  148. {
  149. if (SelectProcedure != null)
  150. {
  151. return _calibrationService.GetAllCalibrations().FirstOrDefault(p => p.Id == SelectProcedure.CalibrationId);
  152. }
  153. else
  154. {
  155. return null;
  156. }
  157. }
  158. set
  159. {
  160. SetProperty(ref _SelectCalibration, value);
  161. if(SelectProcedure==null)
  162. return;
  163. try
  164. {
  165. if (value == null)
  166. SelectProcedure.CalibrationId = Guid.Empty;
  167. if (SelectProcedure != null)
  168. SelectProcedure.CalibrationId = value.Id;
  169. }
  170. catch (Exception)
  171. {
  172. }
  173. }
  174. }
  175. private ObservableCollection<RobotInfo> _RobotList;
  176. public ObservableCollection<RobotInfo> RobotList
  177. {
  178. get { return _RobotList; }
  179. set { SetProperty(ref _RobotList, value); }
  180. }
  181. private ObservableCollection<FeederInfo> _FeederList;
  182. public ObservableCollection<FeederInfo> FeederList
  183. {
  184. get { return _FeederList; }
  185. set { SetProperty(ref _FeederList, value); }
  186. }
  187. private ObservableCollection<CalibrationInfo> _CalibrationList;
  188. public ObservableCollection<CalibrationInfo> CalibrationList
  189. {
  190. get { return _CalibrationList; }
  191. set { SetProperty(ref _CalibrationList, value); }
  192. }
  193. private bool CanNext
  194. {
  195. get
  196. {
  197. if (SelectProcedure!=null && !string.IsNullOrEmpty(SelectProcedure.TriggerCommand))
  198. {
  199. return true;
  200. }
  201. else
  202. {
  203. return false;
  204. }
  205. }
  206. }
  207. #endregion
  208. #region 命令
  209. private DelegateCommand _NextCommand;
  210. public DelegateCommand NextCommand =>
  211. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand).ObservesCanExecute(()=> CanNext).ObservesProperty(()=> SelectProcedure).ObservesProperty(() => SelectProcedure.TriggerCommand));
  212. private DelegateCommand _BackCommand;
  213. public DelegateCommand BackCommand =>
  214. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  215. private DelegateCommand _PointPageCommand;
  216. public DelegateCommand PointPageCommand =>
  217. _PointPageCommand ?? (_PointPageCommand = new DelegateCommand(ExecutePointPageCommand));
  218. private DelegateCommand _LoadedCommand;
  219. public DelegateCommand LoadedCommand =>
  220. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  221. #endregion
  222. #region 事件
  223. #endregion
  224. public ProcessBasicsViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
  225. IConfigService configService, ICalibrationService calibrationService, ISystemDatabaseService systemDatabaseService)
  226. {
  227. _regionManager = regionManager;
  228. _eventAggregator = ea;
  229. _container = container;
  230. _dialogService = dialogService;
  231. _configService = configService;
  232. _calibrationService = calibrationService;
  233. _systemDatabaseService = systemDatabaseService;
  234. //订阅权限登录事件
  235. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  236. }
  237. #region 方法
  238. /// <summary>
  239. /// 窗体加载事件
  240. /// </summary>
  241. void ExecuteLoadedCommand()
  242. {
  243. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  244. {
  245. IsAllowEdit = false;
  246. }
  247. else
  248. {
  249. IsAllowEdit = true;
  250. }
  251. }
  252. /// <summary>
  253. /// 下一步
  254. /// </summary>
  255. void ExecuteNextCommand()
  256. {
  257. try
  258. {
  259. if(SelectProcedure==null)
  260. return;
  261. //if (SelectProduct.IsCreate && SelectProcedure.ToolBlock == null)
  262. //{
  263. // SelectProcedure.ToolBlock = CogSerializer.LoadObjectFromFile(FilePath.SimpleProcedurePath) as CogToolBlock;
  264. //}
  265. //else if (SelectProcedure.ToolBlock == null)
  266. //{
  267. // string prcPath = FilePath.ProductsPath + "//" + SelectProduct.Name + "//" + SelectProcedure.CameraName + "//" + SelectProcedure.Name + ".vpp";
  268. // if (File.Exists(prcPath))
  269. // {
  270. // SelectProcedure.ToolBlock = CogSerializer.LoadObjectFromFile(prcPath) as CogToolBlock;
  271. // }
  272. // else
  273. // {
  274. // SelectProcedure.ToolBlock = CogSerializer.LoadObjectFromFile(FilePath.SimpleProcedurePath) as CogToolBlock;
  275. // }
  276. //}
  277. NavigationParameters param = new NavigationParameters();
  278. param.Add("SelectProduct", SelectProduct);
  279. param.Add("SelectedProcedure", SelectProcedure);
  280. _regionManager.RequestNavigate("ProductRegionContext", "CameraParams", param);
  281. }
  282. catch (Exception ex)
  283. {
  284. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  285. LogHelper.WriteLogError("产品配置-加载流程时出错",ex);
  286. }
  287. }
  288. /// <summary>
  289. /// 上一步
  290. /// </summary>
  291. void ExecuteBackCommand()
  292. {
  293. NavigationParameters param = new NavigationParameters();
  294. param.Add("SelectProduct", SelectProduct);
  295. param.Add("SelectedProcedure", SelectProcedure);
  296. _regionManager.RequestNavigate("ProductRegionContext", "ProductProcessManage", param);
  297. }
  298. /// <summary>
  299. /// 跳转至点位界面
  300. /// </summary>
  301. void ExecutePointPageCommand()
  302. {
  303. NavigationParameters param = new NavigationParameters();
  304. param.Add("SelectProduct", SelectProduct);
  305. param.Add("SelectedProcedure", SelectProcedure);
  306. _regionManager.RequestNavigate("ProductRegionContext", "RobotPointParams", param);
  307. }
  308. /// <summary>
  309. /// 流程选择
  310. /// </summary>
  311. /// <param name="sender"></param>
  312. /// <param name="e"></param>
  313. public void ComboBox_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
  314. {
  315. //ProcedureModels.Clear();
  316. //foreach (var item in SelectProduct.CameraProcedures)
  317. //{
  318. // foreach (var item1 in item.ProcedureModels)
  319. // {
  320. // ProcedureModels.Add(item1);
  321. // }
  322. //}
  323. }
  324. #endregion
  325. #region 继承
  326. /// <summary>
  327. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  328. /// </summary>
  329. /// <param name="navigationContext"></param>
  330. /// <param name="continuationCallback"></param>
  331. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  332. {
  333. continuationCallback(true);
  334. }
  335. /// <summary>
  336. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  337. /// </summary>
  338. /// <param name="navigationContext"></param>
  339. /// <exception cref="NotImplementedException"></exception>
  340. public async void OnNavigatedTo(NavigationContext navigationContext)
  341. {
  342. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  343. ProcedureModels = new ObservableCollection<ProcedureModel>();
  344. foreach (var item in SelectProduct.CameraProcedures)
  345. {
  346. foreach (var item1 in item.ProcedureModels)
  347. {
  348. ProcedureModels.Add(item1);
  349. }
  350. }
  351. RobotList= new ObservableCollection<RobotInfo>(_configService.GetAllRobots());
  352. FeederList= new ObservableCollection<FeederInfo>(_configService.GetAllFeeders());
  353. CalibrationList= new ObservableCollection<CalibrationInfo>(_calibrationService.GetAllCalibrations());
  354. if (navigationContext.Parameters.ContainsKey("SelectedProcedure"))
  355. {
  356. SelectProcedure = navigationContext.Parameters.GetValue<ProcedureModel>("SelectedProcedure");
  357. }
  358. }
  359. /// <summary>
  360. /// 是否允许导航到此视图模型。
  361. /// </summary>
  362. /// <param name="navigationContext"></param>
  363. /// <returns></returns>
  364. public bool IsNavigationTarget(NavigationContext navigationContext)
  365. {
  366. return true;
  367. }
  368. /// <summary>
  369. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  370. /// </summary>
  371. /// <param name="navigationContext"></param>
  372. public void OnNavigatedFrom(NavigationContext navigationContext)
  373. {
  374. }
  375. #endregion
  376. private bool _IsAllowEdit = false;
  377. public bool IsAllowEdit
  378. {
  379. get { return _IsAllowEdit; }
  380. set { SetProperty(ref _IsAllowEdit, value); }
  381. }
  382. private void LoginChange(Models.User user)
  383. {
  384. if (user.userPart == Enums.UserPart.Operator)
  385. {
  386. IsAllowEdit = false;
  387. }
  388. else
  389. {
  390. IsAllowEdit = true;
  391. }
  392. }
  393. }
  394. }