PlcPointParamsViewModel.cs 80 KB

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