Browse through objects in current document

Hello,
using RhinoCommon, I’m looking for a way to “browse” through the objects in the current open document. Suppose I draw 3 objects in the same document: one cone, one sphere and one pyramid.
I need to be able to say: “There are 3 objects in the working area: one cone, one sphere and one pyramid”.

Is there a way to achieve this? Can anyone please give me a hint to which method or class could be helpful?

Thank you

Hi @mcollareda,

You can iterate over all objects of a document using the Objects Property on the the ObjectTable class:

import scriptcontext as sc

for obj in sc.doc.Objects:
    # do stuff

Determining the number of objects for a given geometric property can be quite more challenging. I guess it really depends on what kind of objects/features you are trying to find.
For geometric primitives you can use the primitive checks for BrepFaces

import scriptcontext as sc
import Rhino

for obj in sc.doc.Objects:
    
    if obj.ObjectType == Rhino.DocObjects.ObjectType.Brep:
        for face in obj.Geometry.Faces:
            if face.IsCylinder():
                print "cylinder"
                break
            if face.IsSphere():
                print "sphere"
                break
            if face.IsTorus():
                print "torus"
                break
            if face.IsCone():
                print "cone"
                break

See the Surface methods for reference: https://developer.rhino3d.com/api/RhinoCommon/html/Methods_T_Rhino_Geometry_Surface.htm

This won’t help you finding pyramids for example, you will have to define your own algorithm for finding those. Also there can be a lot of objects in a rhino document which are not surfaces or polysurfaces, so i guess you will have to define a pretty extensive list on what objects you care about.

Thank you very much for your help, Lando.
Do you know, or does anyone know, if it’s possibile to save these 3 objects each one as a single 3dm file? Each file containing one object?

Something like

for obj in sc.doc.Objects:
obj.WriteFile();

Thank you