UserFunctions.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. *
  3. * @author seagle
  4. * @date 2024-7-22
  5. * @Description 脚本中的自定义函数
  6. */
  7. using MECF.Framework.UI.Core.Control;
  8. using MECF.Framework.UI.Core.ExtendedControls;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
  17. using System.Windows.Input;
  18. using System.Xml.Linq;
  19. using Aitex.Core.UI.Control;
  20. using MECF.Framework.UI.Core.DxfScript;
  21. using MECF.Framework.Common.OperationCenter;
  22. using Aitex.Core.Common.DeviceData;
  23. using MECF.Framework.Common;
  24. using static MECF.Framework.UI.Core.DxfScript.Express;
  25. namespace MECF.Framework.UI.Core.DxfScript
  26. {
  27. public partial class Express
  28. {
  29. private UserFunction GetFunAddr(string name)
  30. {
  31. if (name.Equals("Debug")) return Debug;
  32. else if (name.Equals("InitVariable")) return InitVariable;
  33. else if (name.Equals("ValveTouchUp")) return ValveTouchUp;
  34. else if (name.Equals("MfcTouchUp")) return MfcTouchUp;
  35. else if (name.Equals("SwichValue")) return SwichValue;
  36. else throw new Exception("Function not found:" + name);
  37. }
  38. /** 脚本函数名:InitVariable
  39. * 功能: 初始化脚本变量,仅用于开发环境
  40. * 参数:
  41. * 变量名:string
  42. * 表达式,表达式的结算结果的类型作为变量的类型,并把值赋给变量
  43. *
  44. * 返回值: 无意义
  45. */
  46. private void InitVariable(ExpressNode curr, bool OnlyCheckDataType = false)
  47. {
  48. if (!OnlyCheckDataType)
  49. {
  50. if (curr.FirstChild == null)
  51. {
  52. throw new Exception("No arguments in InitVariable");
  53. }
  54. else
  55. {
  56. if (curr.FirstChild.NodeType != KeywordType.KW_OPERATOR_COMMA)
  57. {
  58. throw new Exception("Not enouch arguments in InitVariable");
  59. }
  60. if (curr.FirstChild.FirstChild == null)
  61. {
  62. throw new Exception("No variable name in InitVariable");
  63. }
  64. if (curr.FirstChild.FirstChild.NextBrother == null)
  65. {
  66. throw new Exception("No variable type and value in InitVariable");
  67. }
  68. ComputeNode(curr.FirstChild.FirstChild, false);
  69. string varName = curr.FirstChild.FirstChild.Var.StringValue;
  70. ComputeNode(curr.FirstChild.FirstChild.NextBrother, false);
  71. Variable var = curr.FirstChild.FirstChild.NextBrother.Var;
  72. VariableDataType dataType = curr.FirstChild.FirstChild.NextBrother.Var.DataType;
  73. Variable newVar = new Variable(varName, dataType);
  74. newVar.CopyFrom(var);
  75. if (!ScriptVariables.fakeVariables.ContainsKey(varName))
  76. {
  77. ScriptVariables.fakeVariables.Add(varName, newVar);
  78. }
  79. else
  80. {
  81. ScriptVariables.fakeVariables[varName] = newVar;
  82. }
  83. }
  84. }
  85. }
  86. /** 脚本函数名:Debug
  87. * 功能: 打印并返回表达式的值
  88. * 参数:
  89. * 表达式
  90. * 返回值: 表达式的值
  91. */
  92. private void Debug(ExpressNode curr, bool OnlyCheckDataType = false)
  93. {
  94. if (!OnlyCheckDataType)
  95. {
  96. if (curr.FirstChild == null)
  97. {
  98. MessageBox.Show("No arguments", "Debug", MessageBoxButton.OK);
  99. }
  100. else
  101. {
  102. ComputeNode(curr.FirstChild, false);
  103. string result = null;
  104. switch (curr.FirstChild.Var.DataType)
  105. {
  106. case VariableDataType.BOOL:
  107. result = curr.FirstChild.Var.BoolValue.ToString();
  108. break;
  109. case VariableDataType.DOUBLE:
  110. result = curr.FirstChild.Var.DoubleValue.ToString();
  111. break;
  112. case VariableDataType.STRING:
  113. result = curr.FirstChild.Var.StringValue;
  114. break;
  115. default:
  116. result = "Unknown type";
  117. break;
  118. }
  119. MessageBox.Show(result, "Debug", MessageBoxButton.OK);
  120. curr.Var.CopyFrom(curr.FirstChild.Var, false);
  121. }
  122. }
  123. }
  124. /** 脚本函数名:AIValveTouchUp
  125. * 功能: 设置阀的开关
  126. * 表达式
  127. * 返回值: 表达式的值
  128. */
  129. private void ValveTouchUp(ExpressNode curr, bool OnlyCheckDataType = false)
  130. {
  131. if (OnlyCheckDataType) return;
  132. var shape = (GasAITValve)GasMapProvider.GasMap.SelectedShape;
  133. Dictionary<string, object> param = new Dictionary<string, object>()
  134. {
  135. {"shape", shape},
  136. };
  137. UserFunctionsEvents.RaiseEvent("ValveTouchUp", param);
  138. }
  139. /** 脚本函数名:MfcTouchUp
  140. * 功能: 设置mfc值
  141. * 表达式
  142. * 返回值: 表达式的值
  143. */
  144. private void MfcTouchUp(ExpressNode curr, bool OnlyCheckDataType = false)
  145. {
  146. if (OnlyCheckDataType) return;
  147. var shape = (GasAnalogControl4Jet)GasMapProvider.GasMap.SelectedShape;
  148. Dictionary<string, object> param = new Dictionary<string, object>()
  149. {
  150. {"shape", shape},
  151. };
  152. //0:devicename, 1:operation, 2:args
  153. UserFunctionsEvents.RaiseEvent("MfcFlowTouchUp", param);
  154. }
  155. private void SwichValue(ExpressNode curr, bool OnlyCheckDataType = false)
  156. {
  157. if (OnlyCheckDataType) return;
  158. var parameters = GetParameter(curr);
  159. var name = parameters[0].StringValue;
  160. var showType = (int)parameters[1].DoubleValue;
  161. var shape = (GasButton)GasMapProvider.GasMap.SelectedShape;
  162. Dictionary<string, object> param = new Dictionary<string, object>()
  163. {
  164. {"name",name },
  165. {"shape",shape },
  166. {"showType",showType },
  167. };
  168. UserFunctionsEvents.RaiseEvent("SwichValue", param);
  169. }
  170. public enum ShowType
  171. {
  172. None = 10,
  173. Confirm = 20,
  174. Dialog = 30
  175. }
  176. #region 私有方法
  177. private GasBaseShape GetGasBaseShapeById(string id)
  178. {
  179. return GasMapProvider.GasMap.FindShapeById(int.Parse(id));
  180. }
  181. private List<Variable> GetParameter(ExpressNode node)
  182. {
  183. return ParseVariables(node);
  184. }
  185. public List<Variable> ParseVariables(ExpressNode root)
  186. {
  187. List<Variable> variables = new List<Variable>();
  188. ParseNode(root, variables);
  189. return variables;
  190. }
  191. private void ParseNode(ExpressNode node, List<Variable> variables)
  192. {
  193. if (node == null) return;
  194. // 如果当前节点是变量,将其添加到列表中
  195. if (node.NodeType == KeywordType.KW_VARIABLE && node.Var != null)
  196. {
  197. variables.Add(node.Var);
  198. }
  199. // 递归解析子节点
  200. if (node.FirstChild != null)
  201. {
  202. ParseNode(node.FirstChild, variables);
  203. }
  204. // 递归解析兄弟节点
  205. if (node.NextBrother != null)
  206. {
  207. ParseNode(node.NextBrother, variables);
  208. }
  209. }
  210. #endregion
  211. }
  212. }