CalibrationCameraParamsViewModel.cs 13 KB

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