PlcPointParamsViewModel.cs 82 KB

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