PufStationPositionControl.xaml.cs 15 KB

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