PlcPointParamsViewModel.cs 83 KB

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