MotionAxisNoConverter.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Globalization;
  5. using System.Linq;
  6. using Plugins.Motion.Models;
  7. using TeamAAS.Core;
  8. using TeamAAS.FlowEngine;
  9. using TeamAAS.Motion.Models;
  10. namespace Plugins.Motion.Core
  11. {
  12. /// <summary>
  13. /// 流程属性面板「轴」下拉:选项来自当前节点已选运动卡的轴配置。
  14. /// </summary>
  15. public class MotionAxisNoConverter : StringConverter
  16. {
  17. public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
  18. public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => false;
  19. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  20. {
  21. var labels = GetAxes(GetModel(context))
  22. .Select(a => a.Label)
  23. .ToList();
  24. // PropertyGridLib 在 Count==0 时不会把属性渲染成下拉;未选卡时给占位项,选卡后由面板刷新/展开时重取。
  25. if (labels.Count == 0)
  26. labels.Add("请先选择运动卡");
  27. return new StandardValuesCollection(labels);
  28. }
  29. internal static MotionAxisModel GetModel(ITypeDescriptorContext context)
  30. {
  31. if (context?.Instance is MotionAxisModel m) return m;
  32. try
  33. {
  34. return PluginLoader.Instance.SelectFlow?.PluginModel?.GetModel as MotionAxisModel;
  35. }
  36. catch
  37. {
  38. return null;
  39. }
  40. }
  41. internal static bool AllowAllAxes(MotionAxisModel model) =>
  42. model is MotionEnableModel || model is MotionDisableModel || model is MotionStopModel;
  43. internal static List<(int No, string Label)> GetAxes(MotionAxisModel model)
  44. {
  45. var list = new List<(int, string)>();
  46. bool allowAll = AllowAllAxes(model);
  47. if (allowAll)
  48. list.Add((-1, "-1 全部轴"));
  49. string cardName = model?.MotionCardName;
  50. if (string.IsNullOrWhiteSpace(cardName))
  51. return list;
  52. try
  53. {
  54. var mgr = CoreManager.Motion;
  55. if (mgr == null) return list;
  56. var cfg = mgr.Configs?.FirstOrDefault(c =>
  57. string.Equals(c.Name, cardName, StringComparison.OrdinalIgnoreCase));
  58. if (cfg != null)
  59. {
  60. foreach (var a in MotionAxisCatalog.GetEffective(cfg))
  61. {
  62. if (a == null) continue;
  63. list.Add((a.AxisNo, FormatAxis(a.AxisNo, a.Name, a.KindText)));
  64. }
  65. return list;
  66. }
  67. var card = mgr.GetMotion(cardName);
  68. if (card == null) return list;
  69. var axes = card.GetAxes();
  70. if (axes != null && axes.Count > 0)
  71. {
  72. foreach (var a in axes.OrderBy(x => x.AxisNo))
  73. list.Add((a.AxisNo, FormatAxis(a.AxisNo, a.Name, null)));
  74. return list;
  75. }
  76. for (int i = 0; i < Math.Max(0, card.AxisCount); i++)
  77. list.Add((i, FormatAxis(i, $"Axis-{i}", null)));
  78. }
  79. catch { }
  80. return list;
  81. }
  82. internal static string Format(int axisNo, MotionAxisModel model)
  83. {
  84. foreach (var a in GetAxes(model))
  85. {
  86. if (a.No == axisNo) return a.Label;
  87. }
  88. return axisNo.ToString(CultureInfo.InvariantCulture);
  89. }
  90. internal static int Parse(string text, MotionAxisModel model)
  91. {
  92. if (string.IsNullOrWhiteSpace(text)) return 0;
  93. text = text.Trim();
  94. if (text == "请先选择运动卡") return 0;
  95. var axes = GetAxes(model);
  96. foreach (var a in axes)
  97. {
  98. if (string.Equals(a.Label, text, StringComparison.OrdinalIgnoreCase))
  99. return a.No;
  100. }
  101. int space = text.IndexOf(' ');
  102. string head = space > 0 ? text.Substring(0, space).Trim() : text;
  103. if (int.TryParse(head, NumberStyles.Integer, CultureInfo.InvariantCulture, out int byNo) ||
  104. int.TryParse(head, NumberStyles.Integer, CultureInfo.CurrentCulture, out byNo))
  105. return byNo;
  106. foreach (var a in axes)
  107. {
  108. var name = AxisNameOf(a.Label);
  109. if (!string.IsNullOrEmpty(name) &&
  110. string.Equals(name, text, StringComparison.OrdinalIgnoreCase))
  111. return a.No;
  112. }
  113. return 0;
  114. }
  115. private static string FormatAxis(int no, string name, string kindText)
  116. {
  117. string n = string.IsNullOrWhiteSpace(name) ? $"Axis-{no}" : name.Trim();
  118. if (string.IsNullOrWhiteSpace(kindText) || kindText == "实轴")
  119. return $"{no} {n}";
  120. return $"{no} {n} {kindText}";
  121. }
  122. private static string AxisNameOf(string label)
  123. {
  124. if (string.IsNullOrWhiteSpace(label)) return "";
  125. var parts = label.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  126. return parts.Length >= 2 ? parts[1] : "";
  127. }
  128. }
  129. }