C# create custom parameter from base class

Is there a simple way to create a custom parameter class inherited from GH_Param<BaseClass>, where BaseClass is an abstract class, and construct the parameter with the inherited classes?

I have the abstract class Element which have Truss, Beam and Membrane inherited from it. I woud like to generate an “abstract” parameter which would permit to transfer any generic object with a single custom parameter in Grasshopper.

1 Like

I don’t think so because on startup Grasshopper creates a new (fake) instance of each parameters object of your plugins to register it.

but i have no test.
if it possible, the important thing is to replace the “GH_Param.InstantiateT”

I don’t know if I’m off topic.
but do you know “BHoM” (github)

I’m not entirely clear on what you want, but you can create a non-abstract class derived from GH_Param<Element>, where Element is abstract. But as @kitjmv said, you’ll definitely need to override the InstantiateT method and return an actual instance of some Element derived class.

If however truss, beam, and membrane are not IGH_Goo types to begin with, you can create a single goo type which can store all Element types. That would entirely remove the abstractness from the situation.

Ho! so to understand it is also possible to create it with an interface ?
class MyClass: GH_Param <IMyInterface>

@DavidRutten @kitjmv Thanks for the help!

Actually the solution was quite simple, I have simple classes inherited from Elem, I just created an GH_Goo of Elem and created an parameter inherited from it. Just tested and I manage to recover specific methods and data from the inherited classes.

[Serializable]
public class GH_Elem : GH_Goo<Elem>
{
    public GH_Elem()
    {
        Value = new Truss();
    }
    public GH_Elem(Elem c)
    {
        Value = c;
    }

    public override bool IsValid
    {
        get
        {
            return true;
        }
    }

    public override string TypeDescription
    {
        get
        {
            return "Element";
        }
    }

    public override string TypeName
    {
        get
        {
            return "Element";
        }
    }

    public override IGH_Goo Duplicate()
    {
        GH_Elem dup = new GH_Elem();
        dup.Value = Value.DeepClone();
        return dup;
    }

    public override string ToString()
    {
        return string.Format(Value.GetType().Name.ToString() + ": " + Value.id);
    }
}

public class Param_Elem : GH_Param<GH_Elem>
{
    public Param_Elem() : base("Element", "Elem", "Element", "BATS", "0.Parameters", GH_ParamAccess.item) { }
}
1 Like

I already heard of BHoM, but I need my own classes as they will have specific attributes regarding structural analysis (as my project is a non-linear structural analysis plug-in). But it is in mine request list an integration with BHoM structural classes.

It should work. There are two parameters based on IGH_Goo and IGH_GeometricGoo in the standard set, and those are both interfaces.

1 Like

I think I’m running into a related issue. I have a number of custom GH_Goo types, some of which contain interfaces (GH_Goo<Interface>) and some that contain abstract classes (GH_Goo<AbstractClass>). Creating a custom GH_Param of either seems to work, in that my custom components that use my custom Params function as expected.

However, the Params that contain an abstract class (GH_Param<GH_Goo<AbstractClass>>) do not show up in the component library or in search. In other words, you can’t place a floating abstract class param on the canvas.

On the other hand, GH_Param<GH_Goo<Interface>> appears in search/library as expected.

Any ideas on why this is happening?