using Cognex.VisionPro.ToolBlock; 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.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Documents; using Team.FFFeederService.Interfaces; using TeamAAS_VP.Core; using TeamAAS_VP.Events; using TeamAAS_VP.Interfaces; using TeamAAS_VP.Models; using TeamAAS_VP; using static NPOI.HSSF.Util.HSSFColor; namespace TeamAAS_VP.ViewModels.Calibration { public class CalibrationEditVisionViewModel : BindableBase { IRegionManager _regionManager; IRegionNavigationService _regionNavigationService; IContainerProvider _container; IDialogService _dialogService; IEventAggregator _eventAggregator; #region 属性 private CogToolBlockEditV2 _ToolBlockEdit; public CogToolBlockEditV2 ToolBlockEdit { get { return _ToolBlockEdit; } set { SetProperty(ref _ToolBlockEdit,value); } } private CalibrationInfo _SelectCalibration; public CalibrationInfo SelectCalibration { get { return _SelectCalibration; } set { SetProperty(ref _SelectCalibration, value); } } private IRobot _Robot; public IRobot Robot { get { return _Robot; } set { SetProperty(ref _Robot,value); } } private ICamera _Camera; public ICamera Camera { get { return _Camera; } set { SetProperty(ref _Camera, value); } } #endregion #region 命令 private DelegateCommand _LoadedCommand; public DelegateCommand LoadedCommand => _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand)); private DelegateCommand _NextCommand; public DelegateCommand NextCommand => _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand)); private DelegateCommand _BackCommand; public DelegateCommand BackCommand => _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand)); private DelegateCommand _RunVisionProcessCommand; public DelegateCommand RunVisionProcessCommand => _RunVisionProcessCommand ?? (_RunVisionProcessCommand = new DelegateCommand(ExecuteRunVisionProcessCommand)); #endregion #region 事件 #endregion public CalibrationEditVisionViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService) { _regionManager = regionManager; _regionNavigationService = regionNavigationService; _container = container; _dialogService = dialogService; _eventAggregator = ea; SelectCalibration = _container.Resolve().SelectCalibration; } #region 方法 void ExecuteLoadedCommand() { try { if (_container.Resolve().CurrentUser.userPart == Enums.UserPart.Operator) { IsAllowEdit = false; } else { IsAllowEdit = true; } SelectCalibration = _container.Resolve().SelectCalibration; if (ToolBlockEdit != null) { ToolBlockEdit.Subject = SelectCalibration.ToolBlock; } if (SelectCalibration.RobotId != Guid.Empty) { Robot = _container.Resolve().RobotService.GetRobot(SelectCalibration.RobotId); } Camera = _container.Resolve().CameraService.GetCamera(SelectCalibration.CameraID); } catch (Exception ex) { LogHelper.WriteLogError("相机校准加载视觉工具处理界面时出错!", ex); MessageBox.Show(ex.Message); } } /// /// 下一步 /// void ExecuteNextCommand() { try { SelectCalibration.ToolBlock = ToolBlockEdit.Subject; _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationTeachCenterPoint"); } catch (Exception ex) { LogHelper.WriteLogError("相机校准-视觉工具界面点击下一步时出错!", ex); MessageBox.Show(ex.Message); } } /// /// 上一步 /// void ExecuteBackCommand() { try { _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationCameraParams"); } catch (Exception ex) { LogHelper.WriteLogError("相机校准-视觉工具界面点击上一步时出错!", ex); MessageBox.Show(ex.Message); } } void ExecuteRunVisionProcessCommand() { try { SelectCalibration.ToolBlock = ToolBlockEdit.Subject; ToolBlockEdit.Subject.Inputs["InputImage"].Value = Camera.Grab(); ToolBlockEdit.Subject.Run(); } catch (Exception ex) { LogHelper.WriteLogError("相机校准-运行流程时出错时出错", ex); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); } } #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; } } } }