GetDataTree with costum generic data types

Dear forum,
I am writing a GH plug-in where I’ve created a class called Plank. Into the current component,which I am writing, a tree of Planks are feed. I’ve written a code that compiles, but when I try the component out, I get the following error message:

  1. Solution exception:Unable to cast object of type ‘Grasshopper.Kernel.Types.GH_ObjectWrapper’ to type ‘geoPlankNetworks.DataTypes.Plank’.

And this is my code

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
    {
        pManager.AddGenericParameter("Plank", "p", "p", GH_ParamAccess.tree);
    }

protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
    {
        pManager.AddBrepParameter("Mid surface", "mS", "mS", GH_ParamAccess.tree);
        pManager.AddBrepParameter("Top surface", "tS", "tS", GH_ParamAccess.tree);
        pManager.AddBrepParameter("Bottom surface", "bS", "bS", GH_ParamAccess.tree);
        pManager.AddBrepParameter("Plank solid","pS","pS",GH_ParamAccess.tree);
    }

protected override void SolveInstance(IGH_DataAccess DA)
    {
        GH_Structure<IGH_Goo> iPlankTree;

        if (!DA.GetDataTree(0, out iPlankTree)) return;

        DataTree<Brep> midSurfaces = new DataTree<Brep>();
        DataTree<Brep> topSurfaces = new DataTree<Brep>();
        DataTree<Brep> bottomSurfaces = new DataTree<Brep>();
        DataTree<Brep> plankSolids = new DataTree<Brep>();

        foreach(GH_Path path in iPlankTree.Paths)
        {
            foreach(Plank plank in iPlankTree.get_Branch(path)) 
            {                    
                midSurfaces.Add(plank.MidSurface, path);
                topSurfaces.Add(plank.TopSurface, path);
                bottomSurfaces.Add(plank.BottomSurface, path);
                plankSolids.Add(plank.PlankSolid, path);
            }
        }

        DA.SetDataTree(0,midSurfaces);
        DA.SetDataTree(1,topSurfaces);   
        DA.SetDataTree(2,bottomSurfaces);
        DA.SetDataTree(3,plankSolids);
    }

It would be great if someone could help me out here.
Thanks,
David

Hi,

It’s hard to say without the complete class but a few ideas :

  • foreach(Plank plank in iPlankTree.get_Branch(path))

Maybe you can’t iterate through a branch with a custom type
You may want to try something like this :

foreach(IGH_Goo ighgoo in iPlankTree.get_Branch(path))
{
if(ighgoo.Value is Plank p)
{
...
}
}

  • Define the input parameter of the component as a Plank, not a Data, following this guide. Rhino - Simple Parameters

  • Do you really need this component as tree ? It is best to have the input structures as simple as possible. You could have this as item and let Grasshopper handle the trees for you.

What do you mean with this? I set the input to “item” but I feed a tree anyways?

Yes, the vast majority of components in GH work like that.
You’ll have to change your access type to GH_ParamAccess.item and the input and output methods DA.GetData(0, ref blablabla) and DA.SetData(0, blablabla).

But how do I then access the branches and paths?

You can’t, but it doesn’t seem you need to with your current code.

yes I do. I want to keep the tree structure created earlier in the GH definition.

Yes, but the component will do that for you. I mean, you don’t need to access the tree structure inside the component, since all you are doing is converting each element of the tree independently.

yes, you are right! wow, this is so much easier. Thanks a lot!