TransporterStationPositionControl.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. using MECF.Framework.Common.Beckhoff.AxisProvider;
  2. using MECF.Framework.Common.Beckhoff.Station;
  3. using MECF.Framework.Common.DataCenter;
  4. using MECF.Framework.Common.Layout;
  5. using MECF.Framework.Common.OperationCenter;
  6. using MECF.Framework.Common.Utilities;
  7. using CyberX8_Core;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Linq;
  12. using System.Runtime.CompilerServices;
  13. using System.Text;
  14. using System.Text.RegularExpressions;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Data;
  19. using System.Windows.Documents;
  20. using System.Windows.Input;
  21. using System.Windows.Media;
  22. using System.Windows.Media.Imaging;
  23. using System.Windows.Navigation;
  24. using System.Windows.Shapes;
  25. namespace CyberX8_Themes.UserControls
  26. {
  27. /// <summary>
  28. /// TransporterStationPositionControl.xaml 的交互逻辑
  29. /// </summary>
  30. public partial class TransporterStationPositionControl : UserControl, INotifyPropertyChanged
  31. {
  32. #region 内部变量
  33. /// <summary>
  34. /// 步进
  35. /// </summary>
  36. private double _incrementValue;
  37. /// <summary>
  38. /// 查询后台数据集合
  39. /// </summary>
  40. private List<string> _rtDataKeys = new List<string>();
  41. /// <summary>
  42. /// 查询后台的数据
  43. /// </summary>
  44. private Dictionary<string, object> _rtDataValues;
  45. /// <summary>
  46. /// axis
  47. /// </summary>
  48. BeckhoffStationAxis _axis;
  49. /// <summary>
  50. /// Stations
  51. /// </summary>
  52. private List<string> _moduleStations;
  53. /// <summary>
  54. /// 选择项
  55. /// </summary>
  56. private string _moduleSelectedItem;
  57. /// <summary>
  58. /// 已经保存的位置
  59. /// </summary>
  60. private double _savePosition;
  61. /// <summary>
  62. /// scaleFactor
  63. /// </summary>
  64. private double _scaleFactor;
  65. #endregion
  66. #region 属性
  67. public static readonly DependencyProperty ModuleNameProperty = DependencyProperty.Register( "ModuleName", typeof(string), typeof(TransporterStationPositionControl),
  68. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  69. /// <summary>
  70. /// 模块名称
  71. /// </summary>
  72. public string ModuleName
  73. {
  74. get
  75. {
  76. return (string)this.GetValue(ModuleNameProperty);
  77. }
  78. set
  79. {
  80. this.SetValue(ModuleNameProperty, value);
  81. }
  82. }
  83. public List<string> ModuleItemSource
  84. {
  85. get { return _moduleStations; }
  86. set
  87. {
  88. _moduleStations = value;
  89. InvokePropertyChanged(nameof(ModuleItemSource));
  90. }
  91. }
  92. public static readonly DependencyProperty IsAtStationProperty = DependencyProperty.Register("IsAtStation", typeof(bool), typeof(TransporterStationPositionControl));
  93. public bool IsAtStation
  94. {
  95. get
  96. {
  97. return (bool)this.GetValue(IsAtStationProperty);
  98. }
  99. set
  100. {
  101. SetValue(IsAtStationProperty, value);
  102. InvokePropertyChanged(nameof(IsAtStation));
  103. }
  104. }
  105. /// <summary>
  106. /// 选项索引
  107. /// </summary>
  108. public string ModuleSelectedItem
  109. {
  110. get
  111. {
  112. return _moduleSelectedItem;
  113. }
  114. set
  115. {
  116. SavedPosition = GetStationPosition(_axis, value);
  117. if (_cellNameCellIdDictionary.ContainsKey(value)&&value.ToLower()!="loader")
  118. {
  119. _moduleSelectedCellItem = _cellNameCellIdDictionary[value];
  120. }
  121. else
  122. {
  123. _moduleSelectedCellItem = value;
  124. }
  125. _moduleSelectedItem = value;
  126. InvokePropertyChanged(nameof(ModuleSelectedItem));
  127. }
  128. }
  129. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("OperationCommand", typeof(ICommand), typeof(TransporterStationPositionControl),
  130. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  131. public ICommand OperationCommand
  132. {
  133. get
  134. {
  135. return (ICommand)this.GetValue(CommandProperty);
  136. }
  137. set
  138. {
  139. this.SetValue(CommandProperty, value);
  140. }
  141. }
  142. public static readonly DependencyProperty ModuleTitleProperty = DependencyProperty.Register(
  143. "ModuleTitle", typeof(string), typeof(TransporterStationPositionControl),
  144. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  145. /// <summary>
  146. /// 标题
  147. /// </summary>
  148. public string ModuleTitle
  149. {
  150. get
  151. {
  152. return (string)this.GetValue(ModuleTitleProperty);
  153. }
  154. set
  155. {
  156. this.SetValue(ModuleTitleProperty, value);
  157. }
  158. }
  159. public static readonly DependencyProperty SavedPositionProperty = DependencyProperty.Register(
  160. "SavedPosition", typeof(double), typeof(TransporterStationPositionControl), new FrameworkPropertyMetadata(0.00, new PropertyChangedCallback(OnCurrentPositionChanged)));
  161. /// <summary>
  162. /// 保存位置
  163. /// </summary>
  164. public double SavedPosition
  165. {
  166. get
  167. {
  168. return _savePosition;
  169. }
  170. set
  171. {
  172. _savePosition = value;
  173. SetValue(SavedPositionProperty, value);
  174. IsAtStation = JudgeAtStation(value,CurrentPosition,ToleranceDefault);
  175. InvokePropertyChanged(nameof(SavedPosition));
  176. }
  177. }
  178. public static readonly DependencyProperty ToleranceDefaultProperty = DependencyProperty.Register(
  179. "ToleranceDefault", typeof(double), typeof(TransporterStationPositionControl), new FrameworkPropertyMetadata(0.00, new PropertyChangedCallback(OnCurrentPositionChanged)));
  180. /// <summary>
  181. /// 保存位置
  182. /// </summary>
  183. public double ToleranceDefault
  184. {
  185. get
  186. {
  187. if(_axis!=null)
  188. {
  189. return _axis.ToleranceDefault;
  190. }
  191. else
  192. {
  193. return 0;
  194. }
  195. }
  196. set
  197. {
  198. SetValue(ToleranceDefaultProperty, _axis != null ? _axis.ToleranceDefault : 0);
  199. }
  200. }
  201. public static readonly DependencyProperty CurrentPositionProperty = DependencyProperty.Register(
  202. "CurrentPosition", typeof(double), typeof(TransporterStationPositionControl), new FrameworkPropertyMetadata(0.00, new PropertyChangedCallback(OnCurrentPositionChanged)));
  203. /// <summary>
  204. /// 当前位置
  205. /// </summary>
  206. public double CurrentPosition
  207. {
  208. get
  209. {
  210. return (double)this.GetValue(CurrentPositionProperty);
  211. }
  212. set
  213. {
  214. SetValue(CurrentPositionProperty, value);
  215. }
  216. }
  217. private static void OnCurrentPositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  218. {
  219. if (e.NewValue != null)
  220. {
  221. double tmpSavedPosition = (double)d.GetValue(SavedPositionProperty);
  222. double toleranceDefault = (double)d.GetValue(ToleranceDefaultProperty);
  223. bool result = JudgeAtStation(tmpSavedPosition, (double)e.NewValue, toleranceDefault);
  224. d.SetValue(IsAtStationProperty, result);
  225. }
  226. }
  227. public static readonly DependencyProperty IsMotorOnProperty = DependencyProperty.Register(
  228. "IsMotorOn", typeof(bool), typeof(TransporterStationPositionControl),
  229. new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
  230. /// <summary>
  231. /// AtStation
  232. /// </summary>
  233. public bool IsMotorOn
  234. {
  235. get
  236. {
  237. return (bool)this.GetValue(IsMotorOnProperty);
  238. }
  239. set
  240. {
  241. this.SetValue(IsMotorOnProperty, value);
  242. }
  243. }
  244. public static readonly DependencyProperty DegValueProperty = DependencyProperty.Register(
  245. "DegValue", typeof(double), typeof(TransporterStationPositionControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
  246. /// <summary>
  247. /// 当前位置
  248. /// </summary>
  249. public double DegValue
  250. {
  251. get
  252. {
  253. return (double)this.GetValue(DegValueProperty);
  254. }
  255. set
  256. {
  257. this.SetValue(DegValueProperty, value);
  258. }
  259. }
  260. public static readonly DependencyProperty CurrentStationProperty = DependencyProperty.Register("CurrentStation", typeof(string), typeof(TransporterStationPositionControl));
  261. /// <summary>
  262. /// 当前位置
  263. /// </summary>
  264. public string CurrentStation
  265. {
  266. get
  267. {
  268. return (string) this.GetValue(CurrentStationProperty);
  269. }
  270. set
  271. {
  272. this.SetValue(CurrentStationProperty, value);
  273. }
  274. }
  275. /// <summary>
  276. /// CellId与CellName字典
  277. /// </summary>
  278. private Dictionary<string, string> _cellIdCellNameDictionary = new Dictionary<string, string>();
  279. /// <summary>
  280. /// Cell名称与Cell Id字典
  281. /// </summary>
  282. private Dictionary<string, string> _cellNameCellIdDictionary = new Dictionary<string, string>();
  283. /// <summary>
  284. /// 选择Cell名称
  285. /// </summary>
  286. private string _moduleSelectedCellItem;
  287. /// <summary>
  288. /// 当前位置
  289. /// </summary>
  290. public double IncrementValue
  291. {
  292. get { return _incrementValue; }
  293. set
  294. {
  295. _incrementValue = value;
  296. InvokePropertyChanged(nameof(IncrementValue));
  297. }
  298. }
  299. public event PropertyChangedEventHandler PropertyChanged;
  300. #endregion
  301. /// <summary>
  302. /// 构造函数
  303. /// </summary>
  304. public TransporterStationPositionControl()
  305. {
  306. InitializeComponent();
  307. }
  308. private void GotoSavedPos_Click(object sender, RoutedEventArgs e)
  309. {
  310. if (ModuleName.ToLower().Contains("gantry"))
  311. {
  312. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.GantryGotoSavedPosition", "TargetPosition", _moduleSelectedCellItem);
  313. }
  314. else
  315. {
  316. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.GotoSavedPosition", "TargetPosition", _moduleSelectedCellItem);
  317. }
  318. }
  319. private void MotorPos_Click(object sender, RoutedEventArgs e)
  320. {
  321. if (ModuleName.ToLower().Contains("gantry"))
  322. {
  323. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.SaveWithModifyLayout", $"{_moduleSelectedCellItem}", Math.Round(CurrentPosition, 2));
  324. }
  325. else
  326. {
  327. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Save", $"{ModuleName}.{_moduleSelectedCellItem}", Math.Round(CurrentPosition, 2));
  328. }
  329. SavedPosition = CurrentPosition;
  330. foreach (Station item in _axis.Stations)
  331. {
  332. if (item.Name == ModuleSelectedItem)
  333. {
  334. item.Position = CurrentPosition.ToString();
  335. break;
  336. }
  337. }
  338. }
  339. private void LeftCommand_Click(object sender, RoutedEventArgs e)
  340. {
  341. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.JogDown", "TargetPosition", DegValue,CurrentPosition);
  342. }
  343. private void RightCommand_Click(object sender, RoutedEventArgs e)
  344. {
  345. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.JogUp", "TargetPosition", DegValue,CurrentPosition);
  346. }
  347. /// <summary>
  348. /// 初始化cellId
  349. /// </summary>
  350. /// <param name="items"></param>
  351. private void InitializeCellIdCellNameDictionary(List<ProcessLayoutCellItem> items)
  352. {
  353. if(items==null)
  354. {
  355. return;
  356. }
  357. foreach (ProcessLayoutCellItem item in items)
  358. {
  359. if (!string.IsNullOrEmpty(item.ModuleName))
  360. {
  361. _cellIdCellNameDictionary[$"Cell{item.CellId}"] = item.ModuleName;
  362. _cellNameCellIdDictionary[item.ModuleName] = $"Cell{item.CellId}";
  363. }
  364. }
  365. }
  366. /// <summary>
  367. /// 获取工位集合
  368. /// </summary>
  369. /// <param name="stationAxis"></param>
  370. /// <returns></returns>
  371. private List<string> GetStationList(BeckhoffStationAxis stationAxis)
  372. {
  373. List<string> lst = new List<string>();
  374. if (stationAxis == null)
  375. {
  376. return lst;
  377. }
  378. foreach (var item in stationAxis.Stations)
  379. {
  380. string[] strAry = item.Name.Split('.');
  381. string lastName = strAry[strAry.Length - 1];
  382. if (_cellIdCellNameDictionary!=null&&_cellIdCellNameDictionary.ContainsKey(lastName))
  383. {
  384. string moduleName = _cellIdCellNameDictionary[lastName];
  385. if (!string.IsNullOrEmpty(moduleName))
  386. {
  387. lst.Add(_cellIdCellNameDictionary[lastName]);
  388. }
  389. }
  390. else
  391. {
  392. if (!lastName.StartsWith("Cell"))
  393. {
  394. lst.Add(lastName);
  395. }
  396. }
  397. }
  398. return lst;
  399. }
  400. /// <summary>
  401. /// 获取工位位置
  402. /// </summary>
  403. /// <param name="stationAxis"></param>
  404. /// <param name="station"></param>
  405. /// <returns></returns>
  406. private double GetStationPosition(BeckhoffStationAxis stationAxis, string station)
  407. {
  408. if (stationAxis == null)
  409. {
  410. return 0;
  411. }
  412. foreach (var item in stationAxis.Stations)
  413. {
  414. if (item.Name.EndsWith(station))
  415. {
  416. double.TryParse(item.Position, out var position);
  417. return position;
  418. }
  419. else if (_cellNameCellIdDictionary.ContainsKey(station))
  420. {
  421. if (item.Name.EndsWith(_cellNameCellIdDictionary[station]))
  422. {
  423. double.TryParse(item.Position, out var position);
  424. return position;
  425. }
  426. }
  427. }
  428. return 0;
  429. }
  430. /// <summary>
  431. /// 转换数值
  432. /// </summary>
  433. /// <param name="value"></param>
  434. /// <returns></returns>
  435. private int ConvertValue(double value)
  436. {
  437. if(_scaleFactor!=0)
  438. {
  439. return (int)Math.Round(value * _scaleFactor,0);
  440. }
  441. else
  442. {
  443. return (int)Math.Round(value,0);
  444. }
  445. }
  446. private void InvokePropertyChanged(string propertyName)
  447. {
  448. if (PropertyChanged != null)
  449. {
  450. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  451. }
  452. }
  453. /// <summary>
  454. /// 判定AtStation
  455. /// </summary>
  456. private static bool JudgeAtStation(double saved,double current,double tolerance)
  457. {
  458. return Math.Abs(saved - current) <= tolerance;
  459. }
  460. private void IsControlVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  461. {
  462. if(e.NewValue!=null&&((bool)e.NewValue)==true)
  463. {
  464. if (!string.IsNullOrEmpty(ModuleName))
  465. {
  466. IncrementValue = (double)QueryDataClient.Instance.Service.GetConfig("System.Increment");
  467. _rtDataKeys.Add($"Station.{ModuleName}");
  468. _rtDataKeys.Add($"{ModuleName}.AxisProvider");
  469. _rtDataKeys.Add("System.Layout");
  470. _rtDataValues = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
  471. _axis = CommonFunction.GetValue<BeckhoffStationAxis>(_rtDataValues, $"Station.{ModuleName}");
  472. ProcessLayout processLayout = CommonFunction.GetValue<ProcessLayout>(_rtDataValues, "System.Layout");
  473. if (processLayout != null)
  474. {
  475. InitializeCellIdCellNameDictionary(processLayout.Items);
  476. }
  477. BeckhoffProviderAxis beckhoffProviderAxis = CommonFunction.GetValue<BeckhoffProviderAxis>(_rtDataValues, $"{ModuleName}.AxisProvider");
  478. if (_axis != null)
  479. {
  480. ToleranceDefault = _axis.ToleranceDefault;
  481. ModuleItemSource = GetStationList(_axis);
  482. if (ModuleItemSource != null && ModuleItemSource.Count != 0)
  483. {
  484. ModuleSelectedItem = ModuleItemSource[0];
  485. }
  486. }
  487. if (beckhoffProviderAxis != null)
  488. {
  489. _scaleFactor = beckhoffProviderAxis.ScaleFactor;
  490. }
  491. }
  492. }
  493. }
  494. }
  495. }