I always use JSON to save data, really simple to serialize and deserialize a c# object.
Revit already uses the Newtonsoft.Json package by default.
Here is an example: https://github.com/ricaun/RevitAddin.JsonExample.
@jeremy_tammik wrote:
Since the Revit API is .NET based, and .NET provides powerful scripting support, why not make use of that?
For instance, search the Internet for '.net script':
Interesting but I don't think is a good idea to give the user the ability to run any C# code, looks dangerous.
This ExpressionParser looks that gonna work.
public class ExpressionParser
{
/// <summary>
/// eval
/// Source: https://stackoverflow.com/questions/3972854/parse-math-expression
/// </summary>
/// <param name="exp"></param>
/// <param name="vars"></param>
/// <returns></returns>
public double eval(string exp, Dictionary<string, double> vars)
{
int bracketCounter = 0;
int operatorIndex = -1;
exp = exp.Trim();
for (int i = 0; i < exp.Length; i++)
{
char c = exp[i];
if (c == '(') bracketCounter++;
else if (c == ')') bracketCounter--;
else if ((c == '+' || c == '-') && bracketCounter == 0)
{
operatorIndex = i;
break;
}
else if ((c == '*' || c == '/') && bracketCounter == 0 && operatorIndex < 0)
{
operatorIndex = i;
}
}
if (operatorIndex < 0)
{
exp = exp.Trim();
if (exp[0] == '(' && exp[exp.Length - 1] == ')')
return eval(exp.Substring(1, exp.Length - 1), vars);
else if (vars.ContainsKey(exp))
return vars[exp];
else
return double.Parse(exp);
}
else
{
switch (exp[operatorIndex])
{
case '+':
return eval(exp.Substring(0, operatorIndex), vars) + eval(exp.Substring(operatorIndex + 1), vars);
case '-':
return eval(exp.Substring(0, operatorIndex), vars) - eval(exp.Substring(operatorIndex + 1), vars);
case '*':
return eval(exp.Substring(0, operatorIndex), vars) * eval(exp.Substring(operatorIndex + 1), vars);
case '/':
return eval(exp.Substring(0, operatorIndex), vars) / eval(exp.Substring(operatorIndex + 1), vars);
}
}
return 0;
}
}
With this simple code to test!
var expressionParser = new ExpressionParser();
var vars = new Dictionary<string, double>();
vars.Add("AREA", 10.0);
var area = expressionParser.eval("AREA * AREA", vars);
System.Windows.MessageBox.Show($"AREA {area}");
See yaa!