Create solid from surface

Hi

I am working with C# in RhinoCommon. I want to create a solid-like cube from 6 surfaces. I already have the surfaces coded. Now, I want to create the solid and assign it to a layer to perform further operations. I know one of the ways could be:

Rhino.RhinoApp.RunScript("_-CreateSolid",false);

But this creates a bool type of object which can’t be added to any layer.

What could be the solution to this?
I am flexible with the code i.e. I can use a different way to create this solid than the RunScript method or even a different way to assign it to layers(if that is possible).

Any suggestions are appreciated.
Thanks

Well, if it was rhinoscriptsyntax, you would use LastCreatedObjects() to get the ID of the created solid.

The RhinoCommon method is here:
https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Brep_CreateSolid.htm

Hi
Thanks for the reply.
Actually before posting the question, I had a look on the link you shared. I could not make it work. Could you explain how this method works or maybe a very small template code(maybe 4-5 lines) might help.
When I used this, I was getting the error that it does not accept 7 arguments that I was giving, 6 surfaces and 1 tolerance.

in difference to commandline-Scripting, Rhinoscript, phyton-rhinoscript-Syntax Rhinocommon and the .3dm file distinguishes between geometry and attributes (layers, etc…)
so you create geometry, and you create attributes - finally you add an object with attributes to a document.

Background info:

your startingpoint:

It only wants two arguments - a list (array) of breps and the tolerance.

Hi, thanks for your reply.
I created a list and in theory, the solid. Now, when I add it to a layer, I get the error “Can’t convert Rhino.Geometry.Brep to Rhino.Geometry.Brep”
Here is my code:

   var brep = new Brep[6];
         brep[0] = surf[0].ToBrep();
         brep[1] = surf[1].ToBrep();
         brep[2] = surf[2].ToBrep();
         brep[3] = surf[3].ToBrep();
         brep[4] = surf[4].ToBrep();
         brep[5] = surf[5].ToBrep();

        var solid =  Brep.CreateSolid(brep, RhinoMath.UnsetValue);
                    
            string layer_name = "solids";
            if (!string.IsNullOrEmpty(layer_name))
            {
                int layer_index = doc.Layers.FindByFullPath(layer_name, true);
                if (-1 == layer_index)
                    layer_index = doc.Layers.Add(layer_name, System.Drawing.Color.Black);
                RhinoDoc.ActiveDoc.Layers.SetCurrentLayerIndex(layer_index, true);
                if (layer_index >= 0)
                {
                    Rhino.DocObjects.ObjectAttributes attribs = doc.CreateDefaultAttributes();
                    attribs.LayerIndex = layer_index;
                    attribs.Name = layer_name;
                    doc.Objects.AddBrep(solid, attribs);
                    doc.Views.Redraw();
                   
                }
            }

I know this is an issue with converting array to a single object. But I don’t understand why the object “solid” is an array despite using the createsolid command. Is there any way to retrieve single Brep object?
Sorry for bothering. And thanks in advance for the help

Because CreateSolid could create multiple closed Breps according to the input geometry…

And what could be a possible fix?

The fix is to see what’s in the output array, and if it’s a single valid Brep, simply reference it with the array index 0…

Here is some sample code in Python:

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

def CreateSolidTest():
    msg="Select intersecting surfaces to create solid"
    objIDs=rs.GetObjects(msg,8,preselect=True)
    if not objIDs: return
    
    tol=sc.doc.ModelAbsoluteTolerance
    breps=[rs.coercebrep(objID) for objID in objIDs]
    solid_result=Rhino.Geometry.Brep.CreateSolid(breps,tol)
    new_ids=[]
    if solid_result:
        for item in solid_result: new_ids.append(sc.doc.Objects.AddBrep(item))
    rs.EnableRedraw(False)
    if new_ids:
        rs.DeleteObjects(objIDs)
        rs.SelectObjects(new_ids)
        sc.doc.Views.Redraw()
CreateSolidTest()