PlcPointParamsViewModel.cs 101 KB

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