Base to Brep conversion

Hi,

I am looking for help with conversion (Base to Brep) using the speckle-sharp sdk.

I am unable to convert my Base Objects to speckle Breps using the conversion methods (such as "baseObj as Brep brep, Brep newBrep = (Brep) myBaseObj). I get compile errors or runtime errors when trying to convert. I also can’t access the “value” of the Base object (which is a Brep) which I saw was suggested in some other case.

I am using the grasshopper converter
ISpeckleConverter _converter = KitManager.GetDefaultKit().LoadConverter("Grasshopper8");
and
var newBrep = _converter.ConvertToSpeckle(brep); to convert my Rhino/GH breps

As a result of my conversion I get a list of Base objects:

The reason I would like to send them as Breps, is that when I try to send one Speckle brep, it successfully sends it to the server, but when I try to send a list (List<Speckle.Core.Models.Base> exportObjects = new();, and includes the objects depicted above in the attachment) the send node runs successfully but no geometry shows up on the server.

Am I missing something crucial here?

Thanks a lot in advance for your help!

Hi @katjat,

For me to assist further, I will need a more complete sample of your code than the couple of lines you’ve already shared. Aswell as the exact exception message and stack trace you are seeing when running your code with an explicit cast (Brep)myBaseObj

Hi Jedd, thank you for your response. I will try to explain my problem a bit more in detail:

I have a nested array of Rhino Breps that I am converting to Speckle and adding to the list elementBreps

 List<Speckle.Objects.Geometry.Brep> elementBreps = new();

    // Get geometry of the element and convert each to speckle brep
    Rhino.Geometry.Brep[][] brepsByLayer = elements.GetBrepsByLayer();
    for (int i = 0; i < brepsByLayer.Length; i++)
    {
        Rhino.Geometry.Brep[] layer = brepsByLayer[i];

        for (int j = 0; j < layer.Length; j++)
        {
            Rhino.Geometry.Brep brep = layer[j];
            if (_converter.CanConvertToSpeckle(brep))
            {
                var newSpeckleObj = _converter.ConvertToSpeckle(brep);
                var newBrep = (dynamic)newSpeckleObj as Speckle.Objects.Geometry.Brep;
                elementGeometry.Add(newBrep);
            }
            else
            {
                Console.WriteLine($"Failed to convert element {brep.GetHashCode()}");
            }
        }
    }
}
return elementBreps ;

I changed the conversion to var newBrep = (dynamic)newSpeckleObj as Speckle.Objects.Geometry.Brep which now does not cause errors (runtime or compile), but the returned “newBrep” from the cast becomes null so the above results in a list of null values. While debugging I can see that the conversion results in a Speckle.Core.Models.Base {Objects.Geometry.Brep} with the value {Objects.Geometry.Brep} (the values in the screenshot of the debug panel in my first post), so the initial conversion to speckle works.

What I have also tried, is:
simply sending the resulting list of Base Objects without converting (list is of type System.Collections.Generic.List<Speckle.Core.Models.Base> and contains my converted objects as Speckle.Core.Models.Base {Objects.Geometry.Brep}. But sending this list (export result in the image below) to the servers does not result in any geometry on the server.

For testing the send node my component gives two outputs, one is the list of objects and the other is the first object in the list (seen in the attached image). Sending one brep from the list does show up on the server, so this is why it seems like I should be sending the data as Breps and why I’m trying to convert my objects to breps.

I hope the issue is a little more clear, let me know if I can elaborate further. Basically my goal is just to send a collection of breps to my speckle server, but I am unable to successfully send more than one at a time.

Again, your help is much appreciated!

I think I see your problem.

You are referencing version 3 of Speckle.Objects, while also referencing version 2 of other parts of speckle.

I notice that you are casing newSpeckleObj as Speckle.Objects.Geometry.Brep - this Speckle.Objects.Geometry.Brep does not exist in v2, it is Objects.Geometry.Brep

I suggest you pin your version of Speckle.Objects to the same version as the other Speckle nugets/code you’re referencing. (e.g. the latest 2.21.3)

Using mixed versions will lead to the behaviour your seeing because the conversion function returns a Objects.Geometry.Brep, which then you’re trying to cast to Speckle.Objects.Geometry.Brep which are completely different types.

I see, that is definitely it, the conversion errors I was seeing were between Speckle.Objects.Geometry.Brep and Objects.Geometry.Brep. Thank you so much, this was very helpful!

1 Like