PlcPointParamsViewModel.cs 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  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. //附加轴,轴点动+命令
  381. private DelegateCommand<object> _AdditionalAxisJogPlusCommand;
  382. public DelegateCommand<object> AdditionalAxisJogPlusCommand =>
  383. _AdditionalAxisJogPlusCommand ?? (_AdditionalAxisJogPlusCommand = new DelegateCommand<object>(ExecuteAdditionalAxisJogPlusCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  384. //附加轴,轴点动-命令
  385. private DelegateCommand<object> _AdditionalAxisJogMinusCommand;
  386. public DelegateCommand<object> AdditionalAxisJogMinusCommand =>
  387. _AdditionalAxisJogMinusCommand ?? (_AdditionalAxisJogMinusCommand = new DelegateCommand<object>(ExecuteAdditionalAxisJogMinusCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  388. private DelegateCommand<object> _AdditionalAxisMotorOnCommand;
  389. public DelegateCommand<object> AdditionalAxisMotorOnCommand =>
  390. _AdditionalAxisMotorOnCommand ?? (_AdditionalAxisMotorOnCommand = new DelegateCommand<object>(ExecuteAdditionalAxisMotorOnCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  391. private DelegateCommand<object> _AdditionalAxisMotorOffCommand;
  392. public DelegateCommand<object> AdditionalAxisMotorOffCommand =>
  393. _AdditionalAxisMotorOffCommand ?? (_AdditionalAxisMotorOffCommand = new DelegateCommand<object>(ExecuteAdditionalAxisMotorOffCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  394. private DelegateCommand _GoPickCommand;
  395. public DelegateCommand GoPickCommand =>
  396. _GoPickCommand ?? (_GoPickCommand = new DelegateCommand(ExecuteGoPickCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(() => SelectedPoint));
  397. private DelegateCommand _GoRepositionCommand;
  398. public DelegateCommand GoRepositionCommand =>
  399. _GoRepositionCommand ?? (_GoRepositionCommand = new DelegateCommand(ExecuteGoRepositionCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(() => SelectedPoint));
  400. private DelegateCommand _TakeProductPhotoCommand;
  401. public DelegateCommand TakeProductPhotoCommand =>
  402. _TakeProductPhotoCommand ?? (_TakeProductPhotoCommand = new DelegateCommand(ExecuteTakeProductPhotoCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(() => SelectedPoint));
  403. private DelegateCommand _TakeProductCommand;
  404. public DelegateCommand TakeProductCommand =>
  405. _TakeProductCommand ?? (_TakeProductCommand = new DelegateCommand(ExecuteTakeProductCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(() => SelectedPoint));
  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. /// <param name="obj"></param>
  984. async void ExecuteAdditionalAxisJogPlusCommand(object obj)
  985. {
  986. if (Robot == null || !Robot.IsConnected) return;
  987. try
  988. {
  989. if (obj is AxisExtended axis)
  990. {
  991. //如果移动距离大于10mm,则提示用户确认
  992. if (Math.Abs(Distance) >= 10)
  993. {
  994. if (MessageBox.Show($"确认要点动距离 {Distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  995. {
  996. return;
  997. }
  998. }
  999. await Robot.AdditionalAxisJogAsync(axis.Name, Distance);
  1000. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1001. }
  1002. }
  1003. catch (Exception ex)
  1004. {
  1005. LogHelper.WriteLogError("附加轴点动+时出错!", ex);
  1006. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 0.4 });
  1007. }
  1008. }
  1009. /// <summary>
  1010. /// 附加轴-点动
  1011. /// </summary>
  1012. /// <param name="obj"></param>
  1013. async void ExecuteAdditionalAxisJogMinusCommand(object obj)
  1014. {
  1015. if (Robot == null || !Robot.IsConnected) return;
  1016. try
  1017. {
  1018. if (obj is AxisExtended axis)
  1019. {
  1020. //如果移动距离大于10mm,则提示用户确认
  1021. if (Math.Abs(Distance) >= 10)
  1022. {
  1023. if (MessageBox.Show($"确认要点动距离 {Distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1024. {
  1025. return;
  1026. }
  1027. }
  1028. await Robot.AdditionalAxisJogNegAsync(axis.Name, Distance);
  1029. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1030. }
  1031. }
  1032. catch (Exception ex)
  1033. {
  1034. LogHelper.WriteLogError("附加轴点动-时出错!", ex);
  1035. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 0.4 });
  1036. }
  1037. }
  1038. /// <summary>
  1039. /// 附加轴上使能
  1040. /// </summary>
  1041. /// <param name="obj"></param>
  1042. async void ExecuteAdditionalAxisMotorOffCommand(object obj)
  1043. {
  1044. if (Robot == null || !Robot.IsConnected) return;
  1045. try
  1046. {
  1047. if (obj is AxisExtended axis)
  1048. {
  1049. await Robot.AdditionalAxisMotorAsync(axis.Name, true);
  1050. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1051. }
  1052. }
  1053. catch (Exception ex)
  1054. {
  1055. LogHelper.WriteLogError("附加轴上使能时出错!", ex);
  1056. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 0.4 });
  1057. }
  1058. }
  1059. /// <summary>
  1060. /// 附加轴下使能
  1061. /// </summary>
  1062. /// <param name="obj"></param>
  1063. async void ExecuteAdditionalAxisMotorOnCommand(object obj)
  1064. {
  1065. if (Robot == null || !Robot.IsConnected) return;
  1066. try
  1067. {
  1068. if (obj is AxisExtended axis)
  1069. {
  1070. await Robot.AdditionalAxisMotorAsync(axis.Name, true);
  1071. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  1072. }
  1073. }
  1074. catch (Exception ex)
  1075. {
  1076. LogHelper.WriteLogError("附加轴下使能时出错!", ex);
  1077. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 0.4 });
  1078. }
  1079. }
  1080. /// <summary>
  1081. /// 去取料
  1082. /// </summary>
  1083. async void ExecuteGoPickCommand()
  1084. {
  1085. try
  1086. {
  1087. if (SelectedPoint == null)
  1088. {
  1089. return;
  1090. }
  1091. if (Robot == null || !Robot.IsConnected)
  1092. {
  1093. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行取料!", Duration = 1 });
  1094. return;
  1095. }
  1096. if (MessageBox.Show("确认要执行取料吗?", "去取料", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1097. {
  1098. return;
  1099. }
  1100. var waiting = new WaitingControl();
  1101. //show the dialog
  1102. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1103. // 1. 控制机器人移动至拍照点位
  1104. var rpoint = new RPoint()
  1105. {
  1106. X = SelectedPoint.X_Position,
  1107. Y = SelectedPoint.Y_Position,
  1108. Z = SelectedPoint.Z_Position_Start,
  1109. U = SelectedPoint.U_Position,
  1110. V = SelectedPoint.R_Position,
  1111. };
  1112. await Robot.CalibMotionAsync(rpoint, 0);
  1113. await Robot.CalibParameAsync(new PickInfo() { Inhal = Inhal, Blow = 1 }, 20, 20, true, 200, 200);
  1114. //打开吸真空
  1115. await Robot.CalibOutIOAsync(true);
  1116. await Task.Delay(500);
  1117. rpoint.Z = 0;
  1118. await Robot.CalibMotionAsync(rpoint, 0);
  1119. if (DialogHost.IsDialogOpen("RootDialog"))
  1120. {
  1121. DialogHost.Close("RootDialog");
  1122. }
  1123. await task;
  1124. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料完成!", Duration = 0.4 });
  1125. }
  1126. catch (Exception ex)
  1127. {
  1128. LogHelper.WriteLogError("取料时出错!", ex);
  1129. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1130. }
  1131. }
  1132. bool CanExecuteCommandGoPickCommandName()
  1133. {
  1134. return SelectedPoint != null;
  1135. }
  1136. ToolCoord toolCoord = null;
  1137. double _FixedUpCameraTempletAngle = 0;
  1138. double _MoveCameraTempletAngle = 0;
  1139. PointF _PutPoint = new PointF();
  1140. /// <summary>
  1141. /// 去二次定位
  1142. /// </summary>
  1143. async void ExecuteGoRepositionCommand()
  1144. {
  1145. try
  1146. {
  1147. if (SelectedPoint == null)
  1148. {
  1149. return;
  1150. }
  1151. if (Robot == null || !Robot.IsConnected)
  1152. {
  1153. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行二次定位!", Duration = 1 });
  1154. return;
  1155. }
  1156. if (MessageBox.Show("确认要执行下相机拍产二次定位吗?", "去下相机拍照", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1157. {
  1158. return;
  1159. }
  1160. var waiting = new WaitingControl();
  1161. //show the dialog
  1162. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1163. // 1. 控制机器人移动至拍照点位
  1164. var rpoint = new RPoint()
  1165. {
  1166. X = SelectedPoint.X_Position,
  1167. Y = SelectedPoint.Y_Position,
  1168. Z = SelectedPoint.Z_Position_Start,
  1169. U = SelectedPoint.U_Position,
  1170. V = SelectedPoint.R_Position,
  1171. };
  1172. await Robot.CalibMotionAsync(rpoint, 0);
  1173. await Task.Delay(200);
  1174. //获取固定向上相机1的流程
  1175. var process1 = _productService.GetCurrentProductProcedureModelById(SelectProduct.FixedUpCamera1ProcedureId);
  1176. if (process1 == null)
  1177. {
  1178. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"固定向上相机1视觉流程,流程ID:{SelectProduct.FixedUpCamera1ProcedureId} 未找到!", Duration = 1 });
  1179. if (DialogHost.IsDialogOpen("RootDialog"))
  1180. {
  1181. DialogHost.Close("RootDialog");
  1182. }
  1183. await task;
  1184. return;
  1185. }
  1186. //获取固定向上相机2的流程
  1187. var process2 = _productService.GetCurrentProductProcedureModelById(SelectProduct.FixedUpCamera2ProcedureId);
  1188. if (process2 == null)
  1189. {
  1190. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"固定向上相机2视觉流程,流程ID:{SelectProduct.FixedUpCamera2ProcedureId} 未找到!", Duration = 1 });
  1191. if (DialogHost.IsDialogOpen("RootDialog"))
  1192. {
  1193. DialogHost.Close("RootDialog");
  1194. }
  1195. await task;
  1196. return;
  1197. }
  1198. // 执行视觉任务
  1199. var _cts = new CancellationTokenSource();
  1200. var task1 = _remoteCommandService.ExecutePhotoGetSinglePoint(process1, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
  1201. var task2 = _remoteCommandService.ExecutePhotoGetSinglePoint(process2, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
  1202. var visionResults = await Task.WhenAll(task1, task2);
  1203. if (visionResults[0].IsSucceed && visionResults[1].IsSucceed)
  1204. {
  1205. float toolX = (float)(visionResults[0].X + visionResults[1].X) / 2;
  1206. float toolY = (float)(visionResults[0].Y + visionResults[1].Y) / 2;
  1207. _FixedUpCameraTempletAngle = Common.CalculateAngleBetweenPointsDegrees(new PointF((float)visionResults[0].X, (float)visionResults[0].Y), new PointF((float)visionResults[1].X, (float)visionResults[1].Y));
  1208. toolCoord = new ToolCoord();
  1209. toolCoord.SetTool(toolX, toolY);
  1210. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "视觉定位成功!", Duration = 0.4 });
  1211. if (DialogHost.IsDialogOpen("RootDialog"))
  1212. {
  1213. DialogHost.Close("RootDialog");
  1214. }
  1215. await task;
  1216. return;
  1217. }
  1218. else
  1219. {
  1220. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "视觉定位失败,无法进行二次定位!", Duration = 1 });
  1221. if (DialogHost.IsDialogOpen("RootDialog"))
  1222. {
  1223. DialogHost.Close("RootDialog");
  1224. }
  1225. await task;
  1226. return;
  1227. }
  1228. }
  1229. catch (Exception ex)
  1230. {
  1231. LogHelper.WriteLogError("二次定位时出错!", ex);
  1232. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1233. if (DialogHost.IsDialogOpen("RootDialog"))
  1234. {
  1235. DialogHost.Close("RootDialog");
  1236. }
  1237. }
  1238. }
  1239. /// <summary>
  1240. /// 执行产品拍照
  1241. /// </summary>
  1242. async void ExecuteTakeProductPhotoCommand()
  1243. {
  1244. try
  1245. {
  1246. if (SelectedPoint == null)
  1247. {
  1248. return;
  1249. }
  1250. if (Robot == null || !Robot.IsConnected)
  1251. {
  1252. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行产品拍照!", Duration = 1 });
  1253. return;
  1254. }
  1255. if (MessageBox.Show("确认要执行产品拍照吗?", "去产品位置拍照", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1256. {
  1257. return;
  1258. }
  1259. //如果二次定位的工具坐标为空,则提示用户先进行二次定位
  1260. if (toolCoord == null)
  1261. {
  1262. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "二次定位的工具坐标为空,无法执行产品拍照!", Duration = 1 });
  1263. return;
  1264. }
  1265. var waiting = new WaitingControl();
  1266. //show the dialog
  1267. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1268. // 1. 控制机器人移动至拍照点位
  1269. var rpoint = new RPoint()
  1270. {
  1271. X = SelectedPoint.X_Position,
  1272. Y = SelectedPoint.Y_Position,
  1273. Z = SelectedPoint.Z_Position_Start,
  1274. U = SelectedPoint.U_Position,
  1275. V = SelectedPoint.R_Position,
  1276. };
  1277. await Robot.CalibMotionAsync(rpoint, 0);
  1278. await Task.Delay(200);
  1279. //获取移动向下相机1的流程
  1280. var process1 = _productService.GetCurrentProductProcedureModelById(SelectProduct.MoveDownCamera1LockPhotoProcedureId);
  1281. if (process1 == null)
  1282. {
  1283. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"移动向下相机1视觉流程,流程ID:{SelectProduct.MoveDownCamera1LockPhotoProcedureId} 未找到!", Duration = 1 });
  1284. if (DialogHost.IsDialogOpen("RootDialog"))
  1285. {
  1286. DialogHost.Close("RootDialog");
  1287. }
  1288. await task;
  1289. return;
  1290. }
  1291. //获取移动向下相机2的流程
  1292. var process2 = _productService.GetCurrentProductProcedureModelById(SelectProduct.MoveDownCamera2LockPhotoProcedureId);
  1293. if (process2 == null)
  1294. {
  1295. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"移动向下相机2视觉流程,流程ID:{SelectProduct.MoveDownCamera2LockPhotoProcedureId} 未找到!", Duration = 1 });
  1296. if (DialogHost.IsDialogOpen("RootDialog"))
  1297. {
  1298. DialogHost.Close("RootDialog");
  1299. }
  1300. await task;
  1301. return;
  1302. }
  1303. // 执行视觉任务
  1304. var _cts = new CancellationTokenSource();
  1305. var task1 = _remoteCommandService.ExecutePhotoGetSinglePoint(process1, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
  1306. var task2 = _remoteCommandService.ExecutePhotoGetSinglePoint(process2, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
  1307. var visionResults = await Task.WhenAll(task1, task2);
  1308. if (visionResults[0].IsSucceed && visionResults[1].IsSucceed)
  1309. {
  1310. float centerX = (float)(visionResults[0].X + visionResults[1].X) / 2;
  1311. float centerY = (float)(visionResults[0].Y + visionResults[1].Y) / 2;
  1312. _MoveCameraTempletAngle = Common.CalculateAngleBetweenPointsDegrees(new PointF((float)visionResults[0].X, (float)visionResults[0].Y), new PointF((float)visionResults[1].X, (float)visionResults[1].Y));
  1313. _PutPoint = new PointF(centerX, centerY);
  1314. if (DialogHost.IsDialogOpen("RootDialog"))
  1315. {
  1316. DialogHost.Close("RootDialog");
  1317. }
  1318. await task;
  1319. return;
  1320. }
  1321. else
  1322. {
  1323. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "视觉定位失败,无法进行产品拍照!", Duration = 1 });
  1324. if (DialogHost.IsDialogOpen("RootDialog"))
  1325. {
  1326. DialogHost.Close("RootDialog");
  1327. }
  1328. await task;
  1329. return;
  1330. }
  1331. }
  1332. catch (Exception ex)
  1333. {
  1334. LogHelper.WriteLogError("执行产品拍照时出错!", ex);
  1335. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1336. if (DialogHost.IsDialogOpen("RootDialog"))
  1337. {
  1338. DialogHost.Close("RootDialog");
  1339. }
  1340. }
  1341. }
  1342. /// <summary>
  1343. /// 执行产品贴敷
  1344. /// </summary>
  1345. async void ExecuteTakeProductCommand()
  1346. {
  1347. try
  1348. {
  1349. if (SelectedPoint == null)
  1350. {
  1351. return;
  1352. }
  1353. if (Robot == null || !Robot.IsConnected)
  1354. {
  1355. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行产品贴敷!", Duration = 1 });
  1356. return;
  1357. }
  1358. if (MessageBox.Show("确认要执行产品贴敷吗?", "去产品贴敷", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  1359. {
  1360. return;
  1361. }
  1362. //如果二次定位的工具坐标为空,则提示用户先进行二次定位
  1363. if (toolCoord == null)
  1364. {
  1365. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "二次定位的工具坐标为空,无法执行产品拍照!", Duration = 1 });
  1366. return;
  1367. }
  1368. if (_PutPoint.X == 0 && _PutPoint.Y == 0)
  1369. {
  1370. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "检测到未定位产品", Duration = 1 });
  1371. return;
  1372. }
  1373. var waiting = new WaitingControl();
  1374. //show the dialog
  1375. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  1376. var pn = Vector<double>.Build.Dense(new double[] { _PutPoint.X, _PutPoint.Y });
  1377. //目标角度:
  1378. double angle = SelectedPoint.U_Position + (_FixedUpCameraTempletAngle - SelectProduct.FixedUpCameraTempletAngle) + (_MoveCameraTempletAngle - SelectProduct.MoveDownCamerTempletAngle);
  1379. //将_PutPoint转换为Tool0下要走的点
  1380. var p0 = toolCoord.GetTool0Coord(pn, angle);
  1381. // 1. 控制机器人移动至拍照点位
  1382. var rpoint = new RPoint()
  1383. {
  1384. X = (float)p0[0],
  1385. Y = (float)p0[1],
  1386. Z = SelectedPoint.Z_Position_Start,
  1387. U = (float)angle,
  1388. V = SelectedPoint.R_Position,
  1389. };
  1390. await Robot.CalibMotionAsync(rpoint, 0);
  1391. if (DialogHost.IsDialogOpen("RootDialog"))
  1392. {
  1393. DialogHost.Close("RootDialog");
  1394. }
  1395. await task;
  1396. return;
  1397. }
  1398. catch (Exception ex)
  1399. {
  1400. LogHelper.WriteLogError("执行产品贴敷时出错!", ex);
  1401. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  1402. if (DialogHost.IsDialogOpen("RootDialog"))
  1403. {
  1404. DialogHost.Close("RootDialog");
  1405. }
  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. }
  1442. else if (Robot != null)
  1443. {
  1444. Robot.EnterDebugMode = true;
  1445. }
  1446. ProcedureModels = new ObservableCollection<ProcedureModel>();
  1447. foreach (var item in SelectProduct.CameraProcedures)
  1448. {
  1449. foreach (var item1 in item.ProcedureModels)
  1450. {
  1451. ProcedureModels.Add(item1);
  1452. }
  1453. }
  1454. //对锁付点的批量设置进行初始化
  1455. LockPointStartZ = SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Position_Start ?? 0;
  1456. LockZDepth = SelectProduct.ScrewPoints.FirstOrDefault() != null ? (SelectProduct.ScrewPoints.FirstOrDefault().Z_Position_Start - SelectProduct.ScrewPoints.FirstOrDefault().Z_Position_Stop) : 0;
  1457. LockXSpeed = SelectProduct.ScrewPoints.FirstOrDefault()?.X_Velocity ?? 100;
  1458. LockYSpeed = SelectProduct.ScrewPoints.FirstOrDefault()?.Y_Velocity ?? 100;
  1459. LockZSpeed = SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Velocity_Start ?? 100;
  1460. LockZDescendSpeed = SelectProduct.ScrewPoints.FirstOrDefault()?.Z_Velocity_Stop ?? 100;
  1461. LockFeederNumber = SelectProduct.ScrewPoints.FirstOrDefault()?.Feeder ?? 1;
  1462. LockScrewProgramNumber = SelectProduct.ScrewPoints.FirstOrDefault()?.ScrewProNum ?? 1;
  1463. CameraList = new ObservableCollection<CameraInfo>(_configService.GetAllCameras());
  1464. }
  1465. catch (Exception ex)
  1466. {
  1467. LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
  1468. }
  1469. }
  1470. /// <summary>
  1471. /// 是否允许导航到此视图模型。
  1472. /// </summary>
  1473. /// <param name="navigationContext"></param>
  1474. /// <returns></returns>
  1475. public bool IsNavigationTarget(NavigationContext navigationContext)
  1476. {
  1477. return true;
  1478. }
  1479. /// <summary>
  1480. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  1481. /// </summary>
  1482. /// <param name="navigationContext"></param>
  1483. public void OnNavigatedFrom(NavigationContext navigationContext)
  1484. {
  1485. //if (Robot != null)
  1486. //{
  1487. // Robot.EnterDebugMode = false;
  1488. //}
  1489. try
  1490. {
  1491. IsGrap = false;
  1492. if (Camera != null)
  1493. {
  1494. if (Camera.IsGrabbing)
  1495. {
  1496. Camera.StopGrabbing();
  1497. }
  1498. Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
  1499. Camera = null;
  1500. }
  1501. SelectedCamera = null;
  1502. IsLeftDrawerOpen = false;
  1503. }
  1504. catch (Exception)
  1505. {
  1506. }
  1507. }
  1508. #endregion
  1509. private bool _IsAllowEdit = false;
  1510. public bool IsAllowEdit
  1511. {
  1512. get { return _IsAllowEdit; }
  1513. set { SetProperty(ref _IsAllowEdit, value); }
  1514. }
  1515. private void LoginChange(Models.User user)
  1516. {
  1517. if (user.userPart == Enums.UserPart.Operator)
  1518. {
  1519. IsAllowEdit = false;
  1520. }
  1521. else
  1522. {
  1523. IsAllowEdit = true;
  1524. }
  1525. }
  1526. }
  1527. }