PlcPointParamsViewModel.cs 45 KB

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