PlcPointParamsViewModel.cs 89 KB

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