PlcPointParamsViewModel.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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(() => CanExecute).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(() => CanExecute).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. var values = (object[])parameter;
  322. var point = values[0] as RPoint;
  323. MotionCommand motionCmd = (MotionCommand)values[1];
  324. if (Robot == null || !Robot.IsConnected) return;
  325. try
  326. {
  327. Robot.Timeout = 6000000;
  328. switch (motionCmd)
  329. {
  330. case MotionCommand.Go:
  331. await Robot.GoAsync(point);
  332. break;
  333. case MotionCommand.Jump:
  334. await Robot.JumpAsync(point, 0);
  335. break;
  336. case MotionCommand.Move:
  337. await Robot.MoveAsync(point);
  338. break;
  339. }
  340. Robot.Timeout = 5000;
  341. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  342. }
  343. catch (Exception ex)
  344. {
  345. LogHelper.WriteLogError("机器人点位-执行点位时出错", ex);
  346. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  347. }
  348. }
  349. /// <summary>
  350. /// 示教点位
  351. /// </summary>
  352. /// <param name="point"></param>
  353. async void ExecuteTechPointCommand()
  354. {
  355. if (Robot == null || !Robot.IsConnected) return;
  356. if (SelectedPoint == null) return;
  357. try
  358. {
  359. var curpos = await Robot.GetRobotPosAsync();
  360. SelectedPoint.X_Position = curpos.X;
  361. SelectedPoint.Y_Position = curpos.Y;
  362. SelectedPoint.Z_Position_Start = curpos.Z;
  363. SelectedPoint.U_Position = curpos.U;
  364. SelectedPoint.R_Position = curpos.V;
  365. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  366. }
  367. catch (Exception ex)
  368. {
  369. LogHelper.WriteLogError("机器人点位-示教点位时出错", ex);
  370. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  371. }
  372. }
  373. void ExecuteLoadedCommand()
  374. {
  375. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  376. {
  377. IsAllowEdit = false;
  378. }
  379. else
  380. {
  381. IsAllowEdit = true;
  382. }
  383. }
  384. /// <summary>
  385. /// 电机
  386. /// </summary>
  387. /// <param name="obj"></param>
  388. private async void ExecuteMotorCommand(string obj)
  389. {
  390. if (Robot == null || !Robot.IsConnected) return;
  391. try
  392. {
  393. await Robot.MotorAsync(Convert.ToBoolean(obj));
  394. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  395. }
  396. catch (Exception ex)
  397. {
  398. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  399. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  400. }
  401. }
  402. /// <summary>
  403. /// 重置
  404. /// </summary>
  405. private async void ExecuteResetCommand()
  406. {
  407. if (Robot == null || !Robot.IsConnected) return;
  408. try
  409. {
  410. await Robot.ResetAsync();
  411. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  412. }
  413. catch (Exception ex)
  414. {
  415. LogHelper.WriteLogError("重置机器人时出错!", ex);
  416. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  417. }
  418. }
  419. /// <summary>
  420. /// 锁定刹车
  421. /// </summary>
  422. private async void ExecuteSLockCommand()
  423. {
  424. if (Robot == null || !Robot.IsConnected) return;
  425. try
  426. {
  427. await Robot.SLockAsync();
  428. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  429. }
  430. catch (Exception ex)
  431. {
  432. LogHelper.WriteLogError("机器人锁定刹车时出错!", ex);
  433. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  434. }
  435. }
  436. /// <summary>
  437. /// 释放刹车
  438. /// </summary>
  439. private async void ExecutesFreeCommand()
  440. {
  441. if (Robot == null || !Robot.IsConnected) return;
  442. try
  443. {
  444. await Robot.SFreeAsync();
  445. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  446. }
  447. catch (Exception ex)
  448. {
  449. LogHelper.WriteLogError("机器人释放刹车时出错!", ex);
  450. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  451. }
  452. }
  453. private bool CanExecute
  454. {
  455. get
  456. {
  457. // 命令可执行仅当机器人可执行且已选择点位
  458. if (Robot != null && Robot.CanExecute)
  459. {
  460. return SelectedPointInDataGrid != null;
  461. }
  462. return false;
  463. }
  464. }
  465. /// <summary>
  466. /// 点位表切换时
  467. /// </summary>
  468. void ExecuteSelectionChangedCommand()
  469. {
  470. switch (SelectedIndex)
  471. {
  472. case 0:
  473. Points = SelectProduct.PickCameraPoints;
  474. PickPoints= SelectProduct.PickPoints;
  475. break;
  476. case 1:
  477. Points = SelectProduct.PickPoints;
  478. break;
  479. case 2:
  480. Points = SelectProduct.SecPosCameraPoints;
  481. break;
  482. case 3:
  483. Points = SelectProduct.ScrewCameraPoints;
  484. break;
  485. case 4:
  486. Points = SelectProduct.ScrewPoints;
  487. break;
  488. case 5:
  489. Points = SelectProduct.CheckCameraPoints;
  490. break;
  491. default:
  492. break;
  493. }
  494. }
  495. /// <summary>
  496. /// 增加点位
  497. /// </summary>
  498. void ExecuteAddPointCommand()
  499. {
  500. //添加点位
  501. Points.Add(new PlcPoint()
  502. {
  503. Number = Points.Count + 1,
  504. X_Velocity = 100,
  505. Y_Velocity = 100,
  506. Z_Velocity_Start = 100,
  507. Z_Velocity_Stop = 100,
  508. U_Velocity = 100,
  509. R_Velocity = 100,
  510. });
  511. // update commands
  512. _MovePointUpCommand?.RaiseCanExecuteChanged();
  513. _MovePointDownCommand?.RaiseCanExecuteChanged();
  514. }
  515. /// <summary>
  516. /// 移除点位
  517. /// </summary>
  518. void ExecuteDeletePointCommand()
  519. {
  520. if (SelectedPoint == null)
  521. {
  522. return;
  523. }
  524. // 确认删除
  525. if (MessageBox.Show("确认要删除选中的点位吗?", "删除点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  526. {
  527. return;
  528. }
  529. int removedIndex = Points.IndexOf(SelectedPoint);
  530. Points.Remove(SelectedPoint);
  531. //重新编号
  532. for (int i = 0; i < Points.Count; i++)
  533. {
  534. Points[i].Number = i + 1;
  535. }
  536. // adjust selection
  537. if (Points.Count > 0)
  538. {
  539. int newIndex = Math.Min(removedIndex, Points.Count - 1);
  540. SelectedPointInDataGrid = Points[newIndex];
  541. }
  542. else
  543. {
  544. SelectedPointInDataGrid = null;
  545. }
  546. // update commands
  547. _MovePointUpCommand?.RaiseCanExecuteChanged();
  548. _MovePointDownCommand?.RaiseCanExecuteChanged();
  549. }
  550. private bool CanMovePointUp()
  551. {
  552. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) > 0;
  553. }
  554. private bool CanMovePointDown()
  555. {
  556. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) >= 0 && Points.IndexOf(SelectedPointInDataGrid) < Points.Count - 1;
  557. }
  558. /// <summary>
  559. /// 向上移动点位
  560. /// </summary>
  561. void ExecuteMovePointUpCommand()
  562. {
  563. if (SelectedPointInDataGrid == null) return;
  564. // 确认移动
  565. if (MessageBox.Show("确认将选中点位上移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  566. {
  567. return;
  568. }
  569. int idx = Points.IndexOf(SelectedPointInDataGrid);
  570. if (idx <= 0) return;
  571. var item = SelectedPointInDataGrid;
  572. Points.Move(idx, idx - 1);
  573. // renumber
  574. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  575. SelectedPointInDataGrid = item;
  576. _MovePointUpCommand?.RaiseCanExecuteChanged();
  577. _MovePointDownCommand?.RaiseCanExecuteChanged();
  578. }
  579. /// <summary>
  580. /// 向下移动点位
  581. /// </summary>
  582. void ExecuteMovePointDownCommand()
  583. {
  584. if (SelectedPointInDataGrid == null) return;
  585. // 确认移动
  586. if (MessageBox.Show("确认将选中点位下移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  587. {
  588. return;
  589. }
  590. int idx = Points.IndexOf(SelectedPointInDataGrid);
  591. if (idx < 0 || idx >= Points.Count - 1) return;
  592. var item = SelectedPointInDataGrid;
  593. Points.Move(idx, idx + 1);
  594. // renumber
  595. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  596. SelectedPointInDataGrid = item;
  597. _MovePointUpCommand?.RaiseCanExecuteChanged();
  598. _MovePointDownCommand?.RaiseCanExecuteChanged();
  599. }
  600. /// <summary>
  601. /// CAD导入点位
  602. /// </summary>
  603. async void ExecuteImportCadPointCommand()
  604. {
  605. try
  606. {
  607. var view = new DxfView();
  608. //show the dialog
  609. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  610. if (result != null)
  611. {
  612. var cadPoints = view.Points;
  613. if (cadPoints != null && cadPoints.Count > 0)
  614. {
  615. //要导入的开始点编号
  616. int startNumber = view.StartIndex;
  617. //弹窗提示用户是否确认要导入从该编号开始的点位,点位数量为cadPoints.Count
  618. if (MessageBox.Show($"确认要从点位编号 {startNumber} 开始导入 {cadPoints.Count} 个点位吗?", "导入点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  619. {
  620. return;
  621. }
  622. //导入点位
  623. for (int i = 0; i < cadPoints.Count; i++)
  624. {
  625. //如果点位编号已存在,则更新该点位,否则新增点位,只更新XY坐标
  626. var point = cadPoints[i];
  627. var existingPoint = Points.FirstOrDefault(p => p.Number == startNumber + i);
  628. if (existingPoint != null)
  629. {
  630. existingPoint.X_Position = point.X;
  631. existingPoint.Y_Position = point.Y;
  632. }
  633. else
  634. {
  635. Points.Add(new PlcPoint()
  636. {
  637. Number = startNumber + i,
  638. X_Position = point.X,
  639. Y_Position = point.Y,
  640. X_Velocity=100,
  641. Y_Velocity=100,
  642. Z_Velocity_Start=100,
  643. Z_Velocity_Stop=100,
  644. U_Velocity=100,
  645. R_Velocity=100,
  646. });
  647. }
  648. }
  649. //对所有的点位重新编号,确保编号连续
  650. var sortedPoints = Points.OrderBy(p => p.Number).ToList();
  651. Points.Clear();
  652. for (int i = 0; i < sortedPoints.Count; i++)
  653. {
  654. sortedPoints[i].Number = i + 1;
  655. Points.Add(sortedPoints[i]);
  656. }
  657. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  658. }
  659. }
  660. }
  661. catch (Exception ex)
  662. {
  663. LogHelper.WriteLogError("导入CAD点位时出错!", ex);
  664. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  665. }
  666. }
  667. #endregion
  668. #region 继承
  669. /// <summary>
  670. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  671. /// </summary>
  672. /// <param name="navigationContext"></param>
  673. /// <param name="continuationCallback"></param>
  674. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  675. {
  676. continuationCallback(true);
  677. }
  678. /// <summary>
  679. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  680. /// </summary>
  681. /// <param name="navigationContext"></param>
  682. /// <exception cref="NotImplementedException"></exception>
  683. public async void OnNavigatedTo(NavigationContext navigationContext)
  684. {
  685. try
  686. {
  687. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  688. SelectedIndex = 0;
  689. Points = SelectProduct.PickCameraPoints;
  690. if (Robot == null)
  691. {
  692. Robot = _robotService.GetRobotByNumber(1);
  693. }
  694. if (Robot != null && Robot.IsConnected)
  695. {
  696. this.Distance = _configService.GetRobotStepDistance(Robot.Id);
  697. Robot.EnterDebugMode = true;
  698. }
  699. else if (Robot != null)
  700. {
  701. Robot.EnterDebugMode = true;
  702. }
  703. ProcedureModels = new ObservableCollection<ProcedureModel>();
  704. foreach (var item in SelectProduct.CameraProcedures)
  705. {
  706. foreach (var item1 in item.ProcedureModels)
  707. {
  708. ProcedureModels.Add(item1);
  709. }
  710. }
  711. }
  712. catch (Exception ex)
  713. {
  714. LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
  715. }
  716. }
  717. /// <summary>
  718. /// 是否允许导航到此视图模型。
  719. /// </summary>
  720. /// <param name="navigationContext"></param>
  721. /// <returns></returns>
  722. public bool IsNavigationTarget(NavigationContext navigationContext)
  723. {
  724. return true;
  725. }
  726. /// <summary>
  727. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  728. /// </summary>
  729. /// <param name="navigationContext"></param>
  730. public void OnNavigatedFrom(NavigationContext navigationContext)
  731. {
  732. //if (Robot != null)
  733. //{
  734. // Robot.EnterDebugMode = false;
  735. //}
  736. }
  737. #endregion
  738. #region 取料拍照点位矫正
  739. private PlcPoint _SelectedCameraForPickPoint;
  740. public PlcPoint SelectedCameraForPickPoint
  741. {
  742. get { return _SelectedCameraForPickPoint; }
  743. set { SetProperty(ref _SelectedCameraForPickPoint, value); }
  744. }
  745. private ObservableCollection<PlcPoint> _PickPoints = new ObservableCollection<PlcPoint>();
  746. /// <summary>
  747. /// 取料点位集合
  748. /// </summary>
  749. public ObservableCollection<PlcPoint> PickPoints
  750. {
  751. get { return _PickPoints; }
  752. set { SetProperty(ref _PickPoints, value); }
  753. }
  754. //
  755. private PlcPoint _SelectedPickPoint;
  756. /// <summary>
  757. /// 选中的取料点位
  758. /// </summary>
  759. public PlcPoint SelectedPickPoint
  760. {
  761. get { return _SelectedPickPoint; }
  762. set { SetProperty(ref _SelectedPickPoint, value); }
  763. }
  764. private DelegateCommand _AutoCorrectPickPointCommand;
  765. public DelegateCommand AutoCorrectPickPointCommand =>
  766. _AutoCorrectPickPointCommand ?? (_AutoCorrectPickPointCommand = new DelegateCommand(ExecuteAutoCorrectPickPointCommand, CanExecuteAutoCorrectPickPointCommand)
  767. .ObservesProperty(()=> SelectedCameraForPickPoint).ObservesProperty(() => SelectedPickPoint).ObservesProperty(() => SelectProcedure).ObservesProperty(() => IsExecutingAutoCorrectPickPoint));
  768. //
  769. private bool _IsExecutingAutoCorrectPickPoint = false;
  770. /// <summary>
  771. /// 正在执行取料拍照点位矫正
  772. /// </summary>
  773. public bool IsExecutingAutoCorrectPickPoint
  774. {
  775. get { return _IsExecutingAutoCorrectPickPoint; }
  776. set { SetProperty(ref _IsExecutingAutoCorrectPickPoint, value); }
  777. }
  778. async void ExecuteAutoCorrectPickPointCommand()
  779. {
  780. try
  781. {
  782. IsExecutingAutoCorrectPickPoint = true;
  783. if (SelectedCameraForPickPoint == null || SelectedPickPoint == null || SelectProcedure == null)
  784. {
  785. return;
  786. }
  787. if (Robot == null || !Robot.IsConnected)
  788. {
  789. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行取料拍照点位矫正!", Duration = 1 });
  790. return;
  791. }
  792. if (MessageBox.Show("确认要执行取料拍照点位矫正吗?", "取料拍照点位矫正", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  793. {
  794. return;
  795. }
  796. // 1. 控制机器人移动至拍照点位
  797. var rpoint = new RPoint()
  798. {
  799. X = SelectedCameraForPickPoint.X_Position,
  800. Y = SelectedCameraForPickPoint.Y_Position,
  801. Z = SelectedCameraForPickPoint.Z_Position_Start,
  802. U = SelectedCameraForPickPoint.U_Position,
  803. V = SelectedCameraForPickPoint.R_Position,
  804. };
  805. await Robot.CalibMotionAsync(rpoint, 0);
  806. var _cts = new CancellationTokenSource();
  807. // 2. 执行拍照并获取识别结果
  808. var recognitionResult = _remoteCommandService.ExecutePhotoGetSinglePoint(SelectProcedure, null, new double[] { rpoint.X, rpoint.Y, 0 }, _cts.Token);
  809. if (!recognitionResult.IsSucceed)
  810. {
  811. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料拍照点位矫正失败,识别未成功!", Duration = 1 });
  812. return;
  813. }
  814. // 3. 根据识别结果更新取料点位坐标
  815. SelectedPickPoint.X_Position = (float)recognitionResult.X;
  816. SelectedPickPoint.Y_Position = (float)recognitionResult.Y;
  817. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料拍照点位矫正完成!", Duration = 0.4 });
  818. }
  819. catch (Exception ex)
  820. {
  821. LogHelper.WriteLogError("取料拍照点位矫正时出错!", ex);
  822. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  823. }
  824. finally
  825. {
  826. IsExecutingAutoCorrectPickPoint = false;
  827. }
  828. }
  829. bool CanExecuteAutoCorrectPickPointCommand()
  830. {
  831. return SelectedCameraForPickPoint!=null && SelectedPickPoint!=null && SelectProcedure!=null && !IsExecutingAutoCorrectPickPoint;
  832. }
  833. #endregion
  834. private bool _IsAllowEdit = false;
  835. public bool IsAllowEdit
  836. {
  837. get { return _IsAllowEdit; }
  838. set { SetProperty(ref _IsAllowEdit, value); }
  839. }
  840. private void LoginChange(Models.User user)
  841. {
  842. if (user.userPart == Enums.UserPart.Operator)
  843. {
  844. IsAllowEdit = false;
  845. }
  846. else
  847. {
  848. IsAllowEdit = true;
  849. }
  850. }
  851. }
  852. }