PlcPointParamsViewModel.cs 88 KB

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