ProcessBasicsViewModel.cs 17 KB

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