CalibrationCameraParamsViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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.IO;
  12. using System.Linq;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using Team.FFFeederService.Interfaces;
  16. using TeamAAS_VP.Core;
  17. using TeamAAS_VP.Interfaces;
  18. using TeamAAS_VP.Models;
  19. using TeamAAS_VP;
  20. namespace TeamAAS_VP.ViewModels.Calibration
  21. {
  22. public class CalibrationCameraParamsViewModel : BindableBase
  23. {
  24. IRegionManager _regionManager;
  25. IRegionNavigationService _regionNavigationService;
  26. IContainerProvider _container;
  27. IDialogService _dialogService;
  28. IEventAggregator _eventAggregator;
  29. #region 属性
  30. private CalibrationInfo _SelectCalibration;
  31. public CalibrationInfo SelectCalibration
  32. {
  33. get { return _SelectCalibration; }
  34. set { SetProperty(ref _SelectCalibration, value); }
  35. }
  36. private IRobot _Robot;
  37. public IRobot Robot
  38. {
  39. get { return _Robot; }
  40. set { SetProperty(ref _Robot, value); }
  41. }
  42. private IVoiceCoilMotorFeeder _Feeder;
  43. public IVoiceCoilMotorFeeder Feeder
  44. {
  45. get { return _Feeder; }
  46. set { SetProperty(ref _Feeder, value); }
  47. }
  48. private ICamera _Camera;
  49. public ICamera Camera
  50. {
  51. get { return _Camera; }
  52. set { SetProperty(ref _Camera, value); }
  53. }
  54. private bool _IsShowLight;
  55. public bool IsShowLight
  56. {
  57. get { return _IsShowLight; }
  58. set { SetProperty(ref _IsShowLight, value); }
  59. }
  60. private float _ExposureTime;
  61. /// <summary>
  62. /// 相机曝光时间
  63. /// </summary>
  64. public float ExposureTime
  65. {
  66. get { return _ExposureTime; }
  67. set
  68. {
  69. SetProperty(ref _ExposureTime, value);
  70. SelectCalibration.ExposureTime = value;
  71. if (Camera != null)
  72. {
  73. try
  74. {
  75. Camera.SetExposureTime(value);
  76. }
  77. catch (Exception)
  78. {
  79. }
  80. }
  81. }
  82. }
  83. private float _Gain;
  84. /// <summary>
  85. /// 相机增益
  86. /// </summary>
  87. public float Gain
  88. {
  89. get { return _Gain; }
  90. set
  91. {
  92. SetProperty(ref _Gain, value);
  93. SelectCalibration.Gain = value;
  94. if (Camera != null)
  95. {
  96. try
  97. {
  98. Camera.SetGain(value);
  99. }
  100. catch (Exception)
  101. {
  102. }
  103. }
  104. }
  105. }
  106. private ICogImage _Image;
  107. public ICogImage Image
  108. {
  109. get { return _Image; }
  110. set { SetProperty(ref _Image,value); }
  111. }
  112. #endregion
  113. #region 命令
  114. private DelegateCommand _LoadedCommand;
  115. public DelegateCommand LoadedCommand =>
  116. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  117. private DelegateCommand _NextCommand;
  118. public DelegateCommand NextCommand =>
  119. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  120. private DelegateCommand _BackCommand;
  121. public DelegateCommand BackCommand =>
  122. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  123. private DelegateCommand<double?> _LightValueChangedCommand;
  124. public DelegateCommand<double?> LightValueChangedCommand =>
  125. _LightValueChangedCommand ?? (_LightValueChangedCommand = new DelegateCommand<double?>(ExecuteLightValueChangedCommand));
  126. private DelegateCommand _StartGrabbingCommand;
  127. public DelegateCommand StartGrabbingCommand =>
  128. _StartGrabbingCommand ?? (_StartGrabbingCommand = new DelegateCommand(ExecuteStartGrabbingCommand));
  129. private DelegateCommand _StopGrabbingCommand;
  130. public DelegateCommand StopGrabbingCommand =>
  131. _StopGrabbingCommand ?? (_StopGrabbingCommand = new DelegateCommand(ExecuteStopGrabbingCommand));
  132. #endregion
  133. #region 事件
  134. #endregion
  135. public CalibrationCameraParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService)
  136. {
  137. _regionManager = regionManager;
  138. _regionNavigationService = regionNavigationService;
  139. _container = container;
  140. _dialogService = dialogService;
  141. _eventAggregator = ea;
  142. SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
  143. }
  144. #region 方法
  145. async void ExecuteLoadedCommand()
  146. {
  147. try
  148. {
  149. if (_container.Resolve<Management>().CurrentUser.userPart == Enums.UserPart.Operator)
  150. {
  151. IsAllowEdit = false;
  152. }
  153. else
  154. {
  155. IsAllowEdit = true;
  156. }
  157. SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
  158. Camera = _container.Resolve<Management>().CameraService.GetCamera(SelectCalibration.CameraID);
  159. if (SelectCalibration.ExposureTime == 0)
  160. {
  161. SelectCalibration.ExposureTime = 5000;
  162. }
  163. ExposureTime = SelectCalibration.ExposureTime;
  164. Gain = SelectCalibration.Gain;
  165. if (Camera != null)
  166. {
  167. Camera.SetExposureTime(ExposureTime);
  168. Camera.SetGain(Gain);
  169. Camera.ImageCallbackEvent += Camera_ImageCallbackEvent;
  170. //Camera.Grab();
  171. }
  172. if (SelectCalibration.IsCreate && SelectCalibration.ToolBlock == null)
  173. {
  174. SelectCalibration.ToolBlock = CogSerializer.LoadObjectFromFile(FilePath.CalibrationToolblockPath) as CogToolBlock;
  175. }
  176. else if (SelectCalibration.ToolBlock == null)
  177. {
  178. string prcPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + SelectCalibration.Name + ".vpp";
  179. if (File.Exists(prcPath))
  180. {
  181. SelectCalibration.ToolBlock = CogSerializer.LoadObjectFromFile(prcPath) as CogToolBlock;
  182. }
  183. else
  184. {
  185. SelectCalibration.ToolBlock = CogSerializer.LoadObjectFromFile(FilePath.CalibrationToolblockPath) as CogToolBlock;
  186. }
  187. }
  188. if (SelectCalibration.RobotId != Guid.Empty)
  189. {
  190. Robot = _container.Resolve<Management>().RobotService.GetRobot(SelectCalibration.RobotId);
  191. }
  192. if (SelectCalibration.FeederId != Guid.Empty)
  193. {
  194. Feeder = _container.Resolve<Management>().FeederService.GetFeeder(SelectCalibration.FeederId);
  195. if (Feeder.IsConnected)
  196. {
  197. await Feeder.OpenLightAsync();
  198. await Feeder.SetLightValueAsync((ushort)SelectCalibration.Brightness);
  199. }
  200. IsShowLight = true;
  201. }
  202. else
  203. {
  204. IsShowLight = false;
  205. }
  206. }
  207. catch (Exception ex)
  208. {
  209. LogHelper.WriteLogError("相机校准加载相机参数界面时出错!", ex);
  210. MessageBox.Show(ex.Message);
  211. }
  212. }
  213. /// <summary>
  214. /// 下一步
  215. /// </summary>
  216. void ExecuteNextCommand()
  217. {
  218. SelectCalibration.ExposureTime = ExposureTime;
  219. SelectCalibration.Gain = Gain;
  220. if (Camera != null)
  221. {
  222. Camera.StopGrabbing();
  223. Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
  224. }
  225. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationEditVision");
  226. }
  227. /// <summary>
  228. /// 上一步
  229. /// </summary>
  230. void ExecuteBackCommand()
  231. {
  232. if (Camera != null)
  233. {
  234. Camera.StopGrabbing();
  235. Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
  236. }
  237. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationBasics");
  238. }
  239. /// <summary>
  240. /// 光源亮度调整时
  241. /// </summary>
  242. async void ExecuteLightValueChangedCommand(double? lightvalue)
  243. {
  244. try
  245. {
  246. if (lightvalue == null) return;
  247. if (Feeder == null || !Feeder.IsConnected) return;
  248. await Feeder.SetLightValueAsync((ushort)lightvalue);
  249. }
  250. catch (Exception ex)
  251. {
  252. LogHelper.WriteLogError("相机校准光源亮度调整时出错!", ex);
  253. MessageBox.Show(ex.Message);
  254. }
  255. }
  256. /// <summary>
  257. /// 停止采集
  258. /// </summary>
  259. void ExecuteStopGrabbingCommand()
  260. {
  261. if (Camera != null)
  262. Camera.StopGrabbing();
  263. }
  264. /// <summary>
  265. /// 开始采集
  266. /// </summary>
  267. void ExecuteStartGrabbingCommand()
  268. {
  269. if (Camera != null)
  270. Camera.StartGrabbing();
  271. }
  272. private void Camera_ImageCallbackEvent(ICogImage image,TimeSpan totaltime,string errormessage)
  273. {
  274. Image = image;
  275. }
  276. #endregion
  277. private bool _IsAllowEdit = false;
  278. public bool IsAllowEdit
  279. {
  280. get { return _IsAllowEdit; }
  281. set { SetProperty(ref _IsAllowEdit, value); }
  282. }
  283. private void LoginChange(Models.User user)
  284. {
  285. if (user.userPart == Enums.UserPart.Operator)
  286. {
  287. IsAllowEdit = false;
  288. }
  289. else
  290. {
  291. IsAllowEdit = true;
  292. }
  293. }
  294. }
  295. }