Roslyn script for Grasshopper

unhandled

(jmv) #1

Hello !

I’m writing a component to run a csv file inside Grasshopper and debug it.

I would like convert .NET types (bool, int, Geometry.Mesh, …) to IGH_TypeHint. (The reverse of IGH_TypeHint.Cast)

Does it exist?

nb: This component does not use user interaction to add / remove parameters.
but uses custom attributes in the source file to define the inputs and outputs

[Input ("P", "description")]
Plane plane = plane.IsValid ? plane : Plane.WorldXY;

Thank you.
jmv.


(David Rutten) #2

I presume a *.cs file?

You mean if you’re given some System.Object or System.Type, you want to construct one of the standard IGH_TypeHint implementations?


(jmv) #3

Hello David!

no I was wrong, i would like write .csx
(I updated the readme in the repository to explain this)

Yes !
Currently I’m doing something not terrible with a switch:

And that does not work with extended types.

Do you have a good method?

Thank you !
jmv.


(David Rutten) #4

Not really a better way. There’s a limited (and unchanging) set of CS typehints defined in Grasshopper, so you could just populate a dictionary once and then use it to create the correct typehint.

private static readonly Dictionary<Type, IGH_TypeHint> _dictionary = CreateDictionary();
private static Dictionary<Type, IGH_TypeHint> CreateDictionary()
{
  return new Dictionary<Type, IGH_TypeHint>
  {
    {typeof(bool), new GH_BooleanHint_CS()},
    {typeof(int), new GH_IntegerHint_CS()},
    {typeof(double), new GH_DoubleHint_CS()},
    {typeof(string), new GH_StringHint_CS()},
    {typeof(Brep), new GH_BrepHint()}
  };
}

(jmv) #5

thank you for your reply !

I think about that. But I have a problem when an input or an output in the script is defined with an extended class, like class MyMesh : Mesh { ... }

Ok, This will end with Type.IsInstanceOfType

Thank you,
jmv


(David Rutten) #6

You should be able to perform those casts using the as keyword in C#.

public static bool IsATypeOfMesh(object data, out Mesh mesh)
{
  mesh = data as Mesh;
  return mesh != null;
}

(jmv) #7

oh! Effectively !! Why is not I thinking !!

thank you !


(jmv) #8

I know ! because I have no value at this point, just a few FieldInfo …


(David Rutten) #9

Yup, next stop, Reflection-city.


(jmv) #10

Hello @DavidRutten !

I need your help again :innocent:

I implemented standard type conversions and add basic support for access GH_Param Access.list.
I think now I’m good with conversions.

But I don’t understand where exactly I must to do it?
currently, the conversation is done in the SolveInstance method.
with this:

but if the logic of the inputs is:
all A entries for each B entry.
I recast each time the entries A.

I thought use the OnVolatileDataCollected method, but I’m losing the logic of IGH_DataAccess.

is there a way for don’t recast the datas already passed ?

thank you !
jmv.


(jmv) #12

maybe I should create a GH_Goo class with a cache field, fill it during OnVolatileDataCollected and extract the data in SolveInstance.

But this way will double the memory