PlcPointParamsViewModel.cs 97 KB

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