PlcPointParamsViewModel.cs 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. using MaterialDesignThemes.Wpf;
  2. using MathNet.Numerics.LinearAlgebra;
  3. using NPOI.SS.Formula.Functions;
  4. using Prism.Commands;
  5. using Prism.Events;
  6. using Prism.Ioc;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using Prism.Services.Dialogs;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.Drawing;
  14. using System.Linq;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using TeamAAS_VP.Controls;
  19. using TeamAAS_VP.Core;
  20. using TeamAAS_VP.Core.PLCs;
  21. using TeamAAS_VP.DxfModule;
  22. using TeamAAS_VP.Enums;
  23. using TeamAAS_VP.Events;
  24. using TeamAAS_VP.Interfaces;
  25. using TeamAAS_VP.Models;
  26. using TeamAAS_VP.Models.PLC;
  27. using TeamAAS_VP.Models.Robot;
  28. using TeamAAS_VP.Resources.Languages;
  29. using TeamAAS_VP.Services;
  30. using TeamAAS_VP.Views.Product;
  31. using static TeamAAS_VP.ViewModels.Product.AutoCorrectLockPointViewModel;
  32. namespace TeamAAS_VP.ViewModels.Product
  33. {
  34. public class PlcPointParamsViewModel : BindableBase, IConfirmNavigationRequest
  35. {
  36. IRegionManager _regionManager;
  37. IEventAggregator _eventAggregator;
  38. IContainerProvider _container;
  39. IDialogService _dialogService;
  40. IConfigService _configService;
  41. IRobotService _robotService;
  42. ISystemDatabaseService _systemDatabaseService;
  43. IRemoteCommandService _remoteCommandService;
  44. ICalibrationService _calibrationService;
  45. CoordinateTransformer local;
  46. #region 属性
  47. private ProductModel _SelectProduct;
  48. /// <summary>
  49. /// 选中的产品
  50. /// </summary>
  51. public ProductModel SelectProduct
  52. {
  53. get { return _SelectProduct; }
  54. set { SetProperty(ref _SelectProduct, value); }
  55. }
  56. private PlcPoint _SelectedPoint;
  57. public PlcPoint SelectedPoint
  58. {
  59. get { return _SelectedPoint; }
  60. set { SetProperty(ref _SelectedPoint, value); }
  61. }
  62. private PlcPoint _selectedPointInDataGrid;
  63. public PlcPoint SelectedPointInDataGrid
  64. {
  65. get { return _selectedPointInDataGrid; }
  66. set
  67. {
  68. if (SetProperty(ref _selectedPointInDataGrid, value))
  69. {
  70. SelectedPointInComboBox = value;
  71. // update command availability
  72. _MovePointUpCommand?.RaiseCanExecuteChanged();
  73. _MovePointDownCommand?.RaiseCanExecuteChanged();
  74. // 更新依赖可执行状态的命令
  75. _TechPointCommand?.RaiseCanExecuteChanged();
  76. _ExecuteMotion?.RaiseCanExecuteChanged();
  77. }
  78. // 可以同步到 SelectPoint 属性
  79. SelectedPoint = value;
  80. }
  81. }
  82. private PlcPoint _selectedPointInComboBox;
  83. public PlcPoint SelectedPointInComboBox
  84. {
  85. get { return _selectedPointInComboBox; }
  86. set
  87. {
  88. if (SetProperty(ref _selectedPointInComboBox, value))
  89. {
  90. SelectedPointInDataGrid = value;
  91. }
  92. // 可以同步到 SelectPoint 属性
  93. SelectedPoint = value;
  94. }
  95. }
  96. private ObservableCollection<PlcPoint> _Points = new ObservableCollection<PlcPoint>();
  97. /// <summary>
  98. /// 点位集合
  99. /// </summary>
  100. public ObservableCollection<PlcPoint> Points
  101. {
  102. get { return _Points; }
  103. set { SetProperty(ref _Points, value); }
  104. }
  105. private MotionCommand _SelectMotionCommand;
  106. /// <summary>
  107. /// 选中的运动指令
  108. /// </summary>
  109. public MotionCommand SelectMotionCommand
  110. {
  111. get { return _SelectMotionCommand; }
  112. set { SetProperty(ref _SelectMotionCommand, value); }
  113. }
  114. public Management management { get; set; }
  115. private IRobot _Robot;
  116. public IRobot Robot
  117. {
  118. get { return _Robot; }
  119. set
  120. {
  121. SetProperty(ref _Robot, value);
  122. }
  123. }
  124. private int _SelectedIndex;
  125. public int SelectedIndex
  126. {
  127. get { return _SelectedIndex; }
  128. set { SetProperty(ref _SelectedIndex, value); }
  129. }
  130. private double _Distance = 1;
  131. public double Distance
  132. {
  133. get { return _Distance; }
  134. set
  135. {
  136. SetProperty(ref _Distance, value);
  137. try
  138. {
  139. if (Robot == null)
  140. {
  141. return;
  142. }
  143. _configService.UpdateRobotStepDistance(Robot.Id, value);
  144. }
  145. catch (Exception)
  146. {
  147. }
  148. }
  149. }
  150. private ObservableCollection<ProcedureModel> _ProcedureModels;
  151. /// <summary>
  152. /// 流程集合
  153. /// </summary>
  154. public ObservableCollection<ProcedureModel> ProcedureModels
  155. {
  156. get { return _ProcedureModels; }
  157. set { SetProperty(ref _ProcedureModels, value); }
  158. }
  159. private ProcedureModel _SelectProcedure;
  160. /// <summary>
  161. /// 选中的流程
  162. /// </summary>
  163. public ProcedureModel SelectProcedure
  164. {
  165. get { return _SelectProcedure; }
  166. set
  167. {
  168. SetProperty(ref _SelectProcedure, value);
  169. }
  170. }
  171. private float _LockPointStartZ;
  172. /// <summary>
  173. /// 批量设置锁付点的起始Z坐标
  174. /// </summary>
  175. public float LockPointStartZ
  176. {
  177. get { return _LockPointStartZ; }
  178. set { SetProperty(ref _LockPointStartZ, value); }
  179. }
  180. private float _LockZDepth;
  181. /// <summary>
  182. /// 批量设置锁付点Z深度
  183. /// </summary>
  184. public float LockZDepth
  185. {
  186. get { return _LockZDepth; }
  187. set { SetProperty(ref _LockZDepth, value); }
  188. }
  189. private float _LockXSpeed;
  190. /// <summary>
  191. /// 批量设置锁付点X轴速度
  192. /// </summary>
  193. public float LockXSpeed
  194. {
  195. get { return _LockXSpeed; }
  196. set { SetProperty(ref _LockXSpeed, value); }
  197. }
  198. private float _LockYSpeed;
  199. /// <summary>
  200. /// 批量设置锁付点Y轴速度
  201. /// </summary>
  202. public float LockYSpeed
  203. {
  204. get { return _LockYSpeed; }
  205. set { SetProperty(ref _LockYSpeed, value); }
  206. }
  207. private float _LockZSpeed;
  208. /// <summary>
  209. /// 批量设置锁付点Z轴速度
  210. /// </summary>
  211. public float LockZSpeed
  212. {
  213. get { return _LockZSpeed; }
  214. set { SetProperty(ref _LockZSpeed, value); }
  215. }
  216. private float _LockZDescendSpeed;
  217. /// <summary>
  218. /// 批量设置锁付点Z轴下降速度
  219. /// </summary>
  220. public float LockZDescendSpeed
  221. {
  222. get { return _LockZDescendSpeed; }
  223. set { SetProperty(ref _LockZDescendSpeed, value); }
  224. }
  225. private Int16 _LockFeederNumber;
  226. /// <summary>
  227. /// 批量设置锁付点供料器编号
  228. /// </summary>
  229. public Int16 LockFeederNumber
  230. {
  231. get { return _LockFeederNumber; }
  232. set { SetProperty(ref _LockFeederNumber, value); }
  233. }
  234. private Int16 _LockScrewProgramNumber;
  235. /// <summary>
  236. /// 批量设置锁付点螺丝程序编号
  237. /// </summary>
  238. public Int16 LockScrewProgramNumber
  239. {
  240. get { return _LockScrewProgramNumber; }
  241. set { SetProperty(ref _LockScrewProgramNumber, value); }
  242. }
  243. #endregion
  244. #region 命令
  245. private DelegateCommand _NextCommand;
  246. public DelegateCommand NextCommand =>
  247. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  248. private DelegateCommand _BackCommand;
  249. public DelegateCommand BackCommand =>
  250. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  251. private DelegateCommand _LoadedCommand;
  252. public DelegateCommand LoadedCommand =>
  253. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  254. private DelegateCommand<object> _ExecuteMotion;
  255. public DelegateCommand<object> ExecuteMotion =>
  256. _ExecuteMotion ?? (_ExecuteMotion = new DelegateCommand<object>(ExecuteExecuteMotion).ObservesCanExecute(() => CanExecutePointCommand).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute).ObservesProperty(() => SelectedPointInDataGrid));
  257. private DelegateCommand<object> _JogCommand;
  258. public DelegateCommand<object> JogCommand =>
  259. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  260. private DelegateCommand _TechPointCommand;
  261. public DelegateCommand TechPointCommand =>
  262. _TechPointCommand ?? (_TechPointCommand = new DelegateCommand(ExecuteTechPointCommand).ObservesCanExecute(() => CanExecutePointCommand).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute).ObservesProperty(() => SelectedPointInDataGrid));
  263. private DelegateCommand<string> _MotorCommand;
  264. public DelegateCommand<string> MotorCommand =>
  265. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  266. private DelegateCommand _ResetCommand;
  267. public DelegateCommand ResetCommand =>
  268. _ResetCommand ?? (_ResetCommand = new DelegateCommand(ExecuteResetCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  269. private DelegateCommand _sFreeCommand;
  270. public DelegateCommand sFreeCommand =>
  271. _sFreeCommand ?? (_sFreeCommand = new DelegateCommand(ExecutesFreeCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  272. private DelegateCommand _SLockCommand;
  273. public DelegateCommand SLockCommand =>
  274. _SLockCommand ?? (_SLockCommand = new DelegateCommand(ExecuteSLockCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  275. private DelegateCommand _SelectionChangedCommand;
  276. public DelegateCommand SelectionChangedCommand =>
  277. _SelectionChangedCommand ?? (_SelectionChangedCommand = new DelegateCommand(ExecuteSelectionChangedCommand));
  278. private DelegateCommand _AddPointCommand;
  279. public DelegateCommand AddPointCommand =>
  280. _AddPointCommand ?? (_AddPointCommand = new DelegateCommand(ExecuteAddPointCommand));
  281. private DelegateCommand _DeletePointCommand;
  282. public DelegateCommand DeletePointCommand =>
  283. _DeletePointCommand ?? (_DeletePointCommand = new DelegateCommand(ExecuteDeletePointCommand, CanExecuteDeletePointCommand).ObservesProperty(() => SelectedPointInDataGrid));
  284. private DelegateCommand _MovePointUpCommand;
  285. public DelegateCommand MovePointUpCommand => _MovePointUpCommand ?? (_MovePointUpCommand = new DelegateCommand(ExecuteMovePointUpCommand, CanMovePointUp));
  286. private DelegateCommand _MovePointDownCommand;
  287. public DelegateCommand MovePointDownCommand => _MovePointDownCommand ?? (_MovePointDownCommand = new DelegateCommand(ExecuteMovePointDownCommand, CanMovePointDown));
  288. private DelegateCommand _ImportCadPointCommand;
  289. public DelegateCommand ImportCadPointCommand =>
  290. _ImportCadPointCommand ?? (_ImportCadPointCommand = new DelegateCommand(ExecuteImportCadPointCommand));
  291. private DelegateCommand _ImportCadLockCameraPointCommand;
  292. public DelegateCommand ImportCadLockCameraPointCommand =>
  293. _ImportCadLockCameraPointCommand ?? (_ImportCadLockCameraPointCommand = new DelegateCommand(ExecuteImportCadLockCameraPointCommand));
  294. private DelegateCommand _ApplyLockPointStartZCommand;
  295. public DelegateCommand ApplyLockPointStartZCommand =>
  296. _ApplyLockPointStartZCommand ?? (_ApplyLockPointStartZCommand = new DelegateCommand(ExecuteApplyLockPointStartZCommand));
  297. private DelegateCommand _ApplyLockPointDepthCommand;
  298. public DelegateCommand ApplyLockPointDepthCommand =>
  299. _ApplyLockPointDepthCommand ?? (_ApplyLockPointDepthCommand = new DelegateCommand(ExecuteApplyLockPointDepthCommand));
  300. private DelegateCommand _ApplyLockPointSpeedCommand;
  301. public DelegateCommand ApplyLockPointSpeedCommand =>
  302. _ApplyLockPointSpeedCommand ?? (_ApplyLockPointSpeedCommand = new DelegateCommand(ExecuteApplyLockPointSpeedCommand));
  303. private DelegateCommand _ApplyLockPointFeederAndScrewProgramCommand;
  304. public DelegateCommand ApplyLockPointFeederAndScrewProgramCommand =>
  305. _ApplyLockPointFeederAndScrewProgramCommand ?? (_ApplyLockPointFeederAndScrewProgramCommand = new DelegateCommand(ExecuteApplyLockPointFeederAndScrewProgramCommand));
  306. private DelegateCommand _AutoCorrectLockPointCommand;
  307. public DelegateCommand AutoCorrectLockPointCommand =>
  308. _AutoCorrectLockPointCommand ?? (_AutoCorrectLockPointCommand = new DelegateCommand(ExecuteAutoCorrectLockPoint));
  309. #endregion
  310. #region 事件
  311. #endregion
  312. public PlcPointParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
  313. IConfigService configService, IRobotService robotService, ISystemDatabaseService systemDatabaseService, IRemoteCommandService remoteCommandService,
  314. ICalibrationService calibrationService)
  315. {
  316. _regionManager = regionManager;
  317. _eventAggregator = ea;
  318. _container = container;
  319. _dialogService = dialogService;
  320. _configService = configService;
  321. _systemDatabaseService = systemDatabaseService;
  322. //订阅权限登录事件
  323. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  324. management = _container.Resolve<Management>();
  325. _robotService = robotService;
  326. _remoteCommandService = remoteCommandService;
  327. _calibrationService = calibrationService;
  328. }
  329. #region 方法
  330. /// <summary>
  331. /// 下一步
  332. /// </summary>
  333. void ExecuteNextCommand()
  334. {
  335. NavigationParameters param = new NavigationParameters();
  336. param.Add("SelectProduct", SelectProduct);
  337. _regionManager.RequestNavigate("ProductRegionContext", "ProductParams", param);
  338. }
  339. /// <summary>
  340. /// 上一步
  341. /// </summary>
  342. void ExecuteBackCommand()
  343. {
  344. NavigationParameters param = new NavigationParameters();
  345. param.Add("SelectProduct", SelectProduct);
  346. param.Add("SelectedProcedure", null);
  347. _regionManager.RequestNavigate("ProductRegionContext", "ProcessBasics", param);
  348. }
  349. /// <summary>
  350. /// 点动
  351. /// </summary>
  352. /// <param name="obj"></param>
  353. private async void ExecuteJogCommand(object obj)
  354. {
  355. if (Robot == null || !Robot.IsConnected) return;
  356. try
  357. {
  358. var items = ((string)obj).Split(':');
  359. //如果移动距离大于10mm,则提示用户确认
  360. double distance = double.Parse(items[1]);
  361. if (Math.Abs(distance) >= 10)
  362. {
  363. if (MessageBox.Show($"确认要点动距离 {distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  364. {
  365. return;
  366. }
  367. }
  368. Robot.Timeout = 6000000;
  369. switch (items[0])
  370. {
  371. case "X+":
  372. await Robot.JogAsync("X", distance);
  373. break;
  374. case "X-":
  375. await Robot.JogAsync("X", -distance);
  376. break;
  377. case "Y+":
  378. await Robot.JogAsync("Y", distance);
  379. break;
  380. case "Y-":
  381. await Robot.JogAsync("Y", -distance);
  382. break;
  383. case "Z+":
  384. await Robot.JogAsync("Z", distance);
  385. break;
  386. case "Z-":
  387. await Robot.JogAsync("Z", -distance);
  388. break;
  389. case "U+":
  390. await Robot.JogAsync("U", distance);
  391. break;
  392. case "U-":
  393. await Robot.JogAsync("U", -distance);
  394. break;
  395. case "V+":
  396. await Robot.JogAsync("V", distance);
  397. break;
  398. case "V-":
  399. await Robot.JogAsync("V", -distance);
  400. break;
  401. case "W+":
  402. await Robot.JogAsync("W", distance);
  403. break;
  404. case "W-":
  405. await Robot.JogAsync("W", -distance);
  406. break;
  407. default:
  408. break;
  409. }
  410. Robot.Timeout = 5000;
  411. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  412. }
  413. catch (Exception ex)
  414. {
  415. LogHelper.WriteLogError("机器人点位-机器人点动时出错", ex);
  416. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  417. }
  418. }
  419. /// <summary>
  420. /// 执行运动
  421. /// </summary>
  422. /// <param name="obj"></param>
  423. async void ExecuteExecuteMotion(object parameter)
  424. {
  425. if (parameter == null)
  426. {
  427. return;
  428. }
  429. if (SelectedPoint == null)
  430. {
  431. return;
  432. }
  433. var values = (object[])parameter;
  434. var point = new RPoint()
  435. {
  436. X = SelectedPoint.X_Position,
  437. Y = SelectedPoint.Y_Position,
  438. Z = SelectedPoint.Z_Position_Start,
  439. U = SelectedPoint.U_Position,
  440. V = SelectedPoint.R_Position,
  441. };
  442. MotionCommand motionCmd = (MotionCommand)values[1];
  443. if (Robot == null || !Robot.IsConnected) return;
  444. try
  445. {
  446. if (SelectedIndex == 4)
  447. {
  448. await ExecuteAutoCorrectLockPointAndExecuteCommand(SelectedPoint);
  449. }
  450. else
  451. {
  452. //确认要执行该点位的该运动指令吗
  453. if (MessageBox.Show($"确认要执行点位 {SelectedPoint.Number} 的 {motionCmd} 指令吗?", "执行点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  454. {
  455. return;
  456. }
  457. //Robot.Timeout = 6000000;
  458. switch (motionCmd)
  459. {
  460. case MotionCommand.Go:
  461. await Robot.GoAsync(point);
  462. break;
  463. case MotionCommand.Jump:
  464. await Robot.JumpAsync(point, 0);
  465. break;
  466. case MotionCommand.Move:
  467. await Robot.MoveAsync(point);
  468. break;
  469. }
  470. //Robot.Timeout = 5000;
  471. }
  472. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  473. }
  474. catch (Exception ex)
  475. {
  476. LogHelper.WriteLogError("机器人点位-执行点位时出错", ex);
  477. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  478. }
  479. }
  480. /// <summary>
  481. /// 示教点位
  482. /// </summary>
  483. /// <param name="point"></param>
  484. async void ExecuteTechPointCommand()
  485. {
  486. if (Robot == null || !Robot.IsConnected) return;
  487. if (SelectedPoint == null) return;
  488. try
  489. {
  490. //如果当前的选项卡处于锁付点位,则提示用户当前模式不允许进行示教
  491. if (SelectedIndex == 4)
  492. {
  493. MessageBox.Show("当前处于锁付点位模式,不允许进行示教操作!", "示教点位", MessageBoxButton.OK, MessageBoxImage.Warning);
  494. return;
  495. }
  496. //弹窗用户是否确认要示教该点位
  497. if (MessageBox.Show($"确认要示教点位 {SelectedPoint.Number} 吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  498. {
  499. return;
  500. }
  501. var curpos = await Robot.GetRobotPosAsync();
  502. SelectedPoint.X_Position = curpos.X;
  503. SelectedPoint.Y_Position = curpos.Y;
  504. SelectedPoint.Z_Position_Start = curpos.Z;
  505. SelectedPoint.U_Position = curpos.U;
  506. SelectedPoint.R_Position = curpos.V;
  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. void ExecuteLoadedCommand()
  516. {
  517. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  518. {
  519. IsAllowEdit = false;
  520. }
  521. else
  522. {
  523. IsAllowEdit = true;
  524. }
  525. }
  526. /// <summary>
  527. /// 电机
  528. /// </summary>
  529. /// <param name="obj"></param>
  530. private async void ExecuteMotorCommand(string obj)
  531. {
  532. if (Robot == null || !Robot.IsConnected) return;
  533. try
  534. {
  535. await Robot.MotorAsync(Convert.ToBoolean(obj));
  536. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  537. }
  538. catch (Exception ex)
  539. {
  540. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  541. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  542. }
  543. }
  544. /// <summary>
  545. /// 重置
  546. /// </summary>
  547. private async void ExecuteResetCommand()
  548. {
  549. if (Robot == null || !Robot.IsConnected) return;
  550. try
  551. {
  552. await Robot.ResetAsync();
  553. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  554. }
  555. catch (Exception ex)
  556. {
  557. LogHelper.WriteLogError("重置机器人时出错!", ex);
  558. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  559. }
  560. }
  561. /// <summary>
  562. /// 锁定刹车
  563. /// </summary>
  564. private async void ExecuteSLockCommand()
  565. {
  566. if (Robot == null || !Robot.IsConnected) return;
  567. try
  568. {
  569. await Robot.SLockAsync();
  570. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  571. }
  572. catch (Exception ex)
  573. {
  574. LogHelper.WriteLogError("机器人锁定刹车时出错!", ex);
  575. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  576. }
  577. }
  578. /// <summary>
  579. /// 释放刹车
  580. /// </summary>
  581. private async void ExecutesFreeCommand()
  582. {
  583. if (Robot == null || !Robot.IsConnected) return;
  584. try
  585. {
  586. await Robot.SFreeAsync();
  587. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  588. }
  589. catch (Exception ex)
  590. {
  591. LogHelper.WriteLogError("机器人释放刹车时出错!", ex);
  592. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  593. }
  594. }
  595. private bool CanExecute
  596. {
  597. get
  598. {
  599. // 命令可执行仅当机器人可执行且已选择点位
  600. if (Robot != null && Robot.IsConnected && Robot.CanExecute)
  601. {
  602. return true;
  603. }
  604. return false;
  605. }
  606. }
  607. //是否可以示教或者执行点位
  608. private bool CanExecutePointCommand
  609. {
  610. get
  611. {
  612. if (Robot != null && Robot.IsConnected && Robot.CanExecute)
  613. {
  614. return SelectedPointInDataGrid != null;
  615. }
  616. return false;
  617. }
  618. }
  619. /// <summary>
  620. /// 点位表切换时
  621. /// </summary>
  622. void ExecuteSelectionChangedCommand()
  623. {
  624. switch (SelectedIndex)
  625. {
  626. case 0:
  627. Points = SelectProduct.PickCameraPoints;
  628. PickPoints = SelectProduct.PickPoints;
  629. break;
  630. case 1:
  631. Points = SelectProduct.PickPoints;
  632. break;
  633. case 2:
  634. Points = SelectProduct.SecPosCameraPoints;
  635. break;
  636. case 3:
  637. Points = SelectProduct.ScrewCameraPoints;
  638. break;
  639. case 4:
  640. Points = SelectProduct.ScrewPoints;
  641. break;
  642. case 5:
  643. Points = SelectProduct.CheckCameraPoints;
  644. break;
  645. default:
  646. break;
  647. }
  648. }
  649. /// <summary>
  650. /// 增加点位
  651. /// </summary>
  652. void ExecuteAddPointCommand()
  653. {
  654. //添加点位
  655. Points.Add(new PlcPoint()
  656. {
  657. Number = Points.Count + 1,
  658. X_Velocity = 100,
  659. Y_Velocity = 100,
  660. Z_Velocity_Start = 100,
  661. Z_Velocity_Stop = 100,
  662. U_Velocity = 100,
  663. R_Velocity = 100,
  664. });
  665. // update commands
  666. _MovePointUpCommand?.RaiseCanExecuteChanged();
  667. _MovePointDownCommand?.RaiseCanExecuteChanged();
  668. }
  669. /// <summary>
  670. /// 移除点位
  671. /// </summary>
  672. void ExecuteDeletePointCommand()
  673. {
  674. if (SelectedPoint == null)
  675. {
  676. return;
  677. }
  678. // 确认删除
  679. if (MessageBox.Show("确认要删除选中的点位吗?", "删除点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  680. {
  681. return;
  682. }
  683. int removedIndex = Points.IndexOf(SelectedPoint);
  684. Points.Remove(SelectedPoint);
  685. //重新编号
  686. for (int i = 0; i < Points.Count; i++)
  687. {
  688. Points[i].Number = i + 1;
  689. }
  690. // adjust selection
  691. if (Points.Count > 0)
  692. {
  693. int newIndex = Math.Min(removedIndex, Points.Count - 1);
  694. SelectedPointInDataGrid = Points[newIndex];
  695. }
  696. else
  697. {
  698. SelectedPointInDataGrid = null;
  699. }
  700. // update commands
  701. _MovePointUpCommand?.RaiseCanExecuteChanged();
  702. _MovePointDownCommand?.RaiseCanExecuteChanged();
  703. }
  704. bool CanExecuteDeletePointCommand()
  705. {
  706. return SelectedPointInDataGrid != null;
  707. }
  708. private bool CanMovePointUp()
  709. {
  710. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) > 0;
  711. }
  712. private bool CanMovePointDown()
  713. {
  714. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) >= 0 && Points.IndexOf(SelectedPointInDataGrid) < Points.Count - 1;
  715. }
  716. /// <summary>
  717. /// 向上移动点位
  718. /// </summary>
  719. void ExecuteMovePointUpCommand()
  720. {
  721. if (SelectedPointInDataGrid == null) return;
  722. // 确认移动
  723. if (MessageBox.Show("确认将选中点位上移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  724. {
  725. return;
  726. }
  727. int idx = Points.IndexOf(SelectedPointInDataGrid);
  728. if (idx <= 0) return;
  729. var item = SelectedPointInDataGrid;
  730. Points.Move(idx, idx - 1);
  731. // renumber
  732. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  733. SelectedPointInDataGrid = item;
  734. _MovePointUpCommand?.RaiseCanExecuteChanged();
  735. _MovePointDownCommand?.RaiseCanExecuteChanged();
  736. }
  737. /// <summary>
  738. /// 向下移动点位
  739. /// </summary>
  740. void ExecuteMovePointDownCommand()
  741. {
  742. if (SelectedPointInDataGrid == null) return;
  743. // 确认移动
  744. if (MessageBox.Show("确认将选中点位下移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  745. {
  746. return;
  747. }
  748. int idx = Points.IndexOf(SelectedPointInDataGrid);
  749. if (idx < 0 || idx >= Points.Count - 1) return;
  750. var item = SelectedPointInDataGrid;
  751. Points.Move(idx, idx + 1);
  752. // renumber
  753. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  754. SelectedPointInDataGrid = item;
  755. _MovePointUpCommand?.RaiseCanExecuteChanged();
  756. _MovePointDownCommand?.RaiseCanExecuteChanged();
  757. }
  758. /// <summary>
  759. /// CAD导入点位
  760. /// </summary>
  761. async void ExecuteImportCadPointCommand()
  762. {
  763. try
  764. {
  765. var view = new DxfView();
  766. //show the dialog
  767. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  768. if (result != null)
  769. {
  770. var cadPoints = view.Points;
  771. if (cadPoints != null && cadPoints.Count > 0)
  772. {
  773. //要导入的开始点编号
  774. int startNumber = view.StartIndex;
  775. //弹窗提示用户是否确认要导入从该编号开始的点位,点位数量为cadPoints.Count
  776. if (MessageBox.Show($"确认要从点位编号 {startNumber} 开始导入 {cadPoints.Count} 个点位吗?", "导入点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  777. {
  778. return;
  779. }
  780. //导入点位
  781. for (int i = 0; i < cadPoints.Count; i++)
  782. {
  783. //如果点位编号已存在,则更新该点位,否则新增点位,只更新XY坐标
  784. var point = cadPoints[i];
  785. var existingPoint = Points.FirstOrDefault(p => p.Number == startNumber + i);
  786. if (existingPoint != null)
  787. {
  788. existingPoint.X_Position = point.X;
  789. existingPoint.Y_Position = point.Y;
  790. }
  791. else
  792. {
  793. Points.Add(new PlcPoint()
  794. {
  795. Number = startNumber + i,
  796. X_Position = point.X,
  797. Y_Position = point.Y,
  798. X_Velocity = 100,
  799. Y_Velocity = 100,
  800. Z_Velocity_Start = 100,
  801. Z_Velocity_Stop = 100,
  802. U_Velocity = 100,
  803. R_Velocity = 100,
  804. });
  805. }
  806. }
  807. //对所有的点位重新编号,确保编号连续
  808. var sortedPoints = Points.OrderBy(p => p.Number).ToList();
  809. Points.Clear();
  810. for (int i = 0; i < sortedPoints.Count; i++)
  811. {
  812. sortedPoints[i].Number = i + 1;
  813. Points.Add(sortedPoints[i]);
  814. }
  815. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  816. }
  817. }
  818. }
  819. catch (Exception ex)
  820. {
  821. LogHelper.WriteLogError("导入CAD点位时出错!", ex);
  822. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  823. }
  824. }
  825. /// <summary>
  826. /// 导入锁付拍照引导的产品坐标系中的点位
  827. /// </summary>
  828. async void ExecuteImportCadLockCameraPointCommand()
  829. {
  830. try
  831. {
  832. var view = new DxfView();
  833. //show the dialog
  834. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  835. if (result != null)
  836. {
  837. var cadPoints = view.Points;
  838. if (cadPoints != null && cadPoints.Count == 2)
  839. {
  840. //弹窗提示用户是否确认要导入锁付拍照引导的起始点和结束点
  841. if (MessageBox.Show($"确认要导入锁付拍照引导的起始点和结束点吗?", "导入点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  842. {
  843. return;
  844. }
  845. //导入点位
  846. //起始点
  847. var startPoint = cadPoints[0];
  848. SelectProduct.ScrewCameraLocalPos1.X_Position = startPoint.X;
  849. SelectProduct.ScrewCameraLocalPos1.Y_Position = startPoint.Y;
  850. //结束点
  851. var endPoint = cadPoints[1];
  852. SelectProduct.ScrewCameraLocalPos2.X_Position = endPoint.X;
  853. SelectProduct.ScrewCameraLocalPos2.Y_Position = endPoint.Y;
  854. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  855. }
  856. else
  857. {
  858. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "请导入两个点,分别为锁付拍照引导的起始点和结束点!", Duration = 1 });
  859. }
  860. }
  861. }
  862. catch (Exception ex)
  863. {
  864. LogHelper.WriteLogError("导入CAD点位时出错!", ex);
  865. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  866. }
  867. }
  868. /// <summary>
  869. /// 批量设置锁付点的供料器编号和螺丝程序编号
  870. /// </summary>
  871. void ExecuteApplyLockPointFeederAndScrewProgramCommand()
  872. {
  873. //检查供料器编号和螺丝程序编号是否大于0,如果小于等于0,则提示用户编号是否正确,是否继续写入
  874. if (LockFeederNumber <= 0 || LockScrewProgramNumber <= 0)
  875. {
  876. var result = MessageBox.Show("供料器编号或螺丝程序编号小于等于0,是否继续写入?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  877. if (result != MessageBoxResult.Yes)
  878. {
  879. return;
  880. }
  881. }
  882. //遍历所有点位,设置供料器编号和螺丝程序编号
  883. foreach (var point in Points)
  884. {
  885. point.Feeder = LockFeederNumber;
  886. point.ScrewProNum = LockScrewProgramNumber;
  887. }
  888. }
  889. /// <summary>
  890. /// 批量设置锁付点的速度
  891. /// </summary>
  892. void ExecuteApplyLockPointSpeedCommand()
  893. {
  894. //检查速度是否大于0,如果小于等于0,则提示用户速度是否正确,是否继续写入
  895. if (LockXSpeed <= 0 || LockYSpeed <= 0 || LockZSpeed <= 0 || LockZDescendSpeed <= 0)
  896. {
  897. var result = MessageBox.Show("速度小于等于0,是否继续写入?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  898. if (result != MessageBoxResult.Yes)
  899. {
  900. return;
  901. }
  902. }
  903. //遍历所有点位,设置速度
  904. foreach (var point in Points)
  905. {
  906. point.X_Velocity = LockXSpeed;
  907. point.Y_Velocity = LockYSpeed;
  908. point.Z_Velocity_Start = LockZSpeed;
  909. point.Z_Velocity_Stop = LockZDescendSpeed;
  910. }
  911. }
  912. /// <summary>
  913. /// 批量设置锁付点的Z深度
  914. /// </summary>
  915. void ExecuteApplyLockPointDepthCommand()
  916. {
  917. //检查深度是否大于0,如果小于等于0,则提示用户深度是否正确,是否继续写入
  918. if (LockZDepth <= 0)
  919. {
  920. var result = MessageBox.Show("深度小于等于0,是否继续写入?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  921. if (result != MessageBoxResult.Yes)
  922. {
  923. return;
  924. }
  925. }
  926. //遍历所有点位,设置Z深度
  927. foreach (var point in Points)
  928. {
  929. point.Z_Position_Stop = point.Z_Position_Start - LockZDepth;
  930. }
  931. }
  932. /// <summary>
  933. /// 批量设置锁付点的起始Z坐标
  934. /// </summary>
  935. void ExecuteApplyLockPointStartZCommand()
  936. {
  937. //检查起始Z坐标是否小于0,如果大于等于0,则提示用户坐标是否正确,是否继续写入
  938. if (LockPointStartZ >= 0)
  939. {
  940. var result = MessageBox.Show("起始Z坐标大于等于0,是否继续写入?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  941. if (result != MessageBoxResult.Yes)
  942. {
  943. return;
  944. }
  945. }
  946. //遍历所有点位,设置起始Z坐标
  947. foreach (var point in Points)
  948. {
  949. point.Z_Position_Start = LockPointStartZ;
  950. }
  951. //检查终点坐标是否小于等于起始坐标,如果不是,则提示用户终点坐标大于等于起始坐标,用户需要去调整终点坐标
  952. foreach (var point in Points)
  953. {
  954. if (point.Z_Position_Stop >= point.Z_Position_Start)
  955. {
  956. MessageBox.Show($"点位编号 {point.Number} 的终点坐标大于等于起始坐标,请调整终点坐标!", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
  957. }
  958. }
  959. }
  960. /// <summary>
  961. /// 通过相机自动矫正锁附点位
  962. /// </summary>
  963. async void ExecuteAutoCorrectLockPoint()
  964. {
  965. try
  966. {
  967. //弹窗提示用户是否确认要通过相机自动矫正锁附点位
  968. var resultConfirm = MessageBox.Show("确认要通过相机自动矫正锁附点位吗?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  969. if (resultConfirm != MessageBoxResult.Yes)
  970. {
  971. return;
  972. }
  973. //获取螺丝相机拍照点位
  974. var screwCameraPoint = SelectProduct.ScrewCameraPoints;
  975. //获取相机拍照点对应的LocalPos
  976. var screwCameraLocalPos1 = SelectProduct.ScrewCameraLocalPos1;
  977. var screwCameraLocalPos2 = SelectProduct.ScrewCameraLocalPos2;
  978. //获取螺丝锁付点位
  979. var screwLockPoints = SelectProduct.ScrewPoints;
  980. //检查screwLockPoints中是否有和screwCameraLocalPos1的XY坐标相同的点位
  981. var matchingPoint = screwLockPoints.FirstOrDefault(p => p.X_Position == screwCameraLocalPos1.X_Position && p.Y_Position == screwCameraLocalPos1.Y_Position);
  982. if (matchingPoint != null)
  983. {
  984. //检查两个点位的Z是否一致?如果不一致,则提示用户锁付点位中有和锁付拍照引导起始点Local坐标相同的点位,且Z坐标不一致,是否将锁付拍照引导起始点Local坐标的Z轴坐标更新为锁付点位的Z坐标
  985. if (matchingPoint.Z_Position_Start != screwCameraLocalPos1.Z_Position_Start)
  986. {
  987. var resultUpdateZ = MessageBox.Show("锁付点位中有和锁付拍照引导起始点Local坐标相同的点位,且Z坐标不一致,是否将锁付拍照引导起始点Local坐标的Z轴坐标更新为锁付点位的Z坐标?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  988. if (resultUpdateZ == MessageBoxResult.Yes)
  989. {
  990. screwCameraLocalPos1.Z_Position_Start = matchingPoint.Z_Position_Start;
  991. }
  992. }
  993. }
  994. //检查screwLockPoints中是否有和screwCameraLocalPos2的XY坐标相同的点位
  995. matchingPoint = screwLockPoints.FirstOrDefault(p => p.X_Position == screwCameraLocalPos2.X_Position && p.Y_Position == screwCameraLocalPos2.Y_Position);
  996. if (matchingPoint != null)
  997. {
  998. //检查两个点位的Z是否一致?如果不一致,则提示用户锁付点位中有和锁付拍照引导结束点Local坐标相同的点位,且Z坐标不一致,是否将锁付拍照引导结束点Local坐标的Z轴坐标更新为锁付点位的Z坐标
  999. if (matchingPoint.Z_Position_Start != screwCameraLocalPos2.Z_Position_Start)
  1000. {
  1001. var resultUpdateZ = MessageBox.Show("锁付点位中有和锁付拍照引导结束点Local坐标相同的点位,且Z坐标不一致,是否将锁付拍照引导结束点Local坐标的Z轴坐标更新为锁付点位的Z坐标?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  1002. if (resultUpdateZ == MessageBoxResult.Yes)
  1003. {
  1004. screwCameraLocalPos2.Z_Position_Start = matchingPoint.Z_Position_Start;
  1005. }
  1006. }
  1007. }
  1008. IDialogParameters parameters = new DialogParameters();
  1009. parameters.Add("Robot", Robot);
  1010. parameters.Add("Points", new List<PlcPoint>(SelectProduct.ScrewPoints.ToArray()));
  1011. _dialogService.Show("AutoCorrectLockPoint", parameters, rst =>
  1012. {
  1013. //对话框关闭之后的回调函数,可以在这解析结果。
  1014. ButtonResult result1 = rst.Result;
  1015. if (result1 == ButtonResult.OK)
  1016. {
  1017. var param = rst.Parameters.GetValue<ObservableCollection<PointEx>>("CorrectPoints");
  1018. for (int i = 0; i < SelectProduct.ScrewPoints.Count; i++)
  1019. {
  1020. SelectProduct.ScrewPoints[i].X_Position = param[i].PostX;
  1021. SelectProduct.ScrewPoints[i].Y_Position = param[i].PostY;
  1022. }
  1023. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "完成!", Duration = 1 });
  1024. }
  1025. });
  1026. }
  1027. catch (Exception ex)
  1028. {
  1029. LogHelper.WriteLogError("通过相机自动矫正锁附点位时出错!", ex);
  1030. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1031. }
  1032. }
  1033. /// <summary>
  1034. /// 通过相机先定位产品再执行锁付点位
  1035. /// </summary>
  1036. /// <param name="point"></param>
  1037. private async Task ExecuteAutoCorrectLockPointAndExecuteCommand(PlcPoint point)
  1038. {
  1039. var waiting = new WaitingControl();
  1040. Task<object> task;
  1041. try
  1042. {
  1043. if (Robot == null || !Robot.IsConnected) return;
  1044. //弹窗提示用户是否确认要通过相机自动矫正锁附点位并执行该点位
  1045. var resultConfirm = MessageBox.Show($"确认要通过相机自动矫正锁附点位并执行点位 {point.Number} 吗?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  1046. if (resultConfirm != MessageBoxResult.Yes)
  1047. {
  1048. return;
  1049. }
  1050. if (Robot == null || !Robot.IsConnected)
  1051. {
  1052. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行取料拍照点位矫正!", Duration = 1 });
  1053. return;
  1054. }
  1055. //获取当前产品
  1056. var product = SelectProduct;
  1057. if (product == null)
  1058. {
  1059. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "未选择产品,无法执行取料拍照点位矫正!", Duration = 1 });
  1060. return;
  1061. }
  1062. //如果当前Local为空,则自动进行锁付点位的Local计算
  1063. if (local == null)
  1064. {
  1065. //show the dialog
  1066. task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1067. var isCalibSucceed = await AutoCalibLockPointLocal(product);
  1068. if (!isCalibSucceed)
  1069. {
  1070. return;
  1071. }
  1072. }
  1073. else
  1074. {
  1075. //提示用户,当前系统检测到进入此界面后已经进行过相机产品定位并计算过Local,是否继续使用该Local进行锁付点位的转换计算
  1076. var resultUseExistingLocal = MessageBox.Show("系统检测到进入此界面后已经进行过相机产品定位并计算过Local,是否继续使用该Local进行锁付点位的转换计算?点击“是”继续使用,点击“否”则重新进行锁付点位的Local计算。", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
  1077. if (resultUseExistingLocal == MessageBoxResult.No)
  1078. {
  1079. //show the dialog
  1080. task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1081. var isCalibSucceed = await AutoCalibLockPointLocal(product);
  1082. if (!isCalibSucceed)
  1083. {
  1084. return;
  1085. }
  1086. }
  1087. else
  1088. {
  1089. //show the dialog
  1090. task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1091. }
  1092. }
  1093. //将锁付点位转换到世界坐标系
  1094. var localPoint = new PointF(point.X_Position, point.Y_Position);
  1095. var worldPoint = local.ToOldCoord(localPoint.X, localPoint.Y);
  1096. var point1 = point.Clone();
  1097. point1.X_Position = (float)worldPoint[0];
  1098. point1.Y_Position = (float)worldPoint[1];
  1099. //执行该点位
  1100. //移动机器人至拍照点位,拍照
  1101. var rpoint = new RPoint()
  1102. {
  1103. X = point.X_Position,
  1104. Y = point.Y_Position,
  1105. Z = point.Z_Position_Start,
  1106. };
  1107. await Robot.CalibMotionAsync(rpoint, 0);
  1108. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "完成!", Duration = 1 });
  1109. }
  1110. catch (Exception ex)
  1111. {
  1112. LogHelper.WriteLogError("通过相机自动矫正锁附点位并执行时出错!", ex);
  1113. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1114. }
  1115. finally
  1116. {
  1117. try
  1118. {
  1119. if (DialogHost.IsDialogOpen("RootDialog"))
  1120. {
  1121. DialogHost.Close("RootDialog");
  1122. }
  1123. }
  1124. catch (Exception)
  1125. {
  1126. }
  1127. }
  1128. }
  1129. /// <summary>
  1130. /// 移动相机拍产品两个Msrk点计算Local
  1131. /// </summary>
  1132. /// <param name="product"></param>
  1133. /// <returns></returns>
  1134. private async Task<bool> AutoCalibLockPointLocal(ProductModel product)
  1135. {
  1136. try
  1137. {
  1138. //获取锁付相机拍照点位
  1139. var screwCameraPoint = product.ScrewCameraPoints;
  1140. if (screwCameraPoint == null || screwCameraPoint.Count < 2)
  1141. {
  1142. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "未获取到锁付相机拍照点位,无法执行取料拍照点位矫正!", Duration = 1 });
  1143. return false;
  1144. }
  1145. //获取相机拍照点位对应的锁付点
  1146. var screwCameraLocalPoint1 = product.ScrewCameraLocalPos1;
  1147. if (screwCameraLocalPoint1 == null)
  1148. {
  1149. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "未获取到锁付相机拍照点位对应的锁付点,无法执行取料拍照点位矫正!", Duration = 1 });
  1150. return false;
  1151. }
  1152. var screwCameraLocalPoint2 = product.ScrewCameraLocalPos2;
  1153. if (screwCameraLocalPoint2 == null)
  1154. {
  1155. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "未获取到锁付相机拍照点位对应的锁付点,无法执行取料拍照点位矫正!", Duration = 1 });
  1156. return false;
  1157. }
  1158. var ProcedureModels = new ObservableCollection<ProcedureModel>();
  1159. foreach (var item in product.CameraProcedures)
  1160. {
  1161. foreach (var item1 in item.ProcedureModels)
  1162. {
  1163. ProcedureModels.Add(item1);
  1164. }
  1165. }
  1166. //获取拍照流程
  1167. if (product.MoveDownCameraLockPhotoProcedureId == Guid.Empty)
  1168. {
  1169. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "未获取到锁付相机拍照流程,无法执行取料拍照点位矫正!", Duration = 1 });
  1170. return false;
  1171. }
  1172. var procedure = ProcedureModels.FirstOrDefault(p => p.Id == product.MoveDownCameraLockPhotoProcedureId);
  1173. if (procedure == null)
  1174. {
  1175. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "未获取到锁付相机拍照流程,无法执行取料拍照点位矫正!", Duration = 1 });
  1176. return false;
  1177. }
  1178. CancellationTokenSource _cts = new CancellationTokenSource();
  1179. //移动机器人至拍照点位,拍照
  1180. var rpoint = new RPoint()
  1181. {
  1182. X = screwCameraPoint[0].X_Position,
  1183. Y = screwCameraPoint[0].Y_Position,
  1184. Z = screwCameraPoint[0].Z_Position_Start,
  1185. U = screwCameraPoint[0].U_Position,
  1186. V = screwCameraPoint[0].R_Position,
  1187. };
  1188. await Robot.CalibMotionAsync(rpoint, 0);
  1189. await Task.Delay(200);
  1190. // 执行拍照并获取识别结果
  1191. var recognitionResult = await _remoteCommandService.ExecutePhotoGetSinglePointWithImage(procedure, null, new double[] { rpoint.X, rpoint.Y, 0 }, _cts.Token);
  1192. //var Image = recognitionResult.Image;
  1193. //var Graphic = null;
  1194. //var Graphic = recognitionResult.Graphic;
  1195. if (!recognitionResult.IsSucceed)
  1196. {
  1197. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "拍照获取识别结果失败,无法执行取料拍照点位矫正!", Duration = 1 });
  1198. return false;
  1199. }
  1200. //第一个点位的校正结果
  1201. PointF worldP1 = new PointF((float)recognitionResult.X, (float)recognitionResult.Y);
  1202. //移动机器人至拍照点位,拍照
  1203. rpoint = new RPoint()
  1204. {
  1205. X = screwCameraPoint[1].X_Position,
  1206. Y = screwCameraPoint[1].Y_Position,
  1207. Z = screwCameraPoint[1].Z_Position_Start,
  1208. U = screwCameraPoint[1].U_Position,
  1209. V = screwCameraPoint[1].R_Position,
  1210. };
  1211. await Robot.CalibMotionAsync(rpoint, 0);
  1212. if (_cts.IsCancellationRequested)
  1213. return false;
  1214. await Task.Delay(200);
  1215. // 执行拍照并获取识别结果
  1216. recognitionResult = await _remoteCommandService.ExecutePhotoGetSinglePointWithImage(procedure, null, new double[] { rpoint.X, rpoint.Y, 0 }, _cts.Token);
  1217. //Image = recognitionResult.Image;
  1218. //Graphic = null;
  1219. //Graphic = recognitionResult.Graphic;
  1220. if (!recognitionResult.IsSucceed)
  1221. {
  1222. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "拍照获取识别结果失败,无法执行取料拍照点位矫正!", Duration = 1 });
  1223. return false;
  1224. }
  1225. //第一个点位的校正结果
  1226. PointF worldP2 = new PointF((float)recognitionResult.X, (float)recognitionResult.Y);
  1227. //计算移动相机与拍照点之间的偏差
  1228. //获取校准
  1229. var calibration = _calibrationService.GetCalibration(procedure.CalibrationId);
  1230. //获取mark点
  1231. var mark = calibration.MarkPoint;
  1232. //获取中心点
  1233. var center = calibration.CenterPoint;
  1234. //计算相机中心与披头之间的坐标偏差
  1235. var cameraOffsetX = mark.X - center.X;
  1236. var cameraOffsetY = mark.Y - center.Y;
  1237. //根据UpCameraPutResults[]中的索引1、2,和拍照点对应的Local下的坐标,从创建Local坐标系
  1238. PointF localP1 = new PointF(screwCameraLocalPoint1.X_Position, screwCameraLocalPoint1.Y_Position);
  1239. PointF localP2 = new PointF(screwCameraLocalPoint2.X_Position, screwCameraLocalPoint2.Y_Position);
  1240. local = new CoordinateTransformer(worldP1, worldP2, localP1, localP2);
  1241. return true;
  1242. }
  1243. catch (Exception ex)
  1244. {
  1245. LogHelper.WriteLogError("计算锁付点位产品坐标系坐标时出错!", ex);
  1246. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1247. return false;
  1248. }
  1249. }
  1250. #endregion
  1251. #region 继承
  1252. /// <summary>
  1253. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  1254. /// </summary>
  1255. /// <param name="navigationContext"></param>
  1256. /// <param name="continuationCallback"></param>
  1257. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  1258. {
  1259. continuationCallback(true);
  1260. }
  1261. /// <summary>
  1262. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  1263. /// </summary>
  1264. /// <param name="navigationContext"></param>
  1265. /// <exception cref="NotImplementedException"></exception>
  1266. public async void OnNavigatedTo(NavigationContext navigationContext)
  1267. {
  1268. try
  1269. {
  1270. local = null;
  1271. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  1272. SelectedIndex = 0;
  1273. PickPoints = SelectProduct.PickPoints;
  1274. Points = SelectProduct.PickCameraPoints;
  1275. if (Robot == null)
  1276. {
  1277. Robot = _robotService.GetRobotByNumber(1);
  1278. }
  1279. if (Robot != null && Robot.IsConnected)
  1280. {
  1281. this.Distance = _configService.GetRobotStepDistance(Robot.Id);
  1282. Robot.EnterDebugMode = true;
  1283. }
  1284. else if (Robot != null)
  1285. {
  1286. Robot.EnterDebugMode = true;
  1287. }
  1288. ProcedureModels = new ObservableCollection<ProcedureModel>();
  1289. foreach (var item in SelectProduct.CameraProcedures)
  1290. {
  1291. foreach (var item1 in item.ProcedureModels)
  1292. {
  1293. ProcedureModels.Add(item1);
  1294. }
  1295. }
  1296. //对锁付点的批量设置进行初始化
  1297. LockPointStartZ = SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Position_Start ?? 0;
  1298. LockZDepth = SelectProduct.ScrewPoints.FirstOrDefault() != null ? (SelectProduct.ScrewPoints.FirstOrDefault().Z_Position_Start - SelectProduct.ScrewPoints.FirstOrDefault().Z_Position_Stop) : 0;
  1299. LockXSpeed = SelectProduct.ScrewPoints.FirstOrDefault()?.X_Velocity ?? 100;
  1300. LockYSpeed = SelectProduct.ScrewPoints.FirstOrDefault()?.Y_Velocity ?? 100;
  1301. LockZSpeed = SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Velocity_Start ?? 100;
  1302. LockZDescendSpeed = SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Velocity_Stop ?? 100;
  1303. LockFeederNumber = SelectProduct.ScrewPoints.FirstOrDefault()?.Feeder ?? 1;
  1304. LockScrewProgramNumber = SelectProduct.ScrewPoints.FirstOrDefault()?.ScrewProNum ?? 1;
  1305. }
  1306. catch (Exception ex)
  1307. {
  1308. LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
  1309. }
  1310. }
  1311. /// <summary>
  1312. /// 是否允许导航到此视图模型。
  1313. /// </summary>
  1314. /// <param name="navigationContext"></param>
  1315. /// <returns></returns>
  1316. public bool IsNavigationTarget(NavigationContext navigationContext)
  1317. {
  1318. return true;
  1319. }
  1320. /// <summary>
  1321. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  1322. /// </summary>
  1323. /// <param name="navigationContext"></param>
  1324. public void OnNavigatedFrom(NavigationContext navigationContext)
  1325. {
  1326. //if (Robot != null)
  1327. //{
  1328. // Robot.EnterDebugMode = false;
  1329. //}
  1330. }
  1331. #endregion
  1332. #region 取料拍照点位矫正
  1333. private PlcPoint _SelectedCameraForPickPoint;
  1334. public PlcPoint SelectedCameraForPickPoint
  1335. {
  1336. get { return _SelectedCameraForPickPoint; }
  1337. set { SetProperty(ref _SelectedCameraForPickPoint, value); }
  1338. }
  1339. private ObservableCollection<PlcPoint> _PickPoints = new ObservableCollection<PlcPoint>();
  1340. /// <summary>
  1341. /// 取料点位集合
  1342. /// </summary>
  1343. public ObservableCollection<PlcPoint> PickPoints
  1344. {
  1345. get { return _PickPoints; }
  1346. set { SetProperty(ref _PickPoints, value); }
  1347. }
  1348. //
  1349. private PlcPoint _SelectedPickPoint;
  1350. /// <summary>
  1351. /// 选中的取料点位
  1352. /// </summary>
  1353. public PlcPoint SelectedPickPoint
  1354. {
  1355. get { return _SelectedPickPoint; }
  1356. set { SetProperty(ref _SelectedPickPoint, value); }
  1357. }
  1358. private DelegateCommand _AutoCorrectPickPointCommand;
  1359. public DelegateCommand AutoCorrectPickPointCommand =>
  1360. _AutoCorrectPickPointCommand ?? (_AutoCorrectPickPointCommand = new DelegateCommand(ExecuteAutoCorrectPickPointCommand, CanExecuteAutoCorrectPickPointCommand)
  1361. .ObservesProperty(() => SelectedCameraForPickPoint).ObservesProperty(() => SelectedPickPoint).ObservesProperty(() => SelectProcedure).ObservesProperty(() => IsExecutingAutoCorrectPickPoint));
  1362. //
  1363. private bool _IsExecutingAutoCorrectPickPoint = false;
  1364. /// <summary>
  1365. /// 正在执行取料拍照点位矫正
  1366. /// </summary>
  1367. public bool IsExecutingAutoCorrectPickPoint
  1368. {
  1369. get { return _IsExecutingAutoCorrectPickPoint; }
  1370. set { SetProperty(ref _IsExecutingAutoCorrectPickPoint, value); }
  1371. }
  1372. async void ExecuteAutoCorrectPickPointCommand()
  1373. {
  1374. try
  1375. {
  1376. IsExecutingAutoCorrectPickPoint = true;
  1377. if (SelectedCameraForPickPoint == null || SelectedPickPoint == null || SelectProcedure == null)
  1378. {
  1379. return;
  1380. }
  1381. if (Robot == null || !Robot.IsConnected)
  1382. {
  1383. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行取料拍照点位矫正!", Duration = 1 });
  1384. return;
  1385. }
  1386. if (MessageBox.Show("确认要执行取料拍照点位矫正吗?", "取料拍照点位矫正", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1387. {
  1388. return;
  1389. }
  1390. var waiting = new WaitingControl();
  1391. //show the dialog
  1392. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1393. // 1. 控制机器人移动至拍照点位
  1394. var rpoint = new RPoint()
  1395. {
  1396. X = SelectedCameraForPickPoint.X_Position,
  1397. Y = SelectedCameraForPickPoint.Y_Position,
  1398. Z = SelectedCameraForPickPoint.Z_Position_Start,
  1399. U = SelectedCameraForPickPoint.U_Position,
  1400. V = SelectedCameraForPickPoint.R_Position,
  1401. };
  1402. await Robot.CalibMotionAsync(rpoint, 0);
  1403. var _cts = new CancellationTokenSource();
  1404. // 2. 执行拍照并获取识别结果
  1405. var recognitionResult = await _remoteCommandService.ExecutePhotoGetSinglePoint(SelectProcedure, null, new double[] { rpoint.X, rpoint.Y, 0 }, _cts.Token);
  1406. if (!recognitionResult.IsSucceed)
  1407. {
  1408. if (DialogHost.IsDialogOpen("RootDialog"))
  1409. {
  1410. DialogHost.Close("RootDialog");
  1411. }
  1412. await task;
  1413. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料拍照点位矫正失败,识别未成功!", Duration = 1 });
  1414. return;
  1415. }
  1416. // 3. 根据识别结果更新取料点位坐标
  1417. SelectedPickPoint.X_Position = (float)recognitionResult.X;
  1418. SelectedPickPoint.Y_Position = (float)recognitionResult.Y;
  1419. if (DialogHost.IsDialogOpen("RootDialog"))
  1420. {
  1421. DialogHost.Close("RootDialog");
  1422. }
  1423. await task;
  1424. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料拍照点位矫正完成!", Duration = 0.4 });
  1425. }
  1426. catch (Exception ex)
  1427. {
  1428. LogHelper.WriteLogError("取料拍照点位矫正时出错!", ex);
  1429. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1430. }
  1431. finally
  1432. {
  1433. IsExecutingAutoCorrectPickPoint = false;
  1434. }
  1435. }
  1436. bool CanExecuteAutoCorrectPickPointCommand()
  1437. {
  1438. return SelectedCameraForPickPoint != null && SelectedPickPoint != null && SelectProcedure != null && !IsExecutingAutoCorrectPickPoint;
  1439. }
  1440. #endregion
  1441. private bool _IsAllowEdit = false;
  1442. public bool IsAllowEdit
  1443. {
  1444. get { return _IsAllowEdit; }
  1445. set { SetProperty(ref _IsAllowEdit, value); }
  1446. }
  1447. private void LoginChange(Models.User user)
  1448. {
  1449. if (user.userPart == Enums.UserPart.Operator)
  1450. {
  1451. IsAllowEdit = false;
  1452. }
  1453. else
  1454. {
  1455. IsAllowEdit = true;
  1456. }
  1457. }
  1458. }
  1459. }