PlcPointParamsViewModel.cs 54 KB

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