PlcPointParamsViewModel.cs 88 KB

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