PlcPointParamsViewModel.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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.Windows;
  13. using TeamAAS_VP.Core;
  14. using TeamAAS_VP.Core.PLCs;
  15. using TeamAAS_VP.DxfModule;
  16. using TeamAAS_VP.Enums;
  17. using TeamAAS_VP.Events;
  18. using TeamAAS_VP.Interfaces;
  19. using TeamAAS_VP.Models;
  20. using TeamAAS_VP.Models.PLC;
  21. using TeamAAS_VP.Models.Robot;
  22. using TeamAAS_VP.Resources.Languages;
  23. using TeamAAS_VP.Views.Product;
  24. namespace TeamAAS_VP.ViewModels.Product
  25. {
  26. public class PlcPointParamsViewModel : BindableBase, IConfirmNavigationRequest
  27. {
  28. IRegionManager _regionManager;
  29. IEventAggregator _eventAggregator;
  30. IContainerProvider _container;
  31. IDialogService _dialogService;
  32. IConfigService _configService;
  33. IRobotService _robotService;
  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 PlcPoint _SelectedPoint;
  46. public PlcPoint SelectedPoint
  47. {
  48. get { return _SelectedPoint; }
  49. set { SetProperty(ref _SelectedPoint, value); }
  50. }
  51. private PlcPoint _selectedPointInDataGrid;
  52. public PlcPoint SelectedPointInDataGrid
  53. {
  54. get { return _selectedPointInDataGrid; }
  55. set
  56. {
  57. if (SetProperty(ref _selectedPointInDataGrid, value))
  58. {
  59. SelectedPointInComboBox = value;
  60. // update command availability
  61. _MovePointUpCommand?.RaiseCanExecuteChanged();
  62. _MovePointDownCommand?.RaiseCanExecuteChanged();
  63. // 更新依赖可执行状态的命令
  64. _TechPointCommand?.RaiseCanExecuteChanged();
  65. _ExecuteMotion?.RaiseCanExecuteChanged();
  66. }
  67. // 可以同步到 SelectPoint 属性
  68. SelectedPoint = value;
  69. }
  70. }
  71. private PlcPoint _selectedPointInComboBox;
  72. public PlcPoint SelectedPointInComboBox
  73. {
  74. get { return _selectedPointInComboBox; }
  75. set
  76. {
  77. if (SetProperty(ref _selectedPointInComboBox, value))
  78. {
  79. SelectedPointInDataGrid = value;
  80. }
  81. // 可以同步到 SelectPoint 属性
  82. SelectedPoint = value;
  83. }
  84. }
  85. private ObservableCollection<PlcPoint> _Points = new ObservableCollection<PlcPoint>();
  86. /// <summary>
  87. /// 点位集合
  88. /// </summary>
  89. public ObservableCollection<PlcPoint> Points
  90. {
  91. get { return _Points; }
  92. set { SetProperty(ref _Points, value); }
  93. }
  94. private MotionCommand _SelectMotionCommand;
  95. /// <summary>
  96. /// 选中的运动指令
  97. /// </summary>
  98. public MotionCommand SelectMotionCommand
  99. {
  100. get { return _SelectMotionCommand; }
  101. set { SetProperty(ref _SelectMotionCommand, value); }
  102. }
  103. public Management management { get; set; }
  104. private IRobot _Robot;
  105. public IRobot Robot
  106. {
  107. get { return _Robot; }
  108. set
  109. {
  110. SetProperty(ref _Robot, value);
  111. }
  112. }
  113. private int _SelectedIndex;
  114. public int SelectedIndex
  115. {
  116. get { return _SelectedIndex; }
  117. set { SetProperty(ref _SelectedIndex, value); }
  118. }
  119. private double _Distance = 1;
  120. public double Distance
  121. {
  122. get { return _Distance; }
  123. set
  124. {
  125. SetProperty(ref _Distance, value);
  126. try
  127. {
  128. if (Robot == null)
  129. {
  130. return;
  131. }
  132. _configService.UpdateRobotStepDistance(Robot.Id, value);
  133. }
  134. catch (Exception)
  135. {
  136. }
  137. }
  138. }
  139. #endregion
  140. #region 命令
  141. private DelegateCommand _NextCommand;
  142. public DelegateCommand NextCommand =>
  143. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  144. private DelegateCommand _BackCommand;
  145. public DelegateCommand BackCommand =>
  146. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  147. private DelegateCommand _LoadedCommand;
  148. public DelegateCommand LoadedCommand =>
  149. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  150. private DelegateCommand<object> _ExecuteMotion;
  151. public DelegateCommand<object> ExecuteMotion =>
  152. _ExecuteMotion ?? (_ExecuteMotion = new DelegateCommand<object>(ExecuteExecuteMotion).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute).ObservesProperty(() => SelectedPointInDataGrid));
  153. private DelegateCommand<object> _JogCommand;
  154. public DelegateCommand<object> JogCommand =>
  155. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  156. private DelegateCommand _TechPointCommand;
  157. public DelegateCommand TechPointCommand =>
  158. _TechPointCommand ?? (_TechPointCommand = new DelegateCommand(ExecuteTechPointCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute).ObservesProperty(() => SelectedPointInDataGrid));
  159. private DelegateCommand<string> _MotorCommand;
  160. public DelegateCommand<string> MotorCommand =>
  161. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  162. private DelegateCommand _ResetCommand;
  163. public DelegateCommand ResetCommand =>
  164. _ResetCommand ?? (_ResetCommand = new DelegateCommand(ExecuteResetCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  165. private DelegateCommand _sFreeCommand;
  166. public DelegateCommand sFreeCommand =>
  167. _sFreeCommand ?? (_sFreeCommand = new DelegateCommand(ExecutesFreeCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  168. private DelegateCommand _SLockCommand;
  169. public DelegateCommand SLockCommand =>
  170. _SLockCommand ?? (_SLockCommand = new DelegateCommand(ExecuteSLockCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  171. private DelegateCommand _SelectionChangedCommand;
  172. public DelegateCommand SelectionChangedCommand =>
  173. _SelectionChangedCommand ?? (_SelectionChangedCommand = new DelegateCommand(ExecuteSelectionChangedCommand));
  174. private DelegateCommand _AddPointCommand;
  175. public DelegateCommand AddPointCommand =>
  176. _AddPointCommand ?? (_AddPointCommand = new DelegateCommand(ExecuteAddPointCommand));
  177. private DelegateCommand _DeletePointCommand;
  178. public DelegateCommand DeletePointCommand =>
  179. _DeletePointCommand ?? (_DeletePointCommand = new DelegateCommand(ExecuteDeletePointCommand));
  180. private DelegateCommand _MovePointUpCommand;
  181. public DelegateCommand MovePointUpCommand => _MovePointUpCommand ?? (_MovePointUpCommand = new DelegateCommand(ExecuteMovePointUpCommand, CanMovePointUp));
  182. private DelegateCommand _MovePointDownCommand;
  183. public DelegateCommand MovePointDownCommand => _MovePointDownCommand ?? (_MovePointDownCommand = new DelegateCommand(ExecuteMovePointDownCommand, CanMovePointDown));
  184. private DelegateCommand _ImportCadPointCommand;
  185. public DelegateCommand ImportCadPointCommand =>
  186. _ImportCadPointCommand ?? (_ImportCadPointCommand = new DelegateCommand(ExecuteImportCadPointCommand));
  187. #endregion
  188. #region 事件
  189. #endregion
  190. public PlcPointParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
  191. IConfigService configService, IRobotService robotService, ISystemDatabaseService systemDatabaseService)
  192. {
  193. _regionManager = regionManager;
  194. _eventAggregator = ea;
  195. _container = container;
  196. _dialogService = dialogService;
  197. _configService = configService;
  198. _systemDatabaseService = systemDatabaseService;
  199. //订阅权限登录事件
  200. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  201. management = _container.Resolve<Management>();
  202. _robotService = robotService;
  203. }
  204. #region 方法
  205. /// <summary>
  206. /// 下一步
  207. /// </summary>
  208. void ExecuteNextCommand()
  209. {
  210. NavigationParameters param = new NavigationParameters();
  211. param.Add("SelectProduct", SelectProduct);
  212. _regionManager.RequestNavigate("ProductRegionContext", "ProductParams", param);
  213. }
  214. /// <summary>
  215. /// 上一步
  216. /// </summary>
  217. void ExecuteBackCommand()
  218. {
  219. NavigationParameters param = new NavigationParameters();
  220. param.Add("SelectProduct", SelectProduct);
  221. param.Add("SelectedProcedure", null);
  222. _regionManager.RequestNavigate("ProductRegionContext", "ProcessBasics", param);
  223. }
  224. /// <summary>
  225. /// 点动
  226. /// </summary>
  227. /// <param name="obj"></param>
  228. private async void ExecuteJogCommand(object obj)
  229. {
  230. if (Robot == null || !Robot.IsConnected) return;
  231. try
  232. {
  233. Robot.Timeout = 6000000;
  234. var items = ((string)obj).Split(':');
  235. switch (items[0])
  236. {
  237. case "X+":
  238. await Robot.JogAsync("X", double.Parse(items[1]));
  239. break;
  240. case "X-":
  241. await Robot.JogAsync("X", -double.Parse(items[1]));
  242. break;
  243. case "Y+":
  244. await Robot.JogAsync("Y", double.Parse(items[1]));
  245. break;
  246. case "Y-":
  247. await Robot.JogAsync("Y", -double.Parse(items[1]));
  248. break;
  249. case "Z+":
  250. await Robot.JogAsync("Z", double.Parse(items[1]));
  251. break;
  252. case "Z-":
  253. await Robot.JogAsync("Z", -double.Parse(items[1]));
  254. break;
  255. case "U+":
  256. await Robot.JogAsync("U", double.Parse(items[1]));
  257. break;
  258. case "U-":
  259. await Robot.JogAsync("U", -double.Parse(items[1]));
  260. break;
  261. case "V+":
  262. await Robot.JogAsync("V", double.Parse(items[1]));
  263. break;
  264. case "V-":
  265. await Robot.JogAsync("V", -double.Parse(items[1]));
  266. break;
  267. case "W+":
  268. await Robot.JogAsync("W", double.Parse(items[1]));
  269. break;
  270. case "W-":
  271. await Robot.JogAsync("W", -double.Parse(items[1]));
  272. break;
  273. default:
  274. break;
  275. }
  276. Robot.Timeout = 5000;
  277. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  278. }
  279. catch (Exception ex)
  280. {
  281. LogHelper.WriteLogError("机器人点位-机器人点动时出错", ex);
  282. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  283. }
  284. }
  285. /// <summary>
  286. /// 执行运动
  287. /// </summary>
  288. /// <param name="obj"></param>
  289. async void ExecuteExecuteMotion(object parameter)
  290. {
  291. if (parameter == null)
  292. {
  293. return;
  294. }
  295. var values = (object[])parameter;
  296. var point = values[0] as RPoint;
  297. MotionCommand motionCmd = (MotionCommand)values[1];
  298. if (Robot == null || !Robot.IsConnected) return;
  299. try
  300. {
  301. Robot.Timeout = 6000000;
  302. switch (motionCmd)
  303. {
  304. case MotionCommand.Go:
  305. await Robot.GoAsync(point);
  306. break;
  307. case MotionCommand.Jump:
  308. await Robot.JumpAsync(point, 0);
  309. break;
  310. case MotionCommand.Move:
  311. await Robot.MoveAsync(point);
  312. break;
  313. }
  314. Robot.Timeout = 5000;
  315. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  316. }
  317. catch (Exception ex)
  318. {
  319. LogHelper.WriteLogError("机器人点位-执行点位时出错", ex);
  320. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  321. }
  322. }
  323. /// <summary>
  324. /// 示教点位
  325. /// </summary>
  326. /// <param name="point"></param>
  327. async void ExecuteTechPointCommand()
  328. {
  329. if (Robot == null || !Robot.IsConnected) return;
  330. if (SelectedPoint == null) return;
  331. try
  332. {
  333. var curpos = await Robot.GetRobotPosAsync();
  334. SelectedPoint.X_Position = curpos.X;
  335. SelectedPoint.Y_Position = curpos.Y;
  336. SelectedPoint.Z_Position_Start = curpos.Z;
  337. SelectedPoint.U_Position = curpos.U;
  338. SelectedPoint.R_Position = curpos.V;
  339. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  340. }
  341. catch (Exception ex)
  342. {
  343. LogHelper.WriteLogError("机器人点位-示教点位时出错", ex);
  344. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  345. }
  346. }
  347. void ExecuteLoadedCommand()
  348. {
  349. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  350. {
  351. IsAllowEdit = false;
  352. }
  353. else
  354. {
  355. IsAllowEdit = true;
  356. }
  357. }
  358. /// <summary>
  359. /// 电机
  360. /// </summary>
  361. /// <param name="obj"></param>
  362. private async void ExecuteMotorCommand(string obj)
  363. {
  364. if (Robot == null || !Robot.IsConnected) return;
  365. try
  366. {
  367. await Robot.MotorAsync(Convert.ToBoolean(obj));
  368. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  369. }
  370. catch (Exception ex)
  371. {
  372. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  373. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  374. }
  375. }
  376. /// <summary>
  377. /// 重置
  378. /// </summary>
  379. private async void ExecuteResetCommand()
  380. {
  381. if (Robot == null || !Robot.IsConnected) return;
  382. try
  383. {
  384. await Robot.ResetAsync();
  385. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  386. }
  387. catch (Exception ex)
  388. {
  389. LogHelper.WriteLogError("重置机器人时出错!", ex);
  390. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  391. }
  392. }
  393. /// <summary>
  394. /// 锁定刹车
  395. /// </summary>
  396. private async void ExecuteSLockCommand()
  397. {
  398. if (Robot == null || !Robot.IsConnected) return;
  399. try
  400. {
  401. await Robot.SLockAsync();
  402. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  403. }
  404. catch (Exception ex)
  405. {
  406. LogHelper.WriteLogError("机器人锁定刹车时出错!", ex);
  407. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  408. }
  409. }
  410. /// <summary>
  411. /// 释放刹车
  412. /// </summary>
  413. private async void ExecutesFreeCommand()
  414. {
  415. if (Robot == null || !Robot.IsConnected) return;
  416. try
  417. {
  418. await Robot.SFreeAsync();
  419. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  420. }
  421. catch (Exception ex)
  422. {
  423. LogHelper.WriteLogError("机器人释放刹车时出错!", ex);
  424. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  425. }
  426. }
  427. private bool CanExecute
  428. {
  429. get
  430. {
  431. // 命令可执行仅当机器人可执行且已选择点位
  432. if (Robot != null && Robot.CanExecute)
  433. {
  434. return SelectedPointInDataGrid != null;
  435. }
  436. return false;
  437. }
  438. }
  439. /// <summary>
  440. /// 点位表切换时
  441. /// </summary>
  442. void ExecuteSelectionChangedCommand()
  443. {
  444. switch (SelectedIndex)
  445. {
  446. case 0:
  447. Points = SelectProduct.PickCameraPoints;
  448. break;
  449. case 1:
  450. Points = SelectProduct.PickPoints;
  451. break;
  452. case 2:
  453. Points = SelectProduct.SecPosCameraPoints;
  454. break;
  455. case 3:
  456. Points = SelectProduct.ScrewCameraPoints;
  457. break;
  458. case 4:
  459. Points = SelectProduct.ScrewPoints;
  460. break;
  461. case 5:
  462. Points = SelectProduct.CheckCameraPoints;
  463. break;
  464. default:
  465. break;
  466. }
  467. }
  468. /// <summary>
  469. /// 增加点位
  470. /// </summary>
  471. void ExecuteAddPointCommand()
  472. {
  473. //添加点位
  474. Points.Add(new PlcPoint()
  475. {
  476. Number = Points.Count + 1,
  477. X_Velocity = 100,
  478. Y_Velocity = 100,
  479. Z_Velocity_Start = 100,
  480. Z_Velocity_Stop = 100,
  481. U_Velocity = 100,
  482. R_Velocity = 100,
  483. });
  484. // update commands
  485. _MovePointUpCommand?.RaiseCanExecuteChanged();
  486. _MovePointDownCommand?.RaiseCanExecuteChanged();
  487. }
  488. /// <summary>
  489. /// 移除点位
  490. /// </summary>
  491. void ExecuteDeletePointCommand()
  492. {
  493. if (SelectedPoint == null)
  494. {
  495. return;
  496. }
  497. // 确认删除
  498. if (MessageBox.Show("确认要删除选中的点位吗?", "删除点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  499. {
  500. return;
  501. }
  502. int removedIndex = Points.IndexOf(SelectedPoint);
  503. Points.Remove(SelectedPoint);
  504. //重新编号
  505. for (int i = 0; i < Points.Count; i++)
  506. {
  507. Points[i].Number = i + 1;
  508. }
  509. // adjust selection
  510. if (Points.Count > 0)
  511. {
  512. int newIndex = Math.Min(removedIndex, Points.Count - 1);
  513. SelectedPointInDataGrid = Points[newIndex];
  514. }
  515. else
  516. {
  517. SelectedPointInDataGrid = null;
  518. }
  519. // update commands
  520. _MovePointUpCommand?.RaiseCanExecuteChanged();
  521. _MovePointDownCommand?.RaiseCanExecuteChanged();
  522. }
  523. private bool CanMovePointUp()
  524. {
  525. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) > 0;
  526. }
  527. private bool CanMovePointDown()
  528. {
  529. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) >= 0 && Points.IndexOf(SelectedPointInDataGrid) < Points.Count - 1;
  530. }
  531. /// <summary>
  532. /// 向上移动点位
  533. /// </summary>
  534. void ExecuteMovePointUpCommand()
  535. {
  536. if (SelectedPointInDataGrid == null) return;
  537. // 确认移动
  538. if (MessageBox.Show("确认将选中点位上移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  539. {
  540. return;
  541. }
  542. int idx = Points.IndexOf(SelectedPointInDataGrid);
  543. if (idx <= 0) return;
  544. var item = SelectedPointInDataGrid;
  545. Points.Move(idx, idx - 1);
  546. // renumber
  547. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  548. SelectedPointInDataGrid = item;
  549. _MovePointUpCommand?.RaiseCanExecuteChanged();
  550. _MovePointDownCommand?.RaiseCanExecuteChanged();
  551. }
  552. /// <summary>
  553. /// 向下移动点位
  554. /// </summary>
  555. void ExecuteMovePointDownCommand()
  556. {
  557. if (SelectedPointInDataGrid == null) return;
  558. // 确认移动
  559. if (MessageBox.Show("确认将选中点位下移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  560. {
  561. return;
  562. }
  563. int idx = Points.IndexOf(SelectedPointInDataGrid);
  564. if (idx < 0 || idx >= Points.Count - 1) return;
  565. var item = SelectedPointInDataGrid;
  566. Points.Move(idx, idx + 1);
  567. // renumber
  568. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  569. SelectedPointInDataGrid = item;
  570. _MovePointUpCommand?.RaiseCanExecuteChanged();
  571. _MovePointDownCommand?.RaiseCanExecuteChanged();
  572. }
  573. /// <summary>
  574. /// CAD导入点位
  575. /// </summary>
  576. async void ExecuteImportCadPointCommand()
  577. {
  578. try
  579. {
  580. var view = new DxfView();
  581. //show the dialog
  582. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  583. if (result != null)
  584. {
  585. var cadPoints = view.Points;
  586. if (cadPoints != null && cadPoints.Count > 0)
  587. {
  588. //要导入的开始点编号
  589. int startNumber = view.StartIndex;
  590. //弹窗提示用户是否确认要导入从该编号开始的点位,点位数量为cadPoints.Count
  591. if (MessageBox.Show($"确认要从点位编号 {startNumber} 开始导入 {cadPoints.Count} 个点位吗?", "导入点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  592. {
  593. return;
  594. }
  595. //导入点位
  596. for (int i = 0; i < cadPoints.Count; i++)
  597. {
  598. //如果点位编号已存在,则更新该点位,否则新增点位,只更新XY坐标
  599. var point = cadPoints[i];
  600. var existingPoint = Points.FirstOrDefault(p => p.Number == startNumber + i);
  601. if (existingPoint != null)
  602. {
  603. existingPoint.X_Position = point.X;
  604. existingPoint.Y_Position = point.Y;
  605. }
  606. else
  607. {
  608. Points.Add(new PlcPoint()
  609. {
  610. Number = startNumber + i,
  611. X_Position = point.X,
  612. Y_Position = point.Y,
  613. X_Velocity=100,
  614. Y_Velocity=100,
  615. Z_Velocity_Start=100,
  616. Z_Velocity_Stop=100,
  617. U_Velocity=100,
  618. R_Velocity=100,
  619. });
  620. }
  621. }
  622. //对所有的点位重新编号,确保编号连续
  623. var sortedPoints = Points.OrderBy(p => p.Number).ToList();
  624. Points.Clear();
  625. for (int i = 0; i < sortedPoints.Count; i++)
  626. {
  627. sortedPoints[i].Number = i + 1;
  628. Points.Add(sortedPoints[i]);
  629. }
  630. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  631. }
  632. }
  633. }
  634. catch (Exception ex)
  635. {
  636. LogHelper.WriteLogError("导入CAD点位时出错!", ex);
  637. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  638. }
  639. }
  640. #endregion
  641. #region 继承
  642. /// <summary>
  643. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  644. /// </summary>
  645. /// <param name="navigationContext"></param>
  646. /// <param name="continuationCallback"></param>
  647. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  648. {
  649. continuationCallback(true);
  650. }
  651. /// <summary>
  652. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  653. /// </summary>
  654. /// <param name="navigationContext"></param>
  655. /// <exception cref="NotImplementedException"></exception>
  656. public async void OnNavigatedTo(NavigationContext navigationContext)
  657. {
  658. try
  659. {
  660. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  661. SelectedIndex = 0;
  662. Points = SelectProduct.PickCameraPoints;
  663. if (Robot == null)
  664. {
  665. Robot = _robotService.GetRobotByNumber(1);
  666. }
  667. if (Robot != null && Robot.IsConnected)
  668. {
  669. this.Distance = _configService.GetRobotStepDistance(Robot.Id);
  670. Robot.EnterDebugMode = true;
  671. }
  672. else if (Robot != null)
  673. {
  674. Robot.EnterDebugMode = true;
  675. }
  676. }
  677. catch (Exception ex)
  678. {
  679. LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
  680. }
  681. }
  682. /// <summary>
  683. /// 是否允许导航到此视图模型。
  684. /// </summary>
  685. /// <param name="navigationContext"></param>
  686. /// <returns></returns>
  687. public bool IsNavigationTarget(NavigationContext navigationContext)
  688. {
  689. return true;
  690. }
  691. /// <summary>
  692. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  693. /// </summary>
  694. /// <param name="navigationContext"></param>
  695. public void OnNavigatedFrom(NavigationContext navigationContext)
  696. {
  697. //if (Robot != null)
  698. //{
  699. // Robot.EnterDebugMode = false;
  700. //}
  701. }
  702. #endregion
  703. private bool _IsAllowEdit = false;
  704. public bool IsAllowEdit
  705. {
  706. get { return _IsAllowEdit; }
  707. set { SetProperty(ref _IsAllowEdit, value); }
  708. }
  709. private void LoginChange(Models.User user)
  710. {
  711. if (user.userPart == Enums.UserPart.Operator)
  712. {
  713. IsAllowEdit = false;
  714. }
  715. else
  716. {
  717. IsAllowEdit = true;
  718. }
  719. }
  720. }
  721. }