FixedUpCameraFindP0ViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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.Drawing;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Shapes;
  18. using Team.FFFeederService.Interfaces;
  19. using TeamAAS_VP.Core;
  20. using TeamAAS_VP.Enums;
  21. using TeamAAS_VP.Events;
  22. using TeamAAS_VP.Interfaces;
  23. using TeamAAS_VP.Models;
  24. using TeamAAS_VP.Models.Calibration;
  25. using TeamAAS_VP.Resources.Languages;
  26. using TeamAAS_VP.Services;
  27. namespace TeamAAS_VP.ViewModels.Calibration
  28. {
  29. public class FixedUpCameraFindP0ViewModel : BindableBase, IConfirmNavigationRequest
  30. {
  31. IRegionManager _regionManager;
  32. IRegionNavigationService _regionNavigationService;
  33. IContainerProvider _container;
  34. IDialogService _dialogService;
  35. IEventAggregator _eventAggregator;
  36. IConfigService _configService;
  37. IRobotService _robotService;
  38. ICameraService _cameraService;
  39. IFeederService _feederService;
  40. ISystemDatabaseService _systemDatabaseService;
  41. ICalibrationService _calibrationServvice;
  42. #region 属性
  43. private CogToolBlockEditV2 _ToolBlockEdit;
  44. public CogToolBlockEditV2 ToolBlockEdit
  45. {
  46. get { return _ToolBlockEdit; }
  47. set { SetProperty(ref _ToolBlockEdit, value); }
  48. }
  49. private CalibrationInfo _SelectCalibration;
  50. public CalibrationInfo SelectCalibration
  51. {
  52. get { return _SelectCalibration; }
  53. set { SetProperty(ref _SelectCalibration, value); }
  54. }
  55. private IRobot _Robot;
  56. public IRobot Robot
  57. {
  58. get { return _Robot; }
  59. set { SetProperty(ref _Robot, value); }
  60. }
  61. private ICamera _Camera;
  62. public ICamera Camera
  63. {
  64. get { return _Camera; }
  65. set { SetProperty(ref _Camera, 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. SelectCalibration.FixedUpCameraFindP0Info.ExposureTime = value;
  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. SelectCalibration.FixedUpCameraFindP0Info.Gain = value;
  107. Camera.SetGain(value);
  108. }
  109. catch (Exception)
  110. {
  111. }
  112. }
  113. }
  114. }
  115. private ObservableCollection<CameraInfo> _Cameras;
  116. public ObservableCollection<CameraInfo> Cameras
  117. {
  118. get { return _Cameras; }
  119. set { SetProperty(ref _Cameras, value); }
  120. }
  121. private CameraInfo _SelectCamera;
  122. public CameraInfo SelectCamera
  123. {
  124. get { return _SelectCamera; }
  125. set
  126. {
  127. SetProperty(ref _SelectCamera, value);
  128. if (value != null)
  129. {
  130. Camera = _cameraService.GetCamera(value.Id);
  131. SelectCalibration.FixedUpCameraFindP0Info.CameraId = value.Id;
  132. SelectCalibration.FixedUpCameraFindP0Info.CameraName = value.CameraName;
  133. }
  134. }
  135. }
  136. private ObservableCollection<CalibrationInfo> _CalibrationList;
  137. public ObservableCollection<CalibrationInfo> CalibrationList
  138. {
  139. get { return _CalibrationList; }
  140. set { SetProperty(ref _CalibrationList, value); }
  141. }
  142. private CalibrationInfo _SelectedCalibration;
  143. public CalibrationInfo SelectedCalibration
  144. {
  145. get { return _SelectedCalibration; }
  146. set
  147. {
  148. SetProperty(ref _SelectedCalibration, value);
  149. if (value!=null)
  150. {
  151. SelectCalibration.FixedUpCameraFindP0Info.CalibrationId = value.Id;
  152. SelectCalibration.FixedUpCameraFindP0Info.CalibrationName = value.Name;
  153. }
  154. }
  155. }
  156. #endregion
  157. #region 命令
  158. private DelegateCommand _LoadedCommand;
  159. public DelegateCommand LoadedCommand =>
  160. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  161. private DelegateCommand _NextCommand;
  162. public DelegateCommand NextCommand =>
  163. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  164. private DelegateCommand _BackCommand;
  165. public DelegateCommand BackCommand =>
  166. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  167. private DelegateCommand _RunVisionProcessCommand;
  168. public DelegateCommand RunVisionProcessCommand =>
  169. _RunVisionProcessCommand ?? (_RunVisionProcessCommand = new DelegateCommand(ExecuteRunVisionProcessCommand));
  170. private DelegateCommand _FindP0Command;
  171. public DelegateCommand FindP0Command =>
  172. _FindP0Command ?? (_FindP0Command = new DelegateCommand(ExecuteFindP0Command));
  173. #endregion
  174. #region 事件
  175. #endregion
  176. public FixedUpCameraFindP0ViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
  177. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, ISystemDatabaseService systemDatabaseService, ICalibrationService calibrationService)
  178. {
  179. _regionManager = regionManager;
  180. _regionNavigationService = regionNavigationService;
  181. _container = container;
  182. _dialogService = dialogService;
  183. _eventAggregator = ea;
  184. _configService = configService;
  185. _robotService = robotService;
  186. _cameraService = cameraService;
  187. _feederService = feederService;
  188. _systemDatabaseService = systemDatabaseService;
  189. _calibrationServvice = calibrationService;
  190. }
  191. #region 方法
  192. void ExecuteLoadedCommand()
  193. {
  194. try
  195. {
  196. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  197. {
  198. IsAllowEdit = false;
  199. }
  200. else
  201. {
  202. IsAllowEdit = true;
  203. }
  204. }
  205. catch (Exception ex)
  206. {
  207. LogHelper.WriteLogError(Lang.相机校准加载视觉工具处理界面时出错, ex);
  208. MessageBox.Show(ex.Message);
  209. }
  210. }
  211. /// <summary>
  212. /// 下一步
  213. /// </summary>
  214. void ExecuteNextCommand()
  215. {
  216. try
  217. {
  218. string prcPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + "FixedUpCameraTool.vpp";
  219. if (!Directory.Exists(System.IO.Path.GetDirectoryName(prcPath)))
  220. {
  221. Directory.CreateDirectory(System.IO.Path.GetDirectoryName(prcPath));
  222. }
  223. VisionProFileHelper.SaveObjectToFileSafe(ToolBlockEdit.Subject, prcPath);
  224. NavigationParameters param = new NavigationParameters();
  225. param.Add("SelectCalibration", SelectCalibration);
  226. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationAuto", param);
  227. }
  228. catch (Exception ex)
  229. {
  230. LogHelper.WriteLogError(Lang.相机校准下相机示教P0界面点击下一步时出错, ex);
  231. MessageBox.Show(ex.Message);
  232. }
  233. }
  234. /// <summary>
  235. /// 上一步
  236. /// </summary>
  237. void ExecuteBackCommand()
  238. {
  239. try
  240. {
  241. NavigationParameters param = new NavigationParameters();
  242. param.Add("SelectCalibration", SelectCalibration);
  243. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationTeachCenterPoint", param);
  244. }
  245. catch (Exception ex)
  246. {
  247. LogHelper.WriteLogError(Lang.相机校准下相机示教P0界面点击上一步时出错, ex);
  248. MessageBox.Show(ex.Message);
  249. }
  250. }
  251. /// <summary>
  252. /// 运行视觉流程
  253. /// </summary>
  254. void ExecuteRunVisionProcessCommand()
  255. {
  256. try
  257. {
  258. if (Camera == null)
  259. {
  260. MessageBox.Show(Lang.请先选择相机);
  261. return;
  262. }
  263. ToolBlockEdit.Subject.Inputs["InputImage"].Value = Camera.Grab();
  264. ToolBlockEdit.Subject.Run();
  265. }
  266. catch (Exception ex)
  267. {
  268. LogHelper.WriteLogError(Lang.相机校准运行流程时出错时出错, ex);
  269. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  270. }
  271. }
  272. /// <summary>
  273. /// 自动计算P0点
  274. /// </summary>
  275. async void ExecuteFindP0Command()
  276. {
  277. if (Camera == null)
  278. {
  279. MessageBox.Show(Lang.请先选择相机);
  280. return;
  281. }
  282. if (SelectedCalibration == null)
  283. {
  284. MessageBox.Show(Lang.请先选择标定);
  285. return;
  286. }
  287. var result = await CaptureAndProcess();
  288. if (result.IsSuccess)
  289. {
  290. if (SelectCalibration.MarkPoint == null)
  291. {
  292. SelectCalibration.MarkPoint = new RPoint();
  293. }
  294. SelectCalibration.MarkPoint.X = (float) result.X;
  295. SelectCalibration.MarkPoint.Y = (float)result.Y;
  296. MessageBox.Show(string.Format(Lang.P0点计算成功X0Y1, result.X, result.Y));//$"P0点计算成功!X:{result.X},Y:{result.Y}");
  297. }
  298. else
  299. {
  300. MessageBox.Show(Lang.未找到P0点请调整工件位置后重试);
  301. }
  302. }
  303. /// <summary>
  304. /// 相机拍照
  305. /// </summary>
  306. /// <returns></returns>
  307. private async Task<(bool IsSuccess, double X, double Y, double U, int ImageWidth, int ImageHeight)> CaptureAndProcess()
  308. {
  309. ICogImage image = null;
  310. //采集图像
  311. if (Camera != null)
  312. {
  313. image = Camera.Grab();
  314. ToolBlockEdit.Subject.Inputs["InputImage"].Value = image;
  315. }
  316. ToolBlockEdit.Subject.Run();
  317. CogToolBlockTerminalCollection outputCollection = ToolBlockEdit.Subject.Outputs;
  318. bool Found = (bool)(outputCollection["Found"].Value);
  319. if (Found)
  320. {
  321. var strpoints = outputCollection["Point"].Value.ToString();
  322. string[] items = strpoints.Split(',');
  323. double x = double.Parse(items[0]);
  324. double y = double.Parse(items[1]);
  325. //校准转换
  326. var calibResult = _calibrationServvice.ConvertPixelToPosition((x, y, 0), null, SelectedCalibration, Robot.Brand);
  327. if (calibResult.IsSucceed)
  328. {
  329. x = calibResult.X;
  330. y = calibResult.Y;
  331. return (true, x, y, 0, image.Width, image.Height);
  332. }
  333. return (false, 0, 0, 0, image.Width, image.Height);
  334. }
  335. else
  336. {
  337. return (false, 0, 0, 0, image.Width, image.Height);
  338. }
  339. }
  340. #endregion
  341. #region 继承
  342. /// <summary>
  343. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  344. /// </summary>
  345. /// <param name="navigationContext"></param>
  346. /// <param name="continuationCallback"></param>
  347. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  348. {
  349. continuationCallback(true);
  350. }
  351. /// <summary>
  352. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  353. /// </summary>
  354. /// <param name="navigationContext"></param>
  355. /// <exception cref="NotImplementedException"></exception>
  356. public async void OnNavigatedTo(NavigationContext navigationContext)
  357. {
  358. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  359. if (SelectCalibration.FixedUpCameraFindP0Info == null)
  360. {
  361. SelectCalibration.FixedUpCameraFindP0Info = new FixedUpCameraFindP0Info();
  362. }
  363. if (ToolBlockEdit.Subject==null)
  364. {
  365. string prcPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + "FixedUpCameraTool.vpp";
  366. if (File.Exists(prcPath))
  367. {
  368. ToolBlockEdit.Subject = VisionProFileHelper.LoadObjectFromFileSafe<CogToolBlock>(prcPath);
  369. }
  370. else
  371. {
  372. ToolBlockEdit.Subject = VisionProFileHelper.LoadObjectFromFileSafe<CogToolBlock>(FilePath.CalibrationToolblockPath);
  373. }
  374. }
  375. if (SelectCalibration.RobotId != Guid.Empty)
  376. {
  377. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  378. }
  379. Cameras = new ObservableCollection<CameraInfo>(_configService.GetAllCameras());
  380. if (SelectCalibration.FixedUpCameraFindP0Info.CameraId != Guid.Empty)
  381. {
  382. SelectCamera = Cameras.FirstOrDefault(c => c.Id == SelectCalibration.FixedUpCameraFindP0Info.CameraId);
  383. }
  384. CalibrationList = new ObservableCollection<CalibrationInfo>(_calibrationServvice.GetAllCalibrations());
  385. if (SelectCalibration.FixedUpCameraFindP0Info.CalibrationId != Guid.Empty)
  386. {
  387. SelectedCalibration = CalibrationList.FirstOrDefault(c => c.Id == SelectCalibration.FixedUpCameraFindP0Info.CalibrationId);
  388. }
  389. ExposureTime = SelectCalibration.ExposureTime;
  390. Gain = SelectCalibration.Gain;
  391. }
  392. /// <summary>
  393. /// 是否允许导航到此视图模型。
  394. /// </summary>
  395. /// <param name="navigationContext"></param>
  396. /// <returns></returns>
  397. public bool IsNavigationTarget(NavigationContext navigationContext)
  398. {
  399. return true;
  400. }
  401. /// <summary>
  402. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  403. /// </summary>
  404. /// <param name="navigationContext"></param>
  405. public void OnNavigatedFrom(NavigationContext navigationContext)
  406. {
  407. }
  408. #endregion
  409. private bool _IsAllowEdit = false;
  410. public bool IsAllowEdit
  411. {
  412. get { return _IsAllowEdit; }
  413. set { SetProperty(ref _IsAllowEdit, value); }
  414. }
  415. private void LoginChange(Models.User user)
  416. {
  417. if (user.userPart == Enums.UserPart.Operator)
  418. {
  419. IsAllowEdit = false;
  420. }
  421. else
  422. {
  423. IsAllowEdit = true;
  424. }
  425. }
  426. }
  427. }