TorqueCheckViewModel.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. using MathNet.Numerics;
  2. using NPOI.OpenXml4Net.OPC.Internal;
  3. using NPOI.SS.Formula.Functions;
  4. using OxyPlot;
  5. using Prism.Commands;
  6. using Prism.Events;
  7. using Prism.Ioc;
  8. using Prism.Mvvm;
  9. using Prism.Services.Dialogs;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using TeamAAS_VP.Controls;
  20. using TeamAAS_VP.Core;
  21. using TeamAAS_VP.Core.PLCs;
  22. using TeamAAS_VP.Core.ScrewDriver;
  23. using TeamAAS_VP.Data;
  24. using TeamAAS_VP.Enums;
  25. using TeamAAS_VP.Events;
  26. using TeamAAS_VP.Interfaces;
  27. using TeamAAS_VP.Models;
  28. using TeamAAS_VP.Models.PLC;
  29. using TeamAAS_VP.Models.Torque;
  30. using TeamAAS_VP.Resources.Languages;
  31. using TeamAAS_VP.Services;
  32. using FileHelper = TeamAAS_VP.Core.FileHelper;
  33. namespace TeamAAS_VP.ViewModels.Home
  34. {
  35. public class TorqueCheckViewModel : BindableBase, IDialogAware
  36. {
  37. // 点检扭矩参数存放日志地址
  38. public static readonly string CheckTorqueConfigurationPath = "..//Log//ScrewFeedersConfiguration.cfg";
  39. IEventAggregator _eventAggregator;
  40. IConfigService _configService;
  41. IContainerProvider _container;
  42. IRobotService _robotService;
  43. IPlcService _plcService;
  44. TorqueService _torqueService;
  45. float Value { get; set; } = 0;
  46. #region 属性
  47. private Management _management;
  48. public Management management
  49. {
  50. get { return _management; }
  51. set { SetProperty(ref _management, value); }
  52. }
  53. private bool CanExecute
  54. {
  55. get
  56. {
  57. // 命令可执行仅当机器人可执行且已选择点位
  58. if (Robot != null && Robot.IsConnected && Robot.CanExecute)
  59. {
  60. return true;
  61. }
  62. return false;
  63. }
  64. }
  65. private IRobot _Robot;
  66. public IRobot Robot
  67. {
  68. get { return _Robot; }
  69. set { SetProperty(ref _Robot, value); }
  70. }
  71. private double _Distance = 1;
  72. public double Distance
  73. {
  74. get { return _Distance; }
  75. set
  76. {
  77. SetProperty(ref _Distance, value);
  78. try
  79. {
  80. if (Robot == null)
  81. {
  82. return;
  83. }
  84. _configService.UpdateRobotStepDistance(Robot.Id, value);
  85. }
  86. catch (Exception)
  87. {
  88. }
  89. }
  90. }
  91. private float _Z_Distance;
  92. public float Z_Distance
  93. {
  94. get { return _Z_Distance; }
  95. set { SetProperty(ref _Z_Distance, value); }
  96. }
  97. private float _torqueValue;
  98. public float TorqueValue
  99. {
  100. get { return _torqueValue; }
  101. set { SetProperty(ref _torqueValue, value); }
  102. }
  103. private string _TValue;
  104. public string TValue
  105. {
  106. get { return _TValue; }
  107. set
  108. {
  109. SetProperty(ref _TValue, value);
  110. }
  111. }
  112. private string _PValue;
  113. public string PValue
  114. {
  115. get { return _PValue; }
  116. set
  117. {
  118. SetProperty(ref _PValue, value);
  119. }
  120. }
  121. private string _SubValue;
  122. public string SubValue
  123. {
  124. get { return _SubValue; }
  125. set { SetProperty(ref _SubValue, value); }
  126. }
  127. private string _Result;
  128. public string Result
  129. {
  130. get { return _Result; }
  131. set { SetProperty(ref _Result, value); }
  132. }
  133. private string _FontColor;
  134. public string FontColor
  135. {
  136. get { return _FontColor; }
  137. set { SetProperty(ref _FontColor, value); }
  138. }
  139. private int _SelectedIndex;
  140. public int SelectedIndex
  141. {
  142. get { return _SelectedIndex; }
  143. set { SetProperty(ref _SelectedIndex, value); }
  144. }
  145. private ObservableCollection<PlcPoint_Torque> _Points = new ObservableCollection<PlcPoint_Torque>();
  146. /// <summary>
  147. /// 点位集合
  148. /// </summary>
  149. public ObservableCollection<PlcPoint_Torque> Points
  150. {
  151. get { return _Points; }
  152. set { SetProperty(ref _Points, value); }
  153. }
  154. private TorqueProduceModel _AllProductParameter = new TorqueProduceModel();
  155. /// <summary>
  156. /// 所有产品的参数信息
  157. /// </summary>
  158. public TorqueProduceModel AllProductParameter
  159. {
  160. get { return _AllProductParameter; }
  161. set { SetProperty(ref _AllProductParameter, value); }
  162. }
  163. private PlcPoint_Torque _selectedPointInDataGrid;
  164. public PlcPoint_Torque SelectedPointInDataGrid
  165. {
  166. get { return _selectedPointInDataGrid; }
  167. set
  168. {
  169. SetProperty(ref _selectedPointInDataGrid, value);
  170. // 可以同步到 SelectPoint 属性
  171. SelectedPoint = value;
  172. AllProductParameter.TorquePoints = _Points;
  173. }
  174. }
  175. private PlcPoint_Torque _SelectedPoint;
  176. public PlcPoint_Torque SelectedPoint
  177. {
  178. get { return _SelectedPoint; }
  179. set { SetProperty(ref _SelectedPoint, value); }
  180. }
  181. #endregion
  182. #region 命令
  183. private DelegateCommand _CancelCommand;
  184. public DelegateCommand CancelCommand =>
  185. _CancelCommand ?? (_CancelCommand = new DelegateCommand(ExecuteCancelCommand));
  186. private DelegateCommand _LoadedCommand;
  187. public DelegateCommand LoadedCommand =>
  188. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  189. private DelegateCommand _ConfirmCommand;
  190. public DelegateCommand ConfirmCommand =>
  191. _ConfirmCommand ?? (_ConfirmCommand = new DelegateCommand(ExecuteConfirmCommand));
  192. private DelegateCommand<string> _MotorCommand;
  193. public DelegateCommand<string> MotorCommand =>
  194. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  195. private DelegateCommand<object> _JogCommand;
  196. public DelegateCommand<object> JogCommand =>
  197. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  198. private DelegateCommand _TechPointCommand;
  199. public DelegateCommand TechPointCommand =>
  200. _TechPointCommand ?? (_TechPointCommand = new DelegateCommand(ExecuteTechPointCommand));
  201. private DelegateCommand _ModifyCommand;
  202. public DelegateCommand ModifyCommand =>
  203. _ModifyCommand ?? (_ModifyCommand = new DelegateCommand(ExecuteModifyCommand));
  204. private DelegateCommand _WorkCommand;
  205. public DelegateCommand WorkCommand =>
  206. _WorkCommand ?? (_WorkCommand = new DelegateCommand(ExecuteWorkCommand));
  207. private DelegateCommand _AddPointCommand;
  208. public DelegateCommand AddPointCommand =>
  209. _AddPointCommand ?? (_AddPointCommand = new DelegateCommand(ExecuteAddPointCommand));
  210. private DelegateCommand _SavePointsCommand;
  211. public DelegateCommand SavePointsCommand =>
  212. _SavePointsCommand ?? (_SavePointsCommand = new DelegateCommand(ExecuteSavePointsCommand));
  213. private DelegateCommand _DeletePointCommand;
  214. public DelegateCommand DeletePointCommand =>
  215. _DeletePointCommand ?? (_DeletePointCommand = new DelegateCommand(ExecuteDeletePointCommand, CanExecuteDeletePointCommand).ObservesProperty(() => SelectedPointInDataGrid));
  216. #endregion
  217. #region 方法
  218. public TorqueCheckViewModel(IEventAggregator ea, IConfigService configService, TorqueService torqueService,
  219. IContainerProvider container, IRobotService robotService, IPlcService plcService)
  220. {
  221. _eventAggregator = ea;
  222. _configService = configService;
  223. _container = container;
  224. _robotService = robotService;
  225. _plcService = plcService;
  226. _torqueService = torqueService;
  227. management = _container.Resolve<Management>();
  228. }
  229. bool CanExecuteDeletePointCommand()
  230. {
  231. return SelectedPointInDataGrid != null;
  232. }
  233. void ExecuteCancelCommand()
  234. {
  235. IDialogParameters parameters = new DialogParameters();
  236. RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel, parameters));
  237. }
  238. void ExecuteConfirmCommand()
  239. {
  240. IDialogParameters parameters = new DialogParameters();
  241. RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parameters));
  242. }
  243. /// <summary>
  244. /// 电机
  245. /// </summary>
  246. /// <param name="obj"></param>
  247. private async void ExecuteMotorCommand(string obj)
  248. {
  249. if (Robot == null || !Robot.IsConnected) return;
  250. try
  251. {
  252. await Robot.MotorAsync(Convert.ToBoolean(obj));
  253. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  254. }
  255. catch (Exception ex)
  256. {
  257. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  258. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  259. }
  260. }
  261. /// <summary>
  262. /// 点动
  263. /// </summary>
  264. /// <param name="obj"></param>
  265. private async void ExecuteJogCommand(object obj)
  266. {
  267. if (Robot == null || !Robot.IsConnected) return;
  268. try
  269. {
  270. var items = ((string)obj).Split(':');
  271. //如果移动距离大于10mm,则提示用户确认
  272. double distance = double.Parse(items[1]);
  273. if (Math.Abs(distance) >= 10)
  274. {
  275. if (MessageBox.Show($"确认要点动距离 {distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  276. {
  277. return;
  278. }
  279. }
  280. Robot.Timeout = 6000000;
  281. switch (items[0])
  282. {
  283. case "X+":
  284. await Robot.JogAsync("X", distance);
  285. break;
  286. case "X-":
  287. await Robot.JogAsync("X", -distance);
  288. break;
  289. case "Y+":
  290. await Robot.JogAsync("Y", distance);
  291. break;
  292. case "Y-":
  293. await Robot.JogAsync("Y", -distance);
  294. break;
  295. case "Z+":
  296. await Robot.JogAsync("Z", distance);
  297. break;
  298. case "Z-":
  299. await Robot.JogAsync("Z", -distance);
  300. break;
  301. case "U+":
  302. await Robot.JogAsync("U", distance);
  303. break;
  304. case "U-":
  305. await Robot.JogAsync("U", -distance);
  306. break;
  307. case "V+":
  308. await Robot.JogAsync("V", distance);
  309. break;
  310. case "V-":
  311. await Robot.JogAsync("V", -distance);
  312. break;
  313. case "W+":
  314. await Robot.JogAsync("W", distance);
  315. break;
  316. case "W-":
  317. await Robot.JogAsync("W", -distance);
  318. break;
  319. default:
  320. break;
  321. }
  322. Robot.Timeout = 5000;
  323. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  324. }
  325. catch (Exception ex)
  326. {
  327. LogHelper.WriteLogError("机器人点位-机器人点动时出错", ex);
  328. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  329. }
  330. }
  331. /// <summary>
  332. /// 示教点位
  333. /// </summary>
  334. async void ExecuteTechPointCommand()
  335. {
  336. //弹窗用户是否确认要示教该点位
  337. if (MessageBox.Show($"确认要示教点位吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  338. {
  339. return;
  340. }
  341. if (SelectedPoint == null)
  342. {
  343. MessageBox.Show("请选择示教点位");
  344. return;
  345. }
  346. var curpos = await Robot.GetRobotPosAsync();
  347. SelectedPoint.X_Position = curpos.X;
  348. SelectedPoint.Y_Position = curpos.Y;
  349. SelectedPoint.Z_Position_Start = curpos.Z;
  350. SelectedPoint.Z_Position_Stop = 0;
  351. AllProductParameter.TorqueValue = this.TorqueValue;
  352. AllProductParameter.TorquePoints = Points;
  353. FileHelper.WriteJsonFile(AllProductParameter, FilePath.TorqueCheckPath);
  354. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  355. }
  356. /// <summary>
  357. /// 修改
  358. /// </summary>
  359. async void ExecuteModifyCommand()
  360. {
  361. if (MessageBox.Show($"确认要修改扭力阈值吗?", "修改", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  362. {
  363. return;
  364. }
  365. var res = FileHelper.ReadJsonFile<TorqueProduceModel>(FilePath.TorqueCheckPath);
  366. res.TorqueValue = this.TorqueValue;
  367. try
  368. {
  369. FileHelper.WriteJsonFile(res, FilePath.TorqueCheckPath);
  370. }
  371. catch (Exception)
  372. {
  373. MessageBox.Show("修改失败");
  374. }
  375. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.4 });
  376. }
  377. async void ExecuteWorkCommand()
  378. {
  379. if (!File.Exists(FilePath.TorqueCheckPath))
  380. {
  381. MessageBox.Show("还未示教,请先示教");
  382. return;
  383. }
  384. if (Robot == null || !Robot.IsConnected)
  385. {
  386. return;
  387. }
  388. if (MessageBox.Show($"确认要进行点检吗?", "执行点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  389. {
  390. return;
  391. }
  392. try
  393. {
  394. if (await MoveRobot())
  395. {
  396. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.4 });
  397. }
  398. else
  399. {
  400. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.失败, Duration = 0.4 });
  401. }
  402. }
  403. catch (Exception ex)
  404. {
  405. LogHelper.WriteLogError("执行点检时出错!", ex);
  406. }
  407. }
  408. /// <summary>
  409. /// 点检扭矩总体流程
  410. /// </summary>
  411. /// <returns></returns>
  412. public async Task<bool> MoveRobot()
  413. {
  414. TorqueProduceModel Plc_Point = new TorqueProduceModel();
  415. PlcAddressConfig addressConfig = _configService.GetPlcAddresses();
  416. OpcUaClientPLC plc = _plcService.GetPlcByNumber(addressConfig.PlcNo) as OpcUaClientPLC;
  417. try
  418. {
  419. Plc_Point = FileHelper.ReadJsonFile<TorqueProduceModel>(FilePath.TorqueCheckPath);
  420. if (Plc_Point == null || addressConfig == null || plc == null)
  421. {
  422. return false;
  423. }
  424. //切换手动模式
  425. if (await plc.WriteNodeAsync<bool>(addressConfig.OP_AutoManual.Address, false))
  426. {
  427. //回设置的零点
  428. if (await _torqueService.Safety_Position_Async(Robot, plc, addressConfig, Plc_Point))
  429. {
  430. //执行抛螺丝动作
  431. if (await _torqueService.Throw_Screw_Async(Robot, plc, addressConfig, Plc_Point))
  432. {
  433. //执行取螺丝动作
  434. if (await _torqueService.Pick_Screw_Async(Robot, plc, addressConfig, Plc_Point))
  435. {
  436. //执行锁螺丝点位操作
  437. if (await _torqueService.Screw_Torque_Async(Robot, plc, addressConfig, Plc_Point))
  438. {
  439. //获取电批结果并显示曲线
  440. if (await WatchAsync(plc, addressConfig))
  441. {
  442. await DisplayResult();
  443. return true;
  444. }
  445. MessageBox.Show("未获取到电批结果"); return false;
  446. }
  447. MessageBox.Show("执行锁螺丝点位操作失败"); return false;
  448. }
  449. MessageBox.Show("执行取螺丝动作失败"); return false;
  450. }
  451. MessageBox.Show("执行抛螺丝动作失败"); return false;
  452. }
  453. MessageBox.Show("执行回安全点失败"); return false;
  454. }
  455. MessageBox.Show("点检失败"); return false;
  456. }
  457. catch (Exception ex)
  458. {
  459. LogHelper.WriteLogError("执行点检移动时出错!", ex);
  460. return false;
  461. }
  462. finally
  463. {
  464. await _torqueService.Safety_Position_Async(Robot, plc, addressConfig, Plc_Point);
  465. }
  466. }
  467. public async Task DisplayResult()
  468. {
  469. try
  470. {
  471. await Task.Delay(100);
  472. TValue = management.SignalValue;
  473. double tvalue = double.Parse(TValue.Split(' ')[0]);
  474. double pvalue = Value;
  475. PValue = $"{Math.Round(pvalue, 3)} kgf.cm";
  476. SubValue = $"{Math.Round(Math.Abs(tvalue - pvalue), 3)} kgf.cm";
  477. double sub = Math.Abs(tvalue - pvalue);
  478. var res = FileHelper.ReadJsonFile<TorqueProduceModel>(FilePath.TorqueCheckPath);
  479. if (res != null)
  480. {
  481. if (sub <= res.TorqueValue)
  482. {
  483. FontColor = "Green";
  484. Result = "OK";
  485. }
  486. else
  487. {
  488. FontColor = "Red";
  489. Result = "NG";
  490. }
  491. }
  492. else
  493. {
  494. if (sub <= TorqueValue)
  495. {
  496. FontColor = "Green";
  497. Result = "OK";
  498. }
  499. else
  500. {
  501. FontColor = "Red";
  502. Result = "NG";
  503. }
  504. }
  505. await _torqueService.Save_Torque_Log($"{DateTime.Now},{TValue},{PValue},{Math.Round(sub, 4)},{TorqueValue},{Result}");
  506. SendTaskMessage("点检扭矩成功", MessageLevel.Info);
  507. }
  508. catch (Exception ex)
  509. {
  510. LogHelper.WriteLogError("执行点检显示时出错!", ex);
  511. }
  512. }
  513. public async Task<bool> WatchAsync(OpcUaClientPLC plc, PlcAddressConfig addressConfig)
  514. {
  515. object oking = null;
  516. object nging = null;
  517. int count = 0;
  518. while (count++ < 20)
  519. {
  520. await Task.Delay(100);
  521. oking = plc.ReadNode(addressConfig.Screw_OK.Address);
  522. nging = plc.ReadNode(addressConfig.Screw_NG.Address);
  523. if (oking != null && nging != null)
  524. {
  525. bool okin = (bool)oking; bool ngin = (bool)nging;
  526. //电批OKNG
  527. if (okin || ngin)
  528. {
  529. //获取电批结果
  530. bool isSucced = management.ScrewDriver.GetData();
  531. LockResult lockResult = new LockResult();
  532. lockResult.LockTurns = (float)management.ScrewDriver.Truns;
  533. lockResult.LockTorque = (float)management.ScrewDriver.TorqueValue;
  534. lockResult.WaveDatas = management.ScrewDriver.WaveDatas;
  535. App.Current.Dispatcher.Invoke(() =>
  536. {
  537. _eventAggregator.GetEvent<LockFinishNotification>().Publish(lockResult);
  538. });
  539. //await _systemDatabaseService.RecordLockResultAsync(lockResult);
  540. await plc.WriteNodeAsync(addressConfig.In_WorkDone.Address, (Int16)0);
  541. Value = lockResult.LockTorque;
  542. await plc.WriteNodeAsync(addressConfig.Check_ScrewTorque.Address, false);
  543. return true;
  544. }
  545. }
  546. }
  547. return false;
  548. }
  549. #endregion
  550. public string Title { get; set; } = "扭力点检";
  551. public event Action<IDialogResult> RequestClose;
  552. public bool CanCloseDialog()
  553. {
  554. return true;
  555. }
  556. public void OnDialogClosed()
  557. {
  558. }
  559. public void OnDialogOpened(IDialogParameters parameters)
  560. {
  561. try
  562. {
  563. var res = FileHelper.ReadJsonFile<TorqueProduceModel>(FilePath.TorqueCheckPath);
  564. if (res == null)
  565. {
  566. MessageBox.Show("还未示教,请先示教");
  567. }
  568. }
  569. catch (Exception)
  570. {
  571. MessageBox.Show("还未示教,请先示教");
  572. }
  573. if (Robot == null)
  574. {
  575. Robot = _robotService.GetRobotByNumber(1);
  576. }
  577. if (Robot != null && Robot.IsConnected)
  578. {
  579. this.Distance = _configService.GetRobotStepDistance(Robot.Id);
  580. Robot.EnterDebugMode = true;
  581. }
  582. else if (Robot != null)
  583. {
  584. Robot.EnterDebugMode = true;
  585. }
  586. }
  587. private void ExecuteLoadedCommand()
  588. {
  589. var res = FileHelper.ReadJsonFile<TorqueProduceModel>(FilePath.TorqueCheckPath);
  590. this.Points = res.TorquePoints;
  591. this.TorqueValue = res.TorqueValue;
  592. }
  593. #region 添加点位与删除点位
  594. /// <summary>
  595. /// 增加点位
  596. /// </summary>
  597. private void ExecuteAddPointCommand()
  598. {
  599. //添加点位
  600. Points.Add(new PlcPoint_Torque()
  601. {
  602. Number = Points.Count + 1,
  603. });
  604. }
  605. /// <summary>
  606. /// 移除点位
  607. /// </summary>
  608. private void ExecuteDeletePointCommand()
  609. {
  610. if (SelectedPoint == null)
  611. {
  612. return;
  613. }
  614. // 确认删除
  615. if (MessageBox.Show(Lang.确认要删除选中的点位吗, Lang.删除点位, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  616. {
  617. return;
  618. }
  619. int removedIndex = Points.IndexOf(SelectedPoint);
  620. Points.Remove(SelectedPoint);
  621. //重新编号
  622. for (int i = 0; i < Points.Count; i++)
  623. {
  624. Points[i].Number = i + 1;
  625. }
  626. // adjust selection
  627. if (Points.Count > 0)
  628. {
  629. int newIndex = Math.Min(removedIndex, Points.Count - 1);
  630. SelectedPointInDataGrid = Points[newIndex];
  631. }
  632. else
  633. {
  634. SelectedPointInDataGrid = null;
  635. }
  636. }
  637. #endregion
  638. private void SendTaskMessage(string msg, MessageLevel level)
  639. {
  640. App.Current.Dispatcher.Invoke(() =>
  641. {
  642. _eventAggregator.GetEvent<TaskMessageNotification>().Publish(new Models.MessageStruct() { Message = msg, level = level });
  643. });
  644. }
  645. private void ExecuteSavePointsCommand()
  646. {
  647. AllProductParameter.TorqueValue = this.TorqueValue;
  648. AllProductParameter.TorquePoints = Points;
  649. FileHelper.WriteJsonFile(AllProductParameter, FilePath.TorqueCheckPath);
  650. MessageBox.Show("参数保存成功");
  651. }
  652. }
  653. }