| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391 |
- using Cognex.VisionPro;
- using MahApps.Metro.Controls;
- using MaterialDesignThemes.Wpf;
- 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;
- /// <summary>
- /// 吸真空编号
- /// </summary>
- public int Inhal
- {
- get { return _Inhal; }
- set { SetProperty(ref _Inhal, value); }
- }
- private PalletEx _Pallet1;
- /// <summary>
- /// 托盘1
- /// </summary>
- public PalletEx Pallet1
- {
- get { return _Pallet1; }
- set { SetProperty(ref _Pallet1, value); }
- }
- private PalletEx _Pallet2;
- /// <summary>
- /// 托盘2
- /// </summary>
- public PalletEx Pallet2
- {
- get { return _Pallet2; }
- set { SetProperty(ref _Pallet2, 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> IndependentCamerPoints { get; set; }
- public List<PlcPoint> RemovePoints { get; set; }
- public List<PlcPoint> QrCodePoints { get; set; }
- public List<PlcPoint> PressurePlacePoints { get; set; }
- public List<PlcPoint> Reference_coordinate { 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<string> _CalPickPosCommand;
- public DelegateCommand<string> CalPickPosCommand =>
- _CalPickPosCommand ?? (_CalPickPosCommand = new DelegateCommand<string>(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($"确认要点动距离 {distance} mm 吗?", 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($"确认要执行点位 {SelectedPoint.Number} 的 {motionCmd} 指令吗?", "执行点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- if (SelectedIndex == 6)
- {
- await Robot.WaitCameraMoveFinishedAsync(point.Number, point);
- }
- else if (SelectedIndex == 8)
- {
- //弹窗用户是否确认要示教该点位
- if (MessageBox.Show($"确认要示教点位 {SelectedPoint.Number} 吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- await Robot.AdditionalAxisMoveAsync(Robot.AdditionalAxisList[1].Name, SelectedPoint.Z_Position_Start);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- return;
- }
- else if (SelectedIndex == 1)
- {
- var curpos = await Robot.GetRobotPosAsync();
- //var addpos = await Robot.GetAdditionalAxisCurrentPosAsync(Robot.AdditionalAxisList[0].Name);
- curpos.Z = 0;
- await Robot.CalibMotionAsync(curpos, 0);
- await Robot.AdditionalAxisMoveAsync(Robot.AdditionalAxisList[0].Name, SelectedPoint.Z_Position_Stop);
- curpos.X = SelectedPoint.X_Position;
- curpos.Y = SelectedPoint.Y_Position;
- curpos.Z = SelectedPoint.Z_Position_Start;
- curpos.U = SelectedPoint.U_Position;
- await Robot.CalibMotionAsync(curpos, 0);
- }
- else
- {
- //Robot.Timeout = 6000000;
- switch (motionCmd)
- {
- case MotionCommand.Go:
- await Robot.GoAsync(point);
- break;
- case MotionCommand.Jump:
- await Robot.JumpAsync(point, null);
- 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($"确认要示教点位 {SelectedPoint.Number} 吗?", "示教点位", 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();
- //var addpos = await Robot.GetAdditionalAxisCurrentPosAsync(Robot.AdditionalAxisList[1].Name);
- //将当前点位转换为产品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;
- //SelectedPoint.Z_Position_Stop = addpos;
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- return;
- }
- else if (SelectedIndex == 1)
- {
- //弹窗用户是否确认要示教该点位
- if (MessageBox.Show($"确认要示教点位 {SelectedPoint.Number} 吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- var curpos1 = await Robot.GetRobotPosAsync();
- var addpos = await Robot.GetAdditionalAxisCurrentPosAsync(Robot.AdditionalAxisList[0].Name);
- //将当前点位转换为产品Local坐标系下的点位
- SelectedPoint.X_Position = (float)curpos1.X;
- SelectedPoint.Y_Position = (float)curpos1.Y;
- SelectedPoint.Z_Position_Start = curpos1.Z;
- SelectedPoint.Z_Position_Stop = addpos;
- SelectedPoint.U_Position = curpos1.U;
- SelectedPoint.R_Position = curpos1.V;
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- return;
- }
- else if (SelectedIndex == 6)
- {
- //弹窗用户是否确认要示教该点位
- if (MessageBox.Show($"确认要示教点位 {SelectedPoint.Number} 吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- var curpos1 = await Robot.GetCameraPosAsync(SelectedPoint.Number);
- //将当前点位转换为产品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;
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- return;
- }
- else if (SelectedIndex == 8)
- {
- //弹窗用户是否确认要示教该点位
- if (MessageBox.Show($"确认要示教点位 {SelectedPoint.Number} 吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- var addpos = await Robot.GetAdditionalAxisCurrentPosAsync(Robot.AdditionalAxisList[1].Name);
- SelectedPoint.Z_Position_Start = addpos;
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
- return;
- }
- else
- {
- //弹窗用户是否确认要示教该点位
- if (MessageBox.Show($"确认要示教点位 {SelectedPoint.Number} 吗?", "示教点位", 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.IndependentCamerPoints;
- break;
- case 7:
- Points = SelectProduct.RemovePoints;
- break;
- case 8:
- Points = SelectProduct.PressurePlacePoints;
- break;
- case 9:
- Points = SelectProduct.QrCodePoints;
- break;
- case 10:
- Points = SelectProduct.Reference_coordinate;
- 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("确认要删除选中的点位吗?", "删除点位", 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("确认将选中点位上移一位吗?", "移动点位", 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("确认将选中点位下移一位吗?", "移动点位", 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($"确认要从点位编号 {startNumber} 开始导入 {cadPoints.Count} 个点位吗?", "导入点位", 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", "取料拍照点位-补偿值设置");
- }
- else if (SelectedIndex == 1)
- {
- parameters1.Add("Title", "取料点位-补偿值设置");
- }
- else if (SelectedIndex == 2)
- {
- parameters1.Add("Title", "下相机二次定位点位-补偿值设置");
- }
- else if (SelectedIndex == 3)
- {
- parameters1.Add("Title", "锁附拍照点位-补偿值设置");
- }
- else if (SelectedIndex == 4)
- {
- parameters1.Add("Title", "锁附点位-补偿值设置");
- }
- else if (SelectedIndex == 5)
- {
- parameters1.Add("Title", "复检点位-补偿值设置");
- }
- else
- {
- parameters1.Add("Title", "点位-补偿值设置");
- }
- 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("确认要执行取料吗?", "去取料", 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, null);
- 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, null);
- 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 = "机器人未连接,无法执行二次定位!", Duration = 1 });
- return;
- }
- if (MessageBox.Show("确认要执行下相机拍产二次定位吗?", "去下相机拍照", 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, null);
- 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 = "视觉定位成功!", Duration = 0.4 });
- if (DialogHost.IsDialogOpen("RootDialog"))
- {
- DialogHost.Close("RootDialog");
- }
- await task;
- return;
- }
- else
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "视觉定位失败,无法进行二次定位!", 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 = "机器人未连接,无法执行产品拍照!", Duration = 1 });
- return;
- }
- if (MessageBox.Show("确认要执行产品拍照吗?", "去产品位置拍照", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- //如果二次定位的工具坐标为空,则提示用户先进行二次定位
- if (toolCoord == null)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "二次定位的工具坐标为空,无法执行产品拍照!", 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, null);
- await Task.Delay(200);
- //获取移动向下相机1的流程
- var process1 = _productService.GetCurrentProductProcedureModelById(SelectProduct.MoveDownCamera1LockPhotoProcedureId);
- if (process1 == null)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"移动向下相机1视觉流程,流程ID:{SelectProduct.MoveDownCamera1LockPhotoProcedureId} 未找到!", 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 = "视觉定位失败,无法进行产品拍照!", 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, null);
- await Task.Delay(200);
- //获取移动向下相机1的流程
- var process2 = _productService.GetCurrentProductProcedureModelById(SelectProduct.MoveDownCamera2LockPhotoProcedureId);
- if (process2 == null)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"移动向下相机2视觉流程,流程ID:{SelectProduct.MoveDownCamera2LockPhotoProcedureId} 未找到!", 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 = "视觉定位失败,无法进行产品拍照!", 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 = "机器人未连接,无法执行产品贴敷!", Duration = 1 });
- return;
- }
- if (MessageBox.Show("确认要执行产品贴敷吗?", "去产品贴敷", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- //如果二次定位的工具坐标为空,则提示用户先进行二次定位
- if (toolCoord == null)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "二次定位的工具坐标为空,无法执行产品拍照!", 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 = "检测到未定位产品", 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, null);
- 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($"确认要点动距离 {Distance} mm 吗?", "确认点动", 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($"确认要点动距离 {Distance} mm 吗?", "确认点动", 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($"确认要移动取料点位 {parameter} 吗?", "移动点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- int palletIndex = int.Parse(parameter.Split('-')[0]);
- int pointIndex = int.Parse(parameter.Split('-')[1]);
- var curpos = await Robot.GetRobotPosAsync();
- var addpos = await Robot.GetAdditionalAxisCurrentPosAsync(Robot.AdditionalAxisList[0].Name);
- PlcPoint pn = null;
- if (palletIndex == 1)
- {
- switch (pointIndex)
- {
- case 1:
- pn = Pallet1.P0.Clone();
- break;
- case 2:
- pn = Pallet1.P1.Clone();
- break;
- case 3:
- pn = Pallet1.P2.Clone();
- break;
- default:
- break;
- }
- }
- else
- {
- switch (pointIndex)
- {
- case 1:
- pn = Pallet2.P0.Clone();
- break;
- case 2:
- pn = Pallet2.P1.Clone();
- break;
- case 3:
- pn = Pallet2.P2.Clone();
- break;
- default:
- break;
- }
- }
- if (pn == null)
- {
- return;
- }
- // 1.Z轴上升到安全高度
- curpos.Z = 0;
- bool isSuccess = await Robot.CalibMotionAsync(curpos, null);
- if (!isSuccess)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人移动失败!", Duration = 1 });
- return;
- }
- // 2.附加轴移动
- isSuccess = await Robot.AdditionalAxisMoveAsync(Robot.AdditionalAxisList[0].Name, pn.Z_Position_Stop);
- if (!isSuccess)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "附加轴移动失败!", Duration = 1 });
- return;
- }
- // 3.移动XY轴到位
- curpos.X = pn.X_Position;
- curpos.Y = pn.Y_Position;
- curpos.Z = pn.Z_Position_Start;
- curpos.U = pn.U_Position;
- isSuccess = await Robot.CalibMotionAsync(curpos, null);
- if (!isSuccess)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人移动失败!", Duration = 1 });
- return;
- }
- _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($"确认要示教取料点位 {parameter} 吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
- {
- return;
- }
- int palletIndex = int.Parse(parameter.Split('-')[0]);
- int pointIndex = int.Parse(parameter.Split('-')[1]);
- var curpos = await Robot.GetRobotPosAsync();
- var addpos = await Robot.GetAdditionalAxisCurrentPosAsync(Robot.AdditionalAxisList[0].Name);
- if (palletIndex == 1)
- {
- switch (pointIndex)
- {
- case 1:
- Pallet1.P0.X_Position = curpos.X;
- Pallet1.P0.Y_Position = curpos.Y;
- Pallet1.P0.Z_Position_Start = curpos.Z;
- Pallet1.P0.U_Position = curpos.U;
- Pallet1.P0.Z_Position_Stop = addpos;
- break;
- case 2:
- Pallet1.P1.X_Position = curpos.X;
- Pallet1.P1.Y_Position = curpos.Y;
- Pallet1.P1.Z_Position_Start = curpos.Z;
- Pallet1.P1.U_Position = curpos.U;
- Pallet1.P1.Z_Position_Stop = addpos;
- break;
- case 3:
- Pallet1.P2.X_Position = curpos.X;
- Pallet1.P2.Y_Position = curpos.Y;
- Pallet1.P2.Z_Position_Start = curpos.Z;
- Pallet1.P2.U_Position = curpos.U;
- Pallet1.P2.Z_Position_Stop = addpos;
- break;
- default:
- break;
- }
- }
- else
- {
- switch (pointIndex)
- {
- case 1:
- Pallet2.P0.X_Position = curpos.X;
- Pallet2.P0.Y_Position = curpos.Y;
- Pallet2.P0.Z_Position_Start = curpos.Z;
- Pallet2.P0.U_Position = curpos.U;
- Pallet2.P0.Z_Position_Stop = addpos;
- break;
- case 2:
- Pallet2.P1.X_Position = curpos.X;
- Pallet2.P1.Y_Position = curpos.Y;
- Pallet2.P1.Z_Position_Start = curpos.Z;
- Pallet2.P1.U_Position = curpos.U;
- Pallet2.P1.Z_Position_Stop = addpos;
- break;
- case 3:
- Pallet2.P2.X_Position = curpos.X;
- Pallet2.P2.Y_Position = curpos.Y;
- Pallet2.P2.Z_Position_Start = curpos.Z;
- Pallet2.P2.U_Position = curpos.U;
- Pallet2.P2.Z_Position_Stop = addpos;
- break;
- default:
- 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>
- void ExecuteCalPickPosCommand(string parameter)
- {
- try
- {
- int palletIndex = int.Parse(parameter);
- if (palletIndex == 1)
- {
- PalletTool palletTool = new PalletTool(Pallet1);
- //托盘中所有的点‘
- var points = palletTool.PlcPoints;
- //将所有的点写入到取料点中,如果当前定义的取料点不存在,则新增
- for (int i = 0; i < points.Length; i++)
- {
- //如果当前的取料点数量小于等于i,则新增一个取料点
- if (SelectProduct.PickPoints.Count <= i)
- {
- SelectProduct.PickPoints.Add(new PlcPoint()
- {
- Number = i + 1,
- X_Position = points[i].X_Position,
- X_Velocity = 100,
- Y_Position = points[i].Y_Position,
- Y_Velocity = 100,
- Z_Position_Start = points[i].Z_Position_Start,
- Z_Velocity_Start = 100,
- U_Position = points[i].U_Position,
- U_Velocity = 100,
- Z_Position_Stop = points[i].Z_Position_Stop,
- Z_Velocity_Stop = 100,
- });
- }
- else
- {
- //否则更新当前的取料点
- SelectProduct.PickPoints[i].Number = i + 1;
- SelectProduct.PickPoints[i].X_Position = points[i].X_Position;
- SelectProduct.PickPoints[i].Y_Position = points[i].Y_Position;
- SelectProduct.PickPoints[i].Z_Position_Start = points[i].Z_Position_Start;
- SelectProduct.PickPoints[i].U_Position = points[i].U_Position;
- SelectProduct.PickPoints[i].Z_Position_Stop = points[i].Z_Position_Stop;
- }
- }
- }
- else
- {
- //如果当前的取料点数量小于托盘1的点数量,则提示用户先计算托盘1的点
- if (SelectProduct.PickPoints.Count < Pallet1.Row * Pallet1.Column)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "请先计算托盘1的点位!", Duration = 1 });
- return;
- }
- PalletTool palletTool = new PalletTool(Pallet2);
- //托盘中所有的点‘
- var points = palletTool.PlcPoints;
- //将所有的点写入到取料点中,如果当前定义的取料点不存在,则新增
- for (int i = 0; i < points.Length; i++)
- {
- int index = i + Pallet1.Row * Pallet1.Column;
- //如果当前的取料点数量小于等于index,则新增一个取料点
- if (SelectProduct.PickPoints.Count <= index)
- {
- SelectProduct.PickPoints.Add(new PlcPoint()
- {
- Number = index + 1,
- X_Position = points[i].X_Position,
- X_Velocity = 100,
- Y_Position = points[i].Y_Position,
- Y_Velocity = 100,
- Z_Position_Start = points[i].Z_Position_Start,
- Z_Velocity_Start = 100,
- U_Position = points[i].U_Position,
- U_Velocity = 100,
- Z_Position_Stop = points[i].Z_Position_Stop,
- Z_Velocity_Stop = 100,
- });
- }
- else
- {
- //否则更新当前的取料点
- SelectProduct.PickPoints[index].Number = index + 1;
- SelectProduct.PickPoints[index].X_Position = points[i].X_Position;
- SelectProduct.PickPoints[index].Y_Position = points[i].Y_Position;
- SelectProduct.PickPoints[index].Z_Position_Start = points[i].Z_Position_Start;
- SelectProduct.PickPoints[index].U_Position = points[i].U_Position;
- SelectProduct.PickPoints[index].Z_Position_Stop = points[i].Z_Position_Stop;
- }
- }
- }
- _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.错误, 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(),
- QrCodePoints = SelectProduct.QrCodePoints?.Select(p => p.Clone()).ToList() ?? new List<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>(),
- IndependentCamerPoints = SelectProduct.IndependentCamerPoints?.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>(),
- Reference_coordinate = SelectProduct.Reference_coordinate?.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 = "导出完成", 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.错误, MessageBoxButton.OK, MessageBoxImage.Error);
- LogHelper.WriteLogError("导入点位文件解析失败", ex);
- return;
- }
- }
- if (import == null)
- {
- MessageBox.Show("导入的点位文件为空", 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.QrCodePoints = new ObservableCollection<PlcPoint>(import.QrCodePoints ?? 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.IndependentCamerPoints = new ObservableCollection<PlcPoint>(import.IndependentCamerPoints ?? new List<PlcPoint>());
- SelectProduct.RemovePoints = new ObservableCollection<PlcPoint>(import.RemovePoints ?? new List<PlcPoint>());
- SelectProduct.PressurePlacePoints = new ObservableCollection<PlcPoint>(import.PressurePlacePoints ?? new List<PlcPoint>());
- SelectProduct.Reference_coordinate = new ObservableCollection<PlcPoint>(import.Reference_coordinate ?? 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.Reference_coordinate);
- renumber(SelectProduct.PickPoints);
- renumber(SelectProduct.SecPosCameraPoints);
- renumber(SelectProduct.ScrewCameraPoints);
- renumber(SelectProduct.ScrewPoints);
- renumber(SelectProduct.CheckCameraPoints);
- renumber(SelectProduct.IndependentCamerPoints);
- renumber(SelectProduct.RemovePoints);
- renumber(SelectProduct.PressurePlacePoints);
- renumber(SelectProduct.QrCodePoints);
- // 根据当前 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());
- Pallet1 = new PalletEx() { Row = 4, Column = 2, P1 = new PlcPoint(), P0 = new PlcPoint(), P2 = new PlcPoint() };
- Pallet2 = new PalletEx() { Row = 4, Column = 2, P1 = new PlcPoint(), P0 = new PlcPoint(), P2 = new PlcPoint() };
- 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.IndependentCamerPoints == null)
- {
- SelectProduct.IndependentCamerPoints = new ObservableCollection<PlcPoint>();
- }
- if (SelectProduct.RemovePoints == null)
- {
- SelectProduct.RemovePoints = new ObservableCollection<PlcPoint>();
- }
- if (SelectProduct.PressurePlacePoints == null)
- {
- SelectProduct.PressurePlacePoints = new ObservableCollection<PlcPoint>();
- }
- if (SelectProduct.Reference_coordinate == null)
- {
- SelectProduct.Reference_coordinate = 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;
- }
- }
- }
- }
|