PlcPointParamsViewModel.cs 54 KB

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