PufStationPositionControl.xaml.cs 15 KB

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