PlcPointParamsViewModel.cs 102 KB

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