PlcPointParamsViewModel.cs 67 KB

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