PlcPointParamsViewModel.cs 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  1. using Cognex.VisionPro;
  2. using MahApps.Metro.Controls;
  3. using MaterialDesignThemes.Wpf;
  4. using MathNet.Numerics.LinearAlgebra;
  5. using NPOI.SS.Formula.Functions;
  6. using OpenCvSharp.Flann;
  7. using Prism.Commands;
  8. using Prism.Events;
  9. using Prism.Ioc;
  10. using Prism.Mvvm;
  11. using Prism.Regions;
  12. using Prism.Services.Dialogs;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Collections.ObjectModel;
  16. using System.Drawing;
  17. using System.Linq;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using TeamAAS_VP.Controls;
  22. using TeamAAS_VP.Core;
  23. using TeamAAS_VP.Core.PLCs;
  24. using TeamAAS_VP.Core.Robots;
  25. using TeamAAS_VP.DxfModule;
  26. using TeamAAS_VP.Enums;
  27. using TeamAAS_VP.Events;
  28. using TeamAAS_VP.Interfaces;
  29. using TeamAAS_VP.Models;
  30. using TeamAAS_VP.Models.PLC;
  31. using TeamAAS_VP.Models.Robot;
  32. using TeamAAS_VP.Resources.Languages;
  33. using TeamAAS_VP.Services;
  34. using TeamAAS_VP.Views.Product;
  35. using static TeamAAS_VP.ViewModels.Product.AutoCorrectLockPointViewModel;
  36. namespace TeamAAS_VP.ViewModels.Product
  37. {
  38. public class PlcPointParamsViewModel : BindableBase, IConfirmNavigationRequest
  39. {
  40. IRegionManager _regionManager;
  41. IEventAggregator _eventAggregator;
  42. IContainerProvider _container;
  43. IDialogService _dialogService;
  44. IConfigService _configService;
  45. IRobotService _robotService;
  46. ISystemDatabaseService _systemDatabaseService;
  47. IRemoteCommandService _remoteCommandService;
  48. ICalibrationService _calibrationService;
  49. ICameraService _cameraService;
  50. IProductService _productService;
  51. #region 属性
  52. private ObservableCollection<AxisExtended> _AdditionalAxisList;
  53. public ObservableCollection<AxisExtended> AdditionalAxisList
  54. {
  55. get { return _AdditionalAxisList; }
  56. set { SetProperty(ref _AdditionalAxisList, value); }
  57. }
  58. private ProductModel _SelectProduct;
  59. /// <summary>
  60. /// 选中的产品
  61. /// </summary>
  62. public ProductModel SelectProduct
  63. {
  64. get { return _SelectProduct; }
  65. set { SetProperty(ref _SelectProduct, value); }
  66. }
  67. private PlcPoint _SelectedPoint;
  68. public PlcPoint SelectedPoint
  69. {
  70. get { return _SelectedPoint; }
  71. set { SetProperty(ref _SelectedPoint, value); }
  72. }
  73. private PlcPoint _selectedPointInDataGrid;
  74. public PlcPoint SelectedPointInDataGrid
  75. {
  76. get { return _selectedPointInDataGrid; }
  77. set
  78. {
  79. if (SetProperty(ref _selectedPointInDataGrid, value))
  80. {
  81. SelectedPointInComboBox = value;
  82. // update command availability
  83. _MovePointUpCommand?.RaiseCanExecuteChanged();
  84. _MovePointDownCommand?.RaiseCanExecuteChanged();
  85. // 更新依赖可执行状态的命令
  86. _TechPointCommand?.RaiseCanExecuteChanged();
  87. _ExecuteMotion?.RaiseCanExecuteChanged();
  88. }
  89. // 可以同步到 SelectPoint 属性
  90. SelectedPoint = value;
  91. }
  92. }
  93. private PlcPoint _selectedPointInComboBox;
  94. public PlcPoint SelectedPointInComboBox
  95. {
  96. get { return _selectedPointInComboBox; }
  97. set
  98. {
  99. if (SetProperty(ref _selectedPointInComboBox, value))
  100. {
  101. SelectedPointInDataGrid = value;
  102. }
  103. // 可以同步到 SelectPoint 属性
  104. SelectedPoint = value;
  105. }
  106. }
  107. private ObservableCollection<PlcPoint> _Points = new ObservableCollection<PlcPoint>();
  108. /// <summary>
  109. /// 点位集合
  110. /// </summary>
  111. public ObservableCollection<PlcPoint> Points
  112. {
  113. get { return _Points; }
  114. set { SetProperty(ref _Points, value); }
  115. }
  116. private MotionCommand _SelectMotionCommand;
  117. /// <summary>
  118. /// 选中的运动指令
  119. /// </summary>
  120. public MotionCommand SelectMotionCommand
  121. {
  122. get { return _SelectMotionCommand; }
  123. set { SetProperty(ref _SelectMotionCommand, value); }
  124. }
  125. public Management management { get; set; }
  126. private XYZU_Robot _Robot;
  127. public XYZU_Robot Robot
  128. {
  129. get { return _Robot; }
  130. set
  131. {
  132. SetProperty(ref _Robot, value);
  133. }
  134. }
  135. private int _SelectedIndex;
  136. public int SelectedIndex
  137. {
  138. get { return _SelectedIndex; }
  139. set { SetProperty(ref _SelectedIndex, value); }
  140. }
  141. private double _Distance = 1;
  142. public double Distance
  143. {
  144. get { return _Distance; }
  145. set
  146. {
  147. SetProperty(ref _Distance, value);
  148. try
  149. {
  150. if (Robot == null)
  151. {
  152. return;
  153. }
  154. _configService.UpdateRobotStepDistance(Robot.Id, value);
  155. }
  156. catch (Exception)
  157. {
  158. }
  159. }
  160. }
  161. private ObservableCollection<ProcedureModel> _ProcedureModels;
  162. /// <summary>
  163. /// 流程集合
  164. /// </summary>
  165. public ObservableCollection<ProcedureModel> ProcedureModels
  166. {
  167. get { return _ProcedureModels; }
  168. set { SetProperty(ref _ProcedureModels, value); }
  169. }
  170. private ProcedureModel _SelectProcedure;
  171. /// <summary>
  172. /// 选中的流程
  173. /// </summary>
  174. public ProcedureModel SelectProcedure
  175. {
  176. get { return _SelectProcedure; }
  177. set
  178. {
  179. SetProperty(ref _SelectProcedure, value);
  180. }
  181. }
  182. private bool _IsGrap;
  183. public bool IsGrap
  184. {
  185. get { return _IsGrap; }
  186. set { SetProperty(ref _IsGrap, value); }
  187. }
  188. private ObservableCollection<CameraInfo> _CameraList;
  189. public ObservableCollection<CameraInfo> CameraList
  190. {
  191. get { return _CameraList; }
  192. set { SetProperty(ref _CameraList, value); }
  193. }
  194. private CameraInfo _SelectedCamera;
  195. public CameraInfo SelectedCamera
  196. {
  197. get { return _SelectedCamera; }
  198. set
  199. {
  200. SetProperty(ref _SelectedCamera, value);
  201. if (value != null)
  202. {
  203. if (Camera != null)
  204. {
  205. if (Camera.IsGrabbing)
  206. {
  207. Camera.StopGrabbing();
  208. }
  209. Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
  210. }
  211. Camera = _cameraService.GetCamera(value.Id);
  212. Camera.ImageCallbackEvent += Camera_ImageCallbackEvent;
  213. }
  214. }
  215. }
  216. private ICamera _Camera;
  217. public ICamera Camera
  218. {
  219. get { return _Camera; }
  220. set { SetProperty(ref _Camera, value); }
  221. }
  222. private ICogImage _Image;
  223. public ICogImage Image
  224. {
  225. get { return _Image; }
  226. set { SetProperty(ref _Image, value); }
  227. }
  228. private string _Message;
  229. public string Message
  230. {
  231. get { return _Message; }
  232. set { SetProperty(ref _Message, value); }
  233. }
  234. private bool _IsLeftDrawerOpen;
  235. public bool IsLeftDrawerOpen
  236. {
  237. get { return _IsLeftDrawerOpen; }
  238. set
  239. {
  240. SetProperty(ref _IsLeftDrawerOpen, value);
  241. if (!value)
  242. {
  243. if (IsGrap)
  244. {
  245. ExecuteStopGrabbingCommand();
  246. }
  247. }
  248. }
  249. }
  250. private int _Inhal = 1;
  251. public int Inhal
  252. {
  253. get { return _Inhal; }
  254. set { SetProperty(ref _Inhal, value); }
  255. }
  256. private PlcPoint _PickP1 = new PlcPoint();
  257. public PlcPoint PickP1
  258. {
  259. get { return _PickP1; }
  260. set { SetProperty(ref _PickP1, value); }
  261. }
  262. private PlcPoint _PickP2 = new PlcPoint();
  263. public PlcPoint PickP2
  264. {
  265. get { return _PickP2; }
  266. set { SetProperty(ref _PickP2, value); }
  267. }
  268. private int _ColumnNum = 18;
  269. public int ColumnNum
  270. {
  271. get { return _ColumnNum; }
  272. set { SetProperty(ref _ColumnNum, value); }
  273. }
  274. #endregion
  275. #region 命令
  276. private DelegateCommand _NextCommand;
  277. public DelegateCommand NextCommand =>
  278. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  279. private DelegateCommand _BackCommand;
  280. public DelegateCommand BackCommand =>
  281. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  282. private DelegateCommand _LoadedCommand;
  283. public DelegateCommand LoadedCommand =>
  284. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  285. private DelegateCommand<object> _ExecuteMotion;
  286. public DelegateCommand<object> ExecuteMotion =>
  287. _ExecuteMotion ?? (_ExecuteMotion = new DelegateCommand<object>(ExecuteExecuteMotion).ObservesCanExecute(() => CanExecutePointCommand).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute).ObservesProperty(() => SelectedPointInDataGrid));
  288. private DelegateCommand<object> _JogCommand;
  289. public DelegateCommand<object> JogCommand =>
  290. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  291. private DelegateCommand _TechPointCommand;
  292. public DelegateCommand TechPointCommand =>
  293. _TechPointCommand ?? (_TechPointCommand = new DelegateCommand(ExecuteTechPointCommand).ObservesCanExecute(() => CanExecutePointCommand).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute).ObservesProperty(() => SelectedPointInDataGrid));
  294. private DelegateCommand<string> _MotorCommand;
  295. public DelegateCommand<string> MotorCommand =>
  296. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  297. private DelegateCommand _ResetCommand;
  298. public DelegateCommand ResetCommand =>
  299. _ResetCommand ?? (_ResetCommand = new DelegateCommand(ExecuteResetCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  300. private DelegateCommand _sFreeCommand;
  301. public DelegateCommand sFreeCommand =>
  302. _sFreeCommand ?? (_sFreeCommand = new DelegateCommand(ExecutesFreeCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  303. private DelegateCommand _SLockCommand;
  304. public DelegateCommand SLockCommand =>
  305. _SLockCommand ?? (_SLockCommand = new DelegateCommand(ExecuteSLockCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  306. private DelegateCommand _SelectionChangedCommand;
  307. public DelegateCommand SelectionChangedCommand =>
  308. _SelectionChangedCommand ?? (_SelectionChangedCommand = new DelegateCommand(ExecuteSelectionChangedCommand));
  309. private DelegateCommand _AddPointCommand;
  310. public DelegateCommand AddPointCommand =>
  311. _AddPointCommand ?? (_AddPointCommand = new DelegateCommand(ExecuteAddPointCommand));
  312. private DelegateCommand _DeletePointCommand;
  313. public DelegateCommand DeletePointCommand =>
  314. _DeletePointCommand ?? (_DeletePointCommand = new DelegateCommand(ExecuteDeletePointCommand, CanExecuteDeletePointCommand).ObservesProperty(() => SelectedPointInDataGrid));
  315. private DelegateCommand _MovePointUpCommand;
  316. public DelegateCommand MovePointUpCommand => _MovePointUpCommand ?? (_MovePointUpCommand = new DelegateCommand(ExecuteMovePointUpCommand, CanMovePointUp));
  317. private DelegateCommand _MovePointDownCommand;
  318. public DelegateCommand MovePointDownCommand => _MovePointDownCommand ?? (_MovePointDownCommand = new DelegateCommand(ExecuteMovePointDownCommand, CanMovePointDown));
  319. private DelegateCommand _ImportCadPointCommand;
  320. public DelegateCommand ImportCadPointCommand =>
  321. _ImportCadPointCommand ?? (_ImportCadPointCommand = new DelegateCommand(ExecuteImportCadPointCommand));
  322. private DelegateCommand _StartGrabbingCommand;
  323. public DelegateCommand StartGrabbingCommand =>
  324. _StartGrabbingCommand ?? (_StartGrabbingCommand = new DelegateCommand(ExecuteStartGrabbingCommand, CanExecuteStartGrabbingCommand).ObservesProperty(() => IsGrap).ObservesProperty(() => SelectedCamera));
  325. private DelegateCommand _StopGrabbingCommand;
  326. public DelegateCommand StopGrabbingCommand =>
  327. _StopGrabbingCommand ?? (_StopGrabbingCommand = new DelegateCommand(ExecuteStopGrabbingCommand, CanExecuteStopGrabbingCommand).ObservesProperty(() => IsGrap).ObservesProperty(() => SelectedCamera));
  328. private DelegateCommand _GoPickCommand;
  329. public DelegateCommand GoPickCommand =>
  330. _GoPickCommand ?? (_GoPickCommand = new DelegateCommand(ExecuteGoPickCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(() => SelectedPoint));
  331. private DelegateCommand _GoRepositionCommand;
  332. public DelegateCommand GoRepositionCommand =>
  333. _GoRepositionCommand ?? (_GoRepositionCommand = new DelegateCommand(ExecuteGoRepositionCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(() => SelectedPoint));
  334. private DelegateCommand _TakeProductPhotoCommand;
  335. public DelegateCommand TakeProductPhotoCommand =>
  336. _TakeProductPhotoCommand ?? (_TakeProductPhotoCommand = new DelegateCommand(ExecuteTakeProductPhotoCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(() => SelectedPoint));
  337. private DelegateCommand _TakeProductCommand;
  338. public DelegateCommand TakeProductCommand =>
  339. _TakeProductCommand ?? (_TakeProductCommand = new DelegateCommand(ExecuteTakeProductCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(() => SelectedPoint));
  340. //附加轴,轴点动+命令
  341. private DelegateCommand<object> _AdditionalAxisJogPlusCommand;
  342. public DelegateCommand<object> AdditionalAxisJogPlusCommand =>
  343. _AdditionalAxisJogPlusCommand ?? (_AdditionalAxisJogPlusCommand = new DelegateCommand<object>(ExecuteAdditionalAxisJogPlusCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  344. //附加轴,轴点动-命令
  345. private DelegateCommand<object> _AdditionalAxisJogMinusCommand;
  346. public DelegateCommand<object> AdditionalAxisJogMinusCommand =>
  347. _AdditionalAxisJogMinusCommand ?? (_AdditionalAxisJogMinusCommand = new DelegateCommand<object>(ExecuteAdditionalAxisJogMinusCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  348. private DelegateCommand<object> _AdditionalAxisMotorOnCommand;
  349. public DelegateCommand<object> AdditionalAxisMotorOnCommand =>
  350. _AdditionalAxisMotorOnCommand ?? (_AdditionalAxisMotorOnCommand = new DelegateCommand<object>(ExecuteAdditionalAxisMotorOnCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  351. private DelegateCommand<object> _AdditionalAxisMotorOffCommand;
  352. public DelegateCommand<object> AdditionalAxisMotorOffCommand =>
  353. _AdditionalAxisMotorOffCommand ?? (_AdditionalAxisMotorOffCommand = new DelegateCommand<object>(ExecuteAdditionalAxisMotorOffCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  354. private DelegateCommand<string> _PickTechCommand;
  355. public DelegateCommand<string> PickTechCommand =>
  356. _PickTechCommand ?? (_PickTechCommand = new DelegateCommand<string>(ExecutePickTechCommand));
  357. private DelegateCommand<string> _PickMoveCommand;
  358. public DelegateCommand<string> PickMoveCommand =>
  359. _PickMoveCommand ?? (_PickMoveCommand = new DelegateCommand<string>(ExecutePickMoveCommand));
  360. private DelegateCommand _CalPickPosCommand;
  361. public DelegateCommand CalPickPosCommand =>
  362. _CalPickPosCommand ?? (_CalPickPosCommand = new DelegateCommand(ExecuteCalPickPosCommand));
  363. #endregion
  364. #region 事件
  365. #endregion
  366. public PlcPointParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
  367. IConfigService configService, IRobotService robotService, ISystemDatabaseService systemDatabaseService, IRemoteCommandService remoteCommandService,
  368. ICalibrationService calibrationService, ICameraService cameraService, IProductService productService)
  369. {
  370. _regionManager = regionManager;
  371. _eventAggregator = ea;
  372. _container = container;
  373. _dialogService = dialogService;
  374. _configService = configService;
  375. _systemDatabaseService = systemDatabaseService;
  376. //订阅权限登录事件
  377. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  378. management = _container.Resolve<Management>();
  379. _robotService = robotService;
  380. _remoteCommandService = remoteCommandService;
  381. _calibrationService = calibrationService;
  382. _cameraService = cameraService;
  383. _productService = productService;
  384. }
  385. #region 方法
  386. /// <summary>
  387. /// 下一步
  388. /// </summary>
  389. void ExecuteNextCommand()
  390. {
  391. NavigationParameters param = new NavigationParameters();
  392. param.Add("SelectProduct", SelectProduct);
  393. _regionManager.RequestNavigate("ProductRegionContext", "ProductParams", param);
  394. }
  395. /// <summary>
  396. /// 上一步
  397. /// </summary>
  398. void ExecuteBackCommand()
  399. {
  400. NavigationParameters param = new NavigationParameters();
  401. param.Add("SelectProduct", SelectProduct);
  402. param.Add("SelectedProcedure", null);
  403. _regionManager.RequestNavigate("ProductRegionContext", "ProcessBasics", param);
  404. }
  405. /// <summary>
  406. /// 点动
  407. /// </summary>
  408. /// <param name="obj"></param>
  409. private async void ExecuteJogCommand(object obj)
  410. {
  411. if (Robot == null || !Robot.IsConnected) return;
  412. try
  413. {
  414. var items = ((string)obj).Split(':');
  415. //如果移动距离大于10mm,则提示用户确认
  416. double distance = double.Parse(items[1]);
  417. if (Math.Abs(distance) >= 10)
  418. {
  419. if (MessageBox.Show($"确认要点动距离 {distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  420. {
  421. return;
  422. }
  423. }
  424. Robot.Timeout = 6000000;
  425. switch (items[0])
  426. {
  427. case "X+":
  428. await Robot.JogAsync("X", distance);
  429. break;
  430. case "X-":
  431. await Robot.JogAsync("X", -distance);
  432. break;
  433. case "Y+":
  434. await Robot.JogAsync("Y", distance);
  435. break;
  436. case "Y-":
  437. await Robot.JogAsync("Y", -distance);
  438. break;
  439. case "Z+":
  440. await Robot.JogAsync("Z", distance);
  441. break;
  442. case "Z-":
  443. await Robot.JogAsync("Z", -distance);
  444. break;
  445. case "U+":
  446. await Robot.JogAsync("U", distance);
  447. break;
  448. case "U-":
  449. await Robot.JogAsync("U", -distance);
  450. break;
  451. case "V+":
  452. await Robot.JogAsync("V", distance);
  453. break;
  454. case "V-":
  455. await Robot.JogAsync("V", -distance);
  456. break;
  457. case "W+":
  458. await Robot.JogAsync("W", distance);
  459. break;
  460. case "W-":
  461. await Robot.JogAsync("W", -distance);
  462. break;
  463. default:
  464. break;
  465. }
  466. Robot.Timeout = 5000;
  467. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  468. }
  469. catch (Exception ex)
  470. {
  471. LogHelper.WriteLogError("机器人点位-机器人点动时出错", ex);
  472. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  473. }
  474. }
  475. /// <summary>
  476. /// 执行运动
  477. /// </summary>
  478. /// <param name="obj"></param>
  479. async void ExecuteExecuteMotion(object parameter)
  480. {
  481. if (parameter == null)
  482. {
  483. return;
  484. }
  485. if (SelectedPoint == null)
  486. {
  487. return;
  488. }
  489. var values = (object[])parameter;
  490. var point = new RPoint()
  491. {
  492. X = SelectedPoint.X_Position,
  493. Y = SelectedPoint.Y_Position,
  494. Z = SelectedPoint.Z_Position_Start,
  495. U = SelectedPoint.U_Position,
  496. V = SelectedPoint.R_Position,
  497. };
  498. MotionCommand motionCmd = (MotionCommand)values[1];
  499. if (Robot == null || !Robot.IsConnected) return;
  500. try
  501. {
  502. //确认要执行该点位的该运动指令吗
  503. if (MessageBox.Show($"确认要执行点位 {SelectedPoint.Number} 的 {motionCmd} 指令吗?", "执行点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  504. {
  505. return;
  506. }
  507. if (SelectedIndex == 1)
  508. {
  509. var curpos = await Robot.GetRobotPosAsync();
  510. var addpos = await Robot.GetAdditionalAxisCurrentPosAsync(Robot.AdditionalAxisList[0].Name);
  511. curpos.Z = 0;
  512. await Robot.AdditionalAxisMoveAsync(Robot.AdditionalAxisList[0].Name, PickP1.Z_Position_Stop);
  513. curpos.X = SelectedPoint.X_Position;
  514. curpos.Y = SelectedPoint.Y_Position;
  515. curpos.Z = SelectedPoint.Z_Position_Start;
  516. curpos.U = SelectedPoint.U_Position;
  517. await Robot.CalibMotionAsync(curpos, 0);
  518. }
  519. else
  520. {
  521. //Robot.Timeout = 6000000;
  522. switch (motionCmd)
  523. {
  524. case MotionCommand.Go:
  525. await Robot.GoAsync(point);
  526. break;
  527. case MotionCommand.Jump:
  528. await Robot.JumpAsync(point, 0);
  529. break;
  530. case MotionCommand.Move:
  531. await Robot.MoveAsync(point);
  532. break;
  533. }
  534. }
  535. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  536. }
  537. catch (Exception ex)
  538. {
  539. LogHelper.WriteLogError("机器人点位-执行点位时出错", ex);
  540. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  541. }
  542. }
  543. /// <summary>
  544. /// 示教点位
  545. /// </summary>
  546. /// <param name="point"></param>
  547. async void ExecuteTechPointCommand()
  548. {
  549. if (Robot == null || !Robot.IsConnected) return;
  550. if (SelectedPoint == null) return;
  551. try
  552. {
  553. //如果当前的选项卡处于锁付点位,则提示用户当前模式不允许进行示教
  554. if (SelectedIndex == 4)
  555. {
  556. //如果local为空,则必须先通过相机定位产品,否则无法示教锁付点位
  557. if (toolCoord == null)
  558. {
  559. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "当前模式不允许直接示教贴敷点位,请先通过相机定位产品和定位辅料!", Duration = 1 });
  560. return;
  561. }
  562. //弹窗用户是否确认要示教该点位
  563. if (MessageBox.Show($"确认要示教点位 {SelectedPoint.Number} 吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  564. {
  565. return;
  566. }
  567. double upCameraU = Common.CalculateAngleBetweenPointsDegrees(new PointF((float)_PutPoints[0].X, (float)_PutPoints[0].Y), new PointF((float)_PutPoints[1].X, (float)_PutPoints[1].Y));
  568. var curpos1 = await Robot.GetRobotPosAsync();
  569. //将当前点位转换为产品Local坐标系下的点位
  570. SelectedPoint.X_Position = (float)curpos1.X;
  571. SelectedPoint.Y_Position = (float)curpos1.Y;
  572. SelectedPoint.Z_Position_Start = curpos1.Z;
  573. SelectedPoint.U_Position = curpos1.U;
  574. SelectedPoint.R_Position = curpos1.V;
  575. SelectProduct.FixedUpCameraTempletAngle = _FixedUpCameraTempletAngle;
  576. SelectProduct.MoveDownCamerTempletAngle = upCameraU;
  577. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  578. return;
  579. }
  580. else if (SelectedIndex == 1)
  581. {
  582. //弹窗用户是否确认要示教该点位
  583. if (MessageBox.Show($"确认要示教点位 {SelectedPoint.Number} 吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  584. {
  585. return;
  586. }
  587. var curpos = await Robot.GetRobotPosAsync();
  588. var addpos = await Robot.GetAdditionalAxisCurrentPosAsync(Robot.AdditionalAxisList[0].Name);
  589. SelectedPoint.X_Position = curpos.X;
  590. SelectedPoint.Y_Position = curpos.Y;
  591. SelectedPoint.Z_Position_Start = curpos.Z;
  592. SelectedPoint.U_Position = curpos.U;
  593. SelectedPoint.R_Position = curpos.V;
  594. SelectedPoint.Z_Position_Stop = addpos;
  595. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  596. }
  597. else
  598. {
  599. //弹窗用户是否确认要示教该点位
  600. if (MessageBox.Show($"确认要示教点位 {SelectedPoint.Number} 吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  601. {
  602. return;
  603. }
  604. var curpos = await Robot.GetRobotPosAsync();
  605. SelectedPoint.X_Position = curpos.X;
  606. SelectedPoint.Y_Position = curpos.Y;
  607. SelectedPoint.Z_Position_Start = curpos.Z;
  608. SelectedPoint.U_Position = curpos.U;
  609. SelectedPoint.R_Position = curpos.V;
  610. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  611. }
  612. }
  613. catch (Exception ex)
  614. {
  615. LogHelper.WriteLogError("机器人点位-示教点位时出错", ex);
  616. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  617. }
  618. }
  619. void ExecuteLoadedCommand()
  620. {
  621. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  622. {
  623. IsAllowEdit = false;
  624. }
  625. else
  626. {
  627. IsAllowEdit = true;
  628. }
  629. }
  630. /// <summary>
  631. /// 电机
  632. /// </summary>
  633. /// <param name="obj"></param>
  634. private async void ExecuteMotorCommand(string obj)
  635. {
  636. if (Robot == null || !Robot.IsConnected) return;
  637. try
  638. {
  639. await Robot.MotorAsync(Convert.ToBoolean(obj));
  640. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  641. }
  642. catch (Exception ex)
  643. {
  644. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  645. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  646. }
  647. }
  648. /// <summary>
  649. /// 重置
  650. /// </summary>
  651. private async void ExecuteResetCommand()
  652. {
  653. if (Robot == null || !Robot.IsConnected) return;
  654. try
  655. {
  656. await Robot.ResetAsync();
  657. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  658. }
  659. catch (Exception ex)
  660. {
  661. LogHelper.WriteLogError("重置机器人时出错!", ex);
  662. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  663. }
  664. }
  665. /// <summary>
  666. /// 锁定刹车
  667. /// </summary>
  668. private async void ExecuteSLockCommand()
  669. {
  670. if (Robot == null || !Robot.IsConnected) return;
  671. try
  672. {
  673. await Robot.SLockAsync();
  674. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  675. }
  676. catch (Exception ex)
  677. {
  678. LogHelper.WriteLogError("机器人锁定刹车时出错!", ex);
  679. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  680. }
  681. }
  682. /// <summary>
  683. /// 释放刹车
  684. /// </summary>
  685. private async void ExecutesFreeCommand()
  686. {
  687. if (Robot == null || !Robot.IsConnected) return;
  688. try
  689. {
  690. await Robot.SFreeAsync();
  691. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  692. }
  693. catch (Exception ex)
  694. {
  695. LogHelper.WriteLogError("机器人释放刹车时出错!", ex);
  696. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  697. }
  698. }
  699. private bool CanExecute
  700. {
  701. get
  702. {
  703. // 命令可执行仅当机器人可执行且已选择点位
  704. if (Robot != null && Robot.IsConnected && Robot.CanExecute)
  705. {
  706. return true;
  707. }
  708. return false;
  709. }
  710. }
  711. //是否可以示教或者执行点位
  712. private bool CanExecutePointCommand
  713. {
  714. get
  715. {
  716. if (Robot != null && Robot.IsConnected && Robot.CanExecute)
  717. {
  718. return SelectedPointInDataGrid != null;
  719. }
  720. return false;
  721. }
  722. }
  723. /// <summary>
  724. /// 点位表切换时
  725. /// </summary>
  726. void ExecuteSelectionChangedCommand()
  727. {
  728. switch (SelectedIndex)
  729. {
  730. case 0:
  731. Points = SelectProduct.PickCameraPoints;
  732. break;
  733. case 1:
  734. Points = SelectProduct.PickPoints;
  735. break;
  736. case 2:
  737. Points = SelectProduct.SecPosCameraPoints;
  738. break;
  739. case 3:
  740. Points = SelectProduct.ScrewCameraPoints;
  741. break;
  742. case 4:
  743. Points = SelectProduct.ScrewPoints;
  744. break;
  745. case 5:
  746. Points = SelectProduct.CheckCameraPoints;
  747. break;
  748. default:
  749. break;
  750. }
  751. }
  752. /// <summary>
  753. /// 增加点位
  754. /// </summary>
  755. void ExecuteAddPointCommand()
  756. {
  757. //添加点位
  758. Points.Add(new PlcPoint()
  759. {
  760. Number = Points.Count + 1,
  761. X_Velocity = 100,
  762. Y_Velocity = 100,
  763. Z_Velocity_Start = 100,
  764. Z_Velocity_Stop = 100,
  765. U_Velocity = 100,
  766. R_Velocity = 100,
  767. });
  768. // update commands
  769. _MovePointUpCommand?.RaiseCanExecuteChanged();
  770. _MovePointDownCommand?.RaiseCanExecuteChanged();
  771. }
  772. /// <summary>
  773. /// 移除点位
  774. /// </summary>
  775. void ExecuteDeletePointCommand()
  776. {
  777. if (SelectedPoint == null)
  778. {
  779. return;
  780. }
  781. // 确认删除
  782. if (MessageBox.Show("确认要删除选中的点位吗?", "删除点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  783. {
  784. return;
  785. }
  786. int removedIndex = Points.IndexOf(SelectedPoint);
  787. Points.Remove(SelectedPoint);
  788. //重新编号
  789. for (int i = 0; i < Points.Count; i++)
  790. {
  791. Points[i].Number = i + 1;
  792. }
  793. // adjust selection
  794. if (Points.Count > 0)
  795. {
  796. int newIndex = Math.Min(removedIndex, Points.Count - 1);
  797. SelectedPointInDataGrid = Points[newIndex];
  798. }
  799. else
  800. {
  801. SelectedPointInDataGrid = null;
  802. }
  803. // update commands
  804. _MovePointUpCommand?.RaiseCanExecuteChanged();
  805. _MovePointDownCommand?.RaiseCanExecuteChanged();
  806. }
  807. bool CanExecuteDeletePointCommand()
  808. {
  809. return SelectedPointInDataGrid != null;
  810. }
  811. private bool CanMovePointUp()
  812. {
  813. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) > 0;
  814. }
  815. private bool CanMovePointDown()
  816. {
  817. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) >= 0 && Points.IndexOf(SelectedPointInDataGrid) < Points.Count - 1;
  818. }
  819. /// <summary>
  820. /// 向上移动点位
  821. /// </summary>
  822. void ExecuteMovePointUpCommand()
  823. {
  824. if (SelectedPointInDataGrid == null) return;
  825. // 确认移动
  826. if (MessageBox.Show("确认将选中点位上移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  827. {
  828. return;
  829. }
  830. int idx = Points.IndexOf(SelectedPointInDataGrid);
  831. if (idx <= 0) return;
  832. var item = SelectedPointInDataGrid;
  833. Points.Move(idx, idx - 1);
  834. // renumber
  835. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  836. SelectedPointInDataGrid = item;
  837. _MovePointUpCommand?.RaiseCanExecuteChanged();
  838. _MovePointDownCommand?.RaiseCanExecuteChanged();
  839. }
  840. /// <summary>
  841. /// 向下移动点位
  842. /// </summary>
  843. void ExecuteMovePointDownCommand()
  844. {
  845. if (SelectedPointInDataGrid == null) return;
  846. // 确认移动
  847. if (MessageBox.Show("确认将选中点位下移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  848. {
  849. return;
  850. }
  851. int idx = Points.IndexOf(SelectedPointInDataGrid);
  852. if (idx < 0 || idx >= Points.Count - 1) return;
  853. var item = SelectedPointInDataGrid;
  854. Points.Move(idx, idx + 1);
  855. // renumber
  856. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  857. SelectedPointInDataGrid = item;
  858. _MovePointUpCommand?.RaiseCanExecuteChanged();
  859. _MovePointDownCommand?.RaiseCanExecuteChanged();
  860. }
  861. /// <summary>
  862. /// CAD导入点位
  863. /// </summary>
  864. async void ExecuteImportCadPointCommand()
  865. {
  866. try
  867. {
  868. var view = new DxfView();
  869. //show the dialog
  870. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  871. if (result != null)
  872. {
  873. var cadPoints = view.Points;
  874. if (cadPoints != null && cadPoints.Count > 0)
  875. {
  876. //要导入的开始点编号
  877. int startNumber = view.StartIndex;
  878. //弹窗提示用户是否确认要导入从该编号开始的点位,点位数量为cadPoints.Count
  879. if (MessageBox.Show($"确认要从点位编号 {startNumber} 开始导入 {cadPoints.Count} 个点位吗?", "导入点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  880. {
  881. return;
  882. }
  883. //导入点位
  884. for (int i = 0; i < cadPoints.Count; i++)
  885. {
  886. //如果点位编号已存在,则更新该点位,否则新增点位,只更新XY坐标
  887. var point = cadPoints[i];
  888. var existingPoint = Points.FirstOrDefault(p => p.Number == startNumber + i);
  889. if (existingPoint != null)
  890. {
  891. existingPoint.X_Position = point.X;
  892. existingPoint.Y_Position = point.Y;
  893. }
  894. else
  895. {
  896. Points.Add(new PlcPoint()
  897. {
  898. Number = startNumber + i,
  899. X_Position = point.X,
  900. Y_Position = point.Y,
  901. X_Velocity = 100,
  902. Y_Velocity = 100,
  903. Z_Velocity_Start = 100,
  904. Z_Velocity_Stop = 100,
  905. U_Velocity = 100,
  906. R_Velocity = 100,
  907. });
  908. }
  909. }
  910. //对所有的点位重新编号,确保编号连续
  911. var sortedPoints = Points.OrderBy(p => p.Number).ToList();
  912. Points.Clear();
  913. for (int i = 0; i < sortedPoints.Count; i++)
  914. {
  915. sortedPoints[i].Number = i + 1;
  916. Points.Add(sortedPoints[i]);
  917. }
  918. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  919. }
  920. }
  921. }
  922. catch (Exception ex)
  923. {
  924. LogHelper.WriteLogError("导入CAD点位时出错!", ex);
  925. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  926. }
  927. }
  928. /// <summary>
  929. /// 停止采集
  930. /// </summary>
  931. void ExecuteStopGrabbingCommand()
  932. {
  933. if (Camera != null)
  934. Camera.StopGrabbing();
  935. IsGrap = false;
  936. }
  937. bool CanExecuteStopGrabbingCommand()
  938. {
  939. return IsGrap;
  940. }
  941. /// <summary>
  942. /// 开始采集
  943. /// </summary>
  944. void ExecuteStartGrabbingCommand()
  945. {
  946. if (Camera != null)
  947. Camera.StartGrabbing();
  948. IsGrap = true;
  949. }
  950. bool CanExecuteStartGrabbingCommand()
  951. {
  952. return SelectedCamera != null && !IsGrap;
  953. }
  954. private void Camera_ImageCallbackEvent(ICogImage image, TimeSpan totaltime, string errormessage)
  955. {
  956. Image = image;
  957. App.Current.Dispatcher.Invoke(new
  958. Action(() =>
  959. {
  960. if (errormessage != null && !string.IsNullOrEmpty(errormessage))
  961. {
  962. Message = $"{Lang.耗时}:{totaltime.TotalMilliseconds.ToString("F1")} ms Error:{errormessage}";
  963. }
  964. else
  965. {
  966. Message = $"{Lang.耗时}:{totaltime.TotalMilliseconds.ToString("F1")} ms";
  967. }
  968. }));
  969. }
  970. /// <summary>
  971. /// 去取料
  972. /// </summary>
  973. async void ExecuteGoPickCommand()
  974. {
  975. try
  976. {
  977. if (SelectedPoint == null)
  978. {
  979. return;
  980. }
  981. if (Robot == null || !Robot.IsConnected)
  982. {
  983. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行取料!", Duration = 1 });
  984. return;
  985. }
  986. if (MessageBox.Show("确认要执行取料吗?", "去取料", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  987. {
  988. return;
  989. }
  990. var waiting = new WaitingControl();
  991. //show the dialog
  992. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  993. // 1. 控制机器人移动至拍照点位
  994. var rpoint = new RPoint()
  995. {
  996. X = SelectedPoint.X_Position,
  997. Y = SelectedPoint.Y_Position,
  998. Z = SelectedPoint.Z_Position_Start,
  999. U = SelectedPoint.U_Position,
  1000. V = SelectedPoint.R_Position,
  1001. };
  1002. await Robot.CalibMotionAsync(rpoint, 0);
  1003. await Robot.CalibParameAsync(new PickInfo() { Inhal = Inhal, Blow = 1 }, 20, 20, true, 200, 200);
  1004. //打开吸真空
  1005. await Robot.CalibOutIOAsync(true);
  1006. await Task.Delay(500);
  1007. rpoint.Z = 0;
  1008. await Robot.CalibMotionAsync(rpoint, 0);
  1009. if (DialogHost.IsDialogOpen("RootDialog"))
  1010. {
  1011. DialogHost.Close("RootDialog");
  1012. }
  1013. await task;
  1014. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料完成!", Duration = 0.4 });
  1015. }
  1016. catch (Exception ex)
  1017. {
  1018. LogHelper.WriteLogError("取料时出错!", ex);
  1019. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1020. }
  1021. }
  1022. bool CanExecuteCommandGoPickCommandName()
  1023. {
  1024. return SelectedPoint != null;
  1025. }
  1026. ToolCoord toolCoord = null;
  1027. double _FixedUpCameraTempletAngle = 0;
  1028. OpenCvSharp.Point3f[] _PutPoints = new OpenCvSharp.Point3f[2];
  1029. /// <summary>
  1030. /// 去二次定位
  1031. /// </summary>
  1032. async void ExecuteGoRepositionCommand()
  1033. {
  1034. try
  1035. {
  1036. if (SelectedPoint == null)
  1037. {
  1038. return;
  1039. }
  1040. if (Robot == null || !Robot.IsConnected)
  1041. {
  1042. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行二次定位!", Duration = 1 });
  1043. return;
  1044. }
  1045. if (MessageBox.Show("确认要执行下相机拍产二次定位吗?", "去下相机拍照", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1046. {
  1047. return;
  1048. }
  1049. var waiting = new WaitingControl();
  1050. //show the dialog
  1051. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1052. // 1. 控制机器人移动至拍照点位
  1053. var rpoint = new RPoint()
  1054. {
  1055. X = SelectedPoint.X_Position,
  1056. Y = SelectedPoint.Y_Position,
  1057. Z = SelectedPoint.Z_Position_Start,
  1058. U = SelectedPoint.U_Position,
  1059. V = SelectedPoint.R_Position,
  1060. };
  1061. await Robot.CalibMotionAsync(rpoint, 0);
  1062. await Task.Delay(200);
  1063. //获取固定向上相机1的流程
  1064. var process1 = _productService.GetCurrentProductProcedureModelById(SelectProduct.FixedUpCamera1ProcedureId);
  1065. if (process1 == null)
  1066. {
  1067. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"固定向上相机1视觉流程,流程ID:{SelectProduct.FixedUpCamera1ProcedureId} 未找到!", Duration = 1 });
  1068. if (DialogHost.IsDialogOpen("RootDialog"))
  1069. {
  1070. DialogHost.Close("RootDialog");
  1071. }
  1072. await task;
  1073. return;
  1074. }
  1075. //获取固定向上相机2的流程
  1076. var process2 = _productService.GetCurrentProductProcedureModelById(SelectProduct.FixedUpCamera2ProcedureId);
  1077. if (process2 == null)
  1078. {
  1079. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"固定向上相机2视觉流程,流程ID:{SelectProduct.FixedUpCamera2ProcedureId} 未找到!", Duration = 1 });
  1080. if (DialogHost.IsDialogOpen("RootDialog"))
  1081. {
  1082. DialogHost.Close("RootDialog");
  1083. }
  1084. await task;
  1085. return;
  1086. }
  1087. // 执行视觉任务
  1088. var _cts = new CancellationTokenSource();
  1089. var task1 = _remoteCommandService.ExecutePhotoGetSinglePoint(process1, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
  1090. var task2 = _remoteCommandService.ExecutePhotoGetSinglePoint(process2, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
  1091. var visionResults = await Task.WhenAll(task1, task2);
  1092. if (visionResults[0].IsSucceed && visionResults[1].IsSucceed)
  1093. {
  1094. float toolX = (float)(visionResults[0].X + visionResults[1].X) / 2;
  1095. float toolY = (float)(visionResults[0].Y + visionResults[1].Y) / 2;
  1096. _FixedUpCameraTempletAngle = Common.CalculateAngleBetweenPointsDegrees(new PointF((float)visionResults[0].X, (float)visionResults[0].Y), new PointF((float)visionResults[1].X, (float)visionResults[1].Y));
  1097. toolCoord = new ToolCoord();
  1098. toolCoord.SetTool(toolX, toolY);
  1099. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "视觉定位成功!", Duration = 0.4 });
  1100. if (DialogHost.IsDialogOpen("RootDialog"))
  1101. {
  1102. DialogHost.Close("RootDialog");
  1103. }
  1104. await task;
  1105. return;
  1106. }
  1107. else
  1108. {
  1109. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "视觉定位失败,无法进行二次定位!", Duration = 1 });
  1110. if (DialogHost.IsDialogOpen("RootDialog"))
  1111. {
  1112. DialogHost.Close("RootDialog");
  1113. }
  1114. await task;
  1115. return;
  1116. }
  1117. }
  1118. catch (Exception ex)
  1119. {
  1120. LogHelper.WriteLogError("二次定位时出错!", ex);
  1121. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1122. if (DialogHost.IsDialogOpen("RootDialog"))
  1123. {
  1124. DialogHost.Close("RootDialog");
  1125. }
  1126. }
  1127. }
  1128. /// <summary>
  1129. /// 执行产品拍照
  1130. /// </summary>
  1131. async void ExecuteTakeProductPhotoCommand()
  1132. {
  1133. try
  1134. {
  1135. if (SelectedPoint == null)
  1136. {
  1137. return;
  1138. }
  1139. if (Robot == null || !Robot.IsConnected)
  1140. {
  1141. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行产品拍照!", Duration = 1 });
  1142. return;
  1143. }
  1144. if (MessageBox.Show("确认要执行产品拍照吗?", "去产品位置拍照", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1145. {
  1146. return;
  1147. }
  1148. //如果二次定位的工具坐标为空,则提示用户先进行二次定位
  1149. if (toolCoord == null)
  1150. {
  1151. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "二次定位的工具坐标为空,无法执行产品拍照!", Duration = 1 });
  1152. return;
  1153. }
  1154. var waiting = new WaitingControl();
  1155. //show the dialog
  1156. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1157. // 1. 控制机器人移动至拍照点位
  1158. var rpoint = new RPoint()
  1159. {
  1160. X = Points[0].X_Position,
  1161. Y = Points[0].Y_Position,
  1162. Z = Points[0].Z_Position_Start,
  1163. U = Points[0].U_Position,
  1164. V = Points[0].R_Position,
  1165. };
  1166. await Robot.CalibMotionAsync(rpoint, 0);
  1167. await Task.Delay(200);
  1168. //获取移动向下相机1的流程
  1169. var process1 = _productService.GetCurrentProductProcedureModelById(SelectProduct.MoveDownCamera1LockPhotoProcedureId);
  1170. if (process1 == null)
  1171. {
  1172. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"移动向下相机1视觉流程,流程ID:{SelectProduct.MoveDownCamera1LockPhotoProcedureId} 未找到!", Duration = 1 });
  1173. if (DialogHost.IsDialogOpen("RootDialog"))
  1174. {
  1175. DialogHost.Close("RootDialog");
  1176. }
  1177. await task;
  1178. return;
  1179. }
  1180. // 执行视觉任务
  1181. var _cts = new CancellationTokenSource();
  1182. var visionResult =await _remoteCommandService.ExecutePhotoGetSinglePoint(process1, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
  1183. if (visionResult.IsSucceed)
  1184. {
  1185. _PutPoints[0] = new OpenCvSharp.Point3f((float)visionResult.X, (float)visionResult.Y, (float)visionResult.U);
  1186. }
  1187. else
  1188. {
  1189. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "视觉定位失败,无法进行产品拍照!", Duration = 1 });
  1190. if (DialogHost.IsDialogOpen("RootDialog"))
  1191. {
  1192. DialogHost.Close("RootDialog");
  1193. }
  1194. await task;
  1195. return;
  1196. }
  1197. // 2. 控制机器人移动至拍照点位
  1198. rpoint = new RPoint()
  1199. {
  1200. X = Points[1].X_Position,
  1201. Y = Points[1].Y_Position,
  1202. Z = Points[1].Z_Position_Start,
  1203. U = Points[1].U_Position,
  1204. V = Points[1].R_Position,
  1205. };
  1206. await Robot.CalibMotionAsync(rpoint, 0);
  1207. await Task.Delay(200);
  1208. //获取移动向下相机1的流程
  1209. var process2= _productService.GetCurrentProductProcedureModelById(SelectProduct.MoveDownCamera2LockPhotoProcedureId);
  1210. if (process2 == null)
  1211. {
  1212. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"移动向下相机2视觉流程,流程ID:{SelectProduct.MoveDownCamera2LockPhotoProcedureId} 未找到!", Duration = 1 });
  1213. if (DialogHost.IsDialogOpen("RootDialog"))
  1214. {
  1215. DialogHost.Close("RootDialog");
  1216. }
  1217. await task;
  1218. return;
  1219. }
  1220. // 执行视觉任务
  1221. _cts = new CancellationTokenSource();
  1222. visionResult = await _remoteCommandService.ExecutePhotoGetSinglePoint(process2, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
  1223. if (visionResult.IsSucceed)
  1224. {
  1225. _PutPoints[1] = new OpenCvSharp.Point3f((float)visionResult.X, (float)visionResult.Y, (float)visionResult.U);
  1226. if (DialogHost.IsDialogOpen("RootDialog"))
  1227. {
  1228. DialogHost.Close("RootDialog");
  1229. }
  1230. await task;
  1231. return;
  1232. }
  1233. else
  1234. {
  1235. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "视觉定位失败,无法进行产品拍照!", Duration = 1 });
  1236. if (DialogHost.IsDialogOpen("RootDialog"))
  1237. {
  1238. DialogHost.Close("RootDialog");
  1239. }
  1240. await task;
  1241. return;
  1242. }
  1243. }
  1244. catch (Exception ex)
  1245. {
  1246. LogHelper.WriteLogError("执行产品拍照时出错!", ex);
  1247. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1248. if (DialogHost.IsDialogOpen("RootDialog"))
  1249. {
  1250. DialogHost.Close("RootDialog");
  1251. }
  1252. }
  1253. }
  1254. /// <summary>
  1255. /// 执行产品贴敷
  1256. /// </summary>
  1257. async void ExecuteTakeProductCommand()
  1258. {
  1259. try
  1260. {
  1261. if (SelectedPoint == null)
  1262. {
  1263. return;
  1264. }
  1265. if (Robot == null || !Robot.IsConnected)
  1266. {
  1267. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行产品贴敷!", Duration = 1 });
  1268. return;
  1269. }
  1270. if (MessageBox.Show("确认要执行产品贴敷吗?", "去产品贴敷", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1271. {
  1272. return;
  1273. }
  1274. //如果二次定位的工具坐标为空,则提示用户先进行二次定位
  1275. if (toolCoord == null)
  1276. {
  1277. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "二次定位的工具坐标为空,无法执行产品拍照!", Duration = 1 });
  1278. return;
  1279. }
  1280. if (_PutPoints[0].X == 0 && _PutPoints[0].Y == 0 && _PutPoints[1].X == 0 && _PutPoints[1].Y == 0)
  1281. {
  1282. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "检测到未定位产品", Duration = 1 });
  1283. return;
  1284. }
  1285. var waiting = new WaitingControl();
  1286. //show the dialog
  1287. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1288. double upCameraX = (_PutPoints[0].X + _PutPoints[1].X) / 2;
  1289. double upCameraY = (_PutPoints[0].Y + _PutPoints[1].Y) / 2;
  1290. double upCameraU = Common.CalculateAngleBetweenPointsDegrees(new PointF((float)_PutPoints[0].X, (float)_PutPoints[0].Y), new PointF((float)_PutPoints[1].X, (float)_PutPoints[1].Y));
  1291. var pn = Vector<double>.Build.Dense(new double[] { upCameraX, upCameraY });
  1292. //目标角度:
  1293. double angle = SelectedPoint.U_Position + (_FixedUpCameraTempletAngle - SelectProduct.FixedUpCameraTempletAngle) + (upCameraU - SelectProduct.MoveDownCamerTempletAngle);
  1294. //将_PutPoint转换为Tool0下要走的点
  1295. var p0 = toolCoord.GetTool0Coord(pn, angle);
  1296. // 1. 控制机器人移动至拍照点位
  1297. var rpoint = new RPoint()
  1298. {
  1299. X = (float)p0[0],
  1300. Y = (float)p0[1],
  1301. Z = SelectedPoint.Z_Position_Start,
  1302. U = (float)angle,
  1303. V = SelectedPoint.R_Position,
  1304. };
  1305. await Robot.CalibMotionAsync(rpoint, 0);
  1306. if (DialogHost.IsDialogOpen("RootDialog"))
  1307. {
  1308. DialogHost.Close("RootDialog");
  1309. }
  1310. await task;
  1311. return;
  1312. }
  1313. catch (Exception ex)
  1314. {
  1315. LogHelper.WriteLogError("执行产品贴敷时出错!", ex);
  1316. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1317. if (DialogHost.IsDialogOpen("RootDialog"))
  1318. {
  1319. DialogHost.Close("RootDialog");
  1320. }
  1321. }
  1322. }
  1323. /// <summary>
  1324. /// 附加轴+点动
  1325. /// </summary>
  1326. /// <param name="obj"></param>
  1327. async void ExecuteAdditionalAxisJogPlusCommand(object obj)
  1328. {
  1329. if (Robot == null || !Robot.IsConnected) return;
  1330. try
  1331. {
  1332. if (obj is AxisExtended axis)
  1333. {
  1334. //如果移动距离大于10mm,则提示用户确认
  1335. if (Math.Abs(Distance) >= 10)
  1336. {
  1337. if (MessageBox.Show($"确认要点动距离 {Distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1338. {
  1339. return;
  1340. }
  1341. }
  1342. await Robot.AdditionalAxisJogAsync(axis.Name, Distance);
  1343. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1344. }
  1345. }
  1346. catch (Exception ex)
  1347. {
  1348. LogHelper.WriteLogError("附加轴点动+时出错!", ex);
  1349. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1350. }
  1351. }
  1352. /// <summary>
  1353. /// 附加轴-点动
  1354. /// </summary>
  1355. /// <param name="obj"></param>
  1356. async void ExecuteAdditionalAxisJogMinusCommand(object obj)
  1357. {
  1358. if (Robot == null || !Robot.IsConnected) return;
  1359. try
  1360. {
  1361. if (obj is AxisExtended axis)
  1362. {
  1363. //如果移动距离大于10mm,则提示用户确认
  1364. if (Math.Abs(Distance) >= 10)
  1365. {
  1366. if (MessageBox.Show($"确认要点动距离 {Distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1367. {
  1368. return;
  1369. }
  1370. }
  1371. await Robot.AdditionalAxisJogNegAsync(axis.Name, Distance);
  1372. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1373. }
  1374. }
  1375. catch (Exception ex)
  1376. {
  1377. LogHelper.WriteLogError("附加轴点动-时出错!", ex);
  1378. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1379. }
  1380. }
  1381. /// <summary>
  1382. /// 附加轴下使能
  1383. /// </summary>
  1384. /// <param name="obj"></param>
  1385. async void ExecuteAdditionalAxisMotorOffCommand(object obj)
  1386. {
  1387. if (Robot == null || !Robot.IsConnected) return;
  1388. try
  1389. {
  1390. if (obj is AxisExtended axis)
  1391. {
  1392. await Robot.AdditionalAxisMotorAsync(axis.Name, false);
  1393. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1394. }
  1395. }
  1396. catch (Exception ex)
  1397. {
  1398. LogHelper.WriteLogError("附加轴上使能时出错!", ex);
  1399. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1400. }
  1401. }
  1402. /// <summary>
  1403. /// 附加轴上使能
  1404. /// </summary>
  1405. /// <param name="obj"></param>
  1406. async void ExecuteAdditionalAxisMotorOnCommand(object obj)
  1407. {
  1408. if (Robot == null || !Robot.IsConnected) return;
  1409. try
  1410. {
  1411. if (obj is AxisExtended axis)
  1412. {
  1413. await Robot.AdditionalAxisMotorAsync(axis.Name, true);
  1414. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1415. }
  1416. }
  1417. catch (Exception ex)
  1418. {
  1419. LogHelper.WriteLogError("附加轴下使能时出错!", ex);
  1420. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1421. }
  1422. }
  1423. /// <summary>
  1424. /// 移动取料托盘点
  1425. /// </summary>
  1426. async void ExecutePickMoveCommand(string parameter)
  1427. {
  1428. if (Robot == null || !Robot.IsConnected) return;
  1429. try
  1430. {
  1431. //弹窗用户是否确认要示教该点位
  1432. if (MessageBox.Show($"确认要移动取料点位 {parameter} 吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1433. {
  1434. return;
  1435. }
  1436. int index = int.Parse(parameter);
  1437. var curpos = await Robot.GetRobotPosAsync();
  1438. var addpos = await Robot.GetAdditionalAxisCurrentPosAsync(Robot.AdditionalAxisList[0].Name);
  1439. if (index == 1)
  1440. {
  1441. curpos.Z = 0;
  1442. await Robot.CalibMotionAsync(curpos, 0);
  1443. await Robot.AdditionalAxisMoveAsync(Robot.AdditionalAxisList[0].Name, PickP1.Z_Position_Stop);
  1444. curpos.X = PickP1.X_Position;
  1445. curpos.Y = PickP1.Y_Position;
  1446. curpos.Z = PickP1.Z_Position_Start;
  1447. curpos.U = PickP1.U_Position;
  1448. await Robot.CalibMotionAsync(curpos, 0);
  1449. }
  1450. else if (index == 2)
  1451. {
  1452. curpos.Z = 0;
  1453. await Robot.CalibMotionAsync(curpos, 0);
  1454. await Robot.AdditionalAxisMoveAsync(Robot.AdditionalAxisList[0].Name, PickP2.Z_Position_Stop);
  1455. curpos.X = PickP2.X_Position;
  1456. curpos.Y = PickP2.Y_Position;
  1457. curpos.Z = PickP2.Z_Position_Start;
  1458. curpos.U = PickP2.U_Position;
  1459. await Robot.CalibMotionAsync(curpos, 0);
  1460. }
  1461. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1462. }
  1463. catch (Exception ex)
  1464. {
  1465. LogHelper.WriteLogError("示教取料托盘点时出错!", ex);
  1466. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1467. }
  1468. }
  1469. /// <summary>
  1470. /// 示教取料托盘点
  1471. /// </summary>
  1472. /// <param name="parameter"></param>
  1473. async void ExecutePickTechCommand(string parameter)
  1474. {
  1475. if (Robot == null || !Robot.IsConnected) return;
  1476. try
  1477. {
  1478. //弹窗用户是否确认要示教该点位
  1479. if (MessageBox.Show($"确认要示教取料点位 {parameter} 吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1480. {
  1481. return;
  1482. }
  1483. int index = int.Parse(parameter);
  1484. var curpos = await Robot.GetRobotPosAsync();
  1485. var addpos = await Robot.GetAdditionalAxisCurrentPosAsync(Robot.AdditionalAxisList[0].Name);
  1486. if (index == 1)
  1487. {
  1488. PickP1.X_Position = curpos.X;
  1489. PickP1.Y_Position = curpos.Y;
  1490. PickP1.Z_Position_Start = curpos.Z;
  1491. PickP1.U_Position = curpos.U;
  1492. PickP1.Z_Position_Stop = addpos;
  1493. }
  1494. else if (index == 2)
  1495. {
  1496. PickP2.X_Position = curpos.X;
  1497. PickP2.Y_Position = curpos.Y;
  1498. PickP2.Z_Position_Start = curpos.Z;
  1499. PickP2.U_Position = curpos.U;
  1500. PickP2.Z_Position_Stop = addpos;
  1501. }
  1502. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1503. }
  1504. catch (Exception ex)
  1505. {
  1506. LogHelper.WriteLogError("示教取料托盘点时出错!", ex);
  1507. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1508. }
  1509. }
  1510. /// <summary>
  1511. /// 计算所有托盘点
  1512. /// </summary>
  1513. void ExecuteCalPickPosCommand()
  1514. {
  1515. try
  1516. {
  1517. if (ColumnNum < 1)
  1518. {
  1519. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"请设置托盘列数!", Duration = 1 });
  1520. return;
  1521. }
  1522. //弹窗用户是否确认要示教该点位
  1523. if (MessageBox.Show($"确认要计算托盘 吗?", "计算托盘", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1524. {
  1525. return;
  1526. }
  1527. //托盘间隔
  1528. double invt = (PickP2.Z_Position_Stop - PickP1.Z_Position_Stop) / (ColumnNum - 1);
  1529. for (int i = 0; i < ColumnNum; i++)
  1530. {
  1531. if (Points.Count - 1 <= i)
  1532. {
  1533. Points[i].Number = i + 1;
  1534. Points[i].X_Position = PickP1.X_Position;
  1535. Points[i].X_Velocity = 100;
  1536. Points[i].Y_Position = PickP1.Y_Position;
  1537. Points[i].Y_Velocity = 100;
  1538. Points[i].Z_Position_Start = PickP1.Z_Position_Start;
  1539. Points[i].Z_Velocity_Start = 100;
  1540. Points[i].U_Position = PickP1.U_Position;
  1541. Points[i].U_Velocity = 100;
  1542. Points[i].Z_Position_Stop = (float)(PickP1.Z_Position_Stop + i * invt);
  1543. Points[i].Z_Velocity_Stop = 100;
  1544. }
  1545. else
  1546. {
  1547. Points.Add(new PlcPoint()
  1548. {
  1549. Number= i + 1,
  1550. X_Position = PickP1.X_Position,
  1551. X_Velocity = 100,
  1552. Y_Position = PickP1.Y_Position,
  1553. Y_Velocity = 100,
  1554. Z_Position_Start = PickP1.Z_Position_Start,
  1555. Z_Velocity_Start = 100,
  1556. U_Position = PickP1.U_Position,
  1557. U_Velocity = 100,
  1558. Z_Position_Stop = (float)(PickP1.Z_Position_Stop + i * invt),
  1559. Z_Velocity_Stop = 100
  1560. });
  1561. }
  1562. }
  1563. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1564. }
  1565. catch (Exception ex)
  1566. {
  1567. LogHelper.WriteLogError("示教取料托盘点时出错!", ex);
  1568. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1569. }
  1570. }
  1571. #endregion
  1572. #region 继承
  1573. /// <summary>
  1574. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  1575. /// </summary>
  1576. /// <param name="navigationContext"></param>
  1577. /// <param name="continuationCallback"></param>
  1578. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  1579. {
  1580. continuationCallback(true);
  1581. }
  1582. /// <summary>
  1583. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  1584. /// </summary>
  1585. /// <param name="navigationContext"></param>
  1586. /// <exception cref="NotImplementedException"></exception>
  1587. public async void OnNavigatedTo(NavigationContext navigationContext)
  1588. {
  1589. try
  1590. {
  1591. toolCoord = null;
  1592. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  1593. SelectedIndex = 0;
  1594. Points = SelectProduct.PickCameraPoints;
  1595. if (Robot == null)
  1596. {
  1597. Robot = _robotService.GetRobotByNumber(1) as XYZU_Robot;
  1598. }
  1599. if (Robot != null && Robot.IsConnected)
  1600. {
  1601. this.Distance = _configService.GetRobotStepDistance(Robot.Id);
  1602. Robot.EnterDebugMode = true;
  1603. AdditionalAxisList = Robot.AdditionalAxisList;
  1604. }
  1605. else if (Robot != null)
  1606. {
  1607. Robot.EnterDebugMode = true;
  1608. }
  1609. ProcedureModels = new ObservableCollection<ProcedureModel>();
  1610. foreach (var item in SelectProduct.CameraProcedures)
  1611. {
  1612. foreach (var item1 in item.ProcedureModels)
  1613. {
  1614. ProcedureModels.Add(item1);
  1615. }
  1616. }
  1617. CameraList = new ObservableCollection<CameraInfo>(_configService.GetAllCameras());
  1618. if (SelectProduct.PickPoints!=null && SelectProduct.PickPoints.Count>0)
  1619. {
  1620. PickP1 = SelectProduct.PickPoints.FirstOrDefault().Clone();
  1621. PickP2 = SelectProduct.PickPoints.LastOrDefault().Clone();
  1622. ColumnNum = SelectProduct.PickPoints.Count;
  1623. }
  1624. }
  1625. catch (Exception ex)
  1626. {
  1627. LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
  1628. }
  1629. }
  1630. /// <summary>
  1631. /// 是否允许导航到此视图模型。
  1632. /// </summary>
  1633. /// <param name="navigationContext"></param>
  1634. /// <returns></returns>
  1635. public bool IsNavigationTarget(NavigationContext navigationContext)
  1636. {
  1637. return true;
  1638. }
  1639. /// <summary>
  1640. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  1641. /// </summary>
  1642. /// <param name="navigationContext"></param>
  1643. public void OnNavigatedFrom(NavigationContext navigationContext)
  1644. {
  1645. //if (Robot != null)
  1646. //{
  1647. // Robot.EnterDebugMode = false;
  1648. //}
  1649. try
  1650. {
  1651. IsGrap = false;
  1652. if (Camera != null)
  1653. {
  1654. if (Camera.IsGrabbing)
  1655. {
  1656. Camera.StopGrabbing();
  1657. }
  1658. Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
  1659. Camera = null;
  1660. }
  1661. SelectedCamera = null;
  1662. IsLeftDrawerOpen = false;
  1663. }
  1664. catch (Exception)
  1665. {
  1666. }
  1667. }
  1668. #endregion
  1669. private bool _IsAllowEdit = false;
  1670. public bool IsAllowEdit
  1671. {
  1672. get { return _IsAllowEdit; }
  1673. set { SetProperty(ref _IsAllowEdit, value); }
  1674. }
  1675. private void LoginChange(Models.User user)
  1676. {
  1677. if (user.userPart == Enums.UserPart.Operator)
  1678. {
  1679. IsAllowEdit = false;
  1680. }
  1681. else
  1682. {
  1683. IsAllowEdit = true;
  1684. }
  1685. }
  1686. }
  1687. }