PlcPointParamsViewModel.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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. 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.CanExecute)
  470. {
  471. return SelectedPointInDataGrid != null;
  472. }
  473. return false;
  474. }
  475. }
  476. /// <summary>
  477. /// 点位表切换时
  478. /// </summary>
  479. void ExecuteSelectionChangedCommand()
  480. {
  481. switch (SelectedIndex)
  482. {
  483. case 0:
  484. Points = SelectProduct.PickCameraPoints;
  485. PickPoints= SelectProduct.PickPoints;
  486. break;
  487. case 1:
  488. Points = SelectProduct.PickPoints;
  489. break;
  490. case 2:
  491. Points = SelectProduct.SecPosCameraPoints;
  492. break;
  493. case 3:
  494. Points = SelectProduct.ScrewCameraPoints;
  495. break;
  496. case 4:
  497. Points = SelectProduct.ScrewPoints;
  498. break;
  499. case 5:
  500. Points = SelectProduct.CheckCameraPoints;
  501. break;
  502. default:
  503. break;
  504. }
  505. }
  506. /// <summary>
  507. /// 增加点位
  508. /// </summary>
  509. void ExecuteAddPointCommand()
  510. {
  511. //添加点位
  512. Points.Add(new PlcPoint()
  513. {
  514. Number = Points.Count + 1,
  515. X_Velocity = 100,
  516. Y_Velocity = 100,
  517. Z_Velocity_Start = 100,
  518. Z_Velocity_Stop = 100,
  519. U_Velocity = 100,
  520. R_Velocity = 100,
  521. });
  522. // update commands
  523. _MovePointUpCommand?.RaiseCanExecuteChanged();
  524. _MovePointDownCommand?.RaiseCanExecuteChanged();
  525. }
  526. /// <summary>
  527. /// 移除点位
  528. /// </summary>
  529. void ExecuteDeletePointCommand()
  530. {
  531. if (SelectedPoint == null)
  532. {
  533. return;
  534. }
  535. // 确认删除
  536. if (MessageBox.Show("确认要删除选中的点位吗?", "删除点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  537. {
  538. return;
  539. }
  540. int removedIndex = Points.IndexOf(SelectedPoint);
  541. Points.Remove(SelectedPoint);
  542. //重新编号
  543. for (int i = 0; i < Points.Count; i++)
  544. {
  545. Points[i].Number = i + 1;
  546. }
  547. // adjust selection
  548. if (Points.Count > 0)
  549. {
  550. int newIndex = Math.Min(removedIndex, Points.Count - 1);
  551. SelectedPointInDataGrid = Points[newIndex];
  552. }
  553. else
  554. {
  555. SelectedPointInDataGrid = null;
  556. }
  557. // update commands
  558. _MovePointUpCommand?.RaiseCanExecuteChanged();
  559. _MovePointDownCommand?.RaiseCanExecuteChanged();
  560. }
  561. private bool CanMovePointUp()
  562. {
  563. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) > 0;
  564. }
  565. private bool CanMovePointDown()
  566. {
  567. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) >= 0 && Points.IndexOf(SelectedPointInDataGrid) < Points.Count - 1;
  568. }
  569. /// <summary>
  570. /// 向上移动点位
  571. /// </summary>
  572. void ExecuteMovePointUpCommand()
  573. {
  574. if (SelectedPointInDataGrid == null) return;
  575. // 确认移动
  576. if (MessageBox.Show("确认将选中点位上移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  577. {
  578. return;
  579. }
  580. int idx = Points.IndexOf(SelectedPointInDataGrid);
  581. if (idx <= 0) return;
  582. var item = SelectedPointInDataGrid;
  583. Points.Move(idx, idx - 1);
  584. // renumber
  585. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  586. SelectedPointInDataGrid = item;
  587. _MovePointUpCommand?.RaiseCanExecuteChanged();
  588. _MovePointDownCommand?.RaiseCanExecuteChanged();
  589. }
  590. /// <summary>
  591. /// 向下移动点位
  592. /// </summary>
  593. void ExecuteMovePointDownCommand()
  594. {
  595. if (SelectedPointInDataGrid == null) return;
  596. // 确认移动
  597. if (MessageBox.Show("确认将选中点位下移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  598. {
  599. return;
  600. }
  601. int idx = Points.IndexOf(SelectedPointInDataGrid);
  602. if (idx < 0 || idx >= Points.Count - 1) return;
  603. var item = SelectedPointInDataGrid;
  604. Points.Move(idx, idx + 1);
  605. // renumber
  606. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  607. SelectedPointInDataGrid = item;
  608. _MovePointUpCommand?.RaiseCanExecuteChanged();
  609. _MovePointDownCommand?.RaiseCanExecuteChanged();
  610. }
  611. /// <summary>
  612. /// CAD导入点位
  613. /// </summary>
  614. async void ExecuteImportCadPointCommand()
  615. {
  616. try
  617. {
  618. var view = new DxfView();
  619. //show the dialog
  620. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  621. if (result != null)
  622. {
  623. var cadPoints = view.Points;
  624. if (cadPoints != null && cadPoints.Count > 0)
  625. {
  626. //要导入的开始点编号
  627. int startNumber = view.StartIndex;
  628. //弹窗提示用户是否确认要导入从该编号开始的点位,点位数量为cadPoints.Count
  629. if (MessageBox.Show($"确认要从点位编号 {startNumber} 开始导入 {cadPoints.Count} 个点位吗?", "导入点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  630. {
  631. return;
  632. }
  633. //导入点位
  634. for (int i = 0; i < cadPoints.Count; i++)
  635. {
  636. //如果点位编号已存在,则更新该点位,否则新增点位,只更新XY坐标
  637. var point = cadPoints[i];
  638. var existingPoint = Points.FirstOrDefault(p => p.Number == startNumber + i);
  639. if (existingPoint != null)
  640. {
  641. existingPoint.X_Position = point.X;
  642. existingPoint.Y_Position = point.Y;
  643. }
  644. else
  645. {
  646. Points.Add(new PlcPoint()
  647. {
  648. Number = startNumber + i,
  649. X_Position = point.X,
  650. Y_Position = point.Y,
  651. X_Velocity=100,
  652. Y_Velocity=100,
  653. Z_Velocity_Start=100,
  654. Z_Velocity_Stop=100,
  655. U_Velocity=100,
  656. R_Velocity=100,
  657. });
  658. }
  659. }
  660. //对所有的点位重新编号,确保编号连续
  661. var sortedPoints = Points.OrderBy(p => p.Number).ToList();
  662. Points.Clear();
  663. for (int i = 0; i < sortedPoints.Count; i++)
  664. {
  665. sortedPoints[i].Number = i + 1;
  666. Points.Add(sortedPoints[i]);
  667. }
  668. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  669. }
  670. }
  671. }
  672. catch (Exception ex)
  673. {
  674. LogHelper.WriteLogError("导入CAD点位时出错!", ex);
  675. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  676. }
  677. }
  678. #endregion
  679. #region 继承
  680. /// <summary>
  681. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  682. /// </summary>
  683. /// <param name="navigationContext"></param>
  684. /// <param name="continuationCallback"></param>
  685. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  686. {
  687. continuationCallback(true);
  688. }
  689. /// <summary>
  690. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  691. /// </summary>
  692. /// <param name="navigationContext"></param>
  693. /// <exception cref="NotImplementedException"></exception>
  694. public async void OnNavigatedTo(NavigationContext navigationContext)
  695. {
  696. try
  697. {
  698. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  699. SelectedIndex = 0;
  700. Points = SelectProduct.PickCameraPoints;
  701. if (Robot == null)
  702. {
  703. Robot = _robotService.GetRobotByNumber(1);
  704. }
  705. if (Robot != null && Robot.IsConnected)
  706. {
  707. this.Distance = _configService.GetRobotStepDistance(Robot.Id);
  708. Robot.EnterDebugMode = true;
  709. }
  710. else if (Robot != null)
  711. {
  712. Robot.EnterDebugMode = true;
  713. }
  714. ProcedureModels = new ObservableCollection<ProcedureModel>();
  715. foreach (var item in SelectProduct.CameraProcedures)
  716. {
  717. foreach (var item1 in item.ProcedureModels)
  718. {
  719. ProcedureModels.Add(item1);
  720. }
  721. }
  722. }
  723. catch (Exception ex)
  724. {
  725. LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
  726. }
  727. }
  728. /// <summary>
  729. /// 是否允许导航到此视图模型。
  730. /// </summary>
  731. /// <param name="navigationContext"></param>
  732. /// <returns></returns>
  733. public bool IsNavigationTarget(NavigationContext navigationContext)
  734. {
  735. return true;
  736. }
  737. /// <summary>
  738. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  739. /// </summary>
  740. /// <param name="navigationContext"></param>
  741. public void OnNavigatedFrom(NavigationContext navigationContext)
  742. {
  743. //if (Robot != null)
  744. //{
  745. // Robot.EnterDebugMode = false;
  746. //}
  747. }
  748. #endregion
  749. #region 取料拍照点位矫正
  750. private PlcPoint _SelectedCameraForPickPoint;
  751. public PlcPoint SelectedCameraForPickPoint
  752. {
  753. get { return _SelectedCameraForPickPoint; }
  754. set { SetProperty(ref _SelectedCameraForPickPoint, value); }
  755. }
  756. private ObservableCollection<PlcPoint> _PickPoints = new ObservableCollection<PlcPoint>();
  757. /// <summary>
  758. /// 取料点位集合
  759. /// </summary>
  760. public ObservableCollection<PlcPoint> PickPoints
  761. {
  762. get { return _PickPoints; }
  763. set { SetProperty(ref _PickPoints, value); }
  764. }
  765. //
  766. private PlcPoint _SelectedPickPoint;
  767. /// <summary>
  768. /// 选中的取料点位
  769. /// </summary>
  770. public PlcPoint SelectedPickPoint
  771. {
  772. get { return _SelectedPickPoint; }
  773. set { SetProperty(ref _SelectedPickPoint, value); }
  774. }
  775. private DelegateCommand _AutoCorrectPickPointCommand;
  776. public DelegateCommand AutoCorrectPickPointCommand =>
  777. _AutoCorrectPickPointCommand ?? (_AutoCorrectPickPointCommand = new DelegateCommand(ExecuteAutoCorrectPickPointCommand, CanExecuteAutoCorrectPickPointCommand)
  778. .ObservesProperty(()=> SelectedCameraForPickPoint).ObservesProperty(() => SelectedPickPoint).ObservesProperty(() => SelectProcedure).ObservesProperty(() => IsExecutingAutoCorrectPickPoint));
  779. //
  780. private bool _IsExecutingAutoCorrectPickPoint = false;
  781. /// <summary>
  782. /// 正在执行取料拍照点位矫正
  783. /// </summary>
  784. public bool IsExecutingAutoCorrectPickPoint
  785. {
  786. get { return _IsExecutingAutoCorrectPickPoint; }
  787. set { SetProperty(ref _IsExecutingAutoCorrectPickPoint, value); }
  788. }
  789. async void ExecuteAutoCorrectPickPointCommand()
  790. {
  791. try
  792. {
  793. IsExecutingAutoCorrectPickPoint = true;
  794. if (SelectedCameraForPickPoint == null || SelectedPickPoint == null || SelectProcedure == null)
  795. {
  796. return;
  797. }
  798. if (Robot == null || !Robot.IsConnected)
  799. {
  800. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行取料拍照点位矫正!", Duration = 1 });
  801. return;
  802. }
  803. if (MessageBox.Show("确认要执行取料拍照点位矫正吗?", "取料拍照点位矫正", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  804. {
  805. return;
  806. }
  807. // 1. 控制机器人移动至拍照点位
  808. var rpoint = new RPoint()
  809. {
  810. X = SelectedCameraForPickPoint.X_Position,
  811. Y = SelectedCameraForPickPoint.Y_Position,
  812. Z = SelectedCameraForPickPoint.Z_Position_Start,
  813. U = SelectedCameraForPickPoint.U_Position,
  814. V = SelectedCameraForPickPoint.R_Position,
  815. };
  816. await Robot.CalibMotionAsync(rpoint, 0);
  817. var _cts = new CancellationTokenSource();
  818. // 2. 执行拍照并获取识别结果
  819. var recognitionResult = _remoteCommandService.ExecutePhotoGetSinglePoint(SelectProcedure, null, new double[] { rpoint.X, rpoint.Y, 0 }, _cts.Token);
  820. if (!recognitionResult.IsSucceed)
  821. {
  822. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料拍照点位矫正失败,识别未成功!", Duration = 1 });
  823. return;
  824. }
  825. // 3. 根据识别结果更新取料点位坐标
  826. SelectedPickPoint.X_Position = (float)recognitionResult.X;
  827. SelectedPickPoint.Y_Position = (float)recognitionResult.Y;
  828. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料拍照点位矫正完成!", Duration = 0.4 });
  829. }
  830. catch (Exception ex)
  831. {
  832. LogHelper.WriteLogError("取料拍照点位矫正时出错!", ex);
  833. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  834. }
  835. finally
  836. {
  837. IsExecutingAutoCorrectPickPoint = false;
  838. }
  839. }
  840. bool CanExecuteAutoCorrectPickPointCommand()
  841. {
  842. return SelectedCameraForPickPoint!=null && SelectedPickPoint!=null && SelectProcedure!=null && !IsExecutingAutoCorrectPickPoint;
  843. }
  844. #endregion
  845. private bool _IsAllowEdit = false;
  846. public bool IsAllowEdit
  847. {
  848. get { return _IsAllowEdit; }
  849. set { SetProperty(ref _IsAllowEdit, value); }
  850. }
  851. private void LoginChange(Models.User user)
  852. {
  853. if (user.userPart == Enums.UserPart.Operator)
  854. {
  855. IsAllowEdit = false;
  856. }
  857. else
  858. {
  859. IsAllowEdit = true;
  860. }
  861. }
  862. }
  863. }