PlcPointParamsViewModel.cs 78 KB

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