PlcPointParamsViewModel.cs 36 KB

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