PlcPointParamsViewModel.cs 101 KB

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