123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849 |
- /**
- *
- * @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");
- }
- }
-
- }
- }
|