CalibrationCameraParamsViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationEditVision", param);
  198. }
  199. /// <summary>
  200. /// 上一步
  201. /// </summary>
  202. void ExecuteBackCommand()
  203. {
  204. if (Camera != null)
  205. {
  206. Camera.StopGrabbing();
  207. Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
  208. }
  209. NavigationParameters param = new NavigationParameters();
  210. param.Add("SelectCalibration", SelectCalibration);
  211. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationBasics", param);
  212. }
  213. /// <summary>
  214. /// 光源亮度调整时
  215. /// </summary>
  216. async void ExecuteLightValueChangedCommand(double? lightvalue)
  217. {
  218. try
  219. {
  220. if (lightvalue == null) return;
  221. if (Feeder == null || !Feeder.IsConnected) return;
  222. await Feeder.SetLightValueAsync((ushort)lightvalue);
  223. }
  224. catch (Exception ex)
  225. {
  226. LogHelper.WriteLogError("相机校准光源亮度调整时出错!", ex);
  227. MessageBox.Show(ex.Message);
  228. }
  229. }
  230. /// <summary>
  231. /// 停止采集
  232. /// </summary>
  233. void ExecuteStopGrabbingCommand()
  234. {
  235. if (Camera != null)
  236. Camera.StopGrabbing();
  237. }
  238. /// <summary>
  239. /// 开始采集
  240. /// </summary>
  241. void ExecuteStartGrabbingCommand()
  242. {
  243. if (Camera != null)
  244. Camera.StartGrabbing();
  245. }
  246. private void Camera_ImageCallbackEvent(ICogImage image,TimeSpan totaltime,string errormessage)
  247. {
  248. Image = image;
  249. App.Current.Dispatcher.Invoke(new
  250. Action(() =>
  251. {
  252. if (errormessage != null && !string.IsNullOrEmpty(errormessage))
  253. {
  254. Message = $"{Lang.耗时}:{totaltime.TotalMilliseconds.ToString("F1")} ms Error:{errormessage}";
  255. }
  256. else
  257. {
  258. Message = $"{Lang.耗时}:{totaltime.TotalMilliseconds.ToString("F1")} ms";
  259. }
  260. }));
  261. }
  262. #endregion
  263. #region 继承
  264. /// <summary>
  265. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  266. /// </summary>
  267. /// <param name="navigationContext"></param>
  268. /// <param name="continuationCallback"></param>
  269. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  270. {
  271. continuationCallback(true);
  272. }
  273. /// <summary>
  274. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  275. /// </summary>
  276. /// <param name="navigationContext"></param>
  277. /// <exception cref="NotImplementedException"></exception>
  278. public async void OnNavigatedTo(NavigationContext navigationContext)
  279. {
  280. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  281. Camera = _cameraService.GetCamera(SelectCalibration.CameraID);
  282. if (SelectCalibration.ExposureTime == 0)
  283. {
  284. SelectCalibration.ExposureTime = 5000;
  285. }
  286. ExposureTime = SelectCalibration.ExposureTime;
  287. Gain = SelectCalibration.Gain;
  288. if (Camera != null)
  289. {
  290. Camera.SetExposureTime(ExposureTime);
  291. Camera.SetGain(Gain);
  292. Camera.ImageCallbackEvent += Camera_ImageCallbackEvent;
  293. //Camera.Grab();
  294. }
  295. if (SelectCalibration.IsCreate && SelectCalibration.ToolBlock == null)
  296. {
  297. SelectCalibration.ToolBlock = CogSerializer.LoadObjectFromFile(FilePath.CalibrationToolblockPath) as CogToolBlock;
  298. }
  299. else if (SelectCalibration.ToolBlock == null)
  300. {
  301. string prcPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + SelectCalibration.Name + ".vpp";
  302. if (File.Exists(prcPath))
  303. {
  304. SelectCalibration.ToolBlock = CogSerializer.LoadObjectFromFile(prcPath) as CogToolBlock;
  305. }
  306. else
  307. {
  308. SelectCalibration.ToolBlock = CogSerializer.LoadObjectFromFile(FilePath.CalibrationToolblockPath) as CogToolBlock;
  309. }
  310. }
  311. if (SelectCalibration.RobotId != Guid.Empty)
  312. {
  313. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  314. Robot.EnterDebugMode = true;
  315. }
  316. if (SelectCalibration.FeederId != Guid.Empty)
  317. {
  318. Feeder = _feederService.GetFeeder(SelectCalibration.FeederId);
  319. if (Feeder.IsConnected)
  320. {
  321. await Feeder.OpenLightAsync();
  322. await Feeder.SetLightValueAsync((ushort)SelectCalibration.Brightness);
  323. }
  324. IsShowLight = true;
  325. }
  326. else
  327. {
  328. IsShowLight = false;
  329. }
  330. }
  331. /// <summary>
  332. /// 是否允许导航到此视图模型。
  333. /// </summary>
  334. /// <param name="navigationContext"></param>
  335. /// <returns></returns>
  336. public bool IsNavigationTarget(NavigationContext navigationContext)
  337. {
  338. return true;
  339. }
  340. /// <summary>
  341. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  342. /// </summary>
  343. /// <param name="navigationContext"></param>
  344. public void OnNavigatedFrom(NavigationContext navigationContext)
  345. {
  346. ExecuteStopGrabbingCommand();
  347. }
  348. #endregion
  349. private bool _IsAllowEdit = false;
  350. public bool IsAllowEdit
  351. {
  352. get { return _IsAllowEdit; }
  353. set { SetProperty(ref _IsAllowEdit, value); }
  354. }
  355. private void LoginChange(Models.User user)
  356. {
  357. if (user.userPart == Enums.UserPart.Operator)
  358. {
  359. IsAllowEdit = false;
  360. }
  361. else
  362. {
  363. IsAllowEdit = true;
  364. }
  365. }
  366. }
  367. }