/** * * @author seagle * @date 2024-7-22 * @Description 脚本中的自定义函数 */ using MECF.Framework.UI.Core.Control; using MECF.Framework.UI.Core.ExtendedControls; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window; using System.Windows.Input; using System.Xml.Linq; using Aitex.Core.UI.Control; using MECF.Framework.UI.Core.DxfScript; using MECF.Framework.Common.OperationCenter; using Aitex.Core.Common.DeviceData; using MECF.Framework.Common; using static MECF.Framework.UI.Core.DxfScript.Express; namespace MECF.Framework.UI.Core.DxfScript { public partial class Express { private UserFunction GetFunAddr(string name) { if (name.Equals("Debug")) return Debug; else if (name.Equals("InitVariable")) return InitVariable; else if (name.Equals("ValveTouchUp")) return ValveTouchUp; else if (name.Equals("MfcTouchUp")) return MfcTouchUp; else if (name.Equals("SwichValue")) return SwichValue; else throw new Exception("Function not found:" + name); } /** 脚本函数名:InitVariable * 功能: 初始化脚本变量,仅用于开发环境 * 参数: * 变量名:string * 表达式,表达式的结算结果的类型作为变量的类型,并把值赋给变量 * * 返回值: 无意义 */ private void InitVariable(ExpressNode curr, bool OnlyCheckDataType = false) { if (!OnlyCheckDataType) { if (curr.FirstChild == null) { throw new Exception("No arguments in InitVariable"); } else { if (curr.FirstChild.NodeType != KeywordType.KW_OPERATOR_COMMA) { throw new Exception("Not enouch arguments in InitVariable"); } if (curr.FirstChild.FirstChild == null) { throw new Exception("No variable name in InitVariable"); } if (curr.FirstChild.FirstChild.NextBrother == null) { throw new Exception("No variable type and value in InitVariable"); } ComputeNode(curr.FirstChild.FirstChild, false); string varName = curr.FirstChild.FirstChild.Var.StringValue; ComputeNode(curr.FirstChild.FirstChild.NextBrother, false); Variable var = curr.FirstChild.FirstChild.NextBrother.Var; VariableDataType dataType = curr.FirstChild.FirstChild.NextBrother.Var.DataType; Variable newVar = new Variable(varName, dataType); newVar.CopyFrom(var); if (!ScriptVariables.fakeVariables.ContainsKey(varName)) { ScriptVariables.fakeVariables.Add(varName, newVar); } else { ScriptVariables.fakeVariables[varName] = newVar; } } } } /** 脚本函数名:Debug * 功能: 打印并返回表达式的值 * 参数: * 表达式 * 返回值: 表达式的值 */ private void Debug(ExpressNode curr, bool OnlyCheckDataType = false) { if (!OnlyCheckDataType) { if (curr.FirstChild == null) { MessageBox.Show("No arguments", "Debug", MessageBoxButton.OK); } else { ComputeNode(curr.FirstChild, false); string result = null; switch (curr.FirstChild.Var.DataType) { case VariableDataType.BOOL: result = curr.FirstChild.Var.BoolValue.ToString(); break; case VariableDataType.DOUBLE: result = curr.FirstChild.Var.DoubleValue.ToString(); break; case VariableDataType.STRING: result = curr.FirstChild.Var.StringValue; break; default: result = "Unknown type"; break; } MessageBox.Show(result, "Debug", MessageBoxButton.OK); curr.Var.CopyFrom(curr.FirstChild.Var, false); } } } /** 脚本函数名:AIValveTouchUp * 功能: 设置阀的开关 * 表达式 * 返回值: 表达式的值 */ private void ValveTouchUp(ExpressNode curr, bool OnlyCheckDataType = false) { if (OnlyCheckDataType) return; var shape = (GasAITValve)GasMapProvider.GasMap.SelectedShape; Dictionary param = new Dictionary() { {"shape", shape}, }; UserFunctionsEvents.RaiseEvent("ValveTouchUp", param); } /** 脚本函数名:MfcTouchUp * 功能: 设置mfc值 * 表达式 * 返回值: 表达式的值 */ private void MfcTouchUp(ExpressNode curr, bool OnlyCheckDataType = false) { if (OnlyCheckDataType) return; var shape = (GasAnalogControl4Jet)GasMapProvider.GasMap.SelectedShape; Dictionary param = new Dictionary() { {"shape", shape}, }; //0:devicename, 1:operation, 2:args UserFunctionsEvents.RaiseEvent("MfcFlowTouchUp", param); } private void SwichValue(ExpressNode curr, bool OnlyCheckDataType = false) { if (OnlyCheckDataType) return; var parameters = GetParameter(curr); var name = parameters[0].StringValue; var showType = (int)parameters[1].DoubleValue; var shape = (GasButton)GasMapProvider.GasMap.SelectedShape; Dictionary param = new Dictionary() { {"name",name }, {"shape",shape }, {"showType",showType }, }; UserFunctionsEvents.RaiseEvent("SwichValue", param); } public enum ShowType { None = 10, Confirm = 20, Dialog = 30 } #region 私有方法 private GasBaseShape GetGasBaseShapeById(string id) { return GasMapProvider.GasMap.FindShapeById(int.Parse(id)); } private List GetParameter(ExpressNode node) { return ParseVariables(node); } public List ParseVariables(ExpressNode root) { List variables = new List(); ParseNode(root, variables); return variables; } private void ParseNode(ExpressNode node, List variables) { if (node == null) return; // 如果当前节点是变量,将其添加到列表中 if (node.NodeType == KeywordType.KW_VARIABLE && node.Var != null) { variables.Add(node.Var); } // 递归解析子节点 if (node.FirstChild != null) { ParseNode(node.FirstChild, variables); } // 递归解析兄弟节点 if (node.NextBrother != null) { ParseNode(node.NextBrother, variables); } } #endregion } }