Grasshopper

algorithmic modeling for Rhino

Hi guys, 

I am new to scripts in grasshopper. 

I have a component and I'd like to input an expression (just like the expression editor does), what should I put as a type hint? 

Thank you

Views: 779

Replies to This Discussion

String

Ow, but it would work for formulas (like x+y+z)? 

There is no expression type, so you get a string then you create an expression from that string.

private void RunScript(string f, double x, double y, double z, ref object A)
{
  Grasshopper.Kernel.Expressions.GH_ExpressionParser parser =
    new Grasshopper.Kernel.Expressions.GH_ExpressionParser();
  parser.AddVariable("x", x);
  parser.AddVariable("y", y);
  parser.AddVariable("z", z);
  parser.CacheSymbols(f);
  Grasshopper.Kernel.Expressions.GH_Variant result = parser.Evaluate();
  A = result.ToGoo();
}

Attachments:

You can also use the Evaluate() overload which takes a string, caching symbols only makes sense if you intend to run the expression more than once.

Thanks a lot for your help

I am trying to integrate this into my program, but when I try to convert the final result to a double, it gives me the following error: 

'' error: Unable to cast object of type 'Grasshopper.Kernel.Types.GH_Number' to type 'System.IConvertible'. (line: 0)''

Do you know where that might be coming from? 

Thank you

ToGoo() returns a type of IGH_Goo, which (if the result of the expression is a double) will be GH_Number. There's no build in conversion from GH_Number to double, but you can just get the double out of the GH_Variant directly. There's no need to create an expensive GH_Number if you're not going to use it.

Ok ok, I seen, but it still cannot convert from variant to double: 

"0. error: Unable to cast object of type 'Grasshopper.Kernel.Expressions.GH_Variant' to type 'System.IConvertible'. (line: 0)"

Well ... here's a very clumsy way to cast some stuff (this def is just a demo related with another case). Null breps  ... well ... GH failed to memorize them (as usual).

Attachments:

Thanks, I just used ._double, I think it worked. 

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service