PlcPointParamsViewModel.cs 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970
  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
  588. {
  589. //弹窗用户是否确认要示教该点位
  590. if (MessageBox.Show($"确认要示教点位 {SelectedPoint.Number} 吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  591. {
  592. return;
  593. }
  594. var curpos = await Robot.GetRobotPosAsync();
  595. SelectedPoint.X_Position = curpos.X;
  596. SelectedPoint.Y_Position = curpos.Y;
  597. SelectedPoint.Z_Position_Start = curpos.Z;
  598. SelectedPoint.U_Position = curpos.U;
  599. SelectedPoint.R_Position = curpos.V;
  600. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  601. }
  602. }
  603. catch (Exception ex)
  604. {
  605. LogHelper.WriteLogError("机器人点位-示教点位时出错", ex);
  606. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  607. }
  608. }
  609. void ExecuteLoadedCommand()
  610. {
  611. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  612. {
  613. IsAllowEdit = false;
  614. }
  615. else
  616. {
  617. IsAllowEdit = true;
  618. }
  619. }
  620. /// <summary>
  621. /// 电机
  622. /// </summary>
  623. /// <param name="obj"></param>
  624. private async void ExecuteMotorCommand(string obj)
  625. {
  626. if (Robot == null || !Robot.IsConnected) return;
  627. try
  628. {
  629. await Robot.MotorAsync(Convert.ToBoolean(obj));
  630. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  631. }
  632. catch (Exception ex)
  633. {
  634. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  635. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  636. }
  637. }
  638. /// <summary>
  639. /// 重置
  640. /// </summary>
  641. private async void ExecuteResetCommand()
  642. {
  643. if (Robot == null || !Robot.IsConnected) return;
  644. try
  645. {
  646. await Robot.ResetAsync();
  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 ExecuteSLockCommand()
  659. {
  660. if (Robot == null || !Robot.IsConnected) return;
  661. try
  662. {
  663. await Robot.SLockAsync();
  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 ExecutesFreeCommand()
  676. {
  677. if (Robot == null || !Robot.IsConnected) return;
  678. try
  679. {
  680. await Robot.SFreeAsync();
  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. private bool CanExecute
  690. {
  691. get
  692. {
  693. // 命令可执行仅当机器人可执行且已选择点位
  694. if (Robot != null && Robot.IsConnected && Robot.CanExecute)
  695. {
  696. return true;
  697. }
  698. return false;
  699. }
  700. }
  701. //是否可以示教或者执行点位
  702. private bool CanExecutePointCommand
  703. {
  704. get
  705. {
  706. if (Robot != null && Robot.IsConnected && Robot.CanExecute)
  707. {
  708. return SelectedPointInDataGrid != null;
  709. }
  710. return false;
  711. }
  712. }
  713. /// <summary>
  714. /// 点位表切换时
  715. /// </summary>
  716. void ExecuteSelectionChangedCommand()
  717. {
  718. switch (SelectedIndex)
  719. {
  720. case 0:
  721. Points = SelectProduct.PickCameraPoints;
  722. break;
  723. case 1:
  724. Points = SelectProduct.PickPoints;
  725. break;
  726. case 2:
  727. Points = SelectProduct.SecPosCameraPoints;
  728. break;
  729. case 3:
  730. Points = SelectProduct.ScrewCameraPoints;
  731. break;
  732. case 4:
  733. Points = SelectProduct.ScrewPoints;
  734. break;
  735. case 5:
  736. Points = SelectProduct.CheckCameraPoints;
  737. break;
  738. default:
  739. break;
  740. }
  741. }
  742. /// <summary>
  743. /// 增加点位
  744. /// </summary>
  745. void ExecuteAddPointCommand()
  746. {
  747. //添加点位
  748. Points.Add(new PlcPoint()
  749. {
  750. Number = Points.Count + 1,
  751. X_Velocity = 100,
  752. Y_Velocity = 100,
  753. Z_Velocity_Start = 100,
  754. Z_Velocity_Stop = 100,
  755. U_Velocity = 100,
  756. R_Velocity = 100,
  757. });
  758. // update commands
  759. _MovePointUpCommand?.RaiseCanExecuteChanged();
  760. _MovePointDownCommand?.RaiseCanExecuteChanged();
  761. }
  762. /// <summary>
  763. /// 移除点位
  764. /// </summary>
  765. void ExecuteDeletePointCommand()
  766. {
  767. if (SelectedPoint == null)
  768. {
  769. return;
  770. }
  771. // 确认删除
  772. if (MessageBox.Show("确认要删除选中的点位吗?", "删除点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  773. {
  774. return;
  775. }
  776. int removedIndex = Points.IndexOf(SelectedPoint);
  777. Points.Remove(SelectedPoint);
  778. //重新编号
  779. for (int i = 0; i < Points.Count; i++)
  780. {
  781. Points[i].Number = i + 1;
  782. }
  783. // adjust selection
  784. if (Points.Count > 0)
  785. {
  786. int newIndex = Math.Min(removedIndex, Points.Count - 1);
  787. SelectedPointInDataGrid = Points[newIndex];
  788. }
  789. else
  790. {
  791. SelectedPointInDataGrid = null;
  792. }
  793. // update commands
  794. _MovePointUpCommand?.RaiseCanExecuteChanged();
  795. _MovePointDownCommand?.RaiseCanExecuteChanged();
  796. }
  797. bool CanExecuteDeletePointCommand()
  798. {
  799. return SelectedPointInDataGrid != null;
  800. }
  801. private bool CanMovePointUp()
  802. {
  803. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) > 0;
  804. }
  805. private bool CanMovePointDown()
  806. {
  807. return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) >= 0 && Points.IndexOf(SelectedPointInDataGrid) < Points.Count - 1;
  808. }
  809. /// <summary>
  810. /// 向上移动点位
  811. /// </summary>
  812. void ExecuteMovePointUpCommand()
  813. {
  814. if (SelectedPointInDataGrid == null) return;
  815. // 确认移动
  816. if (MessageBox.Show("确认将选中点位上移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  817. {
  818. return;
  819. }
  820. int idx = Points.IndexOf(SelectedPointInDataGrid);
  821. if (idx <= 0) return;
  822. var item = SelectedPointInDataGrid;
  823. Points.Move(idx, idx - 1);
  824. // renumber
  825. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  826. SelectedPointInDataGrid = item;
  827. _MovePointUpCommand?.RaiseCanExecuteChanged();
  828. _MovePointDownCommand?.RaiseCanExecuteChanged();
  829. }
  830. /// <summary>
  831. /// 向下移动点位
  832. /// </summary>
  833. void ExecuteMovePointDownCommand()
  834. {
  835. if (SelectedPointInDataGrid == null) return;
  836. // 确认移动
  837. if (MessageBox.Show("确认将选中点位下移一位吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  838. {
  839. return;
  840. }
  841. int idx = Points.IndexOf(SelectedPointInDataGrid);
  842. if (idx < 0 || idx >= Points.Count - 1) return;
  843. var item = SelectedPointInDataGrid;
  844. Points.Move(idx, idx + 1);
  845. // renumber
  846. for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
  847. SelectedPointInDataGrid = item;
  848. _MovePointUpCommand?.RaiseCanExecuteChanged();
  849. _MovePointDownCommand?.RaiseCanExecuteChanged();
  850. }
  851. /// <summary>
  852. /// CAD导入点位
  853. /// </summary>
  854. async void ExecuteImportCadPointCommand()
  855. {
  856. try
  857. {
  858. var view = new DxfView();
  859. //show the dialog
  860. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  861. if (result != null)
  862. {
  863. var cadPoints = view.Points;
  864. if (cadPoints != null && cadPoints.Count > 0)
  865. {
  866. //要导入的开始点编号
  867. int startNumber = view.StartIndex;
  868. //弹窗提示用户是否确认要导入从该编号开始的点位,点位数量为cadPoints.Count
  869. if (MessageBox.Show($"确认要从点位编号 {startNumber} 开始导入 {cadPoints.Count} 个点位吗?", "导入点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  870. {
  871. return;
  872. }
  873. //导入点位
  874. for (int i = 0; i < cadPoints.Count; i++)
  875. {
  876. //如果点位编号已存在,则更新该点位,否则新增点位,只更新XY坐标
  877. var point = cadPoints[i];
  878. var existingPoint = Points.FirstOrDefault(p => p.Number == startNumber + i);
  879. if (existingPoint != null)
  880. {
  881. existingPoint.X_Position = point.X;
  882. existingPoint.Y_Position = point.Y;
  883. }
  884. else
  885. {
  886. Points.Add(new PlcPoint()
  887. {
  888. Number = startNumber + i,
  889. X_Position = point.X,
  890. Y_Position = point.Y,
  891. X_Velocity = 100,
  892. Y_Velocity = 100,
  893. Z_Velocity_Start = 100,
  894. Z_Velocity_Stop = 100,
  895. U_Velocity = 100,
  896. R_Velocity = 100,
  897. });
  898. }
  899. }
  900. //对所有的点位重新编号,确保编号连续
  901. var sortedPoints = Points.OrderBy(p => p.Number).ToList();
  902. Points.Clear();
  903. for (int i = 0; i < sortedPoints.Count; i++)
  904. {
  905. sortedPoints[i].Number = i + 1;
  906. Points.Add(sortedPoints[i]);
  907. }
  908. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  909. }
  910. }
  911. }
  912. catch (Exception ex)
  913. {
  914. LogHelper.WriteLogError("导入CAD点位时出错!", ex);
  915. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  916. }
  917. }
  918. /// <summary>
  919. /// 停止采集
  920. /// </summary>
  921. void ExecuteStopGrabbingCommand()
  922. {
  923. if (Camera != null)
  924. Camera.StopGrabbing();
  925. IsGrap = false;
  926. }
  927. bool CanExecuteStopGrabbingCommand()
  928. {
  929. return IsGrap;
  930. }
  931. /// <summary>
  932. /// 开始采集
  933. /// </summary>
  934. void ExecuteStartGrabbingCommand()
  935. {
  936. if (Camera != null)
  937. Camera.StartGrabbing();
  938. IsGrap = true;
  939. }
  940. bool CanExecuteStartGrabbingCommand()
  941. {
  942. return SelectedCamera != null && !IsGrap;
  943. }
  944. private void Camera_ImageCallbackEvent(ICogImage image, TimeSpan totaltime, string errormessage)
  945. {
  946. Image = image;
  947. App.Current.Dispatcher.Invoke(new
  948. Action(() =>
  949. {
  950. if (errormessage != null && !string.IsNullOrEmpty(errormessage))
  951. {
  952. Message = $"{Lang.耗时}:{totaltime.TotalMilliseconds.ToString("F1")} ms Error:{errormessage}";
  953. }
  954. else
  955. {
  956. Message = $"{Lang.耗时}:{totaltime.TotalMilliseconds.ToString("F1")} ms";
  957. }
  958. }));
  959. }
  960. /// <summary>
  961. /// 去取料
  962. /// </summary>
  963. async void ExecuteGoPickCommand()
  964. {
  965. try
  966. {
  967. if (SelectedPoint == null)
  968. {
  969. return;
  970. }
  971. if (Robot == null || !Robot.IsConnected)
  972. {
  973. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行取料!", Duration = 1 });
  974. return;
  975. }
  976. if (MessageBox.Show("确认要执行取料吗?", "去取料", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  977. {
  978. return;
  979. }
  980. var waiting = new WaitingControl();
  981. //show the dialog
  982. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  983. // 1. 控制机器人移动至拍照点位
  984. var rpoint = new RPoint()
  985. {
  986. X = SelectedPoint.X_Position,
  987. Y = SelectedPoint.Y_Position,
  988. Z = SelectedPoint.Z_Position_Start,
  989. U = SelectedPoint.U_Position,
  990. V = SelectedPoint.R_Position,
  991. };
  992. await Robot.CalibMotionAsync(rpoint, null);
  993. await Robot.CalibParameAsync(new PickInfo() { Inhal = Inhal, Blow = 1 }, 20, 20, true, 200, 200);
  994. //打开吸真空
  995. await Robot.CalibOutIOAsync(true);
  996. await Task.Delay(500);
  997. rpoint.Z = 0;
  998. await Robot.CalibMotionAsync(rpoint, null);
  999. if (DialogHost.IsDialogOpen("RootDialog"))
  1000. {
  1001. DialogHost.Close("RootDialog");
  1002. }
  1003. await task;
  1004. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料完成!", Duration = 0.4 });
  1005. }
  1006. catch (Exception ex)
  1007. {
  1008. LogHelper.WriteLogError("取料时出错!", ex);
  1009. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1010. }
  1011. }
  1012. bool CanExecuteCommandGoPickCommandName()
  1013. {
  1014. return SelectedPoint != null;
  1015. }
  1016. ToolCoord toolCoord = null;
  1017. double _FixedUpCameraTempletAngle = 0;
  1018. OpenCvSharp.Point3f[] _PutPoints = new OpenCvSharp.Point3f[2];
  1019. /// <summary>
  1020. /// 去二次定位
  1021. /// </summary>
  1022. async void ExecuteGoRepositionCommand()
  1023. {
  1024. try
  1025. {
  1026. if (SelectedPoint == null)
  1027. {
  1028. return;
  1029. }
  1030. if (Robot == null || !Robot.IsConnected)
  1031. {
  1032. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行二次定位!", Duration = 1 });
  1033. return;
  1034. }
  1035. if (MessageBox.Show("确认要执行下相机拍产二次定位吗?", "去下相机拍照", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1036. {
  1037. return;
  1038. }
  1039. var waiting = new WaitingControl();
  1040. //show the dialog
  1041. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1042. // 1. 控制机器人移动至拍照点位
  1043. var rpoint = new RPoint()
  1044. {
  1045. X = SelectedPoint.X_Position,
  1046. Y = SelectedPoint.Y_Position,
  1047. Z = SelectedPoint.Z_Position_Start,
  1048. U = SelectedPoint.U_Position,
  1049. V = SelectedPoint.R_Position,
  1050. };
  1051. await Robot.CalibMotionAsync(rpoint, null);
  1052. await Task.Delay(200);
  1053. //获取固定向上相机1的流程
  1054. var process1 = _productService.GetCurrentProductProcedureModelById(SelectProduct.FixedUpCamera1ProcedureId);
  1055. if (process1 == null)
  1056. {
  1057. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"固定向上相机1视觉流程,流程ID:{SelectProduct.FixedUpCamera1ProcedureId} 未找到!", Duration = 1 });
  1058. if (DialogHost.IsDialogOpen("RootDialog"))
  1059. {
  1060. DialogHost.Close("RootDialog");
  1061. }
  1062. await task;
  1063. return;
  1064. }
  1065. //获取固定向上相机2的流程
  1066. var process2 = _productService.GetCurrentProductProcedureModelById(SelectProduct.FixedUpCamera2ProcedureId);
  1067. if (process2 == null)
  1068. {
  1069. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"固定向上相机2视觉流程,流程ID:{SelectProduct.FixedUpCamera2ProcedureId} 未找到!", Duration = 1 });
  1070. if (DialogHost.IsDialogOpen("RootDialog"))
  1071. {
  1072. DialogHost.Close("RootDialog");
  1073. }
  1074. await task;
  1075. return;
  1076. }
  1077. // 执行视觉任务
  1078. var _cts = new CancellationTokenSource();
  1079. var task1 = _remoteCommandService.ExecutePhotoGetSinglePoint(process1, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
  1080. var task2 = _remoteCommandService.ExecutePhotoGetSinglePoint(process2, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
  1081. var visionResults = await Task.WhenAll(task1, task2);
  1082. if (visionResults[0].IsSucceed && visionResults[1].IsSucceed)
  1083. {
  1084. float toolX = (float)(visionResults[0].X + visionResults[1].X) / 2;
  1085. float toolY = (float)(visionResults[0].Y + visionResults[1].Y) / 2;
  1086. _FixedUpCameraTempletAngle = Common.CalculateAngleBetweenPointsDegrees(new PointF((float)visionResults[0].X, (float)visionResults[0].Y), new PointF((float)visionResults[1].X, (float)visionResults[1].Y));
  1087. toolCoord = new ToolCoord();
  1088. toolCoord.SetTool(toolX, toolY);
  1089. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "视觉定位成功!", Duration = 0.4 });
  1090. if (DialogHost.IsDialogOpen("RootDialog"))
  1091. {
  1092. DialogHost.Close("RootDialog");
  1093. }
  1094. await task;
  1095. return;
  1096. }
  1097. else
  1098. {
  1099. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "视觉定位失败,无法进行二次定位!", Duration = 1 });
  1100. if (DialogHost.IsDialogOpen("RootDialog"))
  1101. {
  1102. DialogHost.Close("RootDialog");
  1103. }
  1104. await task;
  1105. return;
  1106. }
  1107. }
  1108. catch (Exception ex)
  1109. {
  1110. LogHelper.WriteLogError("二次定位时出错!", ex);
  1111. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1112. if (DialogHost.IsDialogOpen("RootDialog"))
  1113. {
  1114. DialogHost.Close("RootDialog");
  1115. }
  1116. }
  1117. }
  1118. /// <summary>
  1119. /// 执行产品拍照
  1120. /// </summary>
  1121. async void ExecuteTakeProductPhotoCommand()
  1122. {
  1123. try
  1124. {
  1125. if (SelectedPoint == null)
  1126. {
  1127. return;
  1128. }
  1129. if (Robot == null || !Robot.IsConnected)
  1130. {
  1131. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行产品拍照!", Duration = 1 });
  1132. return;
  1133. }
  1134. if (MessageBox.Show("确认要执行产品拍照吗?", "去产品位置拍照", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1135. {
  1136. return;
  1137. }
  1138. //如果二次定位的工具坐标为空,则提示用户先进行二次定位
  1139. if (toolCoord == null)
  1140. {
  1141. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "二次定位的工具坐标为空,无法执行产品拍照!", Duration = 1 });
  1142. return;
  1143. }
  1144. var waiting = new WaitingControl();
  1145. //show the dialog
  1146. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1147. // 1. 控制机器人移动至拍照点位
  1148. var rpoint = new RPoint()
  1149. {
  1150. X = Points[0].X_Position,
  1151. Y = Points[0].Y_Position,
  1152. Z = Points[0].Z_Position_Start,
  1153. U = Points[0].U_Position,
  1154. V = Points[0].R_Position,
  1155. };
  1156. await Robot.CalibMotionAsync(rpoint, null);
  1157. await Task.Delay(200);
  1158. //获取移动向下相机1的流程
  1159. var process1 = _productService.GetCurrentProductProcedureModelById(SelectProduct.MoveDownCamera1LockPhotoProcedureId);
  1160. if (process1 == null)
  1161. {
  1162. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"移动向下相机1视觉流程,流程ID:{SelectProduct.MoveDownCamera1LockPhotoProcedureId} 未找到!", Duration = 1 });
  1163. if (DialogHost.IsDialogOpen("RootDialog"))
  1164. {
  1165. DialogHost.Close("RootDialog");
  1166. }
  1167. await task;
  1168. return;
  1169. }
  1170. // 执行视觉任务
  1171. var _cts = new CancellationTokenSource();
  1172. var visionResult = await _remoteCommandService.ExecutePhotoGetSinglePoint(process1, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
  1173. if (visionResult.IsSucceed)
  1174. {
  1175. _PutPoints[0] = new OpenCvSharp.Point3f((float)visionResult.X, (float)visionResult.Y, (float)visionResult.U);
  1176. }
  1177. else
  1178. {
  1179. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "视觉定位失败,无法进行产品拍照!", Duration = 1 });
  1180. if (DialogHost.IsDialogOpen("RootDialog"))
  1181. {
  1182. DialogHost.Close("RootDialog");
  1183. }
  1184. await task;
  1185. return;
  1186. }
  1187. // 2. 控制机器人移动至拍照点位
  1188. rpoint = new RPoint()
  1189. {
  1190. X = Points[1].X_Position,
  1191. Y = Points[1].Y_Position,
  1192. Z = Points[1].Z_Position_Start,
  1193. U = Points[1].U_Position,
  1194. V = Points[1].R_Position,
  1195. };
  1196. await Robot.CalibMotionAsync(rpoint, null);
  1197. await Task.Delay(200);
  1198. //获取移动向下相机1的流程
  1199. var process2 = _productService.GetCurrentProductProcedureModelById(SelectProduct.MoveDownCamera2LockPhotoProcedureId);
  1200. if (process2 == null)
  1201. {
  1202. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"移动向下相机2视觉流程,流程ID:{SelectProduct.MoveDownCamera2LockPhotoProcedureId} 未找到!", Duration = 1 });
  1203. if (DialogHost.IsDialogOpen("RootDialog"))
  1204. {
  1205. DialogHost.Close("RootDialog");
  1206. }
  1207. await task;
  1208. return;
  1209. }
  1210. // 执行视觉任务
  1211. _cts = new CancellationTokenSource();
  1212. visionResult = await _remoteCommandService.ExecutePhotoGetSinglePoint(process2, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
  1213. if (visionResult.IsSucceed)
  1214. {
  1215. _PutPoints[1] = new OpenCvSharp.Point3f((float)visionResult.X, (float)visionResult.Y, (float)visionResult.U);
  1216. if (DialogHost.IsDialogOpen("RootDialog"))
  1217. {
  1218. DialogHost.Close("RootDialog");
  1219. }
  1220. await task;
  1221. return;
  1222. }
  1223. else
  1224. {
  1225. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "视觉定位失败,无法进行产品拍照!", Duration = 1 });
  1226. if (DialogHost.IsDialogOpen("RootDialog"))
  1227. {
  1228. DialogHost.Close("RootDialog");
  1229. }
  1230. await task;
  1231. return;
  1232. }
  1233. }
  1234. catch (Exception ex)
  1235. {
  1236. LogHelper.WriteLogError("执行产品拍照时出错!", ex);
  1237. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1238. if (DialogHost.IsDialogOpen("RootDialog"))
  1239. {
  1240. DialogHost.Close("RootDialog");
  1241. }
  1242. }
  1243. }
  1244. /// <summary>
  1245. /// 执行产品贴敷
  1246. /// </summary>
  1247. async void ExecuteTakeProductCommand()
  1248. {
  1249. try
  1250. {
  1251. if (SelectedPoint == null)
  1252. {
  1253. return;
  1254. }
  1255. if (Robot == null || !Robot.IsConnected)
  1256. {
  1257. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行产品贴敷!", Duration = 1 });
  1258. return;
  1259. }
  1260. if (MessageBox.Show("确认要执行产品贴敷吗?", "去产品贴敷", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1261. {
  1262. return;
  1263. }
  1264. //如果二次定位的工具坐标为空,则提示用户先进行二次定位
  1265. if (toolCoord == null)
  1266. {
  1267. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "二次定位的工具坐标为空,无法执行产品拍照!", Duration = 1 });
  1268. return;
  1269. }
  1270. if (_PutPoints[0].X == 0 && _PutPoints[0].Y == 0 && _PutPoints[1].X == 0 && _PutPoints[1].Y == 0)
  1271. {
  1272. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "检测到未定位产品", Duration = 1 });
  1273. return;
  1274. }
  1275. var waiting = new WaitingControl();
  1276. //show the dialog
  1277. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1278. double upCameraX = (_PutPoints[0].X + _PutPoints[1].X) / 2;
  1279. double upCameraY = (_PutPoints[0].Y + _PutPoints[1].Y) / 2;
  1280. double upCameraU = Common.CalculateAngleBetweenPointsDegrees(new PointF((float)_PutPoints[0].X, (float)_PutPoints[0].Y), new PointF((float)_PutPoints[1].X, (float)_PutPoints[1].Y));
  1281. var pn = Vector<double>.Build.Dense(new double[] { upCameraX, upCameraY });
  1282. //目标角度:
  1283. double angle = SelectedPoint.U_Position + (_FixedUpCameraTempletAngle - SelectProduct.FixedUpCameraTempletAngle) + (upCameraU - SelectProduct.MoveDownCamerTempletAngle);
  1284. //将_PutPoint转换为Tool0下要走的点
  1285. var p0 = toolCoord.GetTool0Coord(pn, angle);
  1286. // 1. 控制机器人移动至拍照点位
  1287. var rpoint = new RPoint()
  1288. {
  1289. X = (float)p0[0],
  1290. Y = (float)p0[1],
  1291. Z = SelectedPoint.Z_Position_Start,
  1292. U = (float)angle,
  1293. V = SelectedPoint.R_Position,
  1294. };
  1295. await Robot.CalibMotionAsync(rpoint, null);
  1296. if (DialogHost.IsDialogOpen("RootDialog"))
  1297. {
  1298. DialogHost.Close("RootDialog");
  1299. }
  1300. await task;
  1301. return;
  1302. }
  1303. catch (Exception ex)
  1304. {
  1305. LogHelper.WriteLogError("执行产品贴敷时出错!", ex);
  1306. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1307. if (DialogHost.IsDialogOpen("RootDialog"))
  1308. {
  1309. DialogHost.Close("RootDialog");
  1310. }
  1311. }
  1312. }
  1313. /// <summary>
  1314. /// 附加轴+点动
  1315. /// </summary>
  1316. /// <param name="obj"></param>
  1317. async void ExecuteAdditionalAxisJogPlusCommand(object obj)
  1318. {
  1319. if (Robot == null || !Robot.IsConnected) return;
  1320. try
  1321. {
  1322. if (obj is AxisExtended axis)
  1323. {
  1324. //如果移动距离大于10mm,则提示用户确认
  1325. if (Math.Abs(Distance) >= 10)
  1326. {
  1327. if (MessageBox.Show($"确认要点动距离 {Distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1328. {
  1329. return;
  1330. }
  1331. }
  1332. await Robot.AdditionalAxisJogAsync(axis.Name, Distance);
  1333. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1334. }
  1335. }
  1336. catch (Exception ex)
  1337. {
  1338. LogHelper.WriteLogError("附加轴点动+时出错!", ex);
  1339. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1340. }
  1341. }
  1342. /// <summary>
  1343. /// 附加轴-点动
  1344. /// </summary>
  1345. /// <param name="obj"></param>
  1346. async void ExecuteAdditionalAxisJogMinusCommand(object obj)
  1347. {
  1348. if (Robot == null || !Robot.IsConnected) return;
  1349. try
  1350. {
  1351. if (obj is AxisExtended axis)
  1352. {
  1353. //如果移动距离大于10mm,则提示用户确认
  1354. if (Math.Abs(Distance) >= 10)
  1355. {
  1356. if (MessageBox.Show($"确认要点动距离 {Distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1357. {
  1358. return;
  1359. }
  1360. }
  1361. await Robot.AdditionalAxisJogNegAsync(axis.Name, Distance);
  1362. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1363. }
  1364. }
  1365. catch (Exception ex)
  1366. {
  1367. LogHelper.WriteLogError("附加轴点动-时出错!", ex);
  1368. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1369. }
  1370. }
  1371. /// <summary>
  1372. /// 附加轴下使能
  1373. /// </summary>
  1374. /// <param name="obj"></param>
  1375. async void ExecuteAdditionalAxisMotorOffCommand(object obj)
  1376. {
  1377. if (Robot == null || !Robot.IsConnected) return;
  1378. try
  1379. {
  1380. if (obj is AxisExtended axis)
  1381. {
  1382. await Robot.AdditionalAxisMotorAsync(axis.Name, false);
  1383. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1384. }
  1385. }
  1386. catch (Exception ex)
  1387. {
  1388. LogHelper.WriteLogError("附加轴上使能时出错!", ex);
  1389. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1390. }
  1391. }
  1392. /// <summary>
  1393. /// 附加轴上使能
  1394. /// </summary>
  1395. /// <param name="obj"></param>
  1396. async void ExecuteAdditionalAxisMotorOnCommand(object obj)
  1397. {
  1398. if (Robot == null || !Robot.IsConnected) return;
  1399. try
  1400. {
  1401. if (obj is AxisExtended axis)
  1402. {
  1403. await Robot.AdditionalAxisMotorAsync(axis.Name, true);
  1404. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1405. }
  1406. }
  1407. catch (Exception ex)
  1408. {
  1409. LogHelper.WriteLogError("附加轴下使能时出错!", ex);
  1410. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1411. }
  1412. }
  1413. /// <summary>
  1414. /// 移动取料托盘点
  1415. /// </summary>
  1416. async void ExecutePickMoveCommand(string parameter)
  1417. {
  1418. if (Robot == null || !Robot.IsConnected) return;
  1419. try
  1420. {
  1421. //弹窗用户是否确认要示教该点位
  1422. if (MessageBox.Show($"确认要移动取料点位 {parameter} 吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1423. {
  1424. return;
  1425. }
  1426. int palletIndex = int.Parse(parameter.Split('-')[0]);
  1427. int pointIndex = int.Parse(parameter.Split('-')[1]);
  1428. var curpos = await Robot.GetRobotPosAsync();
  1429. var addpos = await Robot.GetAdditionalAxisCurrentPosAsync(Robot.AdditionalAxisList[0].Name);
  1430. PlcPoint pn = null;
  1431. if (palletIndex == 1)
  1432. {
  1433. switch (pointIndex)
  1434. {
  1435. case 1:
  1436. pn = Pallet1.P0.Clone();
  1437. break;
  1438. case 2:
  1439. pn = Pallet1.P1.Clone();
  1440. break;
  1441. case 3:
  1442. pn = Pallet1.P2.Clone();
  1443. break;
  1444. default:
  1445. break;
  1446. }
  1447. }
  1448. else
  1449. {
  1450. switch (pointIndex)
  1451. {
  1452. case 1:
  1453. pn = Pallet2.P0.Clone();
  1454. break;
  1455. case 2:
  1456. pn = Pallet2.P1.Clone();
  1457. break;
  1458. case 3:
  1459. pn = Pallet2.P2.Clone();
  1460. break;
  1461. default:
  1462. break;
  1463. }
  1464. }
  1465. if (pn == null)
  1466. {
  1467. return;
  1468. }
  1469. // 1.Z轴上升到安全高度
  1470. curpos.Z = 0;
  1471. bool isSuccess = await Robot.CalibMotionAsync(curpos, null);
  1472. if (!isSuccess)
  1473. {
  1474. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人移动失败!", Duration = 1 });
  1475. return;
  1476. }
  1477. // 2.附加轴移动
  1478. isSuccess = await Robot.AdditionalAxisMoveAsync(Robot.AdditionalAxisList[0].Name, pn.Z_Position_Stop);
  1479. if (!isSuccess)
  1480. {
  1481. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "附加轴移动失败!", Duration = 1 });
  1482. return;
  1483. }
  1484. // 3.移动XY轴到位
  1485. curpos.X = pn.X_Position;
  1486. curpos.Y = pn.Y_Position;
  1487. curpos.Z = pn.Z_Position_Start;
  1488. curpos.U = pn.U_Position;
  1489. isSuccess = await Robot.CalibMotionAsync(curpos, null);
  1490. if (!isSuccess)
  1491. {
  1492. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人移动失败!", Duration = 1 });
  1493. return;
  1494. }
  1495. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1496. }
  1497. catch (Exception ex)
  1498. {
  1499. LogHelper.WriteLogError("示教取料托盘点时出错!", ex);
  1500. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1501. }
  1502. }
  1503. /// <summary>
  1504. /// 示教取料托盘点
  1505. /// </summary>
  1506. /// <param name="parameter"></param>
  1507. async void ExecutePickTechCommand(string parameter)
  1508. {
  1509. if (Robot == null || !Robot.IsConnected) return;
  1510. try
  1511. {
  1512. //弹窗用户是否确认要示教该点位
  1513. if (MessageBox.Show($"确认要示教取料点位 {parameter} 吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1514. {
  1515. return;
  1516. }
  1517. int palletIndex = int.Parse(parameter.Split('-')[0]);
  1518. int pointIndex = int.Parse(parameter.Split('-')[1]);
  1519. var curpos = await Robot.GetRobotPosAsync();
  1520. var addpos = await Robot.GetAdditionalAxisCurrentPosAsync(Robot.AdditionalAxisList[0].Name);
  1521. if (palletIndex == 1)
  1522. {
  1523. switch (pointIndex)
  1524. {
  1525. case 1:
  1526. Pallet1.P0.X_Position = curpos.X;
  1527. Pallet1.P0.Y_Position = curpos.Y;
  1528. Pallet1.P0.Z_Position_Start = curpos.Z;
  1529. Pallet1.P0.U_Position = curpos.U;
  1530. Pallet1.P0.Z_Position_Stop = addpos;
  1531. break;
  1532. case 2:
  1533. Pallet1.P1.X_Position = curpos.X;
  1534. Pallet1.P1.Y_Position = curpos.Y;
  1535. Pallet1.P1.Z_Position_Start = curpos.Z;
  1536. Pallet1.P1.U_Position = curpos.U;
  1537. Pallet1.P1.Z_Position_Stop = addpos;
  1538. break;
  1539. case 3:
  1540. Pallet1.P2.X_Position = curpos.X;
  1541. Pallet1.P2.Y_Position = curpos.Y;
  1542. Pallet1.P2.Z_Position_Start = curpos.Z;
  1543. Pallet1.P2.U_Position = curpos.U;
  1544. Pallet1.P2.Z_Position_Stop = addpos;
  1545. break;
  1546. default:
  1547. break;
  1548. }
  1549. }
  1550. else
  1551. {
  1552. switch (pointIndex)
  1553. {
  1554. case 1:
  1555. Pallet2.P0.X_Position = curpos.X;
  1556. Pallet2.P0.Y_Position = curpos.Y;
  1557. Pallet2.P0.Z_Position_Start = curpos.Z;
  1558. Pallet2.P0.U_Position = curpos.U;
  1559. Pallet2.P0.Z_Position_Stop = addpos;
  1560. break;
  1561. case 2:
  1562. Pallet2.P1.X_Position = curpos.X;
  1563. Pallet2.P1.Y_Position = curpos.Y;
  1564. Pallet2.P1.Z_Position_Start = curpos.Z;
  1565. Pallet2.P1.U_Position = curpos.U;
  1566. Pallet2.P1.Z_Position_Stop = addpos;
  1567. break;
  1568. case 3:
  1569. Pallet2.P2.X_Position = curpos.X;
  1570. Pallet2.P2.Y_Position = curpos.Y;
  1571. Pallet2.P2.Z_Position_Start = curpos.Z;
  1572. Pallet2.P2.U_Position = curpos.U;
  1573. Pallet2.P2.Z_Position_Stop = addpos;
  1574. break;
  1575. default:
  1576. break;
  1577. }
  1578. }
  1579. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1580. }
  1581. catch (Exception ex)
  1582. {
  1583. LogHelper.WriteLogError("示教取料托盘点时出错!", ex);
  1584. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1585. }
  1586. }
  1587. /// <summary>
  1588. /// 计算所有托盘点
  1589. /// </summary>
  1590. void ExecuteCalPickPosCommand(string parameter)
  1591. {
  1592. try
  1593. {
  1594. int palletIndex = int.Parse(parameter);
  1595. if (palletIndex == 1)
  1596. {
  1597. PalletTool palletTool = new PalletTool(Pallet1);
  1598. //托盘中所有的点‘
  1599. var points = palletTool.PlcPoints;
  1600. //将所有的点写入到取料点中,如果当前定义的取料点不存在,则新增
  1601. for (int i = 0; i < points.Length; i++)
  1602. {
  1603. //如果当前的取料点数量小于等于i,则新增一个取料点
  1604. if (SelectProduct.PickPoints.Count <= i)
  1605. {
  1606. SelectProduct.PickPoints.Add(new PlcPoint()
  1607. {
  1608. Number = i + 1,
  1609. X_Position = points[i].X_Position,
  1610. X_Velocity = 100,
  1611. Y_Position = points[i].Y_Position,
  1612. Y_Velocity = 100,
  1613. Z_Position_Start = points[i].Z_Position_Start,
  1614. Z_Velocity_Start = 100,
  1615. U_Position = points[i].U_Position,
  1616. U_Velocity = 100,
  1617. Z_Position_Stop = points[i].Z_Position_Stop,
  1618. Z_Velocity_Stop = 100,
  1619. });
  1620. }
  1621. else
  1622. {
  1623. //否则更新当前的取料点
  1624. SelectProduct.PickPoints[i].Number = i + 1;
  1625. SelectProduct.PickPoints[i].X_Position = points[i].X_Position;
  1626. SelectProduct.PickPoints[i].Y_Position = points[i].Y_Position;
  1627. SelectProduct.PickPoints[i].Z_Position_Start = points[i].Z_Position_Start;
  1628. SelectProduct.PickPoints[i].U_Position = points[i].U_Position;
  1629. SelectProduct.PickPoints[i].Z_Position_Stop = points[i].Z_Position_Stop;
  1630. }
  1631. }
  1632. }
  1633. else
  1634. {
  1635. //如果当前的取料点数量小于托盘1的点数量,则提示用户先计算托盘1的点
  1636. if (SelectProduct.PickPoints.Count < Pallet1.Row * Pallet1.Column)
  1637. {
  1638. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "请先计算托盘1的点位!", Duration = 1 });
  1639. return;
  1640. }
  1641. PalletTool palletTool = new PalletTool(Pallet2);
  1642. //托盘中所有的点‘
  1643. var points = palletTool.PlcPoints;
  1644. //将所有的点写入到取料点中,如果当前定义的取料点不存在,则新增
  1645. for (int i = 0; i < points.Length; i++)
  1646. {
  1647. int index = i + Pallet1.Row * Pallet1.Column;
  1648. //如果当前的取料点数量小于等于index,则新增一个取料点
  1649. if (SelectProduct.PickPoints.Count <= index)
  1650. {
  1651. SelectProduct.PickPoints.Add(new PlcPoint()
  1652. {
  1653. Number = index + 1,
  1654. X_Position = points[i].X_Position,
  1655. X_Velocity = 100,
  1656. Y_Position = points[i].Y_Position,
  1657. Y_Velocity = 100,
  1658. Z_Position_Start = points[i].Z_Position_Start,
  1659. Z_Velocity_Start = 100,
  1660. U_Position = points[i].U_Position,
  1661. U_Velocity = 100,
  1662. Z_Position_Stop = points[i].Z_Position_Stop,
  1663. Z_Velocity_Stop = 100,
  1664. });
  1665. }
  1666. else
  1667. {
  1668. //否则更新当前的取料点
  1669. SelectProduct.PickPoints[index].Number = index + 1;
  1670. SelectProduct.PickPoints[index].X_Position = points[i].X_Position;
  1671. SelectProduct.PickPoints[index].Y_Position = points[i].Y_Position;
  1672. SelectProduct.PickPoints[index].Z_Position_Start = points[i].Z_Position_Start;
  1673. SelectProduct.PickPoints[index].U_Position = points[i].U_Position;
  1674. SelectProduct.PickPoints[index].Z_Position_Stop = points[i].Z_Position_Stop;
  1675. }
  1676. }
  1677. }
  1678. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1679. }
  1680. catch (Exception ex)
  1681. {
  1682. LogHelper.WriteLogError("示教取料托盘点时出错!", ex);
  1683. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1684. }
  1685. }
  1686. #endregion
  1687. #region 继承
  1688. /// <summary>
  1689. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  1690. /// </summary>
  1691. /// <param name="navigationContext"></param>
  1692. /// <param name="continuationCallback"></param>
  1693. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  1694. {
  1695. continuationCallback(true);
  1696. }
  1697. /// <summary>
  1698. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  1699. /// </summary>
  1700. /// <param name="navigationContext"></param>
  1701. /// <exception cref="NotImplementedException"></exception>
  1702. public async void OnNavigatedTo(NavigationContext navigationContext)
  1703. {
  1704. try
  1705. {
  1706. toolCoord = null;
  1707. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  1708. SelectedIndex = 0;
  1709. Points = SelectProduct.PickCameraPoints;
  1710. if (Robot == null)
  1711. {
  1712. Robot = _robotService.GetRobotByNumber(1) as XYZU_Robot;
  1713. }
  1714. if (Robot != null && Robot.IsConnected)
  1715. {
  1716. this.Distance = _configService.GetRobotStepDistance(Robot.Id);
  1717. Robot.EnterDebugMode = true;
  1718. AdditionalAxisList = Robot.AdditionalAxisList;
  1719. }
  1720. else if (Robot != null)
  1721. {
  1722. Robot.EnterDebugMode = true;
  1723. }
  1724. ProcedureModels = new ObservableCollection<ProcedureModel>();
  1725. foreach (var item in SelectProduct.CameraProcedures)
  1726. {
  1727. foreach (var item1 in item.ProcedureModels)
  1728. {
  1729. ProcedureModels.Add(item1);
  1730. }
  1731. }
  1732. CameraList = new ObservableCollection<CameraInfo>(_configService.GetAllCameras());
  1733. Pallet1 = new PalletEx() { Row = 4, Column = 2, P1 = new PlcPoint(), P0 = new PlcPoint(), P2 = new PlcPoint() };
  1734. Pallet2 = new PalletEx() { Row = 4, Column = 2, P1 = new PlcPoint(), P0 = new PlcPoint(), P2 = new PlcPoint() };
  1735. }
  1736. catch (Exception ex)
  1737. {
  1738. LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
  1739. }
  1740. }
  1741. /// <summary>
  1742. /// 是否允许导航到此视图模型。
  1743. /// </summary>
  1744. /// <param name="navigationContext"></param>
  1745. /// <returns></returns>
  1746. public bool IsNavigationTarget(NavigationContext navigationContext)
  1747. {
  1748. return true;
  1749. }
  1750. /// <summary>
  1751. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  1752. /// </summary>
  1753. /// <param name="navigationContext"></param>
  1754. public void OnNavigatedFrom(NavigationContext navigationContext)
  1755. {
  1756. //if (Robot != null)
  1757. //{
  1758. // Robot.EnterDebugMode = false;
  1759. //}
  1760. try
  1761. {
  1762. IsGrap = false;
  1763. if (Camera != null)
  1764. {
  1765. if (Camera.IsGrabbing)
  1766. {
  1767. Camera.StopGrabbing();
  1768. }
  1769. Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
  1770. Camera = null;
  1771. }
  1772. SelectedCamera = null;
  1773. IsLeftDrawerOpen = false;
  1774. }
  1775. catch (Exception)
  1776. {
  1777. }
  1778. }
  1779. #endregion
  1780. private bool _IsAllowEdit = false;
  1781. public bool IsAllowEdit
  1782. {
  1783. get { return _IsAllowEdit; }
  1784. set { SetProperty(ref _IsAllowEdit, value); }
  1785. }
  1786. private void LoginChange(Models.User user)
  1787. {
  1788. if (user.userPart == Enums.UserPart.Operator)
  1789. {
  1790. IsAllowEdit = false;
  1791. }
  1792. else
  1793. {
  1794. IsAllowEdit = true;
  1795. }
  1796. }
  1797. }
  1798. }