| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159 |
- using Cognex.VisionPro;
- using MahApps.Metro.Controls;
- using MaterialDesignThemes.Wpf;
- using MathNet.Numerics;
- using MathNet.Numerics.LinearAlgebra;
- using Microsoft.Win32;
- using NPOI.SS.Formula.Functions;
- using OpenCvSharp.Flann;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Ioc;
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Xml.Serialization;
- using TeamAAS_VP.Controls;
- using TeamAAS_VP.Core;
- using TeamAAS_VP.Core.PLCs;
- using TeamAAS_VP.Core.Robots;
- using TeamAAS_VP.DxfModule;
- using TeamAAS_VP.Enums;
- using TeamAAS_VP.Events;
- using TeamAAS_VP.Interfaces;
- using TeamAAS_VP.Models;
- using TeamAAS_VP.Models.PLC;
- using TeamAAS_VP.Models.Robot;
- using TeamAAS_VP.Resources.Languages;
- using TeamAAS_VP.Services;
- using TeamAAS_VP.Views.Product;
- using static TeamAAS_VP.ViewModels.Product.AutoCorrectLockPointViewModel;
- namespace TeamAAS_VP.ViewModels.Product
- {
- public class PlcPointParamsViewModel : BindableBase, IConfirmNavigationRequest
- {
- IRegionManager _regionManager;
- IEventAggregator _eventAggregator;
- IContainerProvider _container;
- IDialogService _dialogService;
- IConfigService _configService;
- IRobotService _robotService;
- ISystemDatabaseService _systemDatabaseService;
- IRemoteCommandService _remoteCommandService;
- ICalibrationService _calibrationService;
- ICameraService _cameraService;
- IProductService _productService;
- #region 属性
- private ObservableCollection<AxisExtended> _AdditionalAxisList;
- public ObservableCollection<AxisExtended> AdditionalAxisList
- {
- get { return _AdditionalAxisList; }
- set { SetProperty(ref _AdditionalAxisList, value); }
- }
- private ProductModel _SelectProduct;
- /// <summary>
- /// 选中的产品
- /// </summary>
- public ProductModel SelectProduct
- {
- get { return _SelectProduct; }
- set { SetProperty(ref _SelectProduct, value); }
- }
- private PlcPoint _SelectedPoint;
- public PlcPoint SelectedPoint
- {
- get { return _SelectedPoint; }
- set { SetProperty(ref _SelectedPoint, value); }
- }
- private PlcPoint _selectedPointInDataGrid;
- public PlcPoint SelectedPointInDataGrid
- {
- get { return _selectedPointInDataGrid; }
- set
- {
- if (SetProperty(ref _selectedPointInDataGrid, value))
- {
- SelectedPointInComboBox = value;
- // update command availability
- _MovePointUpCommand?.RaiseCanExecuteChanged();
- _MovePointDownCommand?.RaiseCanExecuteChanged();
- // 更新依赖可执行状态的命令
- _TechPointCommand?.RaiseCanExecuteChanged();
- _ExecuteMotion?.RaiseCanExecuteChanged();
- }
- // 可以同步到 SelectPoint 属性
- SelectedPoint = value;
- }
- }
- private PlcPoint _selectedPointInComboBox;
- public PlcPoint SelectedPointInComboBox
- {
- get { return _selectedPointInComboBox; }
- set
- {
- if (SetProperty(ref _selectedPointInComboBox, value))
- {
- SelectedPointInDataGrid = value;
- }
- // 可以同步到 SelectPoint 属性
- SelectedPoint = value;
- }
- }
- private ObservableCollection<PlcPoint> _Points = new ObservableCollection<PlcPoint>();
- /// <summary>
- /// 点位集合
- /// </summary>
- public ObservableCollection<PlcPoint> Points
- {
- get { return _Points; }
- set { SetProperty(ref _Points, value); }
- }
- private MotionCommand _SelectMotionCommand;
- /// <summary>
- /// 选中的运动指令
- /// </summary>
- public MotionCommand SelectMotionCommand
- {
- get { return _SelectMotionCommand; }
- set { SetProperty(ref _SelectMotionCommand, value); }
- }
- public Management management { get; set; }
- private XYZU_Robot _Robot;
- public XYZU_Robot Robot
- {
- get { return _Robot; }
- set
- {
- SetProperty(ref _Robot, value);
- }
- }
- private int _SelectedIndex;
- public int SelectedIndex
- {
- get { return _SelectedIndex; }
- set { SetProperty(ref _SelectedIndex, value); }
- }
- private double _Distance = 1;
- public double Distance
- {
- get { return _Distance; }
- set
- {
- SetProperty(ref _Distance, value);
- try
- {
- if (Robot == null)
- {
- return;
- }
- _configService.UpdateRobotStepDistance(Robot.Id, value);
- }
- catch (Exception)
- {
- }
- }
- }
- private ObservableCollection<ProcedureModel> _ProcedureModels;
- /// <summary>
- /// 流程集合
- /// </summary>
- public ObservableCollection<ProcedureModel> ProcedureModels
- {
- get { return _ProcedureModels; }
- set { SetProperty(ref _ProcedureModels, value); }
- }
- private ProcedureModel _SelectProcedure;
- /// <summary>
- /// 选中的流程
- /// </summary>
- public ProcedureModel SelectProcedure
- {
- get { return _SelectProcedure; }
- set
- {
- SetProperty(ref _SelectProcedure, value);
- }
- }
- private bool _IsGrap;
- public bool IsGrap
- {
- get { return _IsGrap; }
- set { SetProperty(ref _IsGrap, value); }
- }
- private ObservableCollection<CameraInfo> _CameraList;
- public ObservableCollection<CameraInfo> CameraList
- {
- get { return _CameraList; }
- set { SetProperty(ref _CameraList, value); }
- }
- private CameraInfo _SelectedCamera;
- public CameraInfo SelectedCamera
- {
- get { return _SelectedCamera; }
- set
- {
- SetProperty(ref _SelectedCamera, value);
- if (value != null)
- {
- if (Camera != null)
- {
- if (Camera.IsGrabbing)
- {
- Camera.StopGrabbing();
- }
- Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
- }
- Camera = _cameraService.GetCamera(value.Id);
- Camera.ImageCallbackEvent += Camera_ImageCallbackEvent;
- }
- }
- }
- private ICamera _Camera;
- public ICamera Camera
- {
- get { return _Camera; }
- set { SetProperty(ref _Camera, value); }
- }
- private ICogImage _Image;
- public ICogImage Image
- {
- get { return _Image; }
- set { SetProperty(ref _Image, value); }
- }
- private string _Message;
- public string Message
- {
- get { return _Message; }
- set { SetProperty(ref _Message, value); }
- }
- private bool _IsLeftDrawerOpen;
- public bool IsLeftDrawerOpen
- {
- get { return _IsLeftDrawerOpen; }
- set
- {
- SetProperty(ref _IsLeftDrawerOpen, value);
- if (!value)
- {
- if (IsGrap)
- {
- ExecuteStopGrabbingCommand();
- }
- }
- }
- }
- private int _Inhal = 1;
- public int Inhal
- {
- get { return _Inhal; }
- set { SetProperty(ref _Inhal, value); }
- }
- private PlcPoint _PickP1 = new PlcPoint();
- public PlcPoint PickP1
- {
- get { return _PickP1; }
- set { SetProperty(ref _PickP1, value); }
- }
- private PlcPoint _PickP2 = new PlcPoint();
- public PlcPoint PickP2
- {
- get { return _PickP2; }
- set { SetProperty(ref _PickP2, value); }
- }
- private int _ColumnNum = 18;
- public int ColumnNum
- {
- get { return _ColumnNum; }
- set { SetProperty(ref _ColumnNum, value); }
- }
- #endregion
- #region DTOs for import/export
- public class ProductPointsExport
- {
- /// <summary>
- /// 软件版本号
- /// </summary>
- public string SoftwareVersion { get; set; }
- /// <summary>
- /// 产品名称
- /// </summary>
- public string ProductName { get; set; }
- /// <summary>
- /// 产品描述
- /// </summary>
- public string ProductDescription { get; set; }
- /// <summary>
- /// 设备名称
- /// </summary>
- public string DeviceName { get; set; }
- /// <summary>
- /// 创建日期
- /// </summary>
- public DateTime? Created { get; set; }
- public List<PlcPoint> PickCameraPoints { get; set; }
- public List<PlcPoint> PickPoints { get; set; }
- public List<PlcPoint> SecPosCameraPoints { get; set; }
- public List<PlcPoint> ScrewCameraPoints { get; set; }
- public PlcPoint ScrewCameraLocalPos1 { get; set; }
- public PlcPoint ScrewCameraLocalPos2 { get; set; }
- public List<PlcPoint> ScrewPoints { get; set; }
- public List<PlcPoint> CheckCameraPoints { get; set; }
- public List<PlcPoint> RemovePoints { get; set; }
- public List<PlcPoint> PressurePlacePoints { get; set; }
- }
- #endregion
- #region 命令
- private DelegateCommand _NextCommand;
- public DelegateCommand NextCommand =>
- _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
- private DelegateCommand _BackCommand;
- public DelegateCommand BackCommand =>
- _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
- private DelegateCommand _LoadedCommand;
- public DelegateCommand LoadedCommand =>
- _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
- private DelegateCommand<object> _ExecuteMotion;
- public DelegateCommand<object> ExecuteMotion =>
- _ExecuteMotion ?? (_ExecuteMotion = new DelegateCommand<object>(ExecuteExecuteMotion).ObservesCanExecute(() => CanExecutePointCommand).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute).ObservesProperty(() => SelectedPointInDataGrid));
- private DelegateCommand<object> _JogCommand;
- public DelegateCommand<object> JogCommand =>
- _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- private DelegateCommand _TechPointCommand;
- public DelegateCommand TechPointCommand =>
- _TechPointCommand ?? (_TechPointCommand = new DelegateCommand(ExecuteTechPointCommand).ObservesCanExecute(() => CanExecutePointCommand).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute).ObservesProperty(() => SelectedPointInDataGrid));
- private DelegateCommand<string> _MotorCommand;
- public DelegateCommand<string> MotorCommand =>
- _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- private DelegateCommand _ResetCommand;
- public DelegateCommand ResetCommand =>
- _ResetCommand ?? (_ResetCommand = new DelegateCommand(ExecuteResetCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- private DelegateCommand _sFreeCommand;
- public DelegateCommand sFreeCommand =>
- _sFreeCommand ?? (_sFreeCommand = new DelegateCommand(ExecutesFreeCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- private DelegateCommand _SLockCommand;
- public DelegateCommand SLockCommand =>
- _SLockCommand ?? (_SLockCommand = new DelegateCommand(ExecuteSLockCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- private DelegateCommand _SelectionChangedCommand;
- public DelegateCommand SelectionChangedCommand =>
- _SelectionChangedCommand ?? (_SelectionChangedCommand = new DelegateCommand(ExecuteSelectionChangedCommand));
- private DelegateCommand _AddPointCommand;
- public DelegateCommand AddPointCommand =>
- _AddPointCommand ?? (_AddPointCommand = new DelegateCommand(ExecuteAddPointCommand));
- private DelegateCommand _DeletePointCommand;
- public DelegateCommand DeletePointCommand =>
- _DeletePointCommand ?? (_DeletePointCommand = new DelegateCommand(ExecuteDeletePointCommand, CanExecuteDeletePointCommand).ObservesProperty(() => SelectedPointInDataGrid));
- private DelegateCommand _MovePointUpCommand;
- public DelegateCommand MovePointUpCommand => _MovePointUpCommand ?? (_MovePointUpCommand = new DelegateCommand(ExecuteMovePointUpCommand, CanMovePointUp));
- private DelegateCommand _MovePointDownCommand;
- public DelegateCommand MovePointDownCommand => _MovePointDownCommand ?? (_MovePointDownCommand = new DelegateCommand(ExecuteMovePointDownCommand, CanMovePointDown));
- private DelegateCommand _ImportCadPointCommand;
- public DelegateCommand ImportCadPointCommand =>
- _ImportCadPointCommand ?? (_ImportCadPointCommand = new DelegateCommand(ExecuteImportCadPointCommand));
- private DelegateCommand _StartGrabbingCommand;
- public DelegateCommand StartGrabbingCommand =>
- _StartGrabbingCommand ?? (_StartGrabbingCommand = new DelegateCommand(ExecuteStartGrabbingCommand, CanExecuteStartGrabbingCommand).ObservesProperty(() => IsGrap).ObservesProperty(() => SelectedCamera));
- private DelegateCommand _StopGrabbingCommand;
- public DelegateCommand StopGrabbingCommand =>
- _StopGrabbingCommand ?? (_StopGrabbingCommand = new DelegateCommand(ExecuteStopGrabbingCommand, CanExecuteStopGrabbingCommand).ObservesProperty(() => IsGrap).ObservesProperty(() => SelectedCamera));
- private DelegateCommand _GoPickCommand;
- public DelegateCommand GoPickCommand =>
- _GoPickCommand ?? (_GoPickCommand = new DelegateCommand(ExecuteGoPickCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(() => SelectedPoint));
- private DelegateCommand _GoRepositionCommand;
- public DelegateCommand GoRepositionCommand =>
- _GoRepositionCommand ?? (_GoRepositionCommand = new DelegateCommand(ExecuteGoRepositionCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(() => SelectedPoint));
- private DelegateCommand _TakeProductPhotoCommand;
- public DelegateCommand TakeProductPhotoCommand =>
- _TakeProductPhotoCommand ?? (_TakeProductPhotoCommand = new DelegateCommand(ExecuteTakeProductPhotoCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(() => SelectedPoint));
- private DelegateCommand _TakeProductCommand;
- public DelegateCommand TakeProductCommand =>
- _TakeProductCommand ?? (_TakeProductCommand = new DelegateCommand(ExecuteTakeProductCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(() => SelectedPoint));
- //附加轴,轴点动+命令
- private DelegateCommand<object> _AdditionalAxisJogPlusCommand;
- public DelegateCommand<object> AdditionalAxisJogPlusCommand =>
- _AdditionalAxisJogPlusCommand ?? (_AdditionalAxisJogPlusCommand = new DelegateCommand<object>(ExecuteAdditionalAxisJogPlusCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- //附加轴,轴点动-命令
- private DelegateCommand<object> _AdditionalAxisJogMinusCommand;
- public DelegateCommand<object> AdditionalAxisJogMinusCommand =>
- _AdditionalAxisJogMinusCommand ?? (_AdditionalAxisJogMinusCommand = new DelegateCommand<object>(ExecuteAdditionalAxisJogMinusCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- private DelegateCommand<object> _AdditionalAxisMotorOnCommand;
- public DelegateCommand<object> AdditionalAxisMotorOnCommand =>
- _AdditionalAxisMotorOnCommand ?? (_AdditionalAxisMotorOnCommand = new DelegateCommand<object>(ExecuteAdditionalAxisMotorOnCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- private DelegateCommand<object> _AdditionalAxisMotorOffCommand;
- public DelegateCommand<object> AdditionalAxisMotorOffCommand =>
- _AdditionalAxisMotorOffCommand ?? (_AdditionalAxisMotorOffCommand = new DelegateCommand<object>(ExecuteAdditionalAxisMotorOffCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
- private DelegateCommand<string> _PickTechCommand;
- public DelegateCommand<string> PickTechCommand =>
- _PickTechCommand ?? (_PickTechCommand = new DelegateCommand<string>(ExecutePickTechCommand));
- private DelegateCommand<string> _PickMoveCommand;
- public DelegateCommand<string> PickMoveCommand =>
- _PickMoveCommand ?? (_PickMoveCommand = new DelegateCommand<string>(ExecutePickMoveCommand));
- private DelegateCommand _CalPickPosCommand;
- public DelegateCommand CalPickPosCommand =>
- _CalPickPosCommand ?? (_CalPickPosCommand = new DelegateCommand(ExecuteCalPickPosCommand));
- private DelegateCommand _DynamicTestAnalyzerCommand;
- public DelegateCommand DynamicTestAnalyzerCommand =>
- _DynamicTestAnalyzerCommand ?? (_DynamicTestAnalyzerCommand = new DelegateCommand(ExecuteDynamicTestAnalyzerCommand));
- private DelegateCommand _SetOffsetCommand;
- public DelegateCommand SetOffsetCommand =>
- _SetOffsetCommand ?? (_SetOffsetCommand = new DelegateCommand(ExecuteSetOffsetCommand));
- private DelegateCommand _ImportPointsCommand;
- public DelegateCommand ImportPointsCommand =>
- _ImportPointsCommand ?? (_ImportPointsCommand = new DelegateCommand(ExecuteImportPointsCommand));
- private DelegateCommand _ExportAllPointsCommand;
- public DelegateCommand ExportAllPointsCommand =>
- _ExportAllPointsCommand ?? (_ExportAllPointsCommand = new DelegateCommand(ExecuteExportAllPointsCommand));
- #endregion
- #region 事件
- #endregion
- public PlcPointParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
- IConfigService configService, IRobotService robotService, ISystemDatabaseService systemDatabaseService, IRemoteCommandService remoteCommandService,
- ICalibrationService calibrationService, ICameraService cameraService, IProductService productService)
- {
- _regionManager = regionManager;
- _eventAggregator = ea;
- _container = container;
- _dialogService = dialogService;
- _configService = configService;
- _systemDatabaseService = systemDatabaseService;
- //订阅权限登录事件
- _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
- management = _container.Resolve<Management>();
- _robotService = robotService;
- _remoteCommandService = remoteCommandService;
- _calibrationService = calibrationService;
- _cameraService = cameraService;
- _productService = productService;
- }
- #region 方法
- /// <summary>
- /// 下一步
- /// </summary>
- void ExecuteNextCommand()
- {
- NavigationParameters param = new NavigationParameters();
- param.Add("SelectProduct", SelectProduct);
- _regionManager.RequestNavigate("ProductRegionContext", "ProductParams", param);
- }
- /// <summary>
- /// 上一步
- /// </summary>
- void ExecuteBackCommand()
- {
- NavigationParameters param = new NavigationParameters();
- param.Add("SelectProduct", SelectProduct);
- param.Add("SelectedProcedure", null);
- _regionManager.RequestNavigate("ProductRegionContext", "ProcessBasics", param);
- }
- /// <summary>
- /// 点动
- /// </summary>
- /// <param name="obj"></param>
- private async void ExecuteJogCommand(object obj)
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- var items = ((string)obj).Split(':');
- //如果移动距离大于10mm,则提示用户确认
- double distance = double.Parse(items[1]);
- if (Math.Abs(distance) >= 10)
- {
- if (MessageBox.Show(string.Format(Lang.确认要点动距离0mm吗,distance), Lang.确认点动, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- }
- Robot.Timeout = 6000000;
- switch (items[0])
- {
- case "X+":
- await Robot.JogAsync("X", distance);
- break;
- case "X-":
- await Robot.JogAsync("X", -distance);
- break;
- case "Y+":
- await Robot.JogAsync("Y", distance);
- break;
- case "Y-":
- await Robot.JogAsync("Y", -distance);
- break;
- case "Z+":
- await Robot.JogAsync("Z", distance);
- break;
- case "Z-":
- await Robot.JogAsync("Z", -distance);
- break;
- case "U+":
- await Robot.JogAsync("U", distance);
- break;
- case "U-":
- await Robot.JogAsync("U", -distance);
- break;
- case "V+":
- await Robot.JogAsync("V", distance);
- break;
- case "V-":
- await Robot.JogAsync("V", -distance);
- break;
- case "W+":
- await Robot.JogAsync("W", distance);
- break;
- case "W-":
- await Robot.JogAsync("W", -distance);
- break;
- default:
- break;
- }
- Robot.Timeout = 5000;
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("机器人点位-机器人点动时出错", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 执行运动
- /// </summary>
- /// <param name="obj"></param>
- async void ExecuteExecuteMotion(object parameter)
- {
- if (parameter == null)
- {
- return;
- }
- if (SelectedPoint == null)
- {
- return;
- }
- var values = (object[])parameter;
- var point = new RPoint()
- {
- X = SelectedPoint.X_Position,
- Y = SelectedPoint.Y_Position,
- Z = SelectedPoint.Z_Position_Start,
- U = SelectedPoint.U_Position,
- V = SelectedPoint.R_Position,
- };
- MotionCommand motionCmd = (MotionCommand)values[1];
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- //确认要执行该点位的该运动指令吗
- if (MessageBox.Show(string.Format(Lang.确认要执行点位0的1指令吗,SelectedPoint.Number,motionCmd), Lang.执行点位, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- //Robot.Timeout = 6000000;
- switch (motionCmd)
- {
- case MotionCommand.Go:
- await Robot.GoAsync(point);
- break;
- case MotionCommand.Jump:
- await Robot.JumpAsync(point, 0);
- break;
- case MotionCommand.Move:
- await Robot.MoveAsync(point);
- break;
- }
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("机器人点位-执行点位时出错", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 示教点位
- /// </summary>
- /// <param name="point"></param>
- async void ExecuteTechPointCommand()
- {
- if (Robot == null || !Robot.IsConnected) return;
- if (SelectedPoint == null) return;
- try
- {
- //如果当前的选项卡处于锁付点位,则提示用户当前模式不允许进行示教
- if (SelectedIndex == 4)
- {
- //如果local为空,则必须先通过相机定位产品,否则无法示教锁付点位
- if (toolCoord == null)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "当前模式不允许直接示教贴敷点位,请先通过相机定位产品和定位辅料!", Duration = 1 });
- return;
- }
- //弹窗用户是否确认要示教该点位
- if (MessageBox.Show(string.Format(Lang.确认要示教点位0吗, SelectedPoint.Number), Lang.示教点位, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- double upCameraU = Common.CalculateAngleBetweenPointsDegrees(new PointF((float)_PutPoints[0].X, (float)_PutPoints[0].Y), new PointF((float)_PutPoints[1].X, (float)_PutPoints[1].Y));
- var curpos1 = await Robot.GetRobotPosAsync();
- //将当前点位转换为产品Local坐标系下的点位
- SelectedPoint.X_Position = (float)curpos1.X;
- SelectedPoint.Y_Position = (float)curpos1.Y;
- SelectedPoint.Z_Position_Start = curpos1.Z;
- SelectedPoint.U_Position = curpos1.U;
- SelectedPoint.R_Position = curpos1.V;
- SelectProduct.FixedUpCameraTempletAngle = _FixedUpCameraTempletAngle;
- SelectProduct.MoveDownCamerTempletAngle = upCameraU;
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- return;
- }
- else
- {
- //弹窗用户是否确认要示教该点位
- if (MessageBox.Show(string.Format(Lang.确认要示教点位0吗, SelectedPoint.Number), Lang.示教点位, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- var curpos = await Robot.GetRobotPosAsync();
- SelectedPoint.X_Position = curpos.X;
- SelectedPoint.Y_Position = curpos.Y;
- SelectedPoint.Z_Position_Start = curpos.Z;
- SelectedPoint.U_Position = curpos.U;
- SelectedPoint.R_Position = curpos.V;
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("机器人点位-示教点位时出错", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- void ExecuteLoadedCommand()
- {
- if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
- {
- IsAllowEdit = false;
- }
- else
- {
- IsAllowEdit = true;
- }
- }
- /// <summary>
- /// 电机
- /// </summary>
- /// <param name="obj"></param>
- private async void ExecuteMotorCommand(string obj)
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- await Robot.MotorAsync(Convert.ToBoolean(obj));
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("机器人点击操作时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 重置
- /// </summary>
- private async void ExecuteResetCommand()
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- await Robot.ResetAsync();
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("重置机器人时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 锁定刹车
- /// </summary>
- private async void ExecuteSLockCommand()
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- await Robot.SLockAsync();
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("机器人锁定刹车时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 释放刹车
- /// </summary>
- private async void ExecutesFreeCommand()
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- await Robot.SFreeAsync();
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("机器人释放刹车时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- private bool CanExecute
- {
- get
- {
- // 命令可执行仅当机器人可执行且已选择点位
- if (Robot != null && Robot.IsConnected && Robot.CanExecute)
- {
- return true;
- }
- return false;
- }
- }
- //是否可以示教或者执行点位
- private bool CanExecutePointCommand
- {
- get
- {
- if (Robot != null && Robot.IsConnected && Robot.CanExecute)
- {
- return SelectedPointInDataGrid != null;
- }
- return false;
- }
- }
- /// <summary>
- /// 点位表切换时
- /// </summary>
- void ExecuteSelectionChangedCommand()
- {
- switch (SelectedIndex)
- {
- case 0:
- Points = SelectProduct.PickCameraPoints;
- break;
- case 1:
- Points = SelectProduct.PickPoints;
- break;
- case 2:
- Points = SelectProduct.SecPosCameraPoints;
- break;
- case 3:
- Points = SelectProduct.ScrewCameraPoints;
- break;
- case 4:
- Points = SelectProduct.ScrewPoints;
- break;
- case 5:
- Points = SelectProduct.CheckCameraPoints;
- break;
- case 6:
- Points = SelectProduct.RemovePoints;
- break;
- case 7:
- Points = SelectProduct.PressurePlacePoints;
- break;
- default:
- break;
- }
- }
- /// <summary>
- /// 增加点位
- /// </summary>
- void ExecuteAddPointCommand()
- {
- //添加点位
- Points.Add(new PlcPoint()
- {
- Number = Points.Count + 1,
- X_Velocity = 100,
- Y_Velocity = 100,
- Z_Velocity_Start = 100,
- Z_Velocity_Stop = 100,
- U_Velocity = 100,
- R_Velocity = 100,
- });
- // update commands
- _MovePointUpCommand?.RaiseCanExecuteChanged();
- _MovePointDownCommand?.RaiseCanExecuteChanged();
- }
- /// <summary>
- /// 移除点位
- /// </summary>
- void ExecuteDeletePointCommand()
- {
- if (SelectedPoint == null)
- {
- return;
- }
- // 确认删除
- if (MessageBox.Show(Lang.确认要删除选中的点位吗, Lang.删除点位, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- int removedIndex = Points.IndexOf(SelectedPoint);
- Points.Remove(SelectedPoint);
- //重新编号
- for (int i = 0; i < Points.Count; i++)
- {
- Points[i].Number = i + 1;
- }
- // adjust selection
- if (Points.Count > 0)
- {
- int newIndex = Math.Min(removedIndex, Points.Count - 1);
- SelectedPointInDataGrid = Points[newIndex];
- }
- else
- {
- SelectedPointInDataGrid = null;
- }
- // update commands
- _MovePointUpCommand?.RaiseCanExecuteChanged();
- _MovePointDownCommand?.RaiseCanExecuteChanged();
- }
- bool CanExecuteDeletePointCommand()
- {
- return SelectedPointInDataGrid != null;
- }
- private bool CanMovePointUp()
- {
- return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) > 0;
- }
- private bool CanMovePointDown()
- {
- return SelectedPointInDataGrid != null && Points.IndexOf(SelectedPointInDataGrid) >= 0 && Points.IndexOf(SelectedPointInDataGrid) < Points.Count - 1;
- }
- /// <summary>
- /// 向上移动点位
- /// </summary>
- void ExecuteMovePointUpCommand()
- {
- if (SelectedPointInDataGrid == null) return;
- // 确认移动
- if (MessageBox.Show(Lang.确认将选中点位上移一位吗, Lang.移动点位, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- int idx = Points.IndexOf(SelectedPointInDataGrid);
- if (idx <= 0) return;
- var item = SelectedPointInDataGrid;
- Points.Move(idx, idx - 1);
- // renumber
- for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
- SelectedPointInDataGrid = item;
- _MovePointUpCommand?.RaiseCanExecuteChanged();
- _MovePointDownCommand?.RaiseCanExecuteChanged();
- }
- /// <summary>
- /// 向下移动点位
- /// </summary>
- void ExecuteMovePointDownCommand()
- {
- if (SelectedPointInDataGrid == null) return;
- // 确认移动
- if (MessageBox.Show(Lang.确认将选中点位下移一位吗, Lang.移动点位, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- int idx = Points.IndexOf(SelectedPointInDataGrid);
- if (idx < 0 || idx >= Points.Count - 1) return;
- var item = SelectedPointInDataGrid;
- Points.Move(idx, idx + 1);
- // renumber
- for (int i = 0; i < Points.Count; i++) Points[i].Number = i + 1;
- SelectedPointInDataGrid = item;
- _MovePointUpCommand?.RaiseCanExecuteChanged();
- _MovePointDownCommand?.RaiseCanExecuteChanged();
- }
- /// <summary>
- /// CAD导入点位
- /// </summary>
- async void ExecuteImportCadPointCommand()
- {
- try
- {
- var view = new DxfView();
- //show the dialog
- var result = await DialogHost.Show(view, "RootDialog", null, null, null);
- if (result != null)
- {
- var cadPoints = view.Points;
- if (cadPoints != null && cadPoints.Count > 0)
- {
- //要导入的开始点编号
- int startNumber = view.StartIndex;
- //弹窗提示用户是否确认要导入从该编号开始的点位,点位数量为cadPoints.Count
- if (MessageBox.Show(string.Format(Lang.确认要从点位编号0开始导入1个点位吗, startNumber,cadPoints.Count), Lang.导入点位, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- //导入点位
- for (int i = 0; i < cadPoints.Count; i++)
- {
- //如果点位编号已存在,则更新该点位,否则新增点位,只更新XY坐标
- var point = cadPoints[i];
- var existingPoint = Points.FirstOrDefault(p => p.Number == startNumber + i);
- if (existingPoint != null)
- {
- existingPoint.X_Position = point.X;
- existingPoint.Y_Position = point.Y;
- }
- else
- {
- Points.Add(new PlcPoint()
- {
- Number = startNumber + i,
- X_Position = point.X,
- Y_Position = point.Y,
- X_Velocity = 100,
- Y_Velocity = 100,
- Z_Velocity_Start = 100,
- Z_Velocity_Stop = 100,
- U_Velocity = 100,
- R_Velocity = 100,
- });
- }
- }
- //对所有的点位重新编号,确保编号连续
- var sortedPoints = Points.OrderBy(p => p.Number).ToList();
- Points.Clear();
- for (int i = 0; i < sortedPoints.Count; i++)
- {
- sortedPoints[i].Number = i + 1;
- Points.Add(sortedPoints[i]);
- }
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("导入CAD点位时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 设置偏移
- /// </summary>
- void ExecuteSetOffsetCommand()
- {
- if (SelectedIndex < 0) return;
- IDialogParameters parameters1 = new DialogParameters();
- if (SelectedIndex == 0)
- {
- parameters1.Add("Title", Lang.取料拍照点位补偿值设置);
- }
- else if (SelectedIndex == 1)
- {
- parameters1.Add("Title", Lang.取料点位补偿值设置);
- }
- else if (SelectedIndex == 2)
- {
- parameters1.Add("Title", Lang.下相机二次定位点位补偿值设置);
- }
- else if (SelectedIndex == 3)
- {
- parameters1.Add("Title", Lang.锁附拍照点位补偿值设置);
- }
- else if (SelectedIndex == 4)
- {
- parameters1.Add("Title", Lang.锁附点位补偿值设置);
- }
- else if (SelectedIndex == 5)
- {
- parameters1.Add("Title", Lang.复检点位补偿值设置);
- }
- else
- {
- parameters1.Add("Title", Lang.点位补偿值设置);
- }
- List<PlcPoint> _points = new List<PlcPoint>();
- foreach (var item in Points)
- {
- _points.Add(item.Clone());
- }
- parameters1.Add("Points", _points.ToArray());
- _dialogService.ShowDialog("PlcPointOffserParams", parameters1, rst =>
- {
- //对话框关闭之后的回调函数,可以在这解析结果。
- ButtonResult result1 = rst.Result;
- if (result1 == ButtonResult.OK)
- {
- var param = rst.Parameters.GetValue<PlcPoint[]>("Points");
- //更新点位
- for (int i = 0; i < Points.Count; i++)
- {
- Points[i].X_Offset = param[i].X_Offset;
- Points[i].Y_Offset = param[i].Y_Offset;
- Points[i].Z_Start_Offset = param[i].Z_Start_Offset;
- Points[i].Z_Stop_Offset = param[i].Z_Stop_Offset;
- Points[i].U_Offset = param[i].U_Offset;
- Points[i].R_Offset = param[i].R_Offset;
- }
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}", Duration = 1 });
- }
- });
- }
- /// <summary>
- /// 停止采集
- /// </summary>
- void ExecuteStopGrabbingCommand()
- {
- if (Camera != null)
- Camera.StopGrabbing();
- IsGrap = false;
- }
- bool CanExecuteStopGrabbingCommand()
- {
- return IsGrap;
- }
- /// <summary>
- /// 开始采集
- /// </summary>
- void ExecuteStartGrabbingCommand()
- {
- if (Camera != null)
- Camera.StartGrabbing();
- IsGrap = true;
- }
- bool CanExecuteStartGrabbingCommand()
- {
- return SelectedCamera != null && !IsGrap;
- }
- private void Camera_ImageCallbackEvent(ICogImage image, TimeSpan totaltime, string errormessage)
- {
- Image = image;
- App.Current.Dispatcher.Invoke(new
- Action(() =>
- {
- if (errormessage != null && !string.IsNullOrEmpty(errormessage))
- {
- Message = $"{Lang.耗时}:{totaltime.TotalMilliseconds.ToString("F1")} ms Error:{errormessage}";
- }
- else
- {
- Message = $"{Lang.耗时}:{totaltime.TotalMilliseconds.ToString("F1")} ms";
- }
- }));
- }
- /// <summary>
- /// 去取料
- /// </summary>
- async void ExecuteGoPickCommand()
- {
- try
- {
- if (SelectedPoint == null)
- {
- return;
- }
- if (Robot == null || !Robot.IsConnected)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行取料!", Duration = 1 });
- return;
- }
- if (MessageBox.Show(Lang.确认要执行取料吗, Lang.去取料, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- var waiting = new WaitingControl();
- //show the dialog
- var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
- // 1. 控制机器人移动至拍照点位
- var rpoint = new RPoint()
- {
- X = SelectedPoint.X_Position,
- Y = SelectedPoint.Y_Position,
- Z = SelectedPoint.Z_Position_Start,
- U = SelectedPoint.U_Position,
- V = SelectedPoint.R_Position,
- };
- await Robot.CalibMotionAsync(rpoint, 0);
- await Robot.CalibParameAsync(new PickInfo() { Inhal = Inhal, Blow = 1 }, 20, 20, true, 200, 200);
- //打开吸真空
- await Robot.CalibOutIOAsync(true);
- await Task.Delay(500);
- rpoint.Z = 0;
- await Robot.CalibMotionAsync(rpoint, 0);
- if (DialogHost.IsDialogOpen("RootDialog"))
- {
- DialogHost.Close("RootDialog");
- }
- await task;
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "取料完成!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("取料时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- bool CanExecuteCommandGoPickCommandName()
- {
- return SelectedPoint != null;
- }
- ToolCoord toolCoord = null;
- double _FixedUpCameraTempletAngle = 0;
- OpenCvSharp.Point3f[] _PutPoints = new OpenCvSharp.Point3f[2];
- /// <summary>
- /// 去二次定位
- /// </summary>
- async void ExecuteGoRepositionCommand()
- {
- try
- {
- if (SelectedPoint == null)
- {
- return;
- }
- if (Robot == null || !Robot.IsConnected)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.机器人未连接无法执行二次定位, Duration = 1 });
- return;
- }
- if (MessageBox.Show(Lang.确认要执行下相机拍产二次定位吗, Lang.去下相机拍照, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- var waiting = new WaitingControl();
- //show the dialog
- var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
- // 1. 控制机器人移动至拍照点位
- var rpoint = new RPoint()
- {
- X = SelectedPoint.X_Position,
- Y = SelectedPoint.Y_Position,
- Z = SelectedPoint.Z_Position_Start,
- U = SelectedPoint.U_Position,
- V = SelectedPoint.R_Position,
- };
- await Robot.CalibMotionAsync(rpoint, 0);
- await Task.Delay(200);
- //获取固定向上相机1的流程
- var process1 = _productService.GetCurrentProductProcedureModelById(SelectProduct.FixedUpCamera1ProcedureId);
- if (process1 == null)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"固定向上相机1视觉流程,流程ID:{SelectProduct.FixedUpCamera1ProcedureId} 未找到!", Duration = 1 });
- if (DialogHost.IsDialogOpen("RootDialog"))
- {
- DialogHost.Close("RootDialog");
- }
- await task;
- return;
- }
- //获取固定向上相机2的流程
- var process2 = _productService.GetCurrentProductProcedureModelById(SelectProduct.FixedUpCamera2ProcedureId);
- if (process2 == null)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"固定向上相机2视觉流程,流程ID:{SelectProduct.FixedUpCamera2ProcedureId} 未找到!", Duration = 1 });
- if (DialogHost.IsDialogOpen("RootDialog"))
- {
- DialogHost.Close("RootDialog");
- }
- await task;
- return;
- }
- // 执行视觉任务
- var _cts = new CancellationTokenSource();
- var task1 = _remoteCommandService.ExecutePhotoGetSinglePointWithImage(process1, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
- var task2 = _remoteCommandService.ExecutePhotoGetSinglePointWithImage(process2, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
- var visionResults = await Task.WhenAll(task1, task2);
- if (visionResults[0].IsSucceed && visionResults[1].IsSucceed)
- {
- float toolX = (float)(visionResults[0].X + visionResults[1].X) / 2;
- float toolY = (float)(visionResults[0].Y + visionResults[1].Y) / 2;
- _FixedUpCameraTempletAngle = Common.CalculateAngleBetweenPointsDegrees(new PointF((float)visionResults[0].X, (float)visionResults[0].Y), new PointF((float)visionResults[1].X, (float)visionResults[1].Y));
- toolCoord = new ToolCoord();
- toolCoord.SetTool(toolX, toolY);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.视觉定位成功, Duration = 0.4 });
- if (DialogHost.IsDialogOpen("RootDialog"))
- {
- DialogHost.Close("RootDialog");
- }
- await task;
- return;
- }
- else
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.视觉定位失败无法进行二次定位, Duration = 1 });
- if (DialogHost.IsDialogOpen("RootDialog"))
- {
- DialogHost.Close("RootDialog");
- }
- await task;
- return;
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("二次定位时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- if (DialogHost.IsDialogOpen("RootDialog"))
- {
- DialogHost.Close("RootDialog");
- }
- }
- }
- /// <summary>
- /// 执行产品拍照
- /// </summary>
- async void ExecuteTakeProductPhotoCommand()
- {
- try
- {
- if (SelectedPoint == null)
- {
- return;
- }
- if (Robot == null || !Robot.IsConnected)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.机器人未连接无法执行产品拍照, Duration = 1 });
- return;
- }
- if (MessageBox.Show(Lang.确认要执行产品拍照吗, Lang.去产品位置拍照, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- //如果二次定位的工具坐标为空,则提示用户先进行二次定位
- if (toolCoord == null)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.二次定位的工具坐标为空无法执行产品拍照, Duration = 1 });
- return;
- }
- var waiting = new WaitingControl();
- //show the dialog
- var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
- // 1. 控制机器人移动至拍照点位
- var rpoint = new RPoint()
- {
- X = Points[0].X_Position,
- Y = Points[0].Y_Position,
- Z = Points[0].Z_Position_Start,
- U = Points[0].U_Position,
- V = Points[0].R_Position,
- };
- await Robot.CalibMotionAsync(rpoint, 0);
- await Task.Delay(200);
- //获取移动向下相机1的流程
- var process1 = _productService.GetCurrentProductProcedureModelById(Points[0].VisionProcessID);
- if (process1 == null)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = string.Format(Lang.移动向下相机1视觉流程流程ID0未找到,Points[0].VisionProcessID), Duration = 1 });
- if (DialogHost.IsDialogOpen("RootDialog"))
- {
- DialogHost.Close("RootDialog");
- }
- await task;
- return;
- }
- // 执行视觉任务
- var _cts = new CancellationTokenSource();
- var visionResult =await _remoteCommandService.ExecutePhotoGetSinglePointWithImage(process1, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
- if (visionResult.IsSucceed)
- {
- _PutPoints[0] = new OpenCvSharp.Point3f((float)visionResult.X, (float)visionResult.Y, (float)visionResult.U);
- }
- else
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.视觉定位失败无法进行产品拍照, Duration = 1 });
- if (DialogHost.IsDialogOpen("RootDialog"))
- {
- DialogHost.Close("RootDialog");
- }
- await task;
- return;
- }
- // 2. 控制机器人移动至拍照点位
- rpoint = new RPoint()
- {
- X = Points[1].X_Position,
- Y = Points[1].Y_Position,
- Z = Points[1].Z_Position_Start,
- U = Points[1].U_Position,
- V = Points[1].R_Position,
- };
- await Robot.CalibMotionAsync(rpoint, 0);
- await Task.Delay(200);
- //获取移动向下相机1的流程
- var process2= _productService.GetCurrentProductProcedureModelById(Points[1].VisionProcessID);
- if (process2 == null)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = string.Format(Lang.移动向下相机2视觉流程流程ID0未找到, Points[1].VisionProcessID), Duration = 1 });
- if (DialogHost.IsDialogOpen("RootDialog"))
- {
- DialogHost.Close("RootDialog");
- }
- await task;
- return;
- }
- // 执行视觉任务
- _cts = new CancellationTokenSource();
- visionResult = await _remoteCommandService.ExecutePhotoGetSinglePointWithImage(process2, null, new double[] { rpoint.X, rpoint.Y, rpoint.U }, _cts.Token);
- if (visionResult.IsSucceed)
- {
- _PutPoints[1] = new OpenCvSharp.Point3f((float)visionResult.X, (float)visionResult.Y, (float)visionResult.U);
- if (DialogHost.IsDialogOpen("RootDialog"))
- {
- DialogHost.Close("RootDialog");
- }
- await task;
- return;
- }
- else
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.视觉定位失败无法进行产品拍照, Duration = 1 });
- if (DialogHost.IsDialogOpen("RootDialog"))
- {
- DialogHost.Close("RootDialog");
- }
- await task;
- return;
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("执行产品拍照时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- if (DialogHost.IsDialogOpen("RootDialog"))
- {
- DialogHost.Close("RootDialog");
- }
- }
- }
- /// <summary>
- /// 执行产品贴敷
- /// </summary>
- async void ExecuteTakeProductCommand()
- {
- try
- {
- if (SelectedPoint == null)
- {
- return;
- }
- if (Robot == null || !Robot.IsConnected)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.机器人未连接无法执行产品贴敷, Duration = 1 });
- return;
- }
- if (MessageBox.Show(Lang.确认要执行产品贴敷吗, Lang.去产品贴敷, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- //如果二次定位的工具坐标为空,则提示用户先进行二次定位
- if (toolCoord == null)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.二次定位的工具坐标为空无法执行产品拍照, Duration = 1 });
- return;
- }
- if (_PutPoints[0].X == 0 && _PutPoints[0].Y == 0 && _PutPoints[1].X == 0 && _PutPoints[1].Y == 0)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.检测到未定位产品, Duration = 1 });
- return;
- }
- var waiting = new WaitingControl();
- //show the dialog
- var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
- double upCameraX = (_PutPoints[0].X + _PutPoints[1].X) / 2;
- double upCameraY = (_PutPoints[0].Y + _PutPoints[1].Y) / 2;
- double upCameraU = Common.CalculateAngleBetweenPointsDegrees(new PointF((float)_PutPoints[0].X, (float)_PutPoints[0].Y), new PointF((float)_PutPoints[1].X, (float)_PutPoints[1].Y));
- var pn = Vector<double>.Build.Dense(new double[] { upCameraX, upCameraY });
- //目标角度:
- double angle = SelectedPoint.U_Position + (_FixedUpCameraTempletAngle - SelectProduct.FixedUpCameraTempletAngle) + (upCameraU - SelectProduct.MoveDownCamerTempletAngle);
- //将_PutPoint转换为Tool0下要走的点
- var p0 = toolCoord.GetTool0Coord(pn, angle);
- // 1. 控制机器人移动至拍照点位
- var rpoint = new RPoint()
- {
- X = (float)p0[0],
- Y = (float)p0[1],
- Z = SelectedPoint.Z_Position_Start,
- U = (float)angle,
- V = SelectedPoint.R_Position,
- };
- await Robot.CalibMotionAsync(rpoint, 0);
- if (DialogHost.IsDialogOpen("RootDialog"))
- {
- DialogHost.Close("RootDialog");
- }
- await task;
- return;
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("执行产品贴敷时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- if (DialogHost.IsDialogOpen("RootDialog"))
- {
- DialogHost.Close("RootDialog");
- }
- }
- }
- /// <summary>
- /// 附加轴+点动
- /// </summary>
- /// <param name="obj"></param>
- async void ExecuteAdditionalAxisJogPlusCommand(object obj)
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- if (obj is AxisExtended axis)
- {
- //如果移动距离大于10mm,则提示用户确认
- if (Math.Abs(Distance) >= 10)
- {
- if (MessageBox.Show(string.Format(Lang.确认要点动距离0mm吗, Distance), Lang.确认点动, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- }
- await Robot.AdditionalAxisJogAsync(axis.Name, Distance);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("附加轴点动+时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 附加轴-点动
- /// </summary>
- /// <param name="obj"></param>
- async void ExecuteAdditionalAxisJogMinusCommand(object obj)
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- if (obj is AxisExtended axis)
- {
- //如果移动距离大于10mm,则提示用户确认
- if (Math.Abs(Distance) >= 10)
- {
- if (MessageBox.Show(string.Format(Lang.确认要点动距离0mm吗, Distance), Lang.确认点动, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- }
- await Robot.AdditionalAxisJogNegAsync(axis.Name, Distance);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("附加轴点动-时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 附加轴下使能
- /// </summary>
- /// <param name="obj"></param>
- async void ExecuteAdditionalAxisMotorOffCommand(object obj)
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- if (obj is AxisExtended axis)
- {
- await Robot.AdditionalAxisMotorAsync(axis.Name, false);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("附加轴上使能时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 附加轴上使能
- /// </summary>
- /// <param name="obj"></param>
- async void ExecuteAdditionalAxisMotorOnCommand(object obj)
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- if (obj is AxisExtended axis)
- {
- await Robot.AdditionalAxisMotorAsync(axis.Name, true);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("附加轴下使能时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 移动取料托盘点
- /// </summary>
- async void ExecutePickMoveCommand(string parameter)
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- //弹窗用户是否确认要示教该点位
- if (MessageBox.Show(string.Format(Lang.确认要移动取料点位0吗, parameter), Lang.移动点位, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- int index = int.Parse(parameter);
- var curpos = await Robot.GetRobotPosAsync();
- var addpos = await Robot.GetAdditionalAxisCurrentPosAsync(Robot.AdditionalAxisList[0].Name);
- if (index == 1)
- {
- curpos.Z = 0;
- await Robot.CalibMotionAsync(curpos, 0);
- await Robot.AdditionalAxisMoveAsync(Robot.AdditionalAxisList[0].Name, PickP1.Z_Position_Stop);
- curpos.X = PickP1.X_Position;
- curpos.Y = PickP1.Y_Position;
- curpos.Z = PickP1.Z_Position_Start;
- curpos.U = PickP1.U_Position;
- await Robot.CalibMotionAsync(curpos, 0);
- }
- else if (index == 2)
- {
- curpos.Z = 0;
- await Robot.CalibMotionAsync(curpos, 0);
- await Robot.AdditionalAxisMoveAsync(Robot.AdditionalAxisList[0].Name, PickP2.Z_Position_Stop);
- curpos.X = PickP2.X_Position;
- curpos.Y = PickP2.Y_Position;
- curpos.Z = PickP2.Z_Position_Start;
- curpos.U = PickP2.U_Position;
- await Robot.CalibMotionAsync(curpos, 0);
- }
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("示教取料托盘点时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 示教取料托盘点
- /// </summary>
- /// <param name="parameter"></param>
- async void ExecutePickTechCommand(string parameter)
- {
- if (Robot == null || !Robot.IsConnected) return;
- try
- {
- //弹窗用户是否确认要示教该点位
- if (MessageBox.Show(string.Format(Lang.确认要示教取料点位0吗, parameter), Lang.示教点位, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- int index = int.Parse(parameter);
- var curpos = await Robot.GetRobotPosAsync();
- var addpos = await Robot.GetAdditionalAxisCurrentPosAsync(Robot.AdditionalAxisList[0].Name);
- if (index == 1)
- {
- PickP1.X_Position = curpos.X;
- PickP1.Y_Position = curpos.Y;
- PickP1.Z_Position_Start = curpos.Z;
- PickP1.U_Position = curpos.U;
- PickP1.Z_Position_Stop = addpos;
- }
- else if (index == 2)
- {
- PickP2.X_Position = curpos.X;
- PickP2.Y_Position = curpos.Y;
- PickP2.Z_Position_Start = curpos.Z;
- PickP2.U_Position = curpos.U;
- PickP2.Z_Position_Stop = addpos;
- }
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("示教取料托盘点时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 计算所有托盘点
- /// </summary>
- void ExecuteCalPickPosCommand()
- {
- try
- {
- if (ColumnNum < 1)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.请设置托盘列数, Duration = 1 });
- return;
- }
- //弹窗用户是否确认要示教该点位
- if (MessageBox.Show(Lang.确认要计算托盘吗, Lang.计算托盘, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- //托盘间隔
- double invt = (PickP2.Z_Position_Stop - PickP1.Z_Position_Stop) / (ColumnNum - 1);
- for (int i = 0; i < ColumnNum; i++)
- {
- if (Points.Count - 1 <= i)
- {
- Points[i].Number = i + 1;
- Points[i].X_Position = PickP1.X_Position;
- Points[i].X_Velocity = 100;
- Points[i].Y_Position = PickP1.Y_Position;
- Points[i].Y_Velocity = 100;
- Points[i].Z_Position_Start = PickP1.Z_Position_Start;
- Points[i].Z_Velocity_Start = 100;
- Points[i].U_Position = PickP1.U_Position;
- Points[i].U_Velocity = 100;
- Points[i].Z_Position_Stop = (float)(PickP1.Z_Position_Stop + i * invt);
- Points[i].Z_Velocity_Stop = 100;
- }
- else
- {
- Points.Add(new PlcPoint()
- {
- Number= i + 1,
- X_Position = PickP1.X_Position,
- X_Velocity = 100,
- Y_Position = PickP1.Y_Position,
- Y_Velocity = 100,
- Z_Position_Start = PickP1.Z_Position_Start,
- Z_Velocity_Start = 100,
- U_Position = PickP1.U_Position,
- U_Velocity = 100,
- Z_Position_Stop = (float)(PickP1.Z_Position_Stop + i * invt),
- Z_Velocity_Stop = 100
- });
- }
- }
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("示教取料托盘点时出错!", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 动态测试分析仪
- /// </summary>
- void ExecuteDynamicTestAnalyzerCommand()
- {
- try
- {
- IDialogParameters parameters = new DialogParameters();
- parameters.Add("SelectProduct", SelectProduct);
- parameters.Add("Robot", Robot);
- _dialogService.Show("VisionDynamicAccuracyAnalyzer", parameters, rst =>
- {
- //对话框关闭之后的回调函数,可以在这解析结果。
- ButtonResult result1 = rst.Result;
- if (result1 == ButtonResult.OK)
- {
- //var param = rst.Parameters.GetValue<RobotTool>("Tool");
- //var param1 = rst.Parameters.GetValue<Matrix<double>>("RobotCameraRotationMatrix");
- //var param2 = rst.Parameters.GetValue<double>("Angle");
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
- }
- });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("执行动态测试分析仪命令时出错", ex);
- MessageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// 导出所有点位到本地文件(XML)
- /// </summary>
- void ExecuteExportAllPointsCommand()
- {
- try
- {
- if (SelectProduct == null)
- {
- MessageBox.Show(Lang.未选择产品, Lang.错误, MessageBoxButton.OK, MessageBoxImage.Warning);
- return;
- }
- var dlg = new SaveFileDialog();
- dlg.Filter = "Plc Product Points File|*.plcproductpoints.xml|All files|*.*";
- dlg.FileName = SelectProduct != null ? ($"{SelectProduct.Name}_allpoints_{DateTime.Now:yyyyMMdd_HHmmss}.plcproductpoints.xml") : ($"product_points_{DateTime.Now:yyyyMMdd_HHmmss}.plcproductpoints.xml");
- var res = dlg.ShowDialog();
- if (res != true) return;
- var filePath = dlg.FileName;
- // 构建导出对象
- var export = new ProductPointsExport
- {
- SoftwareVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(),
- ProductName = SelectProduct.Name,
- ProductDescription = SelectProduct.Description,
- DeviceName = _configService.GetDeviceInfo().Station,
- Created = DateTime.Now,
- PickCameraPoints = SelectProduct.PickCameraPoints?.Select(p => p.Clone()).ToList() ?? new List<PlcPoint>(),
- PickPoints = SelectProduct.PickPoints?.Select(p => p.Clone()).ToList() ?? new List<PlcPoint>(),
- SecPosCameraPoints = SelectProduct.SecPosCameraPoints?.Select(p => p.Clone()).ToList() ?? new List<PlcPoint>(),
- ScrewCameraPoints = SelectProduct.ScrewCameraPoints?.Select(p => p.Clone()).ToList() ?? new List<PlcPoint>(),
- ScrewCameraLocalPos1 = SelectProduct.ScrewCameraLocalPos1?.Clone() ?? new PlcPoint(),
- ScrewCameraLocalPos2 = SelectProduct.ScrewCameraLocalPos2?.Clone() ?? new PlcPoint(),
- ScrewPoints = SelectProduct.ScrewPoints?.Select(p => p.Clone()).ToList() ?? new List<PlcPoint>(),
- CheckCameraPoints = SelectProduct.CheckCameraPoints?.Select(p => p.Clone()).ToList() ?? new List<PlcPoint>(),
- RemovePoints = SelectProduct.RemovePoints?.Select(p => p.Clone()).ToList() ?? new List<PlcPoint>(),
- PressurePlacePoints = SelectProduct.PressurePlacePoints?.Select(p => p.Clone()).ToList() ?? new List<PlcPoint>(),
- };
- var serializer = new XmlSerializer(typeof(ProductPointsExport));
- using (var fs = File.Create(filePath))
- {
- serializer.Serialize(fs, export);
- }
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.导出完成, Duration = 1 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("导出点位时出错", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 导入点位文件,覆盖当前点位
- /// </summary>
- void ExecuteImportPointsCommand()
- {
- try
- {
- var dlg = new OpenFileDialog();
- dlg.Filter = "Plc Points File|*.plcproductpoints.xml|All files|*.*";
- var res = dlg.ShowDialog();
- if (res != true) return;
- var filePath = dlg.FileName;
- // 验证并读取文件(ProductPointsExport)
- var serializer = new XmlSerializer(typeof(ProductPointsExport));
- ProductPointsExport import = null;
- using (var fs = File.OpenRead(filePath))
- {
- try
- {
- import = (ProductPointsExport)serializer.Deserialize(fs);
- }
- catch (Exception ex)
- {
- MessageBox.Show(Lang.无效的点位文件或文件已损坏, Lang.错误, MessageBoxButton.OK, MessageBoxImage.Error);
- LogHelper.WriteLogError("导入点位文件解析失败", ex);
- return;
- }
- }
- if (import == null)
- {
- MessageBox.Show(Lang.导入的点位文件为空, Lang.提示, MessageBoxButton.OK, MessageBoxImage.Information);
- return;
- }
- // 提示用户是否覆盖
- if (MessageBox.Show("确认要导入点位文件并覆盖当前所有点位吗", "导入点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- // 覆盖 SelectProduct 下的各类点集合
- SelectProduct.PickCameraPoints = new ObservableCollection<PlcPoint>(import.PickCameraPoints ?? new List<PlcPoint>());
- SelectProduct.PickPoints = new ObservableCollection<PlcPoint>(import.PickPoints ?? new List<PlcPoint>());
- SelectProduct.SecPosCameraPoints = new ObservableCollection<PlcPoint>(import.SecPosCameraPoints ?? new List<PlcPoint>());
- SelectProduct.ScrewCameraPoints = new ObservableCollection<PlcPoint>(import.ScrewCameraPoints ?? new List<PlcPoint>());
- SelectProduct.ScrewCameraLocalPos1 = import.ScrewCameraLocalPos1 ?? new PlcPoint();
- SelectProduct.ScrewCameraLocalPos2 = import.ScrewCameraLocalPos2 ?? new PlcPoint();
- SelectProduct.ScrewPoints = new ObservableCollection<PlcPoint>(import.ScrewPoints ?? new List<PlcPoint>());
- SelectProduct.CheckCameraPoints = new ObservableCollection<PlcPoint>(import.CheckCameraPoints ?? new List<PlcPoint>());
- SelectProduct.RemovePoints = new ObservableCollection<PlcPoint>(import.RemovePoints ?? new List<PlcPoint>());
- SelectProduct.PressurePlacePoints = new ObservableCollection<PlcPoint>(import.PressurePlacePoints ?? new List<PlcPoint>());
- // 重新编号每个集合
- Action<ObservableCollection<PlcPoint>> renumber = (col) =>
- {
- for (int i = 0; i < col.Count; i++) col[i].Number = i + 1;
- };
- renumber(SelectProduct.PickCameraPoints);
- renumber(SelectProduct.PickPoints);
- renumber(SelectProduct.SecPosCameraPoints);
- renumber(SelectProduct.ScrewCameraPoints);
- renumber(SelectProduct.ScrewPoints);
- renumber(SelectProduct.CheckCameraPoints);
- renumber(SelectProduct.RemovePoints);
- renumber(SelectProduct.PressurePlacePoints);
- // 根据当前 SelectedIndex 切换 Points 绑定到对应集合
- ExecuteSelectionChangedCommand();
- LogHelper.WriteLogDebug("导入点位文件成功");
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "导入完成", Duration = 1 });
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("导入点位时出错", ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- #endregion
- #region 继承
- /// <summary>
- /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
- /// </summary>
- /// <param name="navigationContext"></param>
- /// <param name="continuationCallback"></param>
- public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
- {
- continuationCallback(true);
- }
- /// <summary>
- /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
- /// </summary>
- /// <param name="navigationContext"></param>
- /// <exception cref="NotImplementedException"></exception>
- public async void OnNavigatedTo(NavigationContext navigationContext)
- {
- try
- {
- toolCoord = null;
- SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
- SelectedIndex = 0;
- Points = SelectProduct.PickCameraPoints;
- if (Robot == null)
- {
- Robot = _robotService.GetRobotByNumber(1) as XYZU_Robot;
- }
- if (Robot != null && Robot.IsConnected)
- {
- this.Distance = _configService.GetRobotStepDistance(Robot.Id);
- Robot.EnterDebugMode = true;
- AdditionalAxisList = Robot.AdditionalAxisList;
- }
- else if (Robot != null)
- {
- Robot.EnterDebugMode = true;
- }
- ProcedureModels = new ObservableCollection<ProcedureModel>();
- foreach (var item in SelectProduct.CameraProcedures)
- {
- foreach (var item1 in item.ProcedureModels)
- {
- ProcedureModels.Add(item1);
- }
- }
- CameraList = new ObservableCollection<CameraInfo>(_configService.GetAllCameras());
- if (SelectProduct.PickPoints!=null && SelectProduct.PickPoints.Count>0)
- {
- PickP1 = SelectProduct.PickPoints.FirstOrDefault().Clone();
- PickP2 = SelectProduct.PickPoints.LastOrDefault().Clone();
- ColumnNum = SelectProduct.PickPoints.Count;
- }
- if (SelectProduct.PickCameraPoints == null)
- {
- SelectProduct.PickCameraPoints = new ObservableCollection<PlcPoint>();
- }
- if (SelectProduct.PickPoints == null)
- {
- SelectProduct.PickPoints = new ObservableCollection<PlcPoint>();
- }
- if (SelectProduct.SecPosCameraPoints == null)
- {
- SelectProduct.SecPosCameraPoints = new ObservableCollection<PlcPoint>();
- }
- if (SelectProduct.ScrewCameraPoints == null)
- {
- SelectProduct.ScrewCameraPoints = new ObservableCollection<PlcPoint>();
- }
- if (SelectProduct.ScrewPoints == null)
- {
- SelectProduct.ScrewPoints = new ObservableCollection<PlcPoint>();
- }
- if (SelectProduct.CheckCameraPoints == null)
- {
- SelectProduct.CheckCameraPoints = new ObservableCollection<PlcPoint>();
- }
- if (SelectProduct.RemovePoints == null)
- {
- SelectProduct.RemovePoints = new ObservableCollection<PlcPoint>();
- }
- if (SelectProduct.PressurePlacePoints == null)
- {
- SelectProduct.PressurePlacePoints = new ObservableCollection<PlcPoint>();
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
- }
- }
- /// <summary>
- /// 是否允许导航到此视图模型。
- /// </summary>
- /// <param name="navigationContext"></param>
- /// <returns></returns>
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- /// <summary>
- /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
- /// </summary>
- /// <param name="navigationContext"></param>
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- //if (Robot != null)
- //{
- // Robot.EnterDebugMode = false;
- //}
- try
- {
- IsGrap = false;
- if (Camera != null)
- {
- if (Camera.IsGrabbing)
- {
- Camera.StopGrabbing();
- }
- Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
- Camera = null;
- }
- SelectedCamera = null;
- IsLeftDrawerOpen = false;
- }
- catch (Exception)
- {
- }
- }
- #endregion
- private bool _IsAllowEdit = false;
- public bool IsAllowEdit
- {
- get { return _IsAllowEdit; }
- set { SetProperty(ref _IsAllowEdit, value); }
- }
- private void LoginChange(Models.User user)
- {
- if (user.userPart == Enums.UserPart.Operator)
- {
- IsAllowEdit = false;
- }
- else
- {
- IsAllowEdit = true;
- }
- }
- }
- }
|