CalibrationCameraParamsViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.ToolBlock;
  3. using Prism.Commands;
  4. using Prism.Events;
  5. using Prism.Ioc;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using Team.FFFeederService.Interfaces;
  17. using TeamAAS_VP;
  18. using TeamAAS_VP.Core;
  19. using TeamAAS_VP.Interfaces;
  20. using TeamAAS_VP.Models;
  21. using TeamAAS_VP.Models.Calibration;
  22. using TeamAAS_VP.Resources.Languages;
  23. namespace TeamAAS_VP.ViewModels.Calibration
  24. {
  25. public class CalibrationCameraParamsViewModel : BindableBase, IConfirmNavigationRequest
  26. {
  27. IRegionManager _regionManager;
  28. IRegionNavigationService _regionNavigationService;
  29. IContainerProvider _container;
  30. IDialogService _dialogService;
  31. IEventAggregator _eventAggregator;
  32. IConfigService _configService;
  33. IRobotService _robotService;
  34. ICameraService _cameraService;
  35. IFeederService _feederService;
  36. ISystemDatabaseService _systemDatabaseService;
  37. #region 属性
  38. private CalibrationInfo _SelectCalibration;
  39. public CalibrationInfo SelectCalibration
  40. {
  41. get { return _SelectCalibration; }
  42. set { SetProperty(ref _SelectCalibration, value); }
  43. }
  44. private IRobot _Robot;
  45. public IRobot Robot
  46. {
  47. get { return _Robot; }
  48. set { SetProperty(ref _Robot, value); }
  49. }
  50. private IVoiceCoilMotorFeeder _Feeder;
  51. public IVoiceCoilMotorFeeder Feeder
  52. {
  53. get { return _Feeder; }
  54. set { SetProperty(ref _Feeder, value); }
  55. }
  56. private ICamera _Camera;
  57. public ICamera Camera
  58. {
  59. get { return _Camera; }
  60. set { SetProperty(ref _Camera, value); }
  61. }
  62. private bool _IsShowLight;
  63. public bool IsShowLight
  64. {
  65. get { return _IsShowLight; }
  66. set { SetProperty(ref _IsShowLight, value); }
  67. }
  68. private float _ExposureTime;
  69. /// <summary>
  70. /// 相机曝光时间
  71. /// </summary>
  72. public float ExposureTime
  73. {
  74. get { return _ExposureTime; }
  75. set
  76. {
  77. SetProperty(ref _ExposureTime, value);
  78. SelectCalibration.ExposureTime = value;
  79. if (Camera != null)
  80. {
  81. try
  82. {
  83. Camera.SetExposureTime(value);
  84. }
  85. catch (Exception)
  86. {
  87. }
  88. }
  89. }
  90. }
  91. private float _Gain;
  92. /// <summary>
  93. /// 相机增益
  94. /// </summary>
  95. public float Gain
  96. {
  97. get { return _Gain; }
  98. set
  99. {
  100. SetProperty(ref _Gain, value);
  101. SelectCalibration.Gain = value;
  102. if (Camera != null)
  103. {
  104. try
  105. {
  106. Camera.SetGain(value);
  107. }
  108. catch (Exception)
  109. {
  110. }
  111. }
  112. }
  113. }
  114. private ICogImage _Image;
  115. public ICogImage Image
  116. {
  117. get { return _Image; }
  118. set { SetProperty(ref _Image,value); }
  119. }
  120. private string _Message;
  121. public string Message
  122. {
  123. get { return _Message; }
  124. set { SetProperty(ref _Message, value); }
  125. }
  126. #endregion
  127. #region 命令
  128. private DelegateCommand _LoadedCommand;
  129. public DelegateCommand LoadedCommand =>
  130. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  131. private DelegateCommand _NextCommand;
  132. public DelegateCommand NextCommand =>
  133. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  134. private DelegateCommand _BackCommand;
  135. public DelegateCommand BackCommand =>
  136. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  137. private DelegateCommand<double?> _LightValueChangedCommand;
  138. public DelegateCommand<double?> LightValueChangedCommand =>
  139. _LightValueChangedCommand ?? (_LightValueChangedCommand = new DelegateCommand<double?>(ExecuteLightValueChangedCommand));
  140. private DelegateCommand _StartGrabbingCommand;
  141. public DelegateCommand StartGrabbingCommand =>
  142. _StartGrabbingCommand ?? (_StartGrabbingCommand = new DelegateCommand(ExecuteStartGrabbingCommand));
  143. private DelegateCommand _StopGrabbingCommand;
  144. public DelegateCommand StopGrabbingCommand =>
  145. _StopGrabbingCommand ?? (_StopGrabbingCommand = new DelegateCommand(ExecuteStopGrabbingCommand));
  146. #endregion
  147. #region 事件
  148. #endregion
  149. public CalibrationCameraParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
  150. IConfigService configService,IRobotService robotService,ICameraService cameraService,IFeederService feederService, ISystemDatabaseService systemDatabaseService)
  151. {
  152. _regionManager = regionManager;
  153. _regionNavigationService = regionNavigationService;
  154. _container = container;
  155. _dialogService = dialogService;
  156. _eventAggregator = ea;
  157. _configService = configService;
  158. _robotService = robotService;
  159. _cameraService = cameraService;
  160. _feederService = feederService;
  161. _systemDatabaseService = systemDatabaseService;
  162. }
  163. #region 方法
  164. async void ExecuteLoadedCommand()
  165. {
  166. try
  167. {
  168. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  169. {
  170. IsAllowEdit = false;
  171. }
  172. else
  173. {
  174. IsAllowEdit = true;
  175. }
  176. }
  177. catch (Exception ex)
  178. {
  179. LogHelper.WriteLogError("相机校准加载相机参数界面时出错!", ex);
  180. MessageBox.Show(ex.Message);
  181. }
  182. }
  183. /// <summary>
  184. /// 下一步
  185. /// </summary>
  186. void ExecuteNextCommand()
  187. {
  188. SelectCalibration.ExposureTime = ExposureTime;
  189. SelectCalibration.Gain = Gain;
  190. if (Camera != null)
  191. {
  192. Camera.StopGrabbing();
  193. Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
  194. }
  195. NavigationParameters param = new NavigationParameters();
  196. param.Add("SelectCalibration", SelectCalibration);
  197. if (SelectCalibration.IsDistortionCorrection)
  198. {
  199. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationDistortion", param);
  200. }
  201. else
  202. {
  203. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationEditVision", param);
  204. }
  205. }
  206. /// <summary>
  207. /// 上一步
  208. /// </summary>
  209. void ExecuteBackCommand()
  210. {
  211. if (Camera != null)
  212. {
  213. Camera.StopGrabbing();
  214. Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
  215. }
  216. NavigationParameters param = new NavigationParameters();
  217. param.Add("SelectCalibration", SelectCalibration);
  218. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationBasics", param);
  219. }
  220. /// <summary>
  221. /// 光源亮度调整时
  222. /// </summary>
  223. async void ExecuteLightValueChangedCommand(double? lightvalue)
  224. {
  225. try
  226. {
  227. if (lightvalue == null) return;
  228. if (Feeder == null || !Feeder.IsConnected) return;
  229. await Feeder.SetLightValueAsync((ushort)lightvalue);
  230. }
  231. catch (Exception ex)
  232. {
  233. LogHelper.WriteLogError("相机校准光源亮度调整时出错!", ex);
  234. MessageBox.Show(ex.Message);
  235. }
  236. }
  237. /// <summary>
  238. /// 停止采集
  239. /// </summary>
  240. void ExecuteStopGrabbingCommand()
  241. {
  242. if (Camera != null)
  243. Camera.StopGrabbing();
  244. }
  245. /// <summary>
  246. /// 开始采集
  247. /// </summary>
  248. void ExecuteStartGrabbingCommand()
  249. {
  250. if (Camera != null)
  251. Camera.StartGrabbing();
  252. }
  253. private void Camera_ImageCallbackEvent(ICogImage image,TimeSpan totaltime,string errormessage)
  254. {
  255. Image = image;
  256. App.Current.Dispatcher.Invoke(new
  257. Action(() =>
  258. {
  259. if (errormessage != null && !string.IsNullOrEmpty(errormessage))
  260. {
  261. Message = $"{Lang.耗时}:{totaltime.TotalMilliseconds.ToString("F1")} ms Error:{errormessage}";
  262. }
  263. else
  264. {
  265. Message = $"{Lang.耗时}:{totaltime.TotalMilliseconds.ToString("F1")} ms";
  266. }
  267. }));
  268. }
  269. #endregion
  270. #region 继承
  271. /// <summary>
  272. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  273. /// </summary>
  274. /// <param name="navigationContext"></param>
  275. /// <param name="continuationCallback"></param>
  276. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  277. {
  278. continuationCallback(true);
  279. }
  280. /// <summary>
  281. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  282. /// </summary>
  283. /// <param name="navigationContext"></param>
  284. /// <exception cref="NotImplementedException"></exception>
  285. public async void OnNavigatedTo(NavigationContext navigationContext)
  286. {
  287. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  288. Camera = _cameraService.GetCamera(SelectCalibration.CameraID);
  289. if (SelectCalibration.ExposureTime == 0)
  290. {
  291. SelectCalibration.ExposureTime = 5000;
  292. }
  293. ExposureTime = SelectCalibration.ExposureTime;
  294. Gain = SelectCalibration.Gain;
  295. if (Camera != null)
  296. {
  297. Camera.SetExposureTime(ExposureTime);
  298. Camera.SetGain(Gain);
  299. Camera.ImageCallbackEvent += Camera_ImageCallbackEvent;
  300. //Camera.Grab();
  301. }
  302. if (SelectCalibration.IsCreate && SelectCalibration.ToolBlock == null)
  303. {
  304. SelectCalibration.ToolBlock = VisionProFileHelper.LoadObjectFromFileSafe<CogToolBlock>(FilePath.CalibrationToolblockPath);
  305. }
  306. else if (SelectCalibration.ToolBlock == null)
  307. {
  308. string prcPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + SelectCalibration.Name + ".vpp";
  309. if (File.Exists(prcPath))
  310. {
  311. SelectCalibration.ToolBlock = VisionProFileHelper.LoadObjectFromFileSafe<CogToolBlock>(prcPath);
  312. }
  313. else
  314. {
  315. SelectCalibration.ToolBlock = VisionProFileHelper.LoadObjectFromFileSafe<CogToolBlock>(FilePath.CalibrationToolblockPath);
  316. }
  317. }
  318. if (SelectCalibration.RobotId != Guid.Empty)
  319. {
  320. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  321. Robot.EnterDebugMode = true;
  322. }
  323. if (SelectCalibration.FeederId != Guid.Empty)
  324. {
  325. Feeder = _feederService.GetFeeder(SelectCalibration.FeederId);
  326. if (Feeder.IsConnected)
  327. {
  328. await Feeder.OpenLightAsync();
  329. await Feeder.SetLightValueAsync((ushort)SelectCalibration.Brightness);
  330. }
  331. IsShowLight = true;
  332. }
  333. else
  334. {
  335. IsShowLight = false;
  336. }
  337. }
  338. /// <summary>
  339. /// 是否允许导航到此视图模型。
  340. /// </summary>
  341. /// <param name="navigationContext"></param>
  342. /// <returns></returns>
  343. public bool IsNavigationTarget(NavigationContext navigationContext)
  344. {
  345. return true;
  346. }
  347. /// <summary>
  348. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  349. /// </summary>
  350. /// <param name="navigationContext"></param>
  351. public void OnNavigatedFrom(NavigationContext navigationContext)
  352. {
  353. ExecuteStopGrabbingCommand();
  354. }
  355. #endregion
  356. private bool _IsAllowEdit = false;
  357. public bool IsAllowEdit
  358. {
  359. get { return _IsAllowEdit; }
  360. set { SetProperty(ref _IsAllowEdit, value); }
  361. }
  362. private void LoginChange(Models.User user)
  363. {
  364. if (user.userPart == Enums.UserPart.Operator)
  365. {
  366. IsAllowEdit = false;
  367. }
  368. else
  369. {
  370. IsAllowEdit = true;
  371. }
  372. }
  373. }
  374. }