PlcPointParamsViewModel.cs 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. using MaterialDesignThemes.Wpf;
  2. using MathNet.Numerics.LinearAlgebra;
  3. using Prism.Commands;
  4. using Prism.Events;
  5. using Prism.Ioc;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using TeamAAS_VP.Controls;
  17. using TeamAAS_VP.Core;
  18. using TeamAAS_VP.Core.PLCs;
  19. using TeamAAS_VP.DxfModule;
  20. using TeamAAS_VP.Enums;
  21. using TeamAAS_VP.Events;
  22. using TeamAAS_VP.Interfaces;
  23. using TeamAAS_VP.Models;
  24. using TeamAAS_VP.Models.PLC;
  25. using TeamAAS_VP.Models.Robot;
  26. using TeamAAS_VP.Resources.Languages;
  27. using TeamAAS_VP.Services;
  28. using TeamAAS_VP.Views.Product;
  29. using static TeamAAS_VP.ViewModels.Product.AutoCorrectLockPointViewModel;
  30. namespace TeamAAS_VP.ViewModels.Product
  31. {
  32. public class PlcPointParamsViewModel : BindableBase, IConfirmNavigationRequest
  33. {
  34. IRegionManager _regionManager;
  35. IEventAggregator _eventAggregator;
  36. IContainerProvider _container;
  37. IDialogService _dialogService;
  38. IConfigService _configService;
  39. IRobotService _robotService;
  40. ISystemDatabaseService _systemDatabaseService;
  41. IRemoteCommandService _remoteCommandService;
  42. #region 属性
  43. private ProductModel _SelectProduct;
  44. /// <summary>
  45. /// 选中的产品
  46. /// </summary>
  47. public ProductModel SelectProduct
  48. {
  49. get { return _SelectProduct; }
  50. set { SetProperty(ref _SelectProduct, value); }
  51. }
  52. private PlcPoint _SelectedPoint;
  53. public PlcPoint SelectedPoint
  54. {
  55. get { return _SelectedPoint; }
  56. set { SetProperty(ref _SelectedPoint, value); }
  57. }
  58. private PlcPoint _selectedPointInDataGrid;
  59. public PlcPoint SelectedPointInDataGrid
  60. {
  61. get { return _selectedPointInDataGrid; }
  62. set
  63. {
  64. if (SetProperty(ref _selectedPointInDataGrid, value))
  65. {
  66. SelectedPointInComboBox = value;
  67. // update command availability
  68. _MovePointUpCommand?.RaiseCanExecuteChanged();
  69. _MovePointDownCommand?.RaiseCanExecuteChanged();
  70. // 更新依赖可执行状态的命令
  71. _TechPointCommand?.RaiseCanExecuteChanged();
  72. _ExecuteMotion?.RaiseCanExecuteChanged();
  73. }
  74. // 可以同步到 SelectPoint 属性
  75. SelectedPoint = value;
  76. }
  77. }
  78. private PlcPoint _selectedPointInComboBox;
  79. public PlcPoint SelectedPointInComboBox
  80. {
  81. get { return _selectedPointInComboBox; }
  82. set
  83. {
  84. if (SetProperty(ref _selectedPointInComboBox, value))
  85. {
  86. SelectedPointInDataGrid = value;
  87. }
  88. // 可以同步到 SelectPoint 属性
  89. SelectedPoint = value;
  90. }
  91. }
  92. private ObservableCollection<PlcPoint> _Points = new ObservableCollection<PlcPoint>();
  93. /// <summary>
  94. /// 点位集合
  95. /// </summary>
  96. public ObservableCollection<PlcPoint> Points
  97. {
  98. get { return _Points; }
  99. set { SetProperty(ref _Points, value); }
  100. }
  101. private MotionCommand _SelectMotionCommand;
  102. /// <summary>
  103. /// 选中的运动指令
  104. /// </summary>
  105. public MotionCommand SelectMotionCommand
  106. {
  107. get { return _SelectMotionCommand; }
  108. set { SetProperty(ref _SelectMotionCommand, value); }
  109. }
  110. public Management management { get; set; }
  111. private IRobot _Robot;
  112. public IRobot Robot
  113. {
  114. get { return _Robot; }
  115. set
  116. {
  117. SetProperty(ref _Robot, value);
  118. }
  119. }
  120. private int _SelectedIndex;
  121. public int SelectedIndex
  122. {
  123. get { return _SelectedIndex; }
  124. set { SetProperty(ref _SelectedIndex, value); }
  125. }
  126. private double _Distance = 1;
  127. public double Distance
  128. {
  129. get { return _Distance; }
  130. set
  131. {
  132. SetProperty(ref _Distance, value);
  133. try
  134. {
  135. if (Robot == null)
  136. {
  137. return;
  138. }
  139. _configService.UpdateRobotStepDistance(Robot.Id, value);
  140. }
  141. catch (Exception)
  142. {
  143. }
  144. }
  145. }
  146. private ObservableCollection<ProcedureModel> _ProcedureModels;
  147. /// <summary>
  148. /// 流程集合
  149. /// </summary>
  150. public ObservableCollection<ProcedureModel> ProcedureModels
  151. {
  152. get { return _ProcedureModels; }
  153. set { SetProperty(ref _ProcedureModels, value); }
  154. }
  155. private ProcedureModel _SelectProcedure;
  156. /// <summary>
  157. /// 选中的流程
  158. /// </summary>
  159. public ProcedureModel SelectProcedure
  160. {
  161. get { return _SelectProcedure; }
  162. set
  163. {
  164. SetProperty(ref _SelectProcedure, value);
  165. }
  166. }
  167. private float _LockPointStartZ;
  168. /// <summary>
  169. /// 批量设置锁付点的起始Z坐标
  170. /// </summary>
  171. public float LockPointStartZ
  172. {
  173. get { return _LockPointStartZ; }
  174. set { SetProperty(ref _LockPointStartZ, value); }
  175. }
  176. private float _LockZDepth;
  177. /// <summary>
  178. /// 批量设置锁付点Z深度
  179. /// </summary>
  180. public float LockZDepth
  181. {
  182. get { return _LockZDepth; }
  183. set { SetProperty(ref _LockZDepth, value); }
  184. }
  185. private float _LockXSpeed;
  186. /// <summary>
  187. /// 批量设置锁付点X轴速度
  188. /// </summary>
  189. public float LockXSpeed
  190. {
  191. get { return _LockXSpeed; }
  192. set { SetProperty(ref _LockXSpeed, value); }
  193. }
  194. private float _LockYSpeed;
  195. /// <summary>
  196. /// 批量设置锁付点Y轴速度
  197. /// </summary>
  198. public float LockYSpeed
  199. {
  200. get { return _LockYSpeed; }
  201. set { SetProperty(ref _LockYSpeed, value); }
  202. }
  203. private float _LockZSpeed;
  204. /// <summary>
  205. /// 批量设置锁付点Z轴速度
  206. /// </summary>
  207. public float LockZSpeed
  208. {
  209. get { return _LockZSpeed; }
  210. set { SetProperty(ref _LockZSpeed, value); }
  211. }
  212. private float _LockZDescendSpeed;
  213. /// <summary>
  214. /// 批量设置锁付点Z轴下降速度
  215. /// </summary>
  216. public float LockZDescendSpeed
  217. {
  218. get { return _LockZDescendSpeed; }
  219. set { SetProperty(ref _LockZDescendSpeed, value); }
  220. }
  221. private Int16 _LockFeederNumber;
  222. /// <summary>
  223. /// 批量设置锁付点供料器编号
  224. /// </summary>
  225. public Int16 LockFeederNumber
  226. {
  227. get { return _LockFeederNumber; }
  228. set { SetProperty(ref _LockFeederNumber, value); }
  229. }
  230. private Int16 _LockScrewProgramNumber;
  231. /// <summary>
  232. /// 批量设置锁付点螺丝程序编号
  233. /// </summary>
  234. public Int16 LockScrewProgramNumber
  235. {
  236. get { return _LockScrewProgramNumber; }
  237. set { SetProperty(ref _LockScrewProgramNumber, value); }
  238. }
  239. #endregion
  240. #region 命令
  241. private DelegateCommand _NextCommand;
  242. public DelegateCommand NextCommand =>
  243. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  244. private DelegateCommand _BackCommand;
  245. public DelegateCommand BackCommand =>
  246. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  247. private DelegateCommand _LoadedCommand;
  248. public DelegateCommand LoadedCommand =>
  249. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  250. private DelegateCommand<object> _ExecuteMotion;
  251. public DelegateCommand<object> ExecuteMotion =>
  252. _ExecuteMotion ?? (_ExecuteMotion = new DelegateCommand<object>(ExecuteExecuteMotion).ObservesCanExecute(() => CanExecutePointCommand).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute).ObservesProperty(() => SelectedPointInDataGrid));
  253. private DelegateCommand<object> _JogCommand;
  254. public DelegateCommand<object> JogCommand =>
  255. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  256. private DelegateCommand _TechPointCommand;
  257. public DelegateCommand TechPointCommand =>
  258. _TechPointCommand ?? (_TechPointCommand = new DelegateCommand(ExecuteTechPointCommand).ObservesCanExecute(() => CanExecutePointCommand).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute).ObservesProperty(() => SelectedPointInDataGrid));
  259. private DelegateCommand<string> _MotorCommand;
  260. public DelegateCommand<string> MotorCommand =>
  261. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  262. private DelegateCommand _ResetCommand;
  263. public DelegateCommand ResetCommand =>
  264. _ResetCommand ?? (_ResetCommand = new DelegateCommand(ExecuteResetCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  265. private DelegateCommand _sFreeCommand;
  266. public DelegateCommand sFreeCommand =>
  267. _sFreeCommand ?? (_sFreeCommand = new DelegateCommand(ExecutesFreeCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  268. private DelegateCommand _SLockCommand;
  269. public DelegateCommand SLockCommand =>
  270. _SLockCommand ?? (_SLockCommand = new DelegateCommand(ExecuteSLockCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  271. private DelegateCommand _SelectionChangedCommand;
  272. public DelegateCommand SelectionChangedCommand =>
  273. _SelectionChangedCommand ?? (_SelectionChangedCommand = new DelegateCommand(ExecuteSelectionChangedCommand));
  274. private DelegateCommand _AddPointCommand;
  275. public DelegateCommand AddPointCommand =>
  276. _AddPointCommand ?? (_AddPointCommand = new DelegateCommand(ExecuteAddPointCommand));
  277. private DelegateCommand _DeletePointCommand;
  278. public DelegateCommand DeletePointCommand =>
  279. _DeletePointCommand ?? (_DeletePointCommand = new DelegateCommand(ExecuteDeletePointCommand, CanExecuteDeletePointCommand).ObservesProperty(()=>SelectedPointInDataGrid));
  280. private DelegateCommand _MovePointUpCommand;
  281. public DelegateCommand MovePointUpCommand => _MovePointUpCommand ?? (_MovePointUpCommand = new DelegateCommand(ExecuteMovePointUpCommand, CanMovePointUp));
  282. private DelegateCommand _MovePointDownCommand;
  283. public DelegateCommand MovePointDownCommand => _MovePointDownCommand ?? (_MovePointDownCommand = new DelegateCommand(ExecuteMovePointDownCommand, CanMovePointDown));
  284. private DelegateCommand _ImportCadPointCommand;
  285. public DelegateCommand ImportCadPointCommand =>
  286. _ImportCadPointCommand ?? (_ImportCadPointCommand = new DelegateCommand(ExecuteImportCadPointCommand));
  287. private DelegateCommand _ImportCadLockCameraPointCommand;
  288. public DelegateCommand ImportCadLockCameraPointCommand =>
  289. _ImportCadLockCameraPointCommand ?? (_ImportCadLockCameraPointCommand = new DelegateCommand(ExecuteImportCadLockCameraPointCommand));
  290. private DelegateCommand _ApplyLockPointStartZCommand;
  291. public DelegateCommand ApplyLockPointStartZCommand =>
  292. _ApplyLockPointStartZCommand ?? (_ApplyLockPointStartZCommand = new DelegateCommand(ExecuteApplyLockPointStartZCommand));
  293. private DelegateCommand _ApplyLockPointDepthCommand;
  294. public DelegateCommand ApplyLockPointDepthCommand =>
  295. _ApplyLockPointDepthCommand ?? (_ApplyLockPointDepthCommand = new DelegateCommand(ExecuteApplyLockPointDepthCommand));
  296. private DelegateCommand _ApplyLockPointSpeedCommand;
  297. public DelegateCommand ApplyLockPointSpeedCommand =>
  298. _ApplyLockPointSpeedCommand ?? (_ApplyLockPointSpeedCommand = new DelegateCommand(ExecuteApplyLockPointSpeedCommand));
  299. private DelegateCommand _ApplyLockPointFeederAndScrewProgramCommand;
  300. public DelegateCommand ApplyLockPointFeederAndScrewProgramCommand =>
  301. _ApplyLockPointFeederAndScrewProgramCommand ?? (_ApplyLockPointFeederAndScrewProgramCommand = new DelegateCommand(ExecuteApplyLockPointFeederAndScrewProgramCommand));
  302. private DelegateCommand _AutoCorrectLockPointCommand;
  303. public DelegateCommand AutoCorrectLockPointCommand =>
  304. _AutoCorrectLockPointCommand ?? (_AutoCorrectLockPointCommand = new DelegateCommand(ExecuteAutoCorrectLockPoint));
  305. #endregion
  306. #region 事件
  307. #endregion
  308. public PlcPointParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
  309. IConfigService configService, IRobotService robotService, ISystemDatabaseService systemDatabaseService, IRemoteCommandService remoteCommandService)
  310. {
  311. _regionManager = regionManager;
  312. _eventAggregator = ea;
  313. _container = container;
  314. _dialogService = dialogService;
  315. _configService = configService;
  316. _systemDatabaseService = systemDatabaseService;
  317. //订阅权限登录事件
  318. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  319. management = _container.Resolve<Management>();
  320. _robotService = robotService;
  321. _remoteCommandService = remoteCommandService;
  322. }
  323. #region 方法
  324. /// <summary>
  325. /// 下一步
  326. /// </summary>
  327. void ExecuteNextCommand()
  328. {
  329. NavigationParameters param = new NavigationParameters();
  330. param.Add("SelectProduct", SelectProduct);
  331. _regionManager.RequestNavigate("ProductRegionContext", "ProductParams", param);
  332. }
  333. /// <summary>
  334. /// 上一步
  335. /// </summary>
  336. void ExecuteBackCommand()
  337. {
  338. NavigationParameters param = new NavigationParameters();
  339. param.Add("SelectProduct", SelectProduct);
  340. param.Add("SelectedProcedure", null);
  341. _regionManager.RequestNavigate("ProductRegionContext", "ProcessBasics", param);
  342. }
  343. /// <summary>
  344. /// 点动
  345. /// </summary>
  346. /// <param name="obj"></param>
  347. private async void ExecuteJogCommand(object obj)
  348. {
  349. if (Robot == null || !Robot.IsConnected) return;
  350. try
  351. {
  352. Robot.Timeout = 6000000;
  353. var items = ((string)obj).Split(':');
  354. switch (items[0])
  355. {
  356. case "X+":
  357. await Robot.JogAsync("X", double.Parse(items[1]));
  358. break;
  359. case "X-":
  360. await Robot.JogAsync("X", -double.Parse(items[1]));
  361. break;
  362. case "Y+":
  363. await Robot.JogAsync("Y", double.Parse(items[1]));
  364. break;
  365. case "Y-":
  366. await Robot.JogAsync("Y", -double.Parse(items[1]));
  367. break;
  368. case "Z+":
  369. await Robot.JogAsync("Z", double.Parse(items[1]));
  370. break;
  371. case "Z-":
  372. await Robot.JogAsync("Z", -double.Parse(items[1]));
  373. break;
  374. case "U+":
  375. await Robot.JogAsync("U", double.Parse(items[1]));
  376. break;
  377. case "U-":
  378. await Robot.JogAsync("U", -double.Parse(items[1]));
  379. break;
  380. case "V+":
  381. await Robot.JogAsync("V", double.Parse(items[1]));
  382. break;
  383. case "V-":
  384. await Robot.JogAsync("V", -double.Parse(items[1]));
  385. break;
  386. case "W+":
  387. await Robot.JogAsync("W", double.Parse(items[1]));
  388. break;
  389. case "W-":
  390. await Robot.JogAsync("W", -double.Parse(items[1]));
  391. break;
  392. default:
  393. break;
  394. }
  395. Robot.Timeout = 5000;
  396. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  397. }
  398. catch (Exception ex)
  399. {
  400. LogHelper.WriteLogError("机器人点位-机器人点动时出错", ex);
  401. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  402. }
  403. }
  404. /// <summary>
  405. /// 执行运动
  406. /// </summary>
  407. /// <param name="obj"></param>
  408. async void ExecuteExecuteMotion(object parameter)
  409. {
  410. if (parameter == null)
  411. {
  412. return;
  413. }
  414. if (SelectedPoint==null)
  415. {
  416. return;
  417. }
  418. var values = (object[])parameter;
  419. var point = new RPoint()
  420. {
  421. X= SelectedPoint.X_Position,
  422. Y= SelectedPoint.Y_Position,
  423. Z= SelectedPoint.Z_Position_Start,
  424. U=SelectedPoint.U_Position,
  425. V= SelectedPoint.R_Position,
  426. };
  427. MotionCommand motionCmd = (MotionCommand)values[1];
  428. if (Robot == null || !Robot.IsConnected) return;
  429. try
  430. {
  431. Robot.Timeout = 6000000;
  432. switch (motionCmd)
  433. {
  434. case MotionCommand.Go:
  435. await Robot.GoAsync(point);
  436. break;
  437. case MotionCommand.Jump:
  438. await Robot.JumpAsync(point, 0);
  439. break;
  440. case MotionCommand.Move:
  441. await Robot.MoveAsync(point);
  442. break;
  443. }
  444. Robot.Timeout = 5000;
  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. /// <summary>
  454. /// 示教点位
  455. /// </summary>
  456. /// <param name="point"></param>
  457. async void ExecuteTechPointCommand()
  458. {
  459. if (Robot == null || !Robot.IsConnected) return;
  460. if (SelectedPoint == null) return;
  461. try
  462. {
  463. var curpos = await Robot.GetRobotPosAsync();
  464. SelectedPoint.X_Position = curpos.X;
  465. SelectedPoint.Y_Position = curpos.Y;
  466. SelectedPoint.Z_Position_Start = curpos.Z;
  467. SelectedPoint.U_Position = curpos.U;
  468. SelectedPoint.R_Position = curpos.V;
  469. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  470. }
  471. catch (Exception ex)
  472. {
  473. LogHelper.WriteLogError("机器人点位-示教点位时出错", ex);
  474. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  475. }
  476. }
  477. void ExecuteLoadedCommand()
  478. {
  479. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  480. {
  481. IsAllowEdit = false;
  482. }
  483. else
  484. {
  485. IsAllowEdit = true;
  486. }
  487. }
  488. /// <summary>
  489. /// 电机
  490. /// </summary>
  491. /// <param name="obj"></param>
  492. private async void ExecuteMotorCommand(string obj)
  493. {
  494. if (Robot == null || !Robot.IsConnected) return;
  495. try
  496. {
  497. await Robot.MotorAsync(Convert.ToBoolean(obj));
  498. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  499. }
  500. catch (Exception ex)
  501. {
  502. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  503. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  504. }
  505. }
  506. /// <summary>
  507. /// 重置
  508. /// </summary>
  509. private async void ExecuteResetCommand()
  510. {
  511. if (Robot == null || !Robot.IsConnected) return;
  512. try
  513. {
  514. await Robot.ResetAsync();
  515. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  516. }
  517. catch (Exception ex)
  518. {
  519. LogHelper.WriteLogError("重置机器人时出错!", ex);
  520. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  521. }
  522. }
  523. /// <summary>
  524. /// 锁定刹车
  525. /// </summary>
  526. private async void ExecuteSLockCommand()
  527. {
  528. if (Robot == null || !Robot.IsConnected) return;
  529. try
  530. {
  531. await Robot.SLockAsync();
  532. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  533. }
  534. catch (Exception ex)
  535. {
  536. LogHelper.WriteLogError("机器人锁定刹车时出错!", ex);
  537. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  538. }
  539. }
  540. /// <summary>
  541. /// 释放刹车
  542. /// </summary>
  543. private async void ExecutesFreeCommand()
  544. {
  545. if (Robot == null || !Robot.IsConnected) return;
  546. try
  547. {
  548. await Robot.SFreeAsync();
  549. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  550. }
  551. catch (Exception ex)
  552. {
  553. LogHelper.WriteLogError("机器人释放刹车时出错!", ex);
  554. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  555. }
  556. }
  557. private bool CanExecute
  558. {
  559. get
  560. {
  561. // 命令可执行仅当机器人可执行且已选择点位
  562. if (Robot != null && Robot.IsConnected && Robot.CanExecute)
  563. {
  564. return true;
  565. }
  566. return false;
  567. }
  568. }
  569. //是否可以示教或者执行点位
  570. private bool CanExecutePointCommand
  571. {
  572. get
  573. {
  574. if (Robot != null && Robot.IsConnected && Robot.CanExecute)
  575. {
  576. return SelectedPointInDataGrid != null;
  577. }
  578. return false;
  579. }
  580. }
  581. /// <summary>
  582. /// 点位表切换时
  583. /// </summary>
  584. void ExecuteSelectionChangedCommand()
  585. {
  586. switch (SelectedIndex)
  587. {
  588. case 0:
  589. Points = SelectProduct.PickCameraPoints;
  590. PickPoints= SelectProduct.PickPoints;
  591. break;
  592. case 1:
  593. Points = SelectProduct.PickPoints;
  594. break;
  595. case 2:
  596. Points = SelectProduct.SecPosCameraPoints;
  597. break;
  598. case 3:
  599. Points = SelectProduct.ScrewCameraPoints;
  600. break;
  601. case 4:
  602. Points = SelectProduct.ScrewPoints;
  603. break;
  604. case 5:
  605. Points = SelectProduct.CheckCameraPoints;
  606. break;
  607. default:
  608. break;
  609. }
  610. }
  611. /// <summary>
  612. /// 增加点位
  613. /// </summary>
  614. void ExecuteAddPointCommand()
  615. {
  616. //添加点位
  617. Points.Add(new PlcPoint()
  618. {
  619. Number = Points.Count + 1,
  620. X_Velocity = 100,
  621. Y_Velocity = 100,
  622. Z_Velocity_Start = 100,
  623. Z_Velocity_Stop = 100,
  624. U_Velocity = 100,
  625. R_Velocity = 100,
  626. });
  627. // update commands
  628. _MovePointUpCommand?.RaiseCanExecuteChanged();
  629. _MovePointDownCommand?.RaiseCanExecuteChanged();
  630. }
  631. /// <summary>
  632. /// 移除点位
  633. /// </summary>
  634. void ExecuteDeletePointCommand()
  635. {
  636. if (SelectedPoint == null)
  637. {
  638. return;
  639. }
  640. // 确认删除
  641. if (MessageBox.Show("确认要删除选中的点位吗?", "删除点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  642. {
  643. return;
  644. }
  645. int removedIndex = Points.IndexOf(SelectedPoint);
  646. Points.Remove(SelectedPoint);
  647. //重新编号
  648. for (int i = 0; i < Points.Count; i++)
  649. {
  650. Points[i].Number = i + 1;
  651. }
  652. // adjust selection
  653. if (Points.Count > 0)
  654. {
  655. int newIndex = Math.Min(removedIndex, Points.Count - 1);
  656. SelectedPointInDataGrid = Points[newIndex];
  657. }
  658. else
  659. {
  660. SelectedPointInDataGrid = null;
  661. }
  662. // update commands
  663. _MovePointUpCommand?.RaiseCanExecuteChanged();
  664. _MovePointDownCommand?.RaiseCanExecuteChanged();
  665. }
  666. bool CanExecuteDeletePointCommand()
  667. {
  668. return SelectedPointInDataGrid != null;
  669. }
  670. private bool CanMovePointUp()
  671. {
  672. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) > 0;
  673. }
  674. private bool CanMovePointDown()
  675. {
  676. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) >= 0 && Points.IndexOf(SelectedPointInDataGrid) < Points.Count - 1;
  677. }
  678. /// <summary>
  679. /// 向上移动点位
  680. /// </summary>
  681. void ExecuteMovePointUpCommand()
  682. {
  683. if (SelectedPointInDataGrid == null) return;
  684. // 确认移动
  685. if (MessageBox.Show("确认将选中点位上移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  686. {
  687. return;
  688. }
  689. int idx = Points.IndexOf(SelectedPointInDataGrid);
  690. if (idx <= 0) return;
  691. var item = SelectedPointInDataGrid;
  692. Points.Move(idx, idx - 1);
  693. // renumber
  694. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  695. SelectedPointInDataGrid = item;
  696. _MovePointUpCommand?.RaiseCanExecuteChanged();
  697. _MovePointDownCommand?.RaiseCanExecuteChanged();
  698. }
  699. /// <summary>
  700. /// 向下移动点位
  701. /// </summary>
  702. void ExecuteMovePointDownCommand()
  703. {
  704. if (SelectedPointInDataGrid == null) return;
  705. // 确认移动
  706. if (MessageBox.Show("确认将选中点位下移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  707. {
  708. return;
  709. }
  710. int idx = Points.IndexOf(SelectedPointInDataGrid);
  711. if (idx < 0 || idx >= Points.Count - 1) return;
  712. var item = SelectedPointInDataGrid;
  713. Points.Move(idx, idx + 1);
  714. // renumber
  715. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  716. SelectedPointInDataGrid = item;
  717. _MovePointUpCommand?.RaiseCanExecuteChanged();
  718. _MovePointDownCommand?.RaiseCanExecuteChanged();
  719. }
  720. /// <summary>
  721. /// CAD导入点位
  722. /// </summary>
  723. async void ExecuteImportCadPointCommand()
  724. {
  725. try
  726. {
  727. var view = new DxfView();
  728. //show the dialog
  729. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  730. if (result != null)
  731. {
  732. var cadPoints = view.Points;
  733. if (cadPoints != null && cadPoints.Count > 0)
  734. {
  735. //要导入的开始点编号
  736. int startNumber = view.StartIndex;
  737. //弹窗提示用户是否确认要导入从该编号开始的点位,点位数量为cadPoints.Count
  738. if (MessageBox.Show($"确认要从点位编号 {startNumber} 开始导入 {cadPoints.Count} 个点位吗?", "导入点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  739. {
  740. return;
  741. }
  742. //导入点位
  743. for (int i = 0; i < cadPoints.Count; i++)
  744. {
  745. //如果点位编号已存在,则更新该点位,否则新增点位,只更新XY坐标
  746. var point = cadPoints[i];
  747. var existingPoint = Points.FirstOrDefault(p => p.Number == startNumber + i);
  748. if (existingPoint != null)
  749. {
  750. existingPoint.X_Position = point.X;
  751. existingPoint.Y_Position = point.Y;
  752. }
  753. else
  754. {
  755. Points.Add(new PlcPoint()
  756. {
  757. Number = startNumber + i,
  758. X_Position = point.X,
  759. Y_Position = point.Y,
  760. X_Velocity=100,
  761. Y_Velocity=100,
  762. Z_Velocity_Start=100,
  763. Z_Velocity_Stop=100,
  764. U_Velocity=100,
  765. R_Velocity=100,
  766. });
  767. }
  768. }
  769. //对所有的点位重新编号,确保编号连续
  770. var sortedPoints = Points.OrderBy(p => p.Number).ToList();
  771. Points.Clear();
  772. for (int i = 0; i < sortedPoints.Count; i++)
  773. {
  774. sortedPoints[i].Number = i + 1;
  775. Points.Add(sortedPoints[i]);
  776. }
  777. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  778. }
  779. }
  780. }
  781. catch (Exception ex)
  782. {
  783. LogHelper.WriteLogError("导入CAD点位时出错!", ex);
  784. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  785. }
  786. }
  787. /// <summary>
  788. /// 导入锁付拍照引导的产品坐标系中的点位
  789. /// </summary>
  790. async void ExecuteImportCadLockCameraPointCommand()
  791. {
  792. try
  793. {
  794. var view = new DxfView();
  795. //show the dialog
  796. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  797. if (result != null)
  798. {
  799. var cadPoints = view.Points;
  800. if (cadPoints != null && cadPoints.Count == 2)
  801. {
  802. //弹窗提示用户是否确认要导入锁付拍照引导的起始点和结束点
  803. if (MessageBox.Show($"确认要导入锁付拍照引导的起始点和结束点吗?", "导入点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  804. {
  805. return;
  806. }
  807. //导入点位
  808. //起始点
  809. var startPoint = cadPoints[0];
  810. SelectProduct.ScrewCameraLocalPos1.X_Position = startPoint.X;
  811. SelectProduct.ScrewCameraLocalPos1.Y_Position = startPoint.Y;
  812. //结束点
  813. var endPoint = cadPoints[1];
  814. SelectProduct.ScrewCameraLocalPos2.X_Position = endPoint.X;
  815. SelectProduct.ScrewCameraLocalPos2.Y_Position = endPoint.Y;
  816. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  817. }
  818. else
  819. {
  820. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "请导入两个点,分别为锁付拍照引导的起始点和结束点!", Duration = 1 });
  821. }
  822. }
  823. }
  824. catch (Exception ex)
  825. {
  826. LogHelper.WriteLogError("导入CAD点位时出错!", ex);
  827. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  828. }
  829. }
  830. /// <summary>
  831. /// 批量设置锁付点的供料器编号和螺丝程序编号
  832. /// </summary>
  833. void ExecuteApplyLockPointFeederAndScrewProgramCommand()
  834. {
  835. //检查供料器编号和螺丝程序编号是否大于0,如果小于等于0,则提示用户编号是否正确,是否继续写入
  836. if (LockFeederNumber <= 0 || LockScrewProgramNumber <= 0)
  837. {
  838. var result = MessageBox.Show("供料器编号或螺丝程序编号小于等于0,是否继续写入?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  839. if (result != MessageBoxResult.Yes)
  840. {
  841. return;
  842. }
  843. }
  844. //遍历所有点位,设置供料器编号和螺丝程序编号
  845. foreach (var point in Points)
  846. {
  847. point.Feeder = LockFeederNumber;
  848. point.ScrewProNum = LockScrewProgramNumber;
  849. }
  850. }
  851. /// <summary>
  852. /// 批量设置锁付点的速度
  853. /// </summary>
  854. void ExecuteApplyLockPointSpeedCommand()
  855. {
  856. //检查速度是否大于0,如果小于等于0,则提示用户速度是否正确,是否继续写入
  857. if (LockXSpeed <= 0 || LockYSpeed <= 0 || LockZSpeed <= 0 || LockZDescendSpeed <= 0)
  858. {
  859. var result = MessageBox.Show("速度小于等于0,是否继续写入?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  860. if (result != MessageBoxResult.Yes)
  861. {
  862. return;
  863. }
  864. }
  865. //遍历所有点位,设置速度
  866. foreach (var point in Points)
  867. {
  868. point.X_Velocity = LockXSpeed;
  869. point.Y_Velocity = LockYSpeed;
  870. point.Z_Velocity_Start = LockZSpeed;
  871. point.Z_Velocity_Stop = LockZDescendSpeed;
  872. }
  873. }
  874. /// <summary>
  875. /// 批量设置锁付点的Z深度
  876. /// </summary>
  877. void ExecuteApplyLockPointDepthCommand()
  878. {
  879. //检查深度是否大于0,如果小于等于0,则提示用户深度是否正确,是否继续写入
  880. if (LockZDepth <= 0)
  881. {
  882. var result = MessageBox.Show("深度小于等于0,是否继续写入?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  883. if (result != MessageBoxResult.Yes)
  884. {
  885. return;
  886. }
  887. }
  888. //遍历所有点位,设置Z深度
  889. foreach (var point in Points)
  890. {
  891. point.Z_Position_Stop = point.Z_Position_Start - LockZDepth;
  892. }
  893. }
  894. /// <summary>
  895. /// 批量设置锁付点的起始Z坐标
  896. /// </summary>
  897. void ExecuteApplyLockPointStartZCommand()
  898. {
  899. //检查起始Z坐标是否小于0,如果大于等于0,则提示用户坐标是否正确,是否继续写入
  900. if (LockPointStartZ >= 0)
  901. {
  902. var result = MessageBox.Show("起始Z坐标大于等于0,是否继续写入?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  903. if (result != MessageBoxResult.Yes)
  904. {
  905. return;
  906. }
  907. }
  908. //遍历所有点位,设置起始Z坐标
  909. foreach (var point in Points)
  910. {
  911. point.Z_Position_Start = LockPointStartZ;
  912. }
  913. //检查终点坐标是否小于等于起始坐标,如果不是,则提示用户终点坐标大于等于起始坐标,用户需要去调整终点坐标
  914. foreach (var point in Points)
  915. {
  916. if (point.Z_Position_Stop >= point.Z_Position_Start)
  917. {
  918. MessageBox.Show($"点位编号 {point.Number} 的终点坐标大于等于起始坐标,请调整终点坐标!", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
  919. }
  920. }
  921. }
  922. /// <summary>
  923. /// 通过相机自动矫正锁附点位
  924. /// </summary>
  925. async void ExecuteAutoCorrectLockPoint()
  926. {
  927. try
  928. {
  929. //弹窗提示用户是否确认要通过相机自动矫正锁附点位
  930. var resultConfirm = MessageBox.Show("确认要通过相机自动矫正锁附点位吗?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  931. if (resultConfirm != MessageBoxResult.Yes)
  932. {
  933. return;
  934. }
  935. IDialogParameters parameters = new DialogParameters();
  936. parameters.Add("Robot", Robot);
  937. parameters.Add("Points", new List<PlcPoint>(SelectProduct.ScrewPoints.ToArray()));
  938. _dialogService.Show("AutoCorrectLockPoint", parameters, rst =>
  939. {
  940. //对话框关闭之后的回调函数,可以在这解析结果。
  941. ButtonResult result1 = rst.Result;
  942. if (result1 == ButtonResult.OK)
  943. {
  944. var param = rst.Parameters.GetValue<ObservableCollection<PointEx>>("CorrectPoints");
  945. for (int i = 0; i < SelectProduct.ScrewPoints.Count; i++)
  946. {
  947. SelectProduct.ScrewPoints[i].X_Position = param[i].PostX;
  948. SelectProduct.ScrewPoints[i].Y_Position = param[i].PostY;
  949. }
  950. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "完成!", Duration = 1 });
  951. }
  952. });
  953. }
  954. catch (Exception ex)
  955. {
  956. LogHelper.WriteLogError("通过相机自动矫正锁附点位时出错!", ex);
  957. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  958. }
  959. }
  960. #endregion
  961. #region 继承
  962. /// <summary>
  963. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  964. /// </summary>
  965. /// <param name="navigationContext"></param>
  966. /// <param name="continuationCallback"></param>
  967. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  968. {
  969. continuationCallback(true);
  970. }
  971. /// <summary>
  972. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  973. /// </summary>
  974. /// <param name="navigationContext"></param>
  975. /// <exception cref="NotImplementedException"></exception>
  976. public async void OnNavigatedTo(NavigationContext navigationContext)
  977. {
  978. try
  979. {
  980. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  981. SelectedIndex = 0;
  982. PickPoints = SelectProduct.PickPoints;
  983. Points = SelectProduct.PickCameraPoints;
  984. if (Robot == null)
  985. {
  986. Robot = _robotService.GetRobotByNumber(1);
  987. }
  988. if (Robot != null && Robot.IsConnected)
  989. {
  990. this.Distance = _configService.GetRobotStepDistance(Robot.Id);
  991. Robot.EnterDebugMode = true;
  992. }
  993. else if (Robot != null)
  994. {
  995. Robot.EnterDebugMode = true;
  996. }
  997. ProcedureModels = new ObservableCollection<ProcedureModel>();
  998. foreach (var item in SelectProduct.CameraProcedures)
  999. {
  1000. foreach (var item1 in item.ProcedureModels)
  1001. {
  1002. ProcedureModels.Add(item1);
  1003. }
  1004. }
  1005. //对锁付点的批量设置进行初始化
  1006. LockPointStartZ = SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Position_Start ?? 0;
  1007. LockZDepth= SelectProduct.ScrewPoints.FirstOrDefault() != null ? (SelectProduct.ScrewPoints.FirstOrDefault().Z_Position_Start - SelectProduct.ScrewPoints.FirstOrDefault().Z_Position_Stop) : 0;
  1008. LockXSpeed= SelectProduct.ScrewPoints.FirstOrDefault()?.X_Velocity ?? 100;
  1009. LockYSpeed= SelectProduct.ScrewPoints.FirstOrDefault()?.Y_Velocity ?? 100;
  1010. LockZSpeed= SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Velocity_Start ?? 100;
  1011. LockZDescendSpeed= SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Velocity_Stop ?? 100;
  1012. LockFeederNumber= SelectProduct.ScrewPoints.FirstOrDefault()?.Feeder ?? 1;
  1013. LockScrewProgramNumber= SelectProduct.ScrewPoints.FirstOrDefault()?.ScrewProNum ?? 1;
  1014. }
  1015. catch (Exception ex)
  1016. {
  1017. LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
  1018. }
  1019. }
  1020. /// <summary>
  1021. /// 是否允许导航到此视图模型。
  1022. /// </summary>
  1023. /// <param name="navigationContext"></param>
  1024. /// <returns></returns>
  1025. public bool IsNavigationTarget(NavigationContext navigationContext)
  1026. {
  1027. return true;
  1028. }
  1029. /// <summary>
  1030. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  1031. /// </summary>
  1032. /// <param name="navigationContext"></param>
  1033. public void OnNavigatedFrom(NavigationContext navigationContext)
  1034. {
  1035. //if (Robot != null)
  1036. //{
  1037. // Robot.EnterDebugMode = false;
  1038. //}
  1039. }
  1040. #endregion
  1041. #region 取料拍照点位矫正
  1042. private PlcPoint _SelectedCameraForPickPoint;
  1043. public PlcPoint SelectedCameraForPickPoint
  1044. {
  1045. get { return _SelectedCameraForPickPoint; }
  1046. set { SetProperty(ref _SelectedCameraForPickPoint, value); }
  1047. }
  1048. private ObservableCollection<PlcPoint> _PickPoints = new ObservableCollection<PlcPoint>();
  1049. /// <summary>
  1050. /// 取料点位集合
  1051. /// </summary>
  1052. public ObservableCollection<PlcPoint> PickPoints
  1053. {
  1054. get { return _PickPoints; }
  1055. set { SetProperty(ref _PickPoints, value); }
  1056. }
  1057. //
  1058. private PlcPoint _SelectedPickPoint;
  1059. /// <summary>
  1060. /// 选中的取料点位
  1061. /// </summary>
  1062. public PlcPoint SelectedPickPoint
  1063. {
  1064. get { return _SelectedPickPoint; }
  1065. set { SetProperty(ref _SelectedPickPoint, value); }
  1066. }
  1067. private DelegateCommand _AutoCorrectPickPointCommand;
  1068. public DelegateCommand AutoCorrectPickPointCommand =>
  1069. _AutoCorrectPickPointCommand ?? (_AutoCorrectPickPointCommand = new DelegateCommand(ExecuteAutoCorrectPickPointCommand, CanExecuteAutoCorrectPickPointCommand)
  1070. .ObservesProperty(()=> SelectedCameraForPickPoint).ObservesProperty(() => SelectedPickPoint).ObservesProperty(() => SelectProcedure).ObservesProperty(() => IsExecutingAutoCorrectPickPoint));
  1071. //
  1072. private bool _IsExecutingAutoCorrectPickPoint = false;
  1073. /// <summary>
  1074. /// 正在执行取料拍照点位矫正
  1075. /// </summary>
  1076. public bool IsExecutingAutoCorrectPickPoint
  1077. {
  1078. get { return _IsExecutingAutoCorrectPickPoint; }
  1079. set { SetProperty(ref _IsExecutingAutoCorrectPickPoint, value); }
  1080. }
  1081. async void ExecuteAutoCorrectPickPointCommand()
  1082. {
  1083. try
  1084. {
  1085. IsExecutingAutoCorrectPickPoint = true;
  1086. if (SelectedCameraForPickPoint == null || SelectedPickPoint == null || SelectProcedure == null)
  1087. {
  1088. return;
  1089. }
  1090. if (Robot == null || !Robot.IsConnected)
  1091. {
  1092. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行取料拍照点位矫正!", Duration = 1 });
  1093. return;
  1094. }
  1095. if (MessageBox.Show("确认要执行取料拍照点位矫正吗?", "取料拍照点位矫正", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1096. {
  1097. return;
  1098. }
  1099. var waiting = new WaitingControl();
  1100. //show the dialog
  1101. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1102. // 1. 控制机器人移动至拍照点位
  1103. var rpoint = new RPoint()
  1104. {
  1105. X = SelectedCameraForPickPoint.X_Position,
  1106. Y = SelectedCameraForPickPoint.Y_Position,
  1107. Z = SelectedCameraForPickPoint.Z_Position_Start,
  1108. U = SelectedCameraForPickPoint.U_Position,
  1109. V = SelectedCameraForPickPoint.R_Position,
  1110. };
  1111. await Robot.CalibMotionAsync(rpoint, 0);
  1112. var _cts = new CancellationTokenSource();
  1113. // 2. 执行拍照并获取识别结果
  1114. var recognitionResult =await _remoteCommandService.ExecutePhotoGetSinglePoint(SelectProcedure, null, new double[] { rpoint.X, rpoint.Y, 0 }, _cts.Token);
  1115. if (!recognitionResult.IsSucceed)
  1116. {
  1117. if (DialogHost.IsDialogOpen("RootDialog"))
  1118. {
  1119. DialogHost.Close("RootDialog");
  1120. }
  1121. await task;
  1122. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料拍照点位矫正失败,识别未成功!", Duration = 1 });
  1123. return;
  1124. }
  1125. // 3. 根据识别结果更新取料点位坐标
  1126. SelectedPickPoint.X_Position = (float)recognitionResult.X;
  1127. SelectedPickPoint.Y_Position = (float)recognitionResult.Y;
  1128. if (DialogHost.IsDialogOpen("RootDialog"))
  1129. {
  1130. DialogHost.Close("RootDialog");
  1131. }
  1132. await task;
  1133. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料拍照点位矫正完成!", Duration = 0.4 });
  1134. }
  1135. catch (Exception ex)
  1136. {
  1137. LogHelper.WriteLogError("取料拍照点位矫正时出错!", ex);
  1138. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1139. }
  1140. finally
  1141. {
  1142. IsExecutingAutoCorrectPickPoint = false;
  1143. }
  1144. }
  1145. bool CanExecuteAutoCorrectPickPointCommand()
  1146. {
  1147. return SelectedCameraForPickPoint!=null && SelectedPickPoint!=null && SelectProcedure!=null && !IsExecutingAutoCorrectPickPoint;
  1148. }
  1149. #endregion
  1150. private bool _IsAllowEdit = false;
  1151. public bool IsAllowEdit
  1152. {
  1153. get { return _IsAllowEdit; }
  1154. set { SetProperty(ref _IsAllowEdit, value); }
  1155. }
  1156. private void LoginChange(Models.User user)
  1157. {
  1158. if (user.userPart == Enums.UserPart.Operator)
  1159. {
  1160. IsAllowEdit = false;
  1161. }
  1162. else
  1163. {
  1164. IsAllowEdit = true;
  1165. }
  1166. }
  1167. }
  1168. }