PlcPointParamsViewModel.cs 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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. IDialogParameters parameters = new DialogParameters();
  930. parameters.Add("Robot", Robot);
  931. parameters.Add("Points", new List<PlcPoint>(SelectProduct.ScrewPoints.ToArray()));
  932. _dialogService.Show("AutoCorrectLockPoint", parameters, rst =>
  933. {
  934. //对话框关闭之后的回调函数,可以在这解析结果。
  935. ButtonResult result1 = rst.Result;
  936. if (result1 == ButtonResult.OK)
  937. {
  938. var param = rst.Parameters.GetValue<ObservableCollection<PointEx>>("CorrectPoints");
  939. for (int i = 0; i < SelectProduct.ScrewPoints.Count; i++)
  940. {
  941. SelectProduct.ScrewPoints[i].X_Position = param[i].PostX;
  942. SelectProduct.ScrewPoints[i].Y_Position = param[i].PostY;
  943. }
  944. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "完成!", Duration = 1 });
  945. }
  946. });
  947. }
  948. catch (Exception ex)
  949. {
  950. LogHelper.WriteLogError("通过相机自动矫正锁附点位时出错!", ex);
  951. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  952. }
  953. }
  954. #endregion
  955. #region 继承
  956. /// <summary>
  957. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  958. /// </summary>
  959. /// <param name="navigationContext"></param>
  960. /// <param name="continuationCallback"></param>
  961. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  962. {
  963. continuationCallback(true);
  964. }
  965. /// <summary>
  966. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  967. /// </summary>
  968. /// <param name="navigationContext"></param>
  969. /// <exception cref="NotImplementedException"></exception>
  970. public async void OnNavigatedTo(NavigationContext navigationContext)
  971. {
  972. try
  973. {
  974. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  975. SelectedIndex = 0;
  976. PickPoints = SelectProduct.PickPoints;
  977. Points = SelectProduct.PickCameraPoints;
  978. if (Robot == null)
  979. {
  980. Robot = _robotService.GetRobotByNumber(1);
  981. }
  982. if (Robot != null && Robot.IsConnected)
  983. {
  984. this.Distance = _configService.GetRobotStepDistance(Robot.Id);
  985. Robot.EnterDebugMode = true;
  986. }
  987. else if (Robot != null)
  988. {
  989. Robot.EnterDebugMode = true;
  990. }
  991. ProcedureModels = new ObservableCollection<ProcedureModel>();
  992. foreach (var item in SelectProduct.CameraProcedures)
  993. {
  994. foreach (var item1 in item.ProcedureModels)
  995. {
  996. ProcedureModels.Add(item1);
  997. }
  998. }
  999. //对锁付点的批量设置进行初始化
  1000. LockPointStartZ = SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Position_Start ?? 0;
  1001. LockZDepth= SelectProduct.ScrewPoints.FirstOrDefault() != null ? (SelectProduct.ScrewPoints.FirstOrDefault().Z_Position_Start - SelectProduct.ScrewPoints.FirstOrDefault().Z_Position_Stop) : 0;
  1002. LockXSpeed= SelectProduct.ScrewPoints.FirstOrDefault()?.X_Velocity ?? 100;
  1003. LockYSpeed= SelectProduct.ScrewPoints.FirstOrDefault()?.Y_Velocity ?? 100;
  1004. LockZSpeed= SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Velocity_Start ?? 100;
  1005. LockZDescendSpeed= SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Velocity_Stop ?? 100;
  1006. LockFeederNumber= SelectProduct.ScrewPoints.FirstOrDefault()?.Feeder ?? 1;
  1007. LockScrewProgramNumber= SelectProduct.ScrewPoints.FirstOrDefault()?.ScrewProNum ?? 1;
  1008. }
  1009. catch (Exception ex)
  1010. {
  1011. LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
  1012. }
  1013. }
  1014. /// <summary>
  1015. /// 是否允许导航到此视图模型。
  1016. /// </summary>
  1017. /// <param name="navigationContext"></param>
  1018. /// <returns></returns>
  1019. public bool IsNavigationTarget(NavigationContext navigationContext)
  1020. {
  1021. return true;
  1022. }
  1023. /// <summary>
  1024. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  1025. /// </summary>
  1026. /// <param name="navigationContext"></param>
  1027. public void OnNavigatedFrom(NavigationContext navigationContext)
  1028. {
  1029. //if (Robot != null)
  1030. //{
  1031. // Robot.EnterDebugMode = false;
  1032. //}
  1033. }
  1034. #endregion
  1035. #region 取料拍照点位矫正
  1036. private PlcPoint _SelectedCameraForPickPoint;
  1037. public PlcPoint SelectedCameraForPickPoint
  1038. {
  1039. get { return _SelectedCameraForPickPoint; }
  1040. set { SetProperty(ref _SelectedCameraForPickPoint, value); }
  1041. }
  1042. private ObservableCollection<PlcPoint> _PickPoints = new ObservableCollection<PlcPoint>();
  1043. /// <summary>
  1044. /// 取料点位集合
  1045. /// </summary>
  1046. public ObservableCollection<PlcPoint> PickPoints
  1047. {
  1048. get { return _PickPoints; }
  1049. set { SetProperty(ref _PickPoints, value); }
  1050. }
  1051. //
  1052. private PlcPoint _SelectedPickPoint;
  1053. /// <summary>
  1054. /// 选中的取料点位
  1055. /// </summary>
  1056. public PlcPoint SelectedPickPoint
  1057. {
  1058. get { return _SelectedPickPoint; }
  1059. set { SetProperty(ref _SelectedPickPoint, value); }
  1060. }
  1061. private DelegateCommand _AutoCorrectPickPointCommand;
  1062. public DelegateCommand AutoCorrectPickPointCommand =>
  1063. _AutoCorrectPickPointCommand ?? (_AutoCorrectPickPointCommand = new DelegateCommand(ExecuteAutoCorrectPickPointCommand, CanExecuteAutoCorrectPickPointCommand)
  1064. .ObservesProperty(()=> SelectedCameraForPickPoint).ObservesProperty(() => SelectedPickPoint).ObservesProperty(() => SelectProcedure).ObservesProperty(() => IsExecutingAutoCorrectPickPoint));
  1065. //
  1066. private bool _IsExecutingAutoCorrectPickPoint = false;
  1067. /// <summary>
  1068. /// 正在执行取料拍照点位矫正
  1069. /// </summary>
  1070. public bool IsExecutingAutoCorrectPickPoint
  1071. {
  1072. get { return _IsExecutingAutoCorrectPickPoint; }
  1073. set { SetProperty(ref _IsExecutingAutoCorrectPickPoint, value); }
  1074. }
  1075. async void ExecuteAutoCorrectPickPointCommand()
  1076. {
  1077. try
  1078. {
  1079. IsExecutingAutoCorrectPickPoint = true;
  1080. if (SelectedCameraForPickPoint == null || SelectedPickPoint == null || SelectProcedure == null)
  1081. {
  1082. return;
  1083. }
  1084. if (Robot == null || !Robot.IsConnected)
  1085. {
  1086. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行取料拍照点位矫正!", Duration = 1 });
  1087. return;
  1088. }
  1089. if (MessageBox.Show("确认要执行取料拍照点位矫正吗?", "取料拍照点位矫正", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1090. {
  1091. return;
  1092. }
  1093. var waiting = new WaitingControl();
  1094. //show the dialog
  1095. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1096. // 1. 控制机器人移动至拍照点位
  1097. var rpoint = new RPoint()
  1098. {
  1099. X = SelectedCameraForPickPoint.X_Position,
  1100. Y = SelectedCameraForPickPoint.Y_Position,
  1101. Z = SelectedCameraForPickPoint.Z_Position_Start,
  1102. U = SelectedCameraForPickPoint.U_Position,
  1103. V = SelectedCameraForPickPoint.R_Position,
  1104. };
  1105. await Robot.CalibMotionAsync(rpoint, 0);
  1106. var _cts = new CancellationTokenSource();
  1107. // 2. 执行拍照并获取识别结果
  1108. var recognitionResult =await _remoteCommandService.ExecutePhotoGetSinglePoint(SelectProcedure, null, new double[] { rpoint.X, rpoint.Y, 0 }, _cts.Token);
  1109. if (!recognitionResult.IsSucceed)
  1110. {
  1111. if (DialogHost.IsDialogOpen("RootDialog"))
  1112. {
  1113. DialogHost.Close("RootDialog");
  1114. }
  1115. await task;
  1116. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料拍照点位矫正失败,识别未成功!", Duration = 1 });
  1117. return;
  1118. }
  1119. // 3. 根据识别结果更新取料点位坐标
  1120. SelectedPickPoint.X_Position = (float)recognitionResult.X;
  1121. SelectedPickPoint.Y_Position = (float)recognitionResult.Y;
  1122. if (DialogHost.IsDialogOpen("RootDialog"))
  1123. {
  1124. DialogHost.Close("RootDialog");
  1125. }
  1126. await task;
  1127. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料拍照点位矫正完成!", Duration = 0.4 });
  1128. }
  1129. catch (Exception ex)
  1130. {
  1131. LogHelper.WriteLogError("取料拍照点位矫正时出错!", ex);
  1132. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1133. }
  1134. finally
  1135. {
  1136. IsExecutingAutoCorrectPickPoint = false;
  1137. }
  1138. }
  1139. bool CanExecuteAutoCorrectPickPointCommand()
  1140. {
  1141. return SelectedCameraForPickPoint!=null && SelectedPickPoint!=null && SelectProcedure!=null && !IsExecutingAutoCorrectPickPoint;
  1142. }
  1143. #endregion
  1144. private bool _IsAllowEdit = false;
  1145. public bool IsAllowEdit
  1146. {
  1147. get { return _IsAllowEdit; }
  1148. set { SetProperty(ref _IsAllowEdit, value); }
  1149. }
  1150. private void LoginChange(Models.User user)
  1151. {
  1152. if (user.userPart == Enums.UserPart.Operator)
  1153. {
  1154. IsAllowEdit = false;
  1155. }
  1156. else
  1157. {
  1158. IsAllowEdit = true;
  1159. }
  1160. }
  1161. }
  1162. }