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 TeamAAS_VP.Controls; using TeamAAS_VP.Events; using TeamAAS_VP.Core; using TeamAAS_VP.Models; using TeamAAS_VP; using System.Windows; using Cognex.VisionPro.ToolBlock; using Cognex.VisionPro; using TeamAAS_VP.Resources.Languages; namespace TeamAAS_VP.ViewModels.Calibration { public class CalibrationHomeViewModel : BindableBase { IRegionManager _regionManager; IRegionNavigationService _regionNavigationService; IContainerProvider _container; IDialogService _dialogService; IEventAggregator _eventAggregator; #region 属性 private ObservableCollection _Robots; public ObservableCollection Robots { get { return _Robots; } set { SetProperty(ref _Robots, value); } } private ObservableCollection _Feeders; public ObservableCollection Feeders { get { return _Feeders; } set { SetProperty(ref _Feeders, value); } } private ObservableCollection _Calibrations; public ObservableCollection 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 _StartCalibrationCommand; public DelegateCommand StartCalibrationCommand => _StartCalibrationCommand ?? (_StartCalibrationCommand = new DelegateCommand(ExecuteStartCalibrationCommand)); private DelegateCommand _RemoveCalibrationCommand; public DelegateCommand RemoveCalibrationCommand => _RemoveCalibrationCommand ?? (_RemoveCalibrationCommand = new DelegateCommand(ExecuteRemoveCalibrationCommand)); #endregion #region 事件 #endregion public CalibrationHomeViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService) { _regionManager = regionManager; _regionNavigationService = regionNavigationService; _container = container; _dialogService = dialogService; _eventAggregator = ea; Robots = _container.Resolve().DeviceConfig.Robots; Feeders = _container.Resolve().DeviceConfig.Feeders; Calibrations = _container.Resolve().DeviceConfig.Calibrations; } #region 方法 void ExecuteLoadedCommand() { if (_container.Resolve().CurrentUser.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().Publish(new MessageParameter() { Msg = Lang.输入的名称不符合文件名命名规则, Duration = 1 }); return; } if (Calibrations.FirstOrDefault(p => p.Name == result.ToString()) != null) { _eventAggregator.GetEvent().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(), Speed = 20, Accel = 20, Power = true, Width = 100, Height = 80, WaitBlow = 200, WaitPhoto = 200, WaitSuction = 200, IsCreate = true, ToolBlock= CogSerializer.LoadObjectFromFile(FilePath.CalibrationToolblockPath) as CogToolBlock, }; _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationBasics"); } } catch (Exception ex) { LogHelper.WriteLogError("相机校准-Home界面创建校准时出错!", ex); MessageBox.Show(ex.Message); } } void ExecuteStartCalibrationCommand(CalibrationInfo calibration) { if (calibration == null) { return; } _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationBasics"); } void ExecuteRemoveCalibrationCommand(CalibrationInfo calibration) { if (calibration == null) { return; } try { _container.Resolve().RemoveCalibration(calibration); } catch (Exception ex) { _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); LogHelper.WriteLogError("移除校准时出错!", ex); } } #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; } } } }