/** * * @author seagle * @date 2024-7-10 * @Description 处理变量的相关计算 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MECF.Framework.UI.Core.DxfScript { public enum VariableDataType{ DOUBLE,STRING,BOOL } public class Variable { private string _stringValue; public string StringValue { get { if (Name != null && !Name.Equals("")) { //刷新io值 _stringValue = ScriptVariables.GetStringByName(Name); IsNone = false; } return _stringValue; } set { if (DataType != VariableDataType.STRING) { throw new Exception("Variable " + Name + " DataType is not STRING"); } _stringValue = value; if (Name != null && !Name.Equals("")) { //刷新io值 ScriptVariables.SetStringByName(Name, _stringValue); } IsNone = false; } } private double _doubleValue; public double DoubleValue { get { if (Name != null && !Name.Equals("")) { //刷新io值 _doubleValue = ScriptVariables.GetDoubleByName(Name); IsNone = false; } return _doubleValue; } set { if (DataType != VariableDataType.DOUBLE) { throw new Exception("Variable " + Name + " DataType is not DOUBLE"); } _doubleValue = value; if (Name != null && !Name.Equals("")) { //刷新io值 ScriptVariables.SetDoubleByName(Name, _doubleValue); } IsNone = false; } } public bool _boolValue; public bool BoolValue { get { if (Name != null && !Name.Equals("")) { //刷新io值 _boolValue = ScriptVariables.GetBoolByName(Name); IsNone = false; } return _boolValue; } set { if (DataType != VariableDataType.BOOL) { throw new Exception("Variable " + Name + "DataType is not BOOL"); } _boolValue = value; if (Name != null && !Name.Equals("")) { ScriptVariables.SetBoolByName(Name, _boolValue); //刷新io值 } IsNone = false; } } public string Name { get; set; } public bool IsNone { get; set;} public VariableDataType DataType { get; set; } public Variable(string name, VariableDataType dataType) { Name = name; DataType = dataType; if (name == null || name.Equals("")) { IsNone = true; } else { //有名字的变量都来自外部,有值 IsNone = false; } } //从参数变量中复制内容 public void CopyFrom(Variable v,bool OnlyCheckDataType=false) { if (v == null ) { throw new Exception("Variable " + Name + " can not copy from null"); } if(v.IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + v.Name + " is none and can not copy"); } if (Name == null || Name.Equals("")) { //无Name的变量可以认为是临时变量,可以随意复制 DataType = v.DataType; if (OnlyCheckDataType) return; } if (DataType != v.DataType) { throw new Exception("Variable " + Name + "'s DataType is not matched with " + v.Name + "'s DataType"); } switch (DataType) { case VariableDataType.BOOL: BoolValue = v.BoolValue; break; case VariableDataType.DOUBLE: DoubleValue = v.DoubleValue; break; case VariableDataType.STRING: StringValue = v.StringValue; break; } IsNone = false; } //逗号连接参数 public void Comma(Variable other, bool onlyCheckDataType) { if (onlyCheckDataType) { // 如果只检查数据类型,可以在这里实现相应的逻辑 return; } // 逗号拼接逻辑:将当前值与其他值拼接成字符串 if (this.StringValue == null) { this.StringValue = other.StringValue?.ToString(); } else { this.StringValue = this.StringValue.ToString() + "," + other.StringValue?.ToString(); } } //与参数变量相加 public void Plus(Variable v, bool OnlyCheckDataType = false) { if (v == null) { throw new Exception("Variable " + Name + " can not plus from null"); } if (v.IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + v.Name + " is none and can not plus"); } if (IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + Name + " is none and can not plus others"); } if (DataType != v.DataType) { throw new Exception("Variable " + Name + "'s DataType is not matched with " + v.Name + "'s DataType"); } if (OnlyCheckDataType) { if(DataType== VariableDataType.DOUBLE || DataType == VariableDataType.STRING) { return; } else { throw new Exception("Variable " + Name + "'s DataType is not supported to plus"); } } switch (DataType) { case VariableDataType.DOUBLE: DoubleValue += v.DoubleValue; break; case VariableDataType.STRING: StringValue += v.StringValue; break; default: throw new Exception("Variable " + Name + "'s DataType is not supported to plus"); } } //与参数变量相减:当前-参数 public void Minus(Variable v, bool OnlyCheckDataType = false) { if (v == null) { throw new Exception("Variable " + Name + " can not minus from null"); } if (v.IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + v.Name + " is none and can not minus"); } if (IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + Name + " is none and can not minus others"); } if (DataType != v.DataType) { throw new Exception("Variable " + Name + "'s DataType is not matched with " + v.Name + "'s DataType"); } if (OnlyCheckDataType) { if (DataType == VariableDataType.DOUBLE) { return; } else { throw new Exception("Variable " + Name + "'s DataType is not supported to minus"); } } switch (DataType) { case VariableDataType.DOUBLE: DoubleValue -= v.DoubleValue; break; default: throw new Exception("Variable " + Name + "'s DataType is not supported to minus"); } } //与参数变量相乘 public void Multiple(Variable v, bool OnlyCheckDataType = false) { if (v == null) { throw new Exception("Variable " + Name + " can not multiple from null"); } if (v.IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + v.Name + " is none and can not multiple"); } if (IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + Name + " is none and can not multiple others"); } if (DataType != v.DataType) { throw new Exception("Variable " + Name + "'s DataType is not matched with " + v.Name + "'s DataType"); } if (OnlyCheckDataType) { if(DataType== VariableDataType.DOUBLE ) { return; } else { throw new Exception("Variable " + Name + "'s DataType is not supported to multiple"); } } switch (DataType) { case VariableDataType.DOUBLE: DoubleValue *= v.DoubleValue; break; default: throw new Exception("Variable " + Name + "'s DataType is not supported to multiple"); } } //与参数变量相除 public void Devide(Variable v, bool OnlyCheckDataType = false) { if (v == null) { throw new Exception("Variable " + Name + " can not devide from null"); } if (v.IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + v.Name + " is none and can not devide"); } if (IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + Name + " is none and can not devide others"); } if (DataType != v.DataType) { throw new Exception("Variable " + Name + "'s DataType is not matched with " + v.Name + "'s DataType"); } if (OnlyCheckDataType) { if (DataType == VariableDataType.DOUBLE ) { return; } else { throw new Exception("Variable " + Name + "'s DataType is not supported to devide"); } } switch (DataType) { case VariableDataType.DOUBLE: DoubleValue /= v.DoubleValue; break; default: throw new Exception("Variable " + Name + "'s DataType is not supported to devide"); } } //与参数变量相与 public void And(Variable v, bool OnlyCheckDataType = false) { if (v == null) { throw new Exception("Variable " + Name + " can not and from null"); } if (v.IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + v.Name + " is none and can not and"); } if (IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + Name + " is none and can not and others"); } if (DataType != v.DataType) { throw new Exception("Variable " + Name + "'s DataType is not matched with " + v.Name + "'s DataType"); } if (OnlyCheckDataType) { if (DataType == VariableDataType.BOOL) { return; } else { throw new Exception("Variable " + Name + "'s DataType is not supported to and"); } } switch (DataType) { case VariableDataType.BOOL: BoolValue = BoolValue && v.BoolValue; break; default: throw new Exception("Variable " + Name + "'s DataType is not supported to and"); } } //与参数变量相或 public void Or(Variable v, bool OnlyCheckDataType = false) { if (v == null) { throw new Exception("Variable " + Name + " can not OR from null"); } if (v.IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + v.Name + " is none and can not OR"); } if (IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + Name + " is none and can not OR others"); } if (DataType != v.DataType) { throw new Exception("Variable " + Name + "'s DataType is not matched with " + v.Name + "'s DataType"); } if (OnlyCheckDataType) { if (DataType == VariableDataType.BOOL) { return; } else { throw new Exception("Variable " + Name + "'s DataType is not supported to OR"); } } switch (DataType) { case VariableDataType.BOOL: BoolValue = BoolValue || v.BoolValue; break; default: throw new Exception("Variable " + Name + "'s DataType is not supported to OR"); } } //单目求非 public void Not( bool OnlyCheckDataType = false) { if (IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + Name + " is none and can not NOT others"); } if (OnlyCheckDataType) { if (DataType == VariableDataType.BOOL) { return; } else { throw new Exception("Variable " + Name + "'s DataType is not supported to NOT"); } } switch (DataType) { case VariableDataType.BOOL: BoolValue = !BoolValue; break; default: throw new Exception("Variable " + Name + "'s DataType is not supported to NOT"); } } //与参数变量相等判断 public void Equal(Variable v, bool OnlyCheckDataType = false) { if (v == null) { throw new Exception("Variable " + Name + " can not equal from null"); } if (v.IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + v.Name + " is none and can not equal"); } if (IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + Name + " is none and can not equal others"); } if (DataType != v.DataType) { throw new Exception("Variable " + Name + "'s DataType is not matched with " + v.Name + "'s DataType"); } if (OnlyCheckDataType) { return; } switch (DataType) { case VariableDataType.DOUBLE: DataType = VariableDataType.BOOL; BoolValue = Math.Abs(DoubleValue - v.DoubleValue) <= 0.0000001; break; case VariableDataType.STRING: DataType = VariableDataType.BOOL; if (StringValue == null || v.StringValue == null) { BoolValue = false; } else { BoolValue = StringValue.Equals(v.StringValue); } break; case VariableDataType.BOOL: BoolValue = BoolValue == v.BoolValue; break; default: throw new Exception("Variable " + Name + "'s DataType is not supported to equal"); } } //与参数变量不等判断 public void NotEqual(Variable v, bool OnlyCheckDataType = false) { if (v == null) { throw new Exception("Variable " + Name + " can not not_equal from null"); } if (v.IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + v.Name + " is none and can not not_equal"); } if (IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + Name + " is none and can not not_equal others"); } if (DataType != v.DataType) { throw new Exception("Variable " + Name + "'s DataType is not matched with " + v.Name + "'s DataType"); } if (OnlyCheckDataType) { return; } switch (DataType) { case VariableDataType.DOUBLE: DataType = VariableDataType.BOOL; BoolValue = Math.Abs(DoubleValue - v.DoubleValue) > 0.0000001; break; case VariableDataType.STRING: DataType = VariableDataType.BOOL; if (StringValue == null || v.StringValue == null) { BoolValue = true; } else { BoolValue = !StringValue.Equals(v.StringValue); } break; case VariableDataType.BOOL: BoolValue = BoolValue != v.BoolValue; break; default: throw new Exception("Variable " + Name + "'s DataType is not supported to not_equal"); } } //与参数变量做大于 public void Gt(Variable v, bool OnlyCheckDataType = false) { if (v == null) { throw new Exception("Variable " + Name + " can not GT from null"); } if (v.IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + v.Name + " is none and can not GT"); } if (IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + Name + " is none and can not GT others"); } if (DataType != v.DataType) { throw new Exception("Variable " + Name + "'s DataType is not matched with " + v.Name + "'s DataType"); } if (OnlyCheckDataType) { if(DataType== VariableDataType.DOUBLE ||DataType == VariableDataType.STRING) { return; } else { throw new Exception("Variable " + Name + "'s DataType is not supported to GT"); } } switch (DataType) { case VariableDataType.DOUBLE: DataType = VariableDataType.BOOL; BoolValue = DoubleValue - v.DoubleValue> 0.0000001; break; case VariableDataType.STRING: DataType = VariableDataType.BOOL; if (StringValue == null || v.StringValue == null) { BoolValue = false; } else { BoolValue = StringValue.CompareTo(v.StringValue) > 0; } break; default: throw new Exception("Variable " + Name + "'s DataType is not supported to GT"); } } //与参数变量做大于等于 public void Ge(Variable v, bool OnlyCheckDataType = false) { if (v == null) { throw new Exception("Variable " + Name + " can not GE from null"); } if (v.IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + v.Name + " is none and can not GE"); } if (IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + Name + " is none and can not GE others"); } if (DataType != v.DataType) { throw new Exception("Variable " + Name + "'s DataType is not matched with " + v.Name + "'s DataType"); } if (OnlyCheckDataType) { if (DataType == VariableDataType.DOUBLE || DataType == VariableDataType.STRING) { return; } else { throw new Exception("Variable " + Name + "'s DataType is not supported to GE"); } } switch (DataType) { case VariableDataType.DOUBLE: DataType = VariableDataType.BOOL; BoolValue = DoubleValue - v.DoubleValue >= -0.0000001; break; case VariableDataType.STRING: DataType = VariableDataType.BOOL; if (StringValue == null || v.StringValue == null) { BoolValue = false; } else { BoolValue = StringValue.CompareTo(v.StringValue) >= 0; } break; default: throw new Exception("Variable " + Name + "'s DataType is not supported to GE"); } } //与参数变量做小于 public void Lt(Variable v, bool OnlyCheckDataType = false) { if (v == null) { throw new Exception("Variable " + Name + " can not LT from null"); } if (v.IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + v.Name + " is none and can not LT"); } if (IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + Name + " is none and can not lT others"); } if (DataType != v.DataType) { throw new Exception("Variable " + Name + "'s DataType is not matched with " + v.Name + "'s DataType"); } if (OnlyCheckDataType) { if (DataType == VariableDataType.DOUBLE || DataType == VariableDataType.STRING) { return; } else { throw new Exception("Variable " + Name + "'s DataType is not supported to LT"); } } switch (DataType) { case VariableDataType.DOUBLE: DataType = VariableDataType.BOOL; BoolValue = DoubleValue - v.DoubleValue <- 0.0000001; break; case VariableDataType.STRING: DataType = VariableDataType.BOOL; if (StringValue == null || v.StringValue == null) { BoolValue = false; } else { BoolValue = StringValue.CompareTo(v.StringValue) < 0; } break; default: throw new Exception("Variable " + Name + "'s DataType is not supported to LT"); } } //与参数变量做小于等于 public void Le(Variable v, bool OnlyCheckDataType = false) { if (v == null) { throw new Exception("Variable " + Name + " can not LE from null"); } if (v.IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + v.Name + " is none and can not LE"); } if (IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + Name + " is none and can not LE others"); } if (DataType != v.DataType) { throw new Exception("Variable " + Name + "'s DataType is not matched with " + v.Name + "'s DataType"); } if (OnlyCheckDataType) { if (DataType == VariableDataType.DOUBLE || DataType == VariableDataType.STRING) { return; } else { throw new Exception("Variable " + Name + "'s DataType is not supported to LE"); } } switch (DataType) { case VariableDataType.DOUBLE: DataType = VariableDataType.BOOL; BoolValue = DoubleValue - v.DoubleValue <= 0.0000001; break; case VariableDataType.STRING: DataType = VariableDataType.BOOL; if (StringValue == null || v.StringValue == null) { BoolValue = false; } else { BoolValue = StringValue.CompareTo(v.StringValue) <= 0; } break; default: throw new Exception("Variable " + Name + "'s DataType is not supported to LE"); } } //单目取负 public void Nigtive(bool OnlyCheckDataType = false) { if (IsNone && !OnlyCheckDataType) { throw new Exception("Variable " + Name + " is none and can not Nigtive"); } if (OnlyCheckDataType) { if (DataType == VariableDataType.DOUBLE) { return; } else { throw new Exception("Variable " + Name + "'s DataType is not supported to Nigtive"); } } switch (DataType) { case VariableDataType.DOUBLE: DoubleValue = -DoubleValue ; break; default: throw new Exception("Variable " + Name + "'s DataType is not supported to Negtive"); } } } }