algorithmic modeling for Rhino
Hi,
I have to take an expression as a string (written in proper VB or C# syntax) and evaluate it within a component using x,y,z values. Seems much more difficult than I thought. Any idea is very much appreciated.
Say for instance I have F=Math.Sqrt(Math.Abs(x*y*z))
as a string input and I have x,y,z values
I want to let users change the F input to anything they want. The expression above is just an example.
Tags:
May I ask a question? Why do you need to write your own math parser when there is already one in GH? I'm not asking to be difficult, but because I am curious.
You don't have to write your own parser and of course you still can use the parser provided by Grasshopper:
var parser = new Grasshopper.Kernel.Expressions.GH_ExpressionParser();
parser.AddVariable("x", x);
parser.AddVariable("y", y);
parser.AddVariable("z", z);
result = parser.Evaluate(f);
Just keep in mind that the power function has this little bug.
Cheers!
Thanks for your prompt complete response!!!
Did it with a bit of optimization. In case anybody needs in future:
Dim vals(P.count-1) As Double
Dim CorrectExpression = Grasshopper.Kernel.Expressions.GH_ExpressionSyntaxWriter.RewriteAll(F)
Dim Expr As New Grasshopper.Kernel.Expressions.GH_ExpressionParser()
Expr.CacheSymbols(CorrectExpression)
Dim k As Integer
For Each v As point3d In P
Expr.ClearVariables
Expr.AddVariable("x", V.x)
Expr.AddVariable("y", V.y)
Expr.AddVariable("z", V.z)
vals(k) = Expr.Evaluate(Expr.CachedSymbols())._Double
k += 1
Next
E = Vals
Cheers, Pirouz
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by