Converting to Base object

Hi everyone, I feel like I’m missing something obvious. I’m trying to create a component that serializes a list of Speckle objects and sends them to my api to perform some calculations.

How do I convert the Speckle objects, that are all from the BuiltElements objects, to Speckle Base objects?

Hey @marijn, you can just cast the lot to Base, and there’s several ways to do it:

var x = (Base) myThing;
var y = myThing as Base; 

Does this help?

Hey dimitrie! Thanks for your quick reply. I tried this but it gives me the following error:

BuiltElement objects already are Base objects, since they inherit from that class :slight_smile:

If your variable shape is of type GH_SpeckleBase you should just need to do shape.Value to get its internal Base object
cc @AlanRynne

Alright, but how do I interpret the ‘object’ as a GH_SpeckleBase? I will need to cast it to that class first to access the “Value” attribute I assume. However, I’m having trouble locating that class.

1 Like

Hey @marijn!

GH_SpeckleBase is our own wrapper for Base classes, and that lives on the connector project itself, so it’s not easy to access from another project.

My suggestion would be to just check if it is a GH_Goo<Base> instead, which is the direct parent of GH_SpeckleBase.

var shape_list = new List<object>();
if(!DA.GetDataList(0, shape_list)) return;
// Now shape_list is of objects, could be anything really at this point
for (var shape in shape_list) {
    // Check for valid cast in a one-liner condition
    if ( shape is GH_Goo<Base> baseGoo ) {
        // If it's a GH_Goo<Base> it's value is directly typed so the following should work 👇🏼
        Base actual_base = baseGoo.Value;
        Console.WriteLine(actual_base.speckle_type);
        continue;
    }
    // The object is not a Base object, do whatever with it, such as displaying a warning
    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Object must be a Speckle Base Object")
}

Do note that I wrote this code directly on the forum text editor so there may be a typo or 2… but I hope you get the gist of it!

Let me know if that doesn’t work, we’ll be happy to look further into your use-case to enable what you want to achieve

2 Likes

It worked! Thanks everyone for the quick help :slight_smile:

1 Like

Thanks for the question! It was super useful the whole process because I had a similar problem.
I was trying to connect an object to my custom component with a tree structure. I think for that reason, GH was casting it as IGH_Goo which didn’t allowed me to case it as GH_Goo. In order to solve that I just called it as a GH_Goo and casted as GH_SpeckleBase from which I could extract the Value property.

GH_Structure<IGH_Goo> ParameterObjectTree; // Creating the instance of GH_Structure
DA.GetDataTree(0, out ParameterObjectTree); // Feeding the component input into the GH_Structure
var ParameterBranches = ParameterObjectTree.Branches; //Getting the branches elements from the tree object
foreach (var parameterBranch in ParameterBranches) 
{               
  Base TypeObj = (parameterBranch[0] as GH_SpeckleBase).Value;  //Transforming from GH_Goo to Base Object
}

Maybe there is a smarter way of doing this, but it’s working perfectly on this case with branches.

3 Likes