PalletManageViewModel.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. using MaterialDesignThemes.Wpf;
  2. using Prism.Commands;
  3. using Prism.Events;
  4. using Prism.Ioc;
  5. using Prism.Mvvm;
  6. using Prism.Regions;
  7. using Prism.Services.Dialogs;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using System.Web.Hosting;
  13. using TeamAAS_VP.Controls;
  14. using TeamAAS_VP.Enums;
  15. using TeamAAS_VP.Events;
  16. using TeamAAS_VP.Core;
  17. using TeamAAS_VP.Interfaces;
  18. using TeamAAS_VP.Models;
  19. using TeamAAS_VP.Views.Product;
  20. using TeamAAS_VP;
  21. using Microsoft.Win32;
  22. using System.Windows;
  23. using TeamAAS_VP.Resources.Languages;
  24. namespace TeamAAS_VP.ViewModels.Product
  25. {
  26. public class PalletManageViewModel : BindableBase, IDialogAware
  27. {
  28. IRegionManager _regionManager;
  29. IEventAggregator _eventAggregator;
  30. IContainerProvider _container;
  31. IDialogService _dialogService;
  32. ProductsViewModel productsViewModel;
  33. #region 属性
  34. public string Title => "Pallet";
  35. private ObservableCollection<ProductModel> _Products;
  36. /// <summary>
  37. /// 产品列表
  38. /// </summary>
  39. public ObservableCollection<ProductModel> Products
  40. {
  41. get { return _Products; }
  42. set { SetProperty(ref _Products, value); }
  43. }
  44. private ProductModel _SelectProduct;
  45. /// <summary>
  46. /// 选中的产品
  47. /// </summary>
  48. public ProductModel SelectProduct
  49. {
  50. get { return _SelectProduct; }
  51. set { SetProperty(ref _SelectProduct, value); }
  52. }
  53. private ObservableCollection<Pallet> _Pallets;
  54. /// <summary>
  55. /// 托盘集合
  56. /// </summary>
  57. public ObservableCollection<Pallet> Pallets
  58. {
  59. get { return _Pallets; }
  60. set { SetProperty(ref _Pallets, value); }
  61. }
  62. private Pallet _SelectPallet;
  63. /// <summary>
  64. /// 选中的托盘
  65. /// </summary>
  66. public Pallet SelectPallet
  67. {
  68. get { return _SelectPallet; }
  69. set
  70. {
  71. SetProperty(ref _SelectPallet, value);
  72. if (value == null)
  73. return;
  74. Points = new ObservableCollection<RPoint>();
  75. Points.Add(value.P0);
  76. Points.Add(value.P1);
  77. Points.Add(value.P2);
  78. Row = value.Row;
  79. Column = value.Column;
  80. Arrangement = value.Arrangement;
  81. }
  82. }
  83. private RPoint _SelectPoint;
  84. public RPoint SelectPoint
  85. {
  86. get { return _SelectPoint; }
  87. set { SetProperty(ref _SelectPoint, value); }
  88. }
  89. private MotionCommand _SelectMotionCommand;
  90. /// <summary>
  91. /// 选中的运动指令
  92. /// </summary>
  93. public MotionCommand SelectMotionCommand
  94. {
  95. get { return _SelectMotionCommand; }
  96. set { SetProperty(ref _SelectMotionCommand, value); }
  97. }
  98. private int _Row;
  99. public int Row
  100. {
  101. get { return _Row; }
  102. set
  103. {
  104. SetProperty(ref _Row, value);
  105. if (SelectPallet != null)
  106. {
  107. SelectPallet.Row = value;
  108. _eventAggregator.GetEvent<PalletLayoutNotification>().Publish(SelectPallet);
  109. }
  110. }
  111. }
  112. private int _Column;
  113. public int Column
  114. {
  115. get { return _Column; }
  116. set
  117. {
  118. SetProperty(ref _Column, value);
  119. if (SelectPallet != null)
  120. {
  121. SelectPallet.Column = value;
  122. _eventAggregator.GetEvent<PalletLayoutNotification>().Publish(SelectPallet);
  123. }
  124. }
  125. }
  126. private PalletArrangement _Arrangement;
  127. public PalletArrangement Arrangement
  128. {
  129. get
  130. {
  131. return _Arrangement;
  132. }
  133. set
  134. {
  135. SetProperty(ref _Arrangement, value);
  136. if (SelectPallet != null)
  137. {
  138. SelectPallet.Arrangement = value;
  139. _eventAggregator.GetEvent<PalletLayoutNotification>().Publish(SelectPallet);
  140. }
  141. }
  142. }
  143. private ObservableCollection<RPoint> _Points;
  144. public ObservableCollection<RPoint> Points
  145. {
  146. get { return _Points; }
  147. set { SetProperty(ref _Points, value); }
  148. }
  149. public Management management { get; set; }
  150. public RobotService robotService { get; set; }
  151. private IRobot _Robot;
  152. public IRobot Robot
  153. {
  154. get { return _Robot; }
  155. set
  156. {
  157. SetProperty(ref _Robot, value);
  158. try
  159. {
  160. var config = FileHelper.ReadApplicationConfiguration();
  161. if (config.RobotJogSetpDistance.TryGetValue(value.Id, out double distance))
  162. {
  163. this.Distance = distance;
  164. }
  165. }
  166. catch (Exception)
  167. {
  168. }
  169. }
  170. }
  171. private double _Distance = 1;
  172. public double Distance
  173. {
  174. get { return _Distance; }
  175. set
  176. {
  177. SetProperty(ref _Distance, value);
  178. try
  179. {
  180. if (Robot == null)
  181. {
  182. return;
  183. }
  184. var config = FileHelper.ReadApplicationConfiguration();
  185. if (config.RobotJogSetpDistance.ContainsKey(Robot.Id))
  186. {
  187. config.RobotJogSetpDistance[Robot.Id] = value;
  188. }
  189. else
  190. {
  191. config.RobotJogSetpDistance.Add(Robot.Id, value);
  192. }
  193. FileHelper.SaveApplicationConfiguration(config);
  194. }
  195. catch (Exception)
  196. {
  197. }
  198. }
  199. }
  200. private ObservableCollection<RobotInfo> _Robots = new ObservableCollection<RobotInfo>();
  201. /// <summary>
  202. /// 机器人参数
  203. /// </summary>
  204. public ObservableCollection<RobotInfo> Robots
  205. {
  206. get { return _Robots; }
  207. set { SetProperty(ref _Robots, value); }
  208. }
  209. private RobotInfo _RobotInfo = new RobotInfo();
  210. /// <summary>
  211. /// 机器人
  212. /// </summary>
  213. public RobotInfo SelectRobotInfo
  214. {
  215. get { return _RobotInfo; }
  216. set
  217. {
  218. SetProperty(ref _RobotInfo, value);
  219. if (value != null)
  220. {
  221. Robot = robotService.GetRobot(value.Id);
  222. if (value.RobotBrand == RobotBrand.FANUC)
  223. {
  224. Tools = new ObservableCollection<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  225. SelectTool = Robot.SelectedTool;
  226. }
  227. else
  228. {
  229. Tools = new ObservableCollection<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  230. SelectTool = Robot.SelectedTool;
  231. }
  232. }
  233. }
  234. }
  235. private bool CanRemove
  236. {
  237. get
  238. {
  239. if (SelectPallet != null)
  240. return true;
  241. else return false;
  242. }
  243. }
  244. private ObservableCollection<int> _Tools = new ObservableCollection<int>();
  245. /// <summary>
  246. /// 机器人Tool集合
  247. /// </summary>
  248. public ObservableCollection<int> Tools
  249. {
  250. get { return _Tools; }
  251. set { SetProperty(ref _Tools, value); }
  252. }
  253. private int _SelectTool;
  254. /// <summary>
  255. /// 选中的Tool
  256. /// </summary>
  257. public int SelectTool
  258. {
  259. get { return _SelectTool; }
  260. set
  261. {
  262. SetProperty(ref _SelectTool, value);
  263. if (value >= 0)
  264. {
  265. if (Robot != null && Robot.IsConnected)
  266. {
  267. try
  268. {
  269. Robot.SelectToolAsync(value);
  270. }
  271. catch (Exception)
  272. {
  273. }
  274. }
  275. }
  276. }
  277. }
  278. #endregion
  279. #region 命令
  280. private DelegateCommand _CreateCommand;
  281. public DelegateCommand CreateCommand =>
  282. _CreateCommand ?? (_CreateCommand = new DelegateCommand(ExecuteCreateCommand));
  283. private DelegateCommand _RemoveCommand;
  284. public DelegateCommand RemoveCommand =>
  285. _RemoveCommand ?? (_RemoveCommand = new DelegateCommand(ExecuteRemoveCommand).ObservesCanExecute(() => CanRemove).ObservesProperty(() => SelectPallet));
  286. private DelegateCommand _ProductSelectionChangedCommand;
  287. public DelegateCommand ProductSelectionChangedCommand =>
  288. _ProductSelectionChangedCommand ?? (_ProductSelectionChangedCommand = new DelegateCommand(ExecuteProductSelectionChangedCommand));
  289. private DelegateCommand _ConfirmCommand;
  290. public DelegateCommand ConfirmCommand =>
  291. _ConfirmCommand ?? (_ConfirmCommand = new DelegateCommand(ExecuteConfirmCommand));
  292. private DelegateCommand _CancelCommand;
  293. public DelegateCommand CancelCommand =>
  294. _CancelCommand ?? (_CancelCommand = new DelegateCommand(ExecuteCancelCommand));
  295. private DelegateCommand _CopyCommand;
  296. public DelegateCommand CopyCommand =>
  297. _CopyCommand ?? (_CopyCommand = new DelegateCommand(ExecuteCopyCommand));
  298. private DelegateCommand _EditNameCommand;
  299. public DelegateCommand EditNameCommand =>
  300. _EditNameCommand ?? (_EditNameCommand = new DelegateCommand(ExecuteEditNameCommand));
  301. private DelegateCommand<object> _JogCommand;
  302. public DelegateCommand<object> JogCommand =>
  303. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  304. private DelegateCommand<object> _ExecuteMotion;
  305. public DelegateCommand<object> ExecuteMotion =>
  306. _ExecuteMotion ?? (_ExecuteMotion = new DelegateCommand<object>(ExecuteExecuteMotion).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  307. private DelegateCommand<RPoint> _TechPointCommand;
  308. public DelegateCommand<RPoint> TechPointCommand =>
  309. _TechPointCommand ?? (_TechPointCommand = new DelegateCommand<RPoint>(ExecuteTechPointCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  310. private DelegateCommand _CalculatePalletCommand;
  311. public DelegateCommand CalculatePalletCommand =>
  312. _CalculatePalletCommand ?? (_CalculatePalletCommand = new DelegateCommand(ExecuteCalculatePalletCommand));
  313. private DelegateCommand<string> _MotorCommand;
  314. public DelegateCommand<string> MotorCommand =>
  315. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  316. private DelegateCommand _ResetCommand;
  317. public DelegateCommand ResetCommand =>
  318. _ResetCommand ?? (_ResetCommand = new DelegateCommand(ExecuteResetCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  319. private DelegateCommand _sFreeCommand;
  320. public DelegateCommand sFreeCommand =>
  321. _sFreeCommand ?? (_sFreeCommand = new DelegateCommand(ExecutesFreeCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  322. private DelegateCommand _SLockCommand;
  323. public DelegateCommand SLockCommand =>
  324. _SLockCommand ?? (_SLockCommand = new DelegateCommand(ExecuteSLockCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  325. private DelegateCommand _SavePalletImage;
  326. public DelegateCommand SavePalletImage =>
  327. _SavePalletImage ?? (_SavePalletImage = new DelegateCommand(ExecuteSavePalletImage));
  328. #endregion
  329. #region 事件
  330. public event Action<IDialogResult> RequestClose;
  331. #endregion
  332. public PalletManageViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService)
  333. {
  334. _regionManager = regionManager;
  335. _eventAggregator = ea;
  336. _container = container;
  337. _dialogService = dialogService;
  338. //订阅权限登录事件
  339. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  340. productsViewModel = _container.Resolve<ProductsViewModel>();
  341. Products = productsViewModel.Products;
  342. SelectProduct = productsViewModel.SelectProduct;
  343. }
  344. #region 方法
  345. public bool CanCloseDialog()
  346. {
  347. return true;
  348. }
  349. public void OnDialogClosed()
  350. {
  351. }
  352. public void OnDialogOpened(IDialogParameters parameters)
  353. {
  354. if (SelectProduct.RobotPoint == null)
  355. SelectProduct.RobotPoint = new RobotPoint();
  356. if (SelectProduct.RobotPoint.Pallets == null)
  357. SelectProduct.RobotPoint.Pallets = new ObservableCollection<Pallet>();
  358. Pallets = SelectProduct.RobotPoint.Clone().Pallets;
  359. management = _container.Resolve<Management>();
  360. robotService = management.RobotService;
  361. Robots = management.DeviceConfig.Robots;
  362. if (SelectRobotInfo == null && Robots != null && Robots.Count > 0)
  363. {
  364. SelectRobotInfo = Robots[0];
  365. }
  366. }
  367. /// <summary>
  368. /// 创建托盘
  369. /// </summary>
  370. async void ExecuteCreateCommand()
  371. {
  372. //var view = new AddProduct() { Title = "创建托盘:", Item = "Name" };
  373. var view = new AddPalletView();
  374. //show the dialog
  375. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  376. if (result != null && result is AddPalletView vm && !string.IsNullOrEmpty(vm.PalletName))
  377. {
  378. Pallet pallet = new Pallet()
  379. {
  380. Id = Guid.NewGuid(),
  381. Name = vm.PalletName,
  382. Row = 2,
  383. Column = 2,
  384. P0 = new RPoint() { Number = 0 },
  385. P1 = new RPoint() { Number = 1 },
  386. P2 = new RPoint() { Number = 2 },
  387. Arrangement = PalletArrangement.Z,
  388. };
  389. if (!vm.IsCheckModel1)
  390. {
  391. pallet.Extend = new PalletExtend()
  392. {
  393. Pallet_Row = 2,
  394. Pallet_Column = 2,
  395. Pallet_X_Interval = 0,
  396. Pallet_Y_Interval = 0,
  397. CoordinateModel = vm.CoordinateModel
  398. };
  399. }
  400. Pallets.Add(pallet);
  401. SelectPallet = Pallets[Pallets.Count - 1];
  402. }
  403. //if (result != null && !string.IsNullOrEmpty(result.ToString()))
  404. //{
  405. // Pallets.Add(new Pallet()
  406. // {
  407. // Id = Guid.NewGuid(),
  408. // Name = result.ToString(),
  409. // Row=2,
  410. // Column=2,
  411. // P0 = new RPoint() { Number = 0 },
  412. // P1 = new RPoint() { Number = 1 },
  413. // P2 = new RPoint() { Number = 2 },
  414. // Arrangement=PalletArrangement.Z,
  415. // });
  416. // SelectPallet = Pallets[Pallets.Count - 1];
  417. //}
  418. }
  419. /// <summary>
  420. /// 移除托盘
  421. /// </summary>
  422. void ExecuteRemoveCommand()
  423. {
  424. if (SelectPallet != null)
  425. {
  426. Pallets.Remove(SelectPallet);
  427. }
  428. }
  429. /// <summary>
  430. /// 点击选择托盘时触发方法
  431. /// </summary>
  432. void ExecuteProductSelectionChangedCommand()
  433. {
  434. }
  435. /// <summary>
  436. /// 确定
  437. /// </summary>
  438. void ExecuteConfirmCommand()
  439. {
  440. IDialogParameters parameters = new DialogParameters();
  441. parameters.Add("Pallets", Pallets);
  442. RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parameters));
  443. }
  444. /// <summary>
  445. /// 取消
  446. /// </summary>
  447. void ExecuteCancelCommand()
  448. {
  449. IDialogParameters parameters = new DialogParameters();
  450. parameters.Add("Pallets", Pallets);
  451. RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel, parameters));
  452. }
  453. /// <summary>
  454. /// 保存托盘图片
  455. /// </summary>
  456. void ExecuteSavePalletImage()
  457. {
  458. try
  459. {
  460. if (SelectPallet == null)
  461. {
  462. return;
  463. }
  464. SaveFileDialog dlg = new SaveFileDialog();
  465. dlg.Title = "Save Pallet Layout Image";
  466. dlg.DefaultExt = ".png";
  467. dlg.Filter = "Image (*.png)|*.png";
  468. if (dlg.ShowDialog() ?? false)
  469. {
  470. PalletTool palletTool = new PalletTool(SelectPallet);
  471. palletTool.DrawCoordinatesAndSaveImage(dlg.FileName);
  472. MessageBox.Show("OK");
  473. }
  474. }
  475. catch (Exception ex)
  476. {
  477. MessageBox.Show(ex.ToString());
  478. }
  479. }
  480. /// <summary>
  481. /// 复制
  482. /// </summary>
  483. void ExecuteCopyCommand()
  484. {
  485. }
  486. /// <summary>
  487. /// 修改托盘名称
  488. /// </summary>
  489. void ExecuteEditNameCommand()
  490. {
  491. }
  492. /// <summary>
  493. /// 点动
  494. /// </summary>
  495. /// <param name="obj"></param>
  496. private async void ExecuteJogCommand(object obj)
  497. {
  498. if (Robot == null || !Robot.IsConnected) return;
  499. try
  500. {
  501. Robot.Timeout = 6000000;
  502. var items = ((string)obj).Split(':');
  503. switch (items[0])
  504. {
  505. case "X+":
  506. await Robot.JogAsync("X", double.Parse(items[1]));
  507. break;
  508. case "X-":
  509. await Robot.JogAsync("X", -double.Parse(items[1]));
  510. break;
  511. case "Y+":
  512. await Robot.JogAsync("Y", double.Parse(items[1]));
  513. break;
  514. case "Y-":
  515. await Robot.JogAsync("Y", -double.Parse(items[1]));
  516. break;
  517. case "Z+":
  518. await Robot.JogAsync("Z", double.Parse(items[1]));
  519. break;
  520. case "Z-":
  521. await Robot.JogAsync("Z", -double.Parse(items[1]));
  522. break;
  523. case "U+":
  524. await Robot.JogAsync("U", double.Parse(items[1]));
  525. break;
  526. case "U-":
  527. await Robot.JogAsync("U", -double.Parse(items[1]));
  528. break;
  529. case "V+":
  530. await Robot.JogAsync("V", double.Parse(items[1]));
  531. break;
  532. case "V-":
  533. await Robot.JogAsync("V", -double.Parse(items[1]));
  534. break;
  535. case "W+":
  536. await Robot.JogAsync("W", double.Parse(items[1]));
  537. break;
  538. case "W-":
  539. await Robot.JogAsync("W", -double.Parse(items[1]));
  540. break;
  541. default:
  542. break;
  543. }
  544. Robot.Timeout = 5000;
  545. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  546. }
  547. catch (Exception ex)
  548. {
  549. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  550. LogHelper.WriteLogError("机器人点动时出错", ex);
  551. }
  552. }
  553. /// <summary>
  554. /// 执行运动
  555. /// </summary>
  556. /// <param name="obj"></param>
  557. async void ExecuteExecuteMotion(object parameter)
  558. {
  559. var values = (object[])parameter;
  560. var point = values[0] as RPoint;
  561. MotionCommand motionCmd = (MotionCommand)values[1];
  562. if (Robot == null || !Robot.IsConnected) return;
  563. try
  564. {
  565. Robot.Timeout = 6000000;
  566. switch (motionCmd)
  567. {
  568. case MotionCommand.Go:
  569. await Robot.GoAsync(point);
  570. break;
  571. case MotionCommand.Jump:
  572. await Robot.JumpAsync(point, 0);
  573. break;
  574. case MotionCommand.Move:
  575. await Robot.MoveAsync(point);
  576. break;
  577. }
  578. Robot.Timeout = 5000;
  579. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 1 });
  580. }
  581. catch (Exception ex)
  582. {
  583. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  584. LogHelper.WriteLogError("机器人运动至目标点时出错", ex);
  585. }
  586. }
  587. /// <summary>
  588. /// 示教点位
  589. /// </summary>
  590. /// <param name="point"></param>
  591. async void ExecuteTechPointCommand(RPoint point)
  592. {
  593. if (Robot == null || !Robot.IsConnected) return;
  594. try
  595. {
  596. var curpos = await Robot.GetRobotPosAsync();
  597. RPoint p0 = Points.FirstOrDefault(x => x.Number == point.Number);
  598. p0.X = curpos.X;
  599. p0.Y = curpos.Y;
  600. p0.Z = curpos.Z;
  601. p0.U = curpos.U;
  602. p0.V = curpos.V;
  603. p0.W = curpos.W;
  604. p0.Hand = curpos.Hand;
  605. p0.Local = curpos.Local;
  606. p0.Tool = curpos.Tool;
  607. p0 = curpos;
  608. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 1 });
  609. }
  610. catch (Exception ex)
  611. {
  612. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  613. LogHelper.WriteLogError("示教机器人点时出错", ex);
  614. }
  615. }
  616. /// <summary>
  617. /// 计算托盘
  618. /// </summary>
  619. async void ExecuteCalculatePalletCommand()
  620. {
  621. try
  622. {
  623. var view = new CalculatePalletPos();
  624. ((CalculatePalletPosViewModel)view.DataContext).CurrentPallet = ((Pallet)SelectPallet).Clone();
  625. //show the dialog
  626. var result = await DialogHost.Show(view, "PalletCalculateDialog", null, null, null);
  627. if (result != null && result is bool)
  628. {
  629. if (((bool)result))
  630. {
  631. SelectPallet = ((CalculatePalletPosViewModel)view.DataContext).CurrentPallet.Clone();
  632. }
  633. }
  634. }
  635. catch (Exception ex)
  636. {
  637. LogHelper.WriteLogError("计算托盘时出错", ex);
  638. }
  639. }
  640. /// <summary>
  641. /// 电机
  642. /// </summary>
  643. /// <param name="obj"></param>
  644. private async void ExecuteMotorCommand(string obj)
  645. {
  646. if (Robot == null || !Robot.IsConnected) return;
  647. try
  648. {
  649. await Robot.MotorAsync(Convert.ToBoolean(obj));
  650. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 1 });
  651. }
  652. catch (Exception ex)
  653. {
  654. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  655. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  656. }
  657. }
  658. /// <summary>
  659. /// 重置
  660. /// </summary>
  661. private async void ExecuteResetCommand()
  662. {
  663. if (Robot == null || !Robot.IsConnected) return;
  664. try
  665. {
  666. await Robot.ResetAsync();
  667. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 1 });
  668. }
  669. catch (Exception ex)
  670. {
  671. LogHelper.WriteLogError("重置机器人时出错!", ex);
  672. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  673. }
  674. }
  675. /// <summary>
  676. /// 锁定刹车
  677. /// </summary>
  678. private async void ExecuteSLockCommand()
  679. {
  680. if (Robot == null || !Robot.IsConnected) return;
  681. try
  682. {
  683. await Robot.SLockAsync();
  684. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 1 });
  685. }
  686. catch (Exception ex)
  687. {
  688. LogHelper.WriteLogError("机器人锁定刹车时出错!", ex);
  689. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  690. }
  691. }
  692. /// <summary>
  693. /// 释放刹车
  694. /// </summary>
  695. private async void ExecutesFreeCommand()
  696. {
  697. if (Robot == null || !Robot.IsConnected) return;
  698. try
  699. {
  700. await Robot.SFreeAsync();
  701. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 1 });
  702. }
  703. catch (Exception ex)
  704. {
  705. LogHelper.WriteLogError("机器人释放刹车时出错!", ex);
  706. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  707. }
  708. }
  709. private bool CanExecute
  710. {
  711. get
  712. {
  713. if (Robot != null)
  714. {
  715. return Robot.CanExecute;
  716. }
  717. else
  718. {
  719. return false;
  720. }
  721. }
  722. }
  723. #endregion
  724. private bool _IsAllowEdit = false;
  725. public bool IsAllowEdit
  726. {
  727. get { return _IsAllowEdit; }
  728. set { SetProperty(ref _IsAllowEdit, value); }
  729. }
  730. private void LoginChange(Models.User user)
  731. {
  732. if (user.userPart == Enums.UserPart.Operator)
  733. {
  734. IsAllowEdit = false;
  735. }
  736. else
  737. {
  738. IsAllowEdit = true;
  739. }
  740. }
  741. }
  742. }