PlcPointParamsViewModel.cs 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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. var items = ((string)obj).Split(':');
  353. //如果移动距离大于10mm,则提示用户确认
  354. double distance = double.Parse(items[1]);
  355. if (Math.Abs(distance) >= 10)
  356. {
  357. if (MessageBox.Show($"确认要点动距离 {distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  358. {
  359. return;
  360. }
  361. }
  362. Robot.Timeout = 6000000;
  363. switch (items[0])
  364. {
  365. case "X+":
  366. await Robot.JogAsync("X", distance);
  367. break;
  368. case "X-":
  369. await Robot.JogAsync("X", -distance);
  370. break;
  371. case "Y+":
  372. await Robot.JogAsync("Y", distance);
  373. break;
  374. case "Y-":
  375. await Robot.JogAsync("Y", -distance);
  376. break;
  377. case "Z+":
  378. await Robot.JogAsync("Z", distance);
  379. break;
  380. case "Z-":
  381. await Robot.JogAsync("Z", -distance);
  382. break;
  383. case "U+":
  384. await Robot.JogAsync("U", distance);
  385. break;
  386. case "U-":
  387. await Robot.JogAsync("U", -distance);
  388. break;
  389. case "V+":
  390. await Robot.JogAsync("V", distance);
  391. break;
  392. case "V-":
  393. await Robot.JogAsync("V", -distance);
  394. break;
  395. case "W+":
  396. await Robot.JogAsync("W", distance);
  397. break;
  398. case "W-":
  399. await Robot.JogAsync("W", -distance);
  400. break;
  401. default:
  402. break;
  403. }
  404. Robot.Timeout = 5000;
  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. /// <param name="obj"></param>
  417. async void ExecuteExecuteMotion(object parameter)
  418. {
  419. if (parameter == null)
  420. {
  421. return;
  422. }
  423. if (SelectedPoint == null)
  424. {
  425. return;
  426. }
  427. var values = (object[])parameter;
  428. var point = new RPoint()
  429. {
  430. X = SelectedPoint.X_Position,
  431. Y = SelectedPoint.Y_Position,
  432. Z = SelectedPoint.Z_Position_Start,
  433. U = SelectedPoint.U_Position,
  434. V = SelectedPoint.R_Position,
  435. };
  436. MotionCommand motionCmd = (MotionCommand)values[1];
  437. if (Robot == null || !Robot.IsConnected) return;
  438. try
  439. {
  440. Robot.Timeout = 6000000;
  441. switch (motionCmd)
  442. {
  443. case MotionCommand.Go:
  444. await Robot.GoAsync(point);
  445. break;
  446. case MotionCommand.Jump:
  447. await Robot.JumpAsync(point, 0);
  448. break;
  449. case MotionCommand.Move:
  450. await Robot.MoveAsync(point);
  451. break;
  452. }
  453. Robot.Timeout = 5000;
  454. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  455. }
  456. catch (Exception ex)
  457. {
  458. LogHelper.WriteLogError("机器人点位-执行点位时出错", ex);
  459. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  460. }
  461. }
  462. /// <summary>
  463. /// 示教点位
  464. /// </summary>
  465. /// <param name="point"></param>
  466. async void ExecuteTechPointCommand()
  467. {
  468. if (Robot == null || !Robot.IsConnected) return;
  469. if (SelectedPoint == null) return;
  470. try
  471. {
  472. //弹窗用户是否确认要示教该点位
  473. if (MessageBox.Show($"确认要示教点位 {SelectedPoint.Number} 吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  474. {
  475. return;
  476. }
  477. var curpos = await Robot.GetRobotPosAsync();
  478. SelectedPoint.X_Position = curpos.X;
  479. SelectedPoint.Y_Position = curpos.Y;
  480. SelectedPoint.Z_Position_Start = curpos.Z;
  481. SelectedPoint.U_Position = curpos.U;
  482. SelectedPoint.R_Position = curpos.V;
  483. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  484. }
  485. catch (Exception ex)
  486. {
  487. LogHelper.WriteLogError("机器人点位-示教点位时出错", ex);
  488. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  489. }
  490. }
  491. void ExecuteLoadedCommand()
  492. {
  493. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  494. {
  495. IsAllowEdit = false;
  496. }
  497. else
  498. {
  499. IsAllowEdit = true;
  500. }
  501. }
  502. /// <summary>
  503. /// 电机
  504. /// </summary>
  505. /// <param name="obj"></param>
  506. private async void ExecuteMotorCommand(string obj)
  507. {
  508. if (Robot == null || !Robot.IsConnected) return;
  509. try
  510. {
  511. await Robot.MotorAsync(Convert.ToBoolean(obj));
  512. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  513. }
  514. catch (Exception ex)
  515. {
  516. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  517. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  518. }
  519. }
  520. /// <summary>
  521. /// 重置
  522. /// </summary>
  523. private async void ExecuteResetCommand()
  524. {
  525. if (Robot == null || !Robot.IsConnected) return;
  526. try
  527. {
  528. await Robot.ResetAsync();
  529. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  530. }
  531. catch (Exception ex)
  532. {
  533. LogHelper.WriteLogError("重置机器人时出错!", ex);
  534. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  535. }
  536. }
  537. /// <summary>
  538. /// 锁定刹车
  539. /// </summary>
  540. private async void ExecuteSLockCommand()
  541. {
  542. if (Robot == null || !Robot.IsConnected) return;
  543. try
  544. {
  545. await Robot.SLockAsync();
  546. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  547. }
  548. catch (Exception ex)
  549. {
  550. LogHelper.WriteLogError("机器人锁定刹车时出错!", ex);
  551. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  552. }
  553. }
  554. /// <summary>
  555. /// 释放刹车
  556. /// </summary>
  557. private async void ExecutesFreeCommand()
  558. {
  559. if (Robot == null || !Robot.IsConnected) return;
  560. try
  561. {
  562. await Robot.SFreeAsync();
  563. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  564. }
  565. catch (Exception ex)
  566. {
  567. LogHelper.WriteLogError("机器人释放刹车时出错!", ex);
  568. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  569. }
  570. }
  571. private bool CanExecute
  572. {
  573. get
  574. {
  575. // 命令可执行仅当机器人可执行且已选择点位
  576. if (Robot != null && Robot.IsConnected && Robot.CanExecute)
  577. {
  578. return true;
  579. }
  580. return false;
  581. }
  582. }
  583. //是否可以示教或者执行点位
  584. private bool CanExecutePointCommand
  585. {
  586. get
  587. {
  588. if (Robot != null && Robot.IsConnected && Robot.CanExecute)
  589. {
  590. return SelectedPointInDataGrid != null;
  591. }
  592. return false;
  593. }
  594. }
  595. /// <summary>
  596. /// 点位表切换时
  597. /// </summary>
  598. void ExecuteSelectionChangedCommand()
  599. {
  600. switch (SelectedIndex)
  601. {
  602. case 0:
  603. Points = SelectProduct.PickCameraPoints;
  604. PickPoints = SelectProduct.PickPoints;
  605. break;
  606. case 1:
  607. Points = SelectProduct.PickPoints;
  608. break;
  609. case 2:
  610. Points = SelectProduct.SecPosCameraPoints;
  611. break;
  612. case 3:
  613. Points = SelectProduct.ScrewCameraPoints;
  614. break;
  615. case 4:
  616. Points = SelectProduct.ScrewPoints;
  617. break;
  618. case 5:
  619. Points = SelectProduct.CheckCameraPoints;
  620. break;
  621. default:
  622. break;
  623. }
  624. }
  625. /// <summary>
  626. /// 增加点位
  627. /// </summary>
  628. void ExecuteAddPointCommand()
  629. {
  630. //添加点位
  631. Points.Add(new PlcPoint()
  632. {
  633. Number = Points.Count + 1,
  634. X_Velocity = 100,
  635. Y_Velocity = 100,
  636. Z_Velocity_Start = 100,
  637. Z_Velocity_Stop = 100,
  638. U_Velocity = 100,
  639. R_Velocity = 100,
  640. });
  641. // update commands
  642. _MovePointUpCommand?.RaiseCanExecuteChanged();
  643. _MovePointDownCommand?.RaiseCanExecuteChanged();
  644. }
  645. /// <summary>
  646. /// 移除点位
  647. /// </summary>
  648. void ExecuteDeletePointCommand()
  649. {
  650. if (SelectedPoint == null)
  651. {
  652. return;
  653. }
  654. // 确认删除
  655. if (MessageBox.Show("确认要删除选中的点位吗?", "删除点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  656. {
  657. return;
  658. }
  659. int removedIndex = Points.IndexOf(SelectedPoint);
  660. Points.Remove(SelectedPoint);
  661. //重新编号
  662. for (int i = 0; i < Points.Count; i++)
  663. {
  664. Points[i].Number = i + 1;
  665. }
  666. // adjust selection
  667. if (Points.Count > 0)
  668. {
  669. int newIndex = Math.Min(removedIndex, Points.Count - 1);
  670. SelectedPointInDataGrid = Points[newIndex];
  671. }
  672. else
  673. {
  674. SelectedPointInDataGrid = null;
  675. }
  676. // update commands
  677. _MovePointUpCommand?.RaiseCanExecuteChanged();
  678. _MovePointDownCommand?.RaiseCanExecuteChanged();
  679. }
  680. bool CanExecuteDeletePointCommand()
  681. {
  682. return SelectedPointInDataGrid != null;
  683. }
  684. private bool CanMovePointUp()
  685. {
  686. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) > 0;
  687. }
  688. private bool CanMovePointDown()
  689. {
  690. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) >= 0 && Points.IndexOf(SelectedPointInDataGrid) < Points.Count - 1;
  691. }
  692. /// <summary>
  693. /// 向上移动点位
  694. /// </summary>
  695. void ExecuteMovePointUpCommand()
  696. {
  697. if (SelectedPointInDataGrid == null) return;
  698. // 确认移动
  699. if (MessageBox.Show("确认将选中点位上移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  700. {
  701. return;
  702. }
  703. int idx = Points.IndexOf(SelectedPointInDataGrid);
  704. if (idx <= 0) return;
  705. var item = SelectedPointInDataGrid;
  706. Points.Move(idx, idx - 1);
  707. // renumber
  708. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  709. SelectedPointInDataGrid = item;
  710. _MovePointUpCommand?.RaiseCanExecuteChanged();
  711. _MovePointDownCommand?.RaiseCanExecuteChanged();
  712. }
  713. /// <summary>
  714. /// 向下移动点位
  715. /// </summary>
  716. void ExecuteMovePointDownCommand()
  717. {
  718. if (SelectedPointInDataGrid == null) return;
  719. // 确认移动
  720. if (MessageBox.Show("确认将选中点位下移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  721. {
  722. return;
  723. }
  724. int idx = Points.IndexOf(SelectedPointInDataGrid);
  725. if (idx < 0 || idx >= Points.Count - 1) return;
  726. var item = SelectedPointInDataGrid;
  727. Points.Move(idx, idx + 1);
  728. // renumber
  729. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  730. SelectedPointInDataGrid = item;
  731. _MovePointUpCommand?.RaiseCanExecuteChanged();
  732. _MovePointDownCommand?.RaiseCanExecuteChanged();
  733. }
  734. /// <summary>
  735. /// CAD导入点位
  736. /// </summary>
  737. async void ExecuteImportCadPointCommand()
  738. {
  739. try
  740. {
  741. var view = new DxfView();
  742. //show the dialog
  743. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  744. if (result != null)
  745. {
  746. var cadPoints = view.Points;
  747. if (cadPoints != null && cadPoints.Count > 0)
  748. {
  749. //要导入的开始点编号
  750. int startNumber = view.StartIndex;
  751. //弹窗提示用户是否确认要导入从该编号开始的点位,点位数量为cadPoints.Count
  752. if (MessageBox.Show($"确认要从点位编号 {startNumber} 开始导入 {cadPoints.Count} 个点位吗?", "导入点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  753. {
  754. return;
  755. }
  756. //导入点位
  757. for (int i = 0; i < cadPoints.Count; i++)
  758. {
  759. //如果点位编号已存在,则更新该点位,否则新增点位,只更新XY坐标
  760. var point = cadPoints[i];
  761. var existingPoint = Points.FirstOrDefault(p => p.Number == startNumber + i);
  762. if (existingPoint != null)
  763. {
  764. existingPoint.X_Position = point.X;
  765. existingPoint.Y_Position = point.Y;
  766. }
  767. else
  768. {
  769. Points.Add(new PlcPoint()
  770. {
  771. Number = startNumber + i,
  772. X_Position = point.X,
  773. Y_Position = point.Y,
  774. X_Velocity = 100,
  775. Y_Velocity = 100,
  776. Z_Velocity_Start = 100,
  777. Z_Velocity_Stop = 100,
  778. U_Velocity = 100,
  779. R_Velocity = 100,
  780. });
  781. }
  782. }
  783. //对所有的点位重新编号,确保编号连续
  784. var sortedPoints = Points.OrderBy(p => p.Number).ToList();
  785. Points.Clear();
  786. for (int i = 0; i < sortedPoints.Count; i++)
  787. {
  788. sortedPoints[i].Number = i + 1;
  789. Points.Add(sortedPoints[i]);
  790. }
  791. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  792. }
  793. }
  794. }
  795. catch (Exception ex)
  796. {
  797. LogHelper.WriteLogError("导入CAD点位时出错!", ex);
  798. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  799. }
  800. }
  801. /// <summary>
  802. /// 导入锁付拍照引导的产品坐标系中的点位
  803. /// </summary>
  804. async void ExecuteImportCadLockCameraPointCommand()
  805. {
  806. try
  807. {
  808. var view = new DxfView();
  809. //show the dialog
  810. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  811. if (result != null)
  812. {
  813. var cadPoints = view.Points;
  814. if (cadPoints != null && cadPoints.Count == 2)
  815. {
  816. //弹窗提示用户是否确认要导入锁付拍照引导的起始点和结束点
  817. if (MessageBox.Show($"确认要导入锁付拍照引导的起始点和结束点吗?", "导入点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  818. {
  819. return;
  820. }
  821. //导入点位
  822. //起始点
  823. var startPoint = cadPoints[0];
  824. SelectProduct.ScrewCameraLocalPos1.X_Position = startPoint.X;
  825. SelectProduct.ScrewCameraLocalPos1.Y_Position = startPoint.Y;
  826. //结束点
  827. var endPoint = cadPoints[1];
  828. SelectProduct.ScrewCameraLocalPos2.X_Position = endPoint.X;
  829. SelectProduct.ScrewCameraLocalPos2.Y_Position = endPoint.Y;
  830. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  831. }
  832. else
  833. {
  834. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "请导入两个点,分别为锁付拍照引导的起始点和结束点!", Duration = 1 });
  835. }
  836. }
  837. }
  838. catch (Exception ex)
  839. {
  840. LogHelper.WriteLogError("导入CAD点位时出错!", ex);
  841. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  842. }
  843. }
  844. /// <summary>
  845. /// 批量设置锁付点的供料器编号和螺丝程序编号
  846. /// </summary>
  847. void ExecuteApplyLockPointFeederAndScrewProgramCommand()
  848. {
  849. //检查供料器编号和螺丝程序编号是否大于0,如果小于等于0,则提示用户编号是否正确,是否继续写入
  850. if (LockFeederNumber <= 0 || LockScrewProgramNumber <= 0)
  851. {
  852. var result = MessageBox.Show("供料器编号或螺丝程序编号小于等于0,是否继续写入?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  853. if (result != MessageBoxResult.Yes)
  854. {
  855. return;
  856. }
  857. }
  858. //遍历所有点位,设置供料器编号和螺丝程序编号
  859. foreach (var point in Points)
  860. {
  861. point.Feeder = LockFeederNumber;
  862. point.ScrewProNum = LockScrewProgramNumber;
  863. }
  864. }
  865. /// <summary>
  866. /// 批量设置锁付点的速度
  867. /// </summary>
  868. void ExecuteApplyLockPointSpeedCommand()
  869. {
  870. //检查速度是否大于0,如果小于等于0,则提示用户速度是否正确,是否继续写入
  871. if (LockXSpeed <= 0 || LockYSpeed <= 0 || LockZSpeed <= 0 || LockZDescendSpeed <= 0)
  872. {
  873. var result = MessageBox.Show("速度小于等于0,是否继续写入?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  874. if (result != MessageBoxResult.Yes)
  875. {
  876. return;
  877. }
  878. }
  879. //遍历所有点位,设置速度
  880. foreach (var point in Points)
  881. {
  882. point.X_Velocity = LockXSpeed;
  883. point.Y_Velocity = LockYSpeed;
  884. point.Z_Velocity_Start = LockZSpeed;
  885. point.Z_Velocity_Stop = LockZDescendSpeed;
  886. }
  887. }
  888. /// <summary>
  889. /// 批量设置锁付点的Z深度
  890. /// </summary>
  891. void ExecuteApplyLockPointDepthCommand()
  892. {
  893. //检查深度是否大于0,如果小于等于0,则提示用户深度是否正确,是否继续写入
  894. if (LockZDepth <= 0)
  895. {
  896. var result = MessageBox.Show("深度小于等于0,是否继续写入?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  897. if (result != MessageBoxResult.Yes)
  898. {
  899. return;
  900. }
  901. }
  902. //遍历所有点位,设置Z深度
  903. foreach (var point in Points)
  904. {
  905. point.Z_Position_Stop = point.Z_Position_Start - LockZDepth;
  906. }
  907. }
  908. /// <summary>
  909. /// 批量设置锁付点的起始Z坐标
  910. /// </summary>
  911. void ExecuteApplyLockPointStartZCommand()
  912. {
  913. //检查起始Z坐标是否小于0,如果大于等于0,则提示用户坐标是否正确,是否继续写入
  914. if (LockPointStartZ >= 0)
  915. {
  916. var result = MessageBox.Show("起始Z坐标大于等于0,是否继续写入?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  917. if (result != MessageBoxResult.Yes)
  918. {
  919. return;
  920. }
  921. }
  922. //遍历所有点位,设置起始Z坐标
  923. foreach (var point in Points)
  924. {
  925. point.Z_Position_Start = LockPointStartZ;
  926. }
  927. //检查终点坐标是否小于等于起始坐标,如果不是,则提示用户终点坐标大于等于起始坐标,用户需要去调整终点坐标
  928. foreach (var point in Points)
  929. {
  930. if (point.Z_Position_Stop >= point.Z_Position_Start)
  931. {
  932. MessageBox.Show($"点位编号 {point.Number} 的终点坐标大于等于起始坐标,请调整终点坐标!", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
  933. }
  934. }
  935. }
  936. /// <summary>
  937. /// 通过相机自动矫正锁附点位
  938. /// </summary>
  939. async void ExecuteAutoCorrectLockPoint()
  940. {
  941. try
  942. {
  943. //弹窗提示用户是否确认要通过相机自动矫正锁附点位
  944. var resultConfirm = MessageBox.Show("确认要通过相机自动矫正锁附点位吗?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  945. if (resultConfirm != MessageBoxResult.Yes)
  946. {
  947. return;
  948. }
  949. //获取螺丝相机拍照点位
  950. var screwCameraPoint = SelectProduct.ScrewCameraPoints;
  951. //获取相机拍照点对应的LocalPos
  952. var screwCameraLocalPos1 = SelectProduct.ScrewCameraLocalPos1;
  953. var screwCameraLocalPos2 = SelectProduct.ScrewCameraLocalPos2;
  954. //获取螺丝锁付点位
  955. var screwLockPoints = SelectProduct.ScrewPoints;
  956. //检查screwLockPoints中是否有和screwCameraLocalPos1的XY坐标相同的点位
  957. var matchingPoint = screwLockPoints.FirstOrDefault(p => p.X_Position == screwCameraLocalPos1.X_Position && p.Y_Position == screwCameraLocalPos1.Y_Position);
  958. if (matchingPoint != null)
  959. {
  960. //检查两个点位的Z是否一致?如果不一致,则提示用户锁付点位中有和锁付拍照引导起始点Local坐标相同的点位,且Z坐标不一致,是否将锁付拍照引导起始点Local坐标的Z轴坐标更新为锁付点位的Z坐标
  961. if (matchingPoint.Z_Position_Start != screwCameraLocalPos1.Z_Position_Start)
  962. {
  963. var resultUpdateZ = MessageBox.Show("锁付点位中有和锁付拍照引导起始点Local坐标相同的点位,且Z坐标不一致,是否将锁付拍照引导起始点Local坐标的Z轴坐标更新为锁付点位的Z坐标?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  964. if (resultUpdateZ == MessageBoxResult.Yes)
  965. {
  966. screwCameraLocalPos1.Z_Position_Start = matchingPoint.Z_Position_Start;
  967. }
  968. }
  969. }
  970. //检查screwLockPoints中是否有和screwCameraLocalPos2的XY坐标相同的点位
  971. matchingPoint = screwLockPoints.FirstOrDefault(p => p.X_Position == screwCameraLocalPos2.X_Position && p.Y_Position == screwCameraLocalPos2.Y_Position);
  972. if (matchingPoint != null)
  973. {
  974. //检查两个点位的Z是否一致?如果不一致,则提示用户锁付点位中有和锁付拍照引导结束点Local坐标相同的点位,且Z坐标不一致,是否将锁付拍照引导结束点Local坐标的Z轴坐标更新为锁付点位的Z坐标
  975. if (matchingPoint.Z_Position_Start != screwCameraLocalPos2.Z_Position_Start)
  976. {
  977. var resultUpdateZ = MessageBox.Show("锁付点位中有和锁付拍照引导结束点Local坐标相同的点位,且Z坐标不一致,是否将锁付拍照引导结束点Local坐标的Z轴坐标更新为锁付点位的Z坐标?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  978. if (resultUpdateZ == MessageBoxResult.Yes)
  979. {
  980. screwCameraLocalPos2.Z_Position_Start = matchingPoint.Z_Position_Start;
  981. }
  982. }
  983. }
  984. IDialogParameters parameters = new DialogParameters();
  985. parameters.Add("Robot", Robot);
  986. parameters.Add("Points", new List<PlcPoint>(SelectProduct.ScrewPoints.ToArray()));
  987. _dialogService.Show("AutoCorrectLockPoint", parameters, rst =>
  988. {
  989. //对话框关闭之后的回调函数,可以在这解析结果。
  990. ButtonResult result1 = rst.Result;
  991. if (result1 == ButtonResult.OK)
  992. {
  993. var param = rst.Parameters.GetValue<ObservableCollection<PointEx>>("CorrectPoints");
  994. for (int i = 0; i < SelectProduct.ScrewPoints.Count; i++)
  995. {
  996. SelectProduct.ScrewPoints[i].X_Position = param[i].PostX;
  997. SelectProduct.ScrewPoints[i].Y_Position = param[i].PostY;
  998. }
  999. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "完成!", Duration = 1 });
  1000. }
  1001. });
  1002. }
  1003. catch (Exception ex)
  1004. {
  1005. LogHelper.WriteLogError("通过相机自动矫正锁附点位时出错!", ex);
  1006. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1007. }
  1008. }
  1009. #endregion
  1010. #region 继承
  1011. /// <summary>
  1012. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  1013. /// </summary>
  1014. /// <param name="navigationContext"></param>
  1015. /// <param name="continuationCallback"></param>
  1016. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  1017. {
  1018. continuationCallback(true);
  1019. }
  1020. /// <summary>
  1021. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  1022. /// </summary>
  1023. /// <param name="navigationContext"></param>
  1024. /// <exception cref="NotImplementedException"></exception>
  1025. public async void OnNavigatedTo(NavigationContext navigationContext)
  1026. {
  1027. try
  1028. {
  1029. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  1030. SelectedIndex = 0;
  1031. PickPoints = SelectProduct.PickPoints;
  1032. Points = SelectProduct.PickCameraPoints;
  1033. if (Robot == null)
  1034. {
  1035. Robot = _robotService.GetRobotByNumber(1);
  1036. }
  1037. if (Robot != null && Robot.IsConnected)
  1038. {
  1039. this.Distance = _configService.GetRobotStepDistance(Robot.Id);
  1040. Robot.EnterDebugMode = true;
  1041. }
  1042. else if (Robot != null)
  1043. {
  1044. Robot.EnterDebugMode = true;
  1045. }
  1046. ProcedureModels = new ObservableCollection<ProcedureModel>();
  1047. foreach (var item in SelectProduct.CameraProcedures)
  1048. {
  1049. foreach (var item1 in item.ProcedureModels)
  1050. {
  1051. ProcedureModels.Add(item1);
  1052. }
  1053. }
  1054. //对锁付点的批量设置进行初始化
  1055. LockPointStartZ = SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Position_Start ?? 0;
  1056. LockZDepth = SelectProduct.ScrewPoints.FirstOrDefault() != null ? (SelectProduct.ScrewPoints.FirstOrDefault().Z_Position_Start - SelectProduct.ScrewPoints.FirstOrDefault().Z_Position_Stop) : 0;
  1057. LockXSpeed = SelectProduct.ScrewPoints.FirstOrDefault()?.X_Velocity ?? 100;
  1058. LockYSpeed = SelectProduct.ScrewPoints.FirstOrDefault()?.Y_Velocity ?? 100;
  1059. LockZSpeed = SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Velocity_Start ?? 100;
  1060. LockZDescendSpeed = SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Velocity_Stop ?? 100;
  1061. LockFeederNumber = SelectProduct.ScrewPoints.FirstOrDefault()?.Feeder ?? 1;
  1062. LockScrewProgramNumber = SelectProduct.ScrewPoints.FirstOrDefault()?.ScrewProNum ?? 1;
  1063. }
  1064. catch (Exception ex)
  1065. {
  1066. LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
  1067. }
  1068. }
  1069. /// <summary>
  1070. /// 是否允许导航到此视图模型。
  1071. /// </summary>
  1072. /// <param name="navigationContext"></param>
  1073. /// <returns></returns>
  1074. public bool IsNavigationTarget(NavigationContext navigationContext)
  1075. {
  1076. return true;
  1077. }
  1078. /// <summary>
  1079. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  1080. /// </summary>
  1081. /// <param name="navigationContext"></param>
  1082. public void OnNavigatedFrom(NavigationContext navigationContext)
  1083. {
  1084. //if (Robot != null)
  1085. //{
  1086. // Robot.EnterDebugMode = false;
  1087. //}
  1088. }
  1089. #endregion
  1090. #region 取料拍照点位矫正
  1091. private PlcPoint _SelectedCameraForPickPoint;
  1092. public PlcPoint SelectedCameraForPickPoint
  1093. {
  1094. get { return _SelectedCameraForPickPoint; }
  1095. set { SetProperty(ref _SelectedCameraForPickPoint, value); }
  1096. }
  1097. private ObservableCollection<PlcPoint> _PickPoints = new ObservableCollection<PlcPoint>();
  1098. /// <summary>
  1099. /// 取料点位集合
  1100. /// </summary>
  1101. public ObservableCollection<PlcPoint> PickPoints
  1102. {
  1103. get { return _PickPoints; }
  1104. set { SetProperty(ref _PickPoints, value); }
  1105. }
  1106. //
  1107. private PlcPoint _SelectedPickPoint;
  1108. /// <summary>
  1109. /// 选中的取料点位
  1110. /// </summary>
  1111. public PlcPoint SelectedPickPoint
  1112. {
  1113. get { return _SelectedPickPoint; }
  1114. set { SetProperty(ref _SelectedPickPoint, value); }
  1115. }
  1116. private DelegateCommand _AutoCorrectPickPointCommand;
  1117. public DelegateCommand AutoCorrectPickPointCommand =>
  1118. _AutoCorrectPickPointCommand ?? (_AutoCorrectPickPointCommand = new DelegateCommand(ExecuteAutoCorrectPickPointCommand, CanExecuteAutoCorrectPickPointCommand)
  1119. .ObservesProperty(() => SelectedCameraForPickPoint).ObservesProperty(() => SelectedPickPoint).ObservesProperty(() => SelectProcedure).ObservesProperty(() => IsExecutingAutoCorrectPickPoint));
  1120. //
  1121. private bool _IsExecutingAutoCorrectPickPoint = false;
  1122. /// <summary>
  1123. /// 正在执行取料拍照点位矫正
  1124. /// </summary>
  1125. public bool IsExecutingAutoCorrectPickPoint
  1126. {
  1127. get { return _IsExecutingAutoCorrectPickPoint; }
  1128. set { SetProperty(ref _IsExecutingAutoCorrectPickPoint, value); }
  1129. }
  1130. async void ExecuteAutoCorrectPickPointCommand()
  1131. {
  1132. try
  1133. {
  1134. IsExecutingAutoCorrectPickPoint = true;
  1135. if (SelectedCameraForPickPoint == null || SelectedPickPoint == null || SelectProcedure == null)
  1136. {
  1137. return;
  1138. }
  1139. if (Robot == null || !Robot.IsConnected)
  1140. {
  1141. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行取料拍照点位矫正!", Duration = 1 });
  1142. return;
  1143. }
  1144. if (MessageBox.Show("确认要执行取料拍照点位矫正吗?", "取料拍照点位矫正", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1145. {
  1146. return;
  1147. }
  1148. var waiting = new WaitingControl();
  1149. //show the dialog
  1150. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1151. // 1. 控制机器人移动至拍照点位
  1152. var rpoint = new RPoint()
  1153. {
  1154. X = SelectedCameraForPickPoint.X_Position,
  1155. Y = SelectedCameraForPickPoint.Y_Position,
  1156. Z = SelectedCameraForPickPoint.Z_Position_Start,
  1157. U = SelectedCameraForPickPoint.U_Position,
  1158. V = SelectedCameraForPickPoint.R_Position,
  1159. };
  1160. await Robot.CalibMotionAsync(rpoint, 0);
  1161. var _cts = new CancellationTokenSource();
  1162. // 2. 执行拍照并获取识别结果
  1163. var recognitionResult = await _remoteCommandService.ExecutePhotoGetSinglePoint(SelectProcedure, null, new double[] { rpoint.X, rpoint.Y, 0 }, _cts.Token);
  1164. if (!recognitionResult.IsSucceed)
  1165. {
  1166. if (DialogHost.IsDialogOpen("RootDialog"))
  1167. {
  1168. DialogHost.Close("RootDialog");
  1169. }
  1170. await task;
  1171. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料拍照点位矫正失败,识别未成功!", Duration = 1 });
  1172. return;
  1173. }
  1174. // 3. 根据识别结果更新取料点位坐标
  1175. SelectedPickPoint.X_Position = (float)recognitionResult.X;
  1176. SelectedPickPoint.Y_Position = (float)recognitionResult.Y;
  1177. if (DialogHost.IsDialogOpen("RootDialog"))
  1178. {
  1179. DialogHost.Close("RootDialog");
  1180. }
  1181. await task;
  1182. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料拍照点位矫正完成!", Duration = 0.4 });
  1183. }
  1184. catch (Exception ex)
  1185. {
  1186. LogHelper.WriteLogError("取料拍照点位矫正时出错!", ex);
  1187. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1188. }
  1189. finally
  1190. {
  1191. IsExecutingAutoCorrectPickPoint = false;
  1192. }
  1193. }
  1194. bool CanExecuteAutoCorrectPickPointCommand()
  1195. {
  1196. return SelectedCameraForPickPoint != null && SelectedPickPoint != null && SelectProcedure != null && !IsExecutingAutoCorrectPickPoint;
  1197. }
  1198. #endregion
  1199. private bool _IsAllowEdit = false;
  1200. public bool IsAllowEdit
  1201. {
  1202. get { return _IsAllowEdit; }
  1203. set { SetProperty(ref _IsAllowEdit, value); }
  1204. }
  1205. private void LoginChange(Models.User user)
  1206. {
  1207. if (user.userPart == Enums.UserPart.Operator)
  1208. {
  1209. IsAllowEdit = false;
  1210. }
  1211. else
  1212. {
  1213. IsAllowEdit = true;
  1214. }
  1215. }
  1216. }
  1217. }