LoaderStationPositionControl.xaml.cs 16 KB

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