| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- using Cognex.VisionPro;
- 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.Collections.ObjectModel;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Shapes;
- using Team.FFFeederService.Interfaces;
- using TeamAAS_VP.Core;
- using TeamAAS_VP.Enums;
- using TeamAAS_VP.Events;
- using TeamAAS_VP.Interfaces;
- using TeamAAS_VP.Models;
- using TeamAAS_VP.Models.Calibration;
- using TeamAAS_VP.Resources.Languages;
- using TeamAAS_VP.Services;
- namespace TeamAAS_VP.ViewModels.Calibration
- {
- public class FixedUpCameraFindP0ViewModel : BindableBase, IConfirmNavigationRequest
- {
- IRegionManager _regionManager;
- IRegionNavigationService _regionNavigationService;
- IContainerProvider _container;
- IDialogService _dialogService;
- IEventAggregator _eventAggregator;
- IConfigService _configService;
- IRobotService _robotService;
- ICameraService _cameraService;
- IFeederService _feederService;
- ISystemDatabaseService _systemDatabaseService;
- ICalibrationService _calibrationServvice;
- #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); }
- }
- private float _ExposureTime;
- /// <summary>
- /// 相机曝光时间
- /// </summary>
- public float ExposureTime
- {
- get { return _ExposureTime; }
- set
- {
- SetProperty(ref _ExposureTime, value);
- SelectCalibration.ExposureTime = value;
- if (Camera != null)
- {
- try
- {
- SelectCalibration.FixedUpCameraFindP0Info.ExposureTime = value;
- Camera.SetExposureTime(value);
- }
- catch (Exception)
- {
- }
- }
- }
- }
- private float _Gain;
- /// <summary>
- /// 相机增益
- /// </summary>
- public float Gain
- {
- get { return _Gain; }
- set
- {
- SetProperty(ref _Gain, value);
- SelectCalibration.Gain = value;
- if (Camera != null)
- {
- try
- {
- SelectCalibration.FixedUpCameraFindP0Info.Gain = value;
- Camera.SetGain(value);
- }
- catch (Exception)
- {
- }
- }
- }
- }
- private ObservableCollection<CameraInfo> _Cameras;
- public ObservableCollection<CameraInfo> Cameras
- {
- get { return _Cameras; }
- set { SetProperty(ref _Cameras, value); }
- }
- private CameraInfo _SelectCamera;
- public CameraInfo SelectCamera
- {
- get { return _SelectCamera; }
- set
- {
- SetProperty(ref _SelectCamera, value);
- if (value != null)
- {
- Camera = _cameraService.GetCamera(value.Id);
- SelectCalibration.FixedUpCameraFindP0Info.CameraId = value.Id;
- SelectCalibration.FixedUpCameraFindP0Info.CameraName = value.CameraName;
- }
- }
- }
- private ObservableCollection<CalibrationInfo> _CalibrationList;
- public ObservableCollection<CalibrationInfo> CalibrationList
- {
- get { return _CalibrationList; }
- set { SetProperty(ref _CalibrationList, value); }
- }
- private CalibrationInfo _SelectedCalibration;
- public CalibrationInfo SelectedCalibration
- {
- get { return _SelectedCalibration; }
- set
- {
- SetProperty(ref _SelectedCalibration, value);
- if (value!=null)
- {
- SelectCalibration.FixedUpCameraFindP0Info.CalibrationId = value.Id;
- SelectCalibration.FixedUpCameraFindP0Info.CalibrationName = value.Name;
- }
- }
- }
- #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));
- private DelegateCommand _FindP0Command;
- public DelegateCommand FindP0Command =>
- _FindP0Command ?? (_FindP0Command = new DelegateCommand(ExecuteFindP0Command));
- #endregion
- #region 事件
- #endregion
- public FixedUpCameraFindP0ViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
- IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, ISystemDatabaseService systemDatabaseService, ICalibrationService calibrationService)
- {
- _regionManager = regionManager;
- _regionNavigationService = regionNavigationService;
- _container = container;
- _dialogService = dialogService;
- _eventAggregator = ea;
- _configService = configService;
- _robotService = robotService;
- _cameraService = cameraService;
- _feederService = feederService;
- _systemDatabaseService = systemDatabaseService;
- _calibrationServvice = calibrationService;
- }
- #region 方法
- void ExecuteLoadedCommand()
- {
- try
- {
- if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
- {
- IsAllowEdit = false;
- }
- else
- {
- IsAllowEdit = true;
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError(Lang.相机校准加载视觉工具处理界面时出错, ex);
- MessageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// 下一步
- /// </summary>
- void ExecuteNextCommand()
- {
- try
- {
- string prcPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + "FixedUpCameraTool.vpp";
- if (!Directory.Exists(System.IO.Path.GetDirectoryName(prcPath)))
- {
- Directory.CreateDirectory(System.IO.Path.GetDirectoryName(prcPath));
- }
- VisionProFileHelper.SaveObjectToFileSafe(ToolBlockEdit.Subject, prcPath);
- NavigationParameters param = new NavigationParameters();
- param.Add("SelectCalibration", SelectCalibration);
- _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationAuto", param);
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError(Lang.相机校准下相机示教P0界面点击下一步时出错, ex);
- MessageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// 上一步
- /// </summary>
- void ExecuteBackCommand()
- {
- try
- {
- NavigationParameters param = new NavigationParameters();
- param.Add("SelectCalibration", SelectCalibration);
- _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationTeachCenterPoint", param);
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError(Lang.相机校准下相机示教P0界面点击上一步时出错, ex);
- MessageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// 运行视觉流程
- /// </summary>
- void ExecuteRunVisionProcessCommand()
- {
- try
- {
- if (Camera == null)
- {
- MessageBox.Show(Lang.请先选择相机);
- return;
- }
- ToolBlockEdit.Subject.Inputs["InputImage"].Value = Camera.Grab();
- ToolBlockEdit.Subject.Run();
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError(Lang.相机校准运行流程时出错时出错, ex);
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
- }
- /// <summary>
- /// 自动计算P0点
- /// </summary>
- async void ExecuteFindP0Command()
- {
- if (Camera == null)
- {
- MessageBox.Show(Lang.请先选择相机);
- return;
- }
- if (SelectedCalibration == null)
- {
- MessageBox.Show(Lang.请先选择标定);
- return;
- }
- var result = await CaptureAndProcess();
- if (result.IsSuccess)
- {
- if (SelectCalibration.MarkPoint == null)
- {
- SelectCalibration.MarkPoint = new RPoint();
- }
- SelectCalibration.MarkPoint.X = (float) result.X;
- SelectCalibration.MarkPoint.Y = (float)result.Y;
- MessageBox.Show(string.Format(Lang.P0点计算成功X0Y1, result.X, result.Y));//$"P0点计算成功!X:{result.X},Y:{result.Y}");
- }
- else
- {
- MessageBox.Show(Lang.未找到P0点请调整工件位置后重试);
- }
- }
- /// <summary>
- /// 相机拍照
- /// </summary>
- /// <returns></returns>
- private async Task<(bool IsSuccess, double X, double Y, double U, int ImageWidth, int ImageHeight)> CaptureAndProcess()
- {
- ICogImage image = null;
- //采集图像
- if (Camera != null)
- {
- image = Camera.Grab();
- ToolBlockEdit.Subject.Inputs["InputImage"].Value = image;
- }
- ToolBlockEdit.Subject.Run();
- CogToolBlockTerminalCollection outputCollection = ToolBlockEdit.Subject.Outputs;
- bool Found = (bool)(outputCollection["Found"].Value);
- if (Found)
- {
- var strpoints = outputCollection["Point"].Value.ToString();
- string[] items = strpoints.Split(',');
- double x = double.Parse(items[0]);
- double y = double.Parse(items[1]);
- //校准转换
- var calibResult = _calibrationServvice.ConvertPixelToPosition((x, y, 0), null, SelectedCalibration, Robot.Brand);
- if (calibResult.IsSucceed)
- {
- x = calibResult.X;
- y = calibResult.Y;
- return (true, x, y, 0, image.Width, image.Height);
- }
- return (false, 0, 0, 0, image.Width, image.Height);
- }
- else
- {
- return (false, 0, 0, 0, image.Width, image.Height);
- }
- }
- #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)
- {
- SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
- if (SelectCalibration.FixedUpCameraFindP0Info == null)
- {
- SelectCalibration.FixedUpCameraFindP0Info = new FixedUpCameraFindP0Info();
- }
- if (ToolBlockEdit.Subject==null)
- {
- string prcPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + "FixedUpCameraTool.vpp";
- if (File.Exists(prcPath))
- {
- ToolBlockEdit.Subject = VisionProFileHelper.LoadObjectFromFileSafe<CogToolBlock>(prcPath);
- }
- else
- {
- ToolBlockEdit.Subject = VisionProFileHelper.LoadObjectFromFileSafe<CogToolBlock>(FilePath.CalibrationToolblockPath);
- }
- }
- if (SelectCalibration.RobotId != Guid.Empty)
- {
- Robot = _robotService.GetRobot(SelectCalibration.RobotId);
- }
- Cameras = new ObservableCollection<CameraInfo>(_configService.GetAllCameras());
- if (SelectCalibration.FixedUpCameraFindP0Info.CameraId != Guid.Empty)
- {
- SelectCamera = Cameras.FirstOrDefault(c => c.Id == SelectCalibration.FixedUpCameraFindP0Info.CameraId);
- }
- CalibrationList = new ObservableCollection<CalibrationInfo>(_calibrationServvice.GetAllCalibrations());
- if (SelectCalibration.FixedUpCameraFindP0Info.CalibrationId != Guid.Empty)
- {
- SelectedCalibration = CalibrationList.FirstOrDefault(c => c.Id == SelectCalibration.FixedUpCameraFindP0Info.CalibrationId);
- }
- ExposureTime = SelectCalibration.ExposureTime;
- Gain = SelectCalibration.Gain;
- }
- /// <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;
- }
- }
- }
- }
|