| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- 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.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows;
- using Team.FFFeederService.Interfaces;
- using TeamAAS_VP.Core;
- using TeamAAS_VP.Interfaces;
- using TeamAAS_VP.Models;
- using TeamAAS_VP;
- namespace TeamAAS_VP.ViewModels.Calibration
- {
- public class CalibrationCameraParamsViewModel : BindableBase
- {
- IRegionManager _regionManager;
- IRegionNavigationService _regionNavigationService;
- IContainerProvider _container;
- IDialogService _dialogService;
- IEventAggregator _eventAggregator;
- #region 属性
- 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 IVoiceCoilMotorFeeder _Feeder;
- public IVoiceCoilMotorFeeder Feeder
- {
- get { return _Feeder; }
- set { SetProperty(ref _Feeder, value); }
- }
- private ICamera _Camera;
- public ICamera Camera
- {
- get { return _Camera; }
- set { SetProperty(ref _Camera, value); }
- }
- private bool _IsShowLight;
- public bool IsShowLight
- {
- get { return _IsShowLight; }
- set { SetProperty(ref _IsShowLight, value); }
- }
- private float _ExposureTime;
- /// <summary>
- /// 相机曝光时间
- /// </summary>
- public float ExposureTime
- {
- get { return _ExposureTime; }
- set
- {
- SetProperty(ref _ExposureTime, value);
- SelectCalibration.ExposureTime = value;
- if (Camera != null)
- {
- try
- {
- 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
- {
- Camera.SetGain(value);
- }
- catch (Exception)
- {
- }
- }
- }
- }
- private ICogImage _Image;
- public ICogImage Image
- {
- get { return _Image; }
- set { SetProperty(ref _Image,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<double?> _LightValueChangedCommand;
- public DelegateCommand<double?> LightValueChangedCommand =>
- _LightValueChangedCommand ?? (_LightValueChangedCommand = new DelegateCommand<double?>(ExecuteLightValueChangedCommand));
- private DelegateCommand _StartGrabbingCommand;
- public DelegateCommand StartGrabbingCommand =>
- _StartGrabbingCommand ?? (_StartGrabbingCommand = new DelegateCommand(ExecuteStartGrabbingCommand));
- private DelegateCommand _StopGrabbingCommand;
- public DelegateCommand StopGrabbingCommand =>
- _StopGrabbingCommand ?? (_StopGrabbingCommand = new DelegateCommand(ExecuteStopGrabbingCommand));
- #endregion
- #region 事件
- #endregion
- public CalibrationCameraParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService)
- {
- _regionManager = regionManager;
- _regionNavigationService = regionNavigationService;
- _container = container;
- _dialogService = dialogService;
- _eventAggregator = ea;
- SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
- }
- #region 方法
- async void ExecuteLoadedCommand()
- {
- try
- {
- if (_container.Resolve<Management>().CurrentUser.userPart == Enums.UserPart.Operator)
- {
- IsAllowEdit = false;
- }
- else
- {
- IsAllowEdit = true;
- }
- SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
- Camera = _container.Resolve<Management>().CameraService.GetCamera(SelectCalibration.CameraID);
- if (SelectCalibration.ExposureTime == 0)
- {
- SelectCalibration.ExposureTime = 5000;
- }
- ExposureTime = SelectCalibration.ExposureTime;
- Gain = SelectCalibration.Gain;
- if (Camera != null)
- {
- Camera.SetExposureTime(ExposureTime);
- Camera.SetGain(Gain);
- Camera.ImageCallbackEvent += Camera_ImageCallbackEvent;
- //Camera.Grab();
- }
- if (SelectCalibration.IsCreate && SelectCalibration.ToolBlock == null)
- {
- SelectCalibration.ToolBlock = CogSerializer.LoadObjectFromFile(FilePath.CalibrationToolblockPath) as CogToolBlock;
- }
- else if (SelectCalibration.ToolBlock == null)
- {
- string prcPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + SelectCalibration.Name + ".vpp";
- if (File.Exists(prcPath))
- {
- SelectCalibration.ToolBlock = CogSerializer.LoadObjectFromFile(prcPath) as CogToolBlock;
- }
- else
- {
- SelectCalibration.ToolBlock = CogSerializer.LoadObjectFromFile(FilePath.CalibrationToolblockPath) as CogToolBlock;
- }
- }
- if (SelectCalibration.RobotId != Guid.Empty)
- {
- Robot = _container.Resolve<Management>().RobotService.GetRobot(SelectCalibration.RobotId);
- }
- if (SelectCalibration.FeederId != Guid.Empty)
- {
- Feeder = _container.Resolve<Management>().FeederService.GetFeeder(SelectCalibration.FeederId);
- if (Feeder.IsConnected)
- {
- await Feeder.OpenLightAsync();
- await Feeder.SetLightValueAsync((ushort)SelectCalibration.Brightness);
- }
- IsShowLight = true;
- }
- else
- {
- IsShowLight = false;
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("相机校准加载相机参数界面时出错!", ex);
- MessageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// 下一步
- /// </summary>
- void ExecuteNextCommand()
- {
- SelectCalibration.ExposureTime = ExposureTime;
- SelectCalibration.Gain = Gain;
- if (Camera != null)
- {
- Camera.StopGrabbing();
- Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
- }
- _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationEditVision");
- }
- /// <summary>
- /// 上一步
- /// </summary>
- void ExecuteBackCommand()
- {
- if (Camera != null)
- {
- Camera.StopGrabbing();
- Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
- }
- _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationBasics");
- }
- /// <summary>
- /// 光源亮度调整时
- /// </summary>
- async void ExecuteLightValueChangedCommand(double? lightvalue)
- {
- try
- {
- if (lightvalue == null) return;
- if (Feeder == null || !Feeder.IsConnected) return;
- await Feeder.SetLightValueAsync((ushort)lightvalue);
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("相机校准光源亮度调整时出错!", ex);
- MessageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// 停止采集
- /// </summary>
- void ExecuteStopGrabbingCommand()
- {
- if (Camera != null)
- Camera.StopGrabbing();
- }
- /// <summary>
- /// 开始采集
- /// </summary>
- void ExecuteStartGrabbingCommand()
- {
- if (Camera != null)
- Camera.StartGrabbing();
- }
- private void Camera_ImageCallbackEvent(ICogImage image,TimeSpan totaltime,string errormessage)
- {
- Image = image;
- }
- #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;
- }
- }
- }
- }
|