using Cognex.VisionPro;
using Cognex.VisionPro.CalibFix;
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.Windows;
using Team.FFFeederService.Interfaces;
using TeamAAS_VP.Core;
using TeamAAS_VP.Events;
using TeamAAS_VP.Interfaces;
using TeamAAS_VP.Models.Calibration;
namespace TeamAAS_VP.ViewModels.Calibration
{
public class CalibrationDistortionViewModel : BindableBase, IConfirmNavigationRequest
{
IRegionManager _regionManager;
IRegionNavigationService _regionNavigationService;
IContainerProvider _container;
IDialogService _dialogService;
IEventAggregator _eventAggregator;
IConfigService _configService;
IRobotService _robotService;
ICameraService _cameraService;
IFeederService _feederService;
ISystemDatabaseService _systemDatabaseService;
#region 属性
private CogCalibCheckerboardEditV2 _CalibCheckerboardEdit;
public CogCalibCheckerboardEditV2 CalibCheckerboardEdit
{
get { return _CalibCheckerboardEdit; }
set { SetProperty(ref _CalibCheckerboardEdit, value); }
}
private CalibrationInfo _SelectCalibration;
public CalibrationInfo SelectCalibration
{
get { return _SelectCalibration; }
set { SetProperty(ref _SelectCalibration, 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 _RunCommand;
public DelegateCommand RunCommand =>
_RunCommand ?? (_RunCommand = new DelegateCommand(ExecuteRunCommand));
#endregion
#region 事件
#endregion
public CalibrationDistortionViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, ISystemDatabaseService systemDatabaseService)
{
_regionManager = regionManager;
_regionNavigationService = regionNavigationService;
_container = container;
_dialogService = dialogService;
_eventAggregator = ea;
_configService = configService;
_robotService = robotService;
_cameraService = cameraService;
_feederService = feederService;
_systemDatabaseService = systemDatabaseService;
}
#region 方法
void ExecuteLoadedCommand()
{
try
{
if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
{
IsAllowEdit = false;
}
else
{
IsAllowEdit = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
///
/// 下一步
///
void ExecuteNextCommand()
{
try
{
string calibToolPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + "CalibCheckerboard.vpp";
if (!Directory.Exists(Path.GetDirectoryName(calibToolPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(calibToolPath));
}
SelectCalibration.CalibCheckerboardTool = CalibCheckerboardEdit.Subject;
//保存校准工具
CogSerializer.SaveObjectToFile(CalibCheckerboardEdit.Subject, calibToolPath);
NavigationParameters param = new NavigationParameters();
param.Add("SelectCalibration", SelectCalibration);
_regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationEditVision", param);
}
catch (Exception ex)
{
LogHelper.WriteLogError("相机校准-畸变校准工具界面点击下一步时出错!", ex);
MessageBox.Show(ex.Message);
}
}
///
/// 上一步
///
void ExecuteBackCommand()
{
try
{
NavigationParameters param = new NavigationParameters();
param.Add("SelectCalibration", SelectCalibration);
_regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationCameraParams", param);
}
catch (Exception ex)
{
LogHelper.WriteLogError("相机校准-畸变校准工具界面点击上一步时出错!", ex);
MessageBox.Show(ex.Message);
}
}
///
/// 运行
///
void ExecuteRunCommand()
{
try
{
SelectCalibration.CalibCheckerboardTool = CalibCheckerboardEdit.Subject;
CalibCheckerboardEdit.Subject.InputImage = Camera.Grab();
CalibCheckerboardEdit.Subject.Run();
}
catch (Exception ex)
{
LogHelper.WriteLogError("相机校准-畸变校准时出错", ex);
_eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
}
}
#endregion
#region 继承
///
/// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
///
///
///
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action continuationCallback)
{
continuationCallback(true);
}
///
/// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
///
///
///
public async void OnNavigatedTo(NavigationContext navigationContext)
{
SelectCalibration = navigationContext.Parameters.GetValue("SelectCalibration");
if (CalibCheckerboardEdit != null)
{
//先判断校准文件夹下是否存在校准工具VPP文件,存在则加载,不存在则新建一个工具
string calibToolPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + "CalibCheckerboard.vpp";
if (System.IO.File.Exists(calibToolPath))
{
SelectCalibration.CalibCheckerboardTool = CogSerializer.LoadObjectFromFile(calibToolPath) as CogCalibCheckerboardTool;
CalibCheckerboardEdit.Subject = SelectCalibration.CalibCheckerboardTool;
}
else
{
SelectCalibration.CalibCheckerboardTool = new CogCalibCheckerboardTool();
CalibCheckerboardEdit.Subject = SelectCalibration.CalibCheckerboardTool;
}
}
Camera = _cameraService.GetCamera(SelectCalibration.CameraID);
}
///
/// 是否允许导航到此视图模型。
///
///
///
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
///
/// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
///
///
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;
}
}
}
}