Extrusion and other types of Brep like geometries conversion to Brep

Instead of doing the if statement below is it possible to cast rhino_obj.Geometry to brep in one single line?
Breps can be extrusions and other types, but regardless of the sub-types I want to have only
brep types.

      if rhino_obj:
          if rhino_obj.Geometry and isinstance(rhino_obj.Geometry, Rhino.Geometry.Brep):
              selected_breps.append(rhino_obj.Geometry)
          elif rhino_obj.Geometry:
              selected_breps.append(rhino_obj.Geometry.ToBrep())

full code:

def handle_brep_input(option_name: str, hide : bool = True) -> list[Rhino.Geometry.Brep]:
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt(f"Select {option_name}")
    go.GeometryFilter = Rhino.DocObjects.ObjectType.Brep  # Filter to curves
    go.EnablePreSelect(True, True)
    go.SubObjectSelect = False
    go.DeselectAllBeforePostSelect = False
    res = go.GetMultiple(1, 0)

    if go.CommandResult() == Rhino.Commands.Result.Success:
        selected_breps = []
        for i in range(go.ObjectCount):
            rhino_obj = go.Object(i).Object()  # Get the RhinoObject

            if hide:
                Rhino.RhinoDoc.ActiveDoc.Objects.Hide(rhino_obj.Id, True)
                
            if rhino_obj:
                if rhino_obj.Geometry and isinstance(rhino_obj.Geometry, Rhino.Geometry.Brep):
                    selected_breps.append(rhino_obj.Geometry)
                elif rhino_obj.Geometry:
                    selected_breps.append(rhino_obj.Geometry.ToBrep())

        Rhino.RhinoDoc.ActiveDoc.Views.Redraw()  # Refresh view after hiding objects
        return selected_breps
    return []

Hi @Petras_Vestartas, maybe like this ?

#! python 2
import Rhino

def DoSomething():
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt("Select breps or extrusions to hide")
    go.GeometryFilter = Rhino.DocObjects.ObjectType.Brep
    go.EnablePreSelect(True, True)
    go.SubObjectSelect = False
    go.DeselectAllBeforePostSelect = False
    res = go.GetMultiple(1, 0)

    if go.CommandResult() == Rhino.Commands.Result.Success:
        selected_breps = []
        for i in range(go.ObjectCount):
            obj_ref = go.Object(i)
            Rhino.RhinoDoc.ActiveDoc.Objects.Hide(obj_ref, False)
            selected_breps.append(obj_ref.Brep())
    
        return selected_breps
    
print DoSomething()

It uses ObjRef which has the Brep() method.

_
c.

Or this:

#! python 2
import Rhino

def DoSomething():
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt("Select surfaces or polysurfaces to hide")
    go.GeometryFilter = Rhino.DocObjects.ObjectType.Surface | Rhino.DocObjects.ObjectType.PolysrfFilter
    go.SubObjectSelect = False
    res = go.GetMultiple(1, 0)
    if go.CommandResult() == Rhino.Commands.Result.Success:
        selected_breps = []
        for obj_ref in go.Objects():
            Rhino.RhinoDoc.ActiveDoc.Objects.Hide(obj_ref, False)
            selected_breps.append(obj_ref.Brep())
        return selected_breps
    
print DoSomething()

– Dale

Hi @dale, one question about all these examples: Is it expected that SubD objects are allowed to be selected if GeometryFilter does not specify them ? (I understand that Extrusion is accepted).

So far i can only prevent SubD selection if i add this ugly thing to the getter:

go.SetCustomGeometryFilter(lambda o,g,c: o.ObjectType != ObjectType.SubD)

which can of course be an external function too. Trying something like bitwise exclusion failed here for unknown reason, it still allows srf, brep, extrusion and subd:

go.GeometryFilter = ObjectType.Brep ^ ObjectType.SubD

thanks,
c.

As of Rhino 8 (I think), yes.

Think of GeometryFilter as “get objects that can be represented as”.

If you need specific object types, then a custom geometry filter is always advised.

– Dale

1 Like

Thank you @dale and @clement this solves my issue.

One question how would this work for mesh selection?

I am doing the following below and I am not sure when to use rhino_obj = go.Object(i).Object() and when rhino_obj = go.Object(i):


def handle_mesh_input(option_name: str, hide: bool = True) -> list[Rhino.Geometry.Mesh]:
    """Select Meshes from Rhino document."""
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt(f"Select {option_name}")
    go.GeometryFilter = Rhino.DocObjects.ObjectType.Mesh  # Filter to meshes
    go.EnablePreSelect(True, True)
    go.SubObjectSelect = False
    go.DeselectAllBeforePostSelect = False
    res = go.GetMultiple(1, 0)

    selected_meshes = []
    if go.CommandResult() == Rhino.Commands.Result.Success:
        
        for i in range(go.ObjectCount):
            rhino_obj = go.Object(i).Object()  # Get the RhinoObject
            
            if hide:
                Rhino.RhinoDoc.ActiveDoc.Objects.Hide(rhino_obj.Id, True)
            
            if rhino_obj and rhino_obj.Geometry and isinstance(rhino_obj.Geometry, Rhino.Geometry.Mesh):
                selected_meshes.append(rhino_obj.Geometry)
        
        Rhino.RhinoDoc.ActiveDoc.Views.Redraw()  # Refresh view after hiding objects
        

    return selected_meshes

Hi @Petras_Vestartas, i’d do it like this using ObjRef which is what go.Objects() returns, so you can use it directly to hide and to get mesh geometry using obj_ref.Mesh() from it:

def handle_mesh_input(option_name: str, hide: bool = True) -> list[Rhino.Geometry.Mesh]:
    """Select Meshes from Rhino document."""
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt(f"Select {option_name}")
    go.GeometryFilter = Rhino.DocObjects.ObjectType.Mesh 
    go.EnablePreSelect(True, True)
    go.SubObjectSelect = False
    go.DeselectAllBeforePostSelect = False
    res = go.GetMultiple(1, 0)

    selected_meshes = []
    if go.CommandResult() == Rhino.Commands.Result.Success:
        for obj_ref in go.Objects():
            if hide:
                Rhino.RhinoDoc.ActiveDoc.Objects.Hide(obj_ref, True)
            selected_meshes.append(obj_ref.Mesh())
        Rhino.RhinoDoc.ActiveDoc.Views.Redraw()

    return selected_meshes

The first should give you a RhinoObject the second an ObjRef which is just a reference to it.

_
c.

1 Like

Thank you very much!

1 Like

solved ?

1 Like

Yes :slight_smile:

1 Like