PlcPointParamsViewModel.cs 63 KB

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