PlcPointParamsViewModel.cs 89 KB

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