Join surfaces in rhinocommon

Hi Guys

Im quite new to Rhinocommon and i am struggling a bit with something that i want to do.

I want to export all the geometry on each layers seperately to a stp file and then in the end join all geometry and export the joined “hopefully closed” polysurface.

I have come this far by now, but im kinda stuck joining the geometry, does anyone have a trick to do this

Also, am i doing this in the smartest way?

        RhinoDoc doc1 = RhinoDoc.CreateHeadless(null); // Creating a headless document for exporting layer contents
        RhinoDoc doc2 = RhinoDoc.CreateHeadless(null); // Creating a headless document for exporting the joined content
        var options1 = new Rhino.FileIO.FileStpWriteOptions(); // step file write options

        for (int q = 0; q < doc.Layers.ActiveCount; q++) // Looping through the layers in the main document
        {
            Rhino.DocObjects.RhinoObject[] rhobjs = doc.Objects.FindByLayer(doc.Layers[q].Name); // getting all objects in each layer
            for (int i = 0; i < rhobjs.Length; i++) // loop through the objects
            {
                GeometryBase gb_tmp = rhobjs[i].Geometry; // get the underlying geometry
                doc1.Objects.Add(gb_tmp); // Add the geometry to doc1 for layer export
                doc2.Objects.Add(gb_tmp); // Add the geometry to doc2 for joined export
            }
            Rhino.FileIO.FileStp.Write(doc.Layers[q].Name+".stp", doc1, options1); // output layer content step files
            doc1.Objects.Clear(); // clear out doc1
        }
        
        // TODO: Join all surfaces in doc2 to a closed polysurface

        Rhino.FileIO.FileStp.Write("master.stp", doc2, options1); // Output master step file

Thanks, i figured it out, I had some issue at first but setting the tolerance higher fixed it