Saving geometry for easy access in C#

Hi everyone!

I’m creating some custom components in c# in visual studio and I’m currently trying to create a component where the user gets to pick a house type from a list of 5 types (A-E). I’ve created a house class where I store the properties of the house and the user can later “explode” the object to get back all the properties of the selected house.

What I’m thinking is that it would be neat to render low level version of the house when the user picks the type by default, inside the component itself, instead of having to reference the geometry and interpolate which I’m doing right now.

What do you think is the best way of doing this? Should I save the data to a .json file somehow and reference that file in c# or is there a way of actually saving a brep as pure c# code in Rhinocommon?

Thank you!
Erik

My next version of Peacock has several assets libraries, as default geometries. What I’ve done is serialize those geometries in an XML (it could also be a JSON, but I already had experience with XML) and load that files as resources in Visual Studio. When the plugin is loaded, I deserialize all the libraries and store the geometries in their respective object galleries. Some components take care of finding, cloning and using them.

Consider loading them only when you need them (like instantiating a component that requires them) if they are too heavy to be loaded all at once when starting GH.

OpenNurbs/Rhino allows to serialize GeometryBases in binary and vice versa. But for ease of use, you can use the GH methods:
Grasshopper.Kernel.GH_Convert.CommonObjectToByteArray() and Grasshopper.Kernel.GH_Convert.ByteArrayToCommonObject()

Hi Dani!

Thank you for your reply, It was very helpful. I’ve been thinking along the same lines with loading all the objects at once (they’re not that heavy), but I just need to get the serialisation to work first. I tried using CommonObjectToByteArray() and ByteArrayToCommonObject() in a c# component inside grasshopper and that works really well for a brep:

When I try using the same method in visual studio I run into a problem though. I think the byte[.] is fine and that it’s reading it correctly, but I haven’t worked with it before so I’m not sure. Here’s what it looks like and the code for it anyway:

public static class ReadGeometry
    {
        public static Brep ReadHouseGeometry(string type)
        {
            string pt1 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string path = pt1 + "\\A1.txt"; //only a placeholder for now as I have only one geometry in binary
            byte[\*] A1 = System.IO.File.ReadAllBytes(path);
            GeometryBase geometry = GH_Convert.ByteArrayToCommonObject<GeometryBase>(A1);
            return geometry as Brep;
        }
    }

The problem is that the GeometryBase object becomes null on the ByteArrayToCommonObject() line. Not sure if this is a problem with the byte[.] or some reference missing.

Thanks again!

Erik