PlcPointParamsViewModel.cs 91 KB

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