| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- using Cognex.VisionPro;
- using Cognex.VisionPro.ToolBlock;
- using MaterialDesignThemes.Wpf;
- using NPOI.SS.Formula.Functions;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Ioc;
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- using SixLabors.Fonts.Unicode;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Windows;
- using Team.FFFeederService.Interfaces;
- using TeamAAS_VP;
- using TeamAAS_VP.Controls;
- using TeamAAS_VP.Core;
- using TeamAAS_VP.Events;
- using TeamAAS_VP.Interfaces;
- using TeamAAS_VP.Models;
- using TeamAAS_VP.Models.Calibration;
- using TeamAAS_VP.Models.Robot;
- using TeamAAS_VP.Resources.Languages;
- using TeamAAS_VP.Services;
- namespace TeamAAS_VP.ViewModels.Calibration
- {
- public class CalibrationHomeViewModel : BindableBase, IConfirmNavigationRequest
- {
- IRegionManager _regionManager;
- IRegionNavigationService _regionNavigationService;
- IContainerProvider _container;
- IDialogService _dialogService;
- IEventAggregator _eventAggregator;
- IConfigService _configService;
- IFeederService _feederService;
- IRobotService _robotService;
- ICameraService _cameraService;
- ICalibrationService _calibrationService;
- ISystemDatabaseService _systemDatabaseService;
- #region 属性
- private ObservableCollection<RobotInfo> _Robots;
- public ObservableCollection<RobotInfo> Robots
- {
- get { return _Robots; }
- set { SetProperty(ref _Robots, value); }
- }
- private ObservableCollection<FeederInfo> _Feeders;
- public ObservableCollection<FeederInfo> Feeders
- {
- get { return _Feeders; }
- set { SetProperty(ref _Feeders, value); }
- }
- private ObservableCollection<CalibrationInfo> _Calibrations;
- public ObservableCollection<CalibrationInfo> Calibrations
- {
- get { return _Calibrations; }
- set { SetProperty(ref _Calibrations, value); }
- }
- private CalibrationInfo _SelectCalibration;
- public CalibrationInfo SelectCalibration
- {
- get { return _SelectCalibration; }
- set
- {
- if (_SelectCalibration != null && _SelectCalibration.ToolBlock != null)
- {
- _SelectCalibration.ToolBlock.Dispose();
- _SelectCalibration.ToolBlock = null;
- }
- SetProperty(ref _SelectCalibration, value);
- }
- }
- #endregion
- #region 命令
- private DelegateCommand _LoadedCommand;
- public DelegateCommand LoadedCommand =>
- _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
- private DelegateCommand _CreateCalibrationCommand;
- public DelegateCommand CreateCalibrationCommand =>
- _CreateCalibrationCommand ?? (_CreateCalibrationCommand = new DelegateCommand(ExecuteCreateCalibrationCommand));
- private DelegateCommand<CalibrationInfo> _StartCalibrationCommand;
- public DelegateCommand<CalibrationInfo> StartCalibrationCommand =>
- _StartCalibrationCommand ?? (_StartCalibrationCommand = new DelegateCommand<CalibrationInfo>(ExecuteStartCalibrationCommand));
- private DelegateCommand<CalibrationInfo> _RemoveCalibrationCommand;
- public DelegateCommand<CalibrationInfo> RemoveCalibrationCommand =>
- _RemoveCalibrationCommand ?? (_RemoveCalibrationCommand = new DelegateCommand<CalibrationInfo>(ExecuteRemoveCalibrationCommand));
- #endregion
- #region 事件
- #endregion
- public CalibrationHomeViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
- IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, ICalibrationService calibrationService, ISystemDatabaseService systemDatabaseService)
- {
- _regionManager = regionManager;
- _regionNavigationService = regionNavigationService;
- _container = container;
- _dialogService = dialogService;
- _eventAggregator = ea;
- _configService = configService;
- _robotService = robotService;
- _cameraService = cameraService;
- _feederService = feederService;
- _calibrationService = calibrationService;
- _systemDatabaseService = systemDatabaseService;
- }
- #region 方法
- void ExecuteLoadedCommand()
- {
- if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
- {
- IsAllowEdit = false;
- }
- else
- {
- IsAllowEdit = true;
- }
- }
- async void ExecuteCreateCalibrationCommand()
- {
- try
- {
- var view = new AddProduct() { Title = $"{Lang.创建校准}:", Item = "Name" };
- //show the dialog
- var result = await DialogHost.Show(view, "RootDialog", null, null, null);
- if (result != null && !string.IsNullOrEmpty(result.ToString()))
- {
- if (!FileHelper.CheckFileName(result.ToString()))
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.输入的名称不符合文件名命名规则, Duration = 1 });
- return;
- }
- if (Calibrations.FirstOrDefault(p => p.Name == result.ToString()) != null)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.已存在同名校准, Duration = 1 });
- return;
- }
- if (SelectCalibration != null && SelectCalibration.ToolBlock != null)
- {
- SelectCalibration.ToolBlock.Dispose();
- SelectCalibration.ToolBlock = null;
- SelectCalibration = null;
- }
- SelectCalibration = new CalibrationInfo()
- {
- Id = Guid.NewGuid(),
- Name = result.ToString(),
- DateTime = DateTime.Now,
- CenterPoint = new RPoint(),
- HomePoint = new RPoint(),
- Pick = new PickInfo(),
- Tool = new RobotTool(),
- Arm = new RobotArm(),
- CalibPoints = new ObservableCollection<RobotPixelPoint>(),
- Speed = 20,
- Accel = 20,
- Power = true,
- Width = 100,
- Height = 80,
- WaitBlow = 200,
- WaitPhoto = 200,
- WaitSuction = 200,
- IsCreate = true,
- ToolBlock = VisionProFileHelper.LoadObjectFromFileSafe<CogToolBlock>(FilePath.CalibrationToolblockPath),
- };
- NavigationParameters param= new NavigationParameters();
- param.Add("SelectCalibration", SelectCalibration);
- _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationBasics", param);
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("相机校准-Home界面创建校准时出错!", ex);
- MessageBox.Show(ex.Message);
- }
- }
- void ExecuteStartCalibrationCommand(CalibrationInfo calibration)
- {
- if (calibration == null)
- {
- return;
- }
- SelectCalibration.ToolBlock = null;
- NavigationParameters param = new NavigationParameters();
- param.Add("SelectCalibration", SelectCalibration);
- _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationBasics", param);
- }
- void ExecuteRemoveCalibrationCommand(CalibrationInfo calibration)
- {
- if (calibration == null)
- {
- return;
- }
- try
- {
- var calib = Calibrations.FirstOrDefault(x => x.Id == calibration.Id);
- Calibrations.Remove(calibration);
- _calibrationService.RemoveCalibration(calib.Id);
- }
- catch (Exception ex)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- LogHelper.WriteLogError("移除校准时出错!", ex);
- }
- }
- #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)
- {
- Robots = new ObservableCollection<RobotInfo>();
- var robots = _configService.GetAllRobots();
- foreach (var item in robots)
- {
- Robots.Add(item);
- }
- Feeders = new ObservableCollection<FeederInfo>();
- var feeders = _configService.GetAllFeeders();
- foreach (var item in feeders)
- {
- Feeders.Add(item);
- }
- Calibrations = new ObservableCollection<CalibrationInfo>();
- var calibs = _calibrationService.GetAllCalibrations();
- foreach (var item in calibs)
- {
- Calibrations.Add(item);
- }
- try
- {
- //SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
- if (SelectCalibration != null)
- {
- if (SelectCalibration.RobotId != Guid.Empty)
- {
- IRobot Robot = _robotService.GetRobot(SelectCalibration.RobotId);
- Robot.EnterDebugMode = false;
- }
- }
- }
- catch (Exception)
- {
- }
- }
- /// <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)
- {
- }
- #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;
- }
- }
- }
- }
|