Script for exporting triangulated bodies

I’m working on some tools for doing electronic simulation and need a way to tesselate all the bodies into meshes of triangles, and then export each one as a separate file. I’m fluent with python but not with Rhino’s scripting API. How do I go about enumerating the solid bodies, then creating triangular meshes of each body, enumerate through all the triangles while getting the coordinates of each point of the triangle, and then cleaning up the workspace (that is, delete any geometry created during this process)?

Note that this would not be all that different from exporting an STL file. I just don’t want to dump all the geometry into one file – I’d like to do this body by body and into my own format. The output format is not STL.

HI Evan,

What do you have so far?

API: Rhino Namespace

does this get you started:

import rhinoscriptsyntax as rs
import Rhino

all_objs = rs.AllObjects()

for obj in all_objs:
    brep = rs.coercebrep(obj)
    if brep:
        
        mesh_parameters = Rhino.Geometry.MeshingParameters().Default
        brep_meshes = Rhino.Geometry.Mesh.CreateFromBrep(brep, mesh_parameters)
        
        mesh = Rhino.Geometry.Mesh()
        for brep_mesh in brep_meshes:
            mesh.Append(brep_mesh)
        
        mesh.Faces.ConvertQuadsToTriangles()
        
        for face in mesh.Faces:
            ptA = mesh.Vertices[face.A]
            ptB = mesh.Vertices[face.B]
            ptC = mesh.Vertices[face.C]
            
            print ptA, ptB, ptC

-Willem