algorithmic modeling for Rhino
Hi,
I am not sure if this question has not already been asked on this forum but I have been wondering about passing custom grasshopper parameters into (and out of) custom components, based on a base class (or interface).
Say I have to classes A and B that share common similarities (properties methods etc). Now I want to create a component that can operate on both A and B class instances, without having to either create two separate components or to register to separate input parameters in one component, let's call this component "Operator".
My idea was to achieve this by using a baseclass; 'baseclass' from which both A and B inherit.
Next for all three classes I define two classes; (1) data classes GH_AData, GH_BData, GH_baseclassData and (2) param classes GH_AParam, GH_BParam, GH_baseclassParam using the following inheritance (I override the necessary methods) as:
public class GH_baseclassData: GH_Goo<baseclass> {}
public class GH_baseclassParam : GH_Param<GH_baseclassData> {}
Now I am able to assign in the GH_InputParamManager of my "Operator" component:
pManager.AddParameter(new GH_baseclassParam ())
I create the Grasshopper A and B custom parameters in separate components, but if I try to pass one of these into the "Operator" component I get:
2. Data conversion failed from A (or B) to Operator
So my question is this the right way to go? If so, how do I avoid this conversion problem?
(I did find a public override method from the GH_Goo<T> class called CastTo<Q> but I cannot seem to make this work for me)
Thanks!
Dion
Tags:
Hi Dion,
forget about CastTo and CastFrom, those are used when adding data to parameters that are not of the ultimate type.
What you want is possible, and there's several ways to go about it. I'd recommend writing a custom GH_Param<IGH_Goo> parameter which is smart enough to reject everything other than GH_AData and GH_BData. That way you don't have to make a baseclass, so it'll work even when A and B have little or nothing in common. All you need to do is override OnVolatileDataCollected and remove all data which is neither A, B or null.
I attached source for a parameter which handles GH_Colour, GH_Curve and nothing else. Though it will perform some casting if the data supplied is not quite a colour or a curve.
To use this parameter in a component, just use DA.GetData() with IGH_Goo type, and then inspect the data using the is or as keywords to decide whether you got A or B.
--
David Rutten
david@mcneel.com
Tirol, Austria
Thanks David, exactly what I needed, works perfectly!
You're interested in an object which only handles curves and colours? That's oddly specific and precipitously fortunate :)
The C# code is still attached, if you can't see it something else must be wrong.
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