kitjmv
(jmv)
February 10, 2019, 11:34pm
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.
DavidRutten
(David Rutten)
February 11, 2019, 12:09pm
2
kitjmv:
[…] run a csv file […]
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?
kitjmv
(jmv)
February 11, 2019, 1:02pm
3
Hello David!
DavidRutten:
I presume a *.cs
file?
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:
IGH_Param UpdateParam(Param_ScriptVariable param, FieldInfo field, IOAttribute attr)
{
param.Optional = true;
param.Access = GH_ParamAccess.item;
param.Name = field.Name;
param.NickName = attr.NickName;
param.Description = attr.Description;
param.ShowHints = true;
//TODO: Hints
switch (field.FieldType)
{
case var t when t == typeof(bool):
param.TypeHint = new GH_BooleanHint_CS ();
break;
case var t when t == typeof(int):
param.TypeHint = new GH_IntegerHint_CS();
break;
case var t when t == typeof(double):
param.TypeHint = new GH_DoubleHint_CS();
break;
And that does not work with extended types.
Do you have a good method?
Thank you !
jmv.
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()}
};
}
kitjmv
(jmv)
February 11, 2019, 1:24pm
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
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;
}
kitjmv
(jmv)
February 11, 2019, 2:47pm
7
DavidRutten:
mesh = data as Mesh;
oh! Effectively !! Why is not I thinking !!
thank you !
kitjmv
(jmv)
February 11, 2019, 2:56pm
8
I know ! because I have no value at this point, just a few FieldInfo …
Yup, next stop, Reflection-city.
kitjmv
(jmv)
February 14, 2019, 12:35am
10
Hello @DavidRutten !
I need your help again
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:
{
ClearRuntimeMessages();
if (VolatileDataCount == 0)
{
var attr = m_field.Attribute;
m_field.Info.SetValue(program, attr.DefaultValue);
return true;
}
switch (Access)
{
case GH_ParamAccess.item:
IGH_Goo val = null;
DA.GetData(Name, ref val);
var rval = TypeCast(val);
m_field.Info.SetValue(program, rval);
break;
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.
kitjmv
(jmv)
February 14, 2019, 4:21pm
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