PlcPointParamsViewModel.cs 87 KB

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