[issue] brep trim

Could anyone, please, enlighten me why this code doesn’t work:

###################
### trim by box ###
###################
import System
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
import time
import math
#import uuid



tol = sc.doc.ModelAbsoluteTolerance

def TST():
    cutter_brep_obj_id = rs.GetObject("select cutting box: ",rs.filter.polysurface)
    cutter_brep_obj = rs.coercebrep(cutter_brep_obj_id)
    to_be_trimmed_ids = rs.GetObjects("select objects to be trimmed: ",rs.filter.surface|rs.filter.polysurface)
    to_be_trimmed = [rs.coercebrep(id) for id in to_be_trimmed_ids if id is not None]
    if cutter_brep_obj_id == None or to_be_trimmed_ids == None: return
    for obj in to_be_trimmed:
        if cutter_brep_obj.Faces.Count > 1:
            print obj.Trim.Overloads[Rhino.Geometry.Brep,System.Double](cutter_brep_obj,tol)
        if cutter_brep_obj.Faces.Count == 1:
            cutter_surface = cutter_brep_obj.Faces[0]
            cutter_plane = cutter_surface.TryGetPlane(tol)[1]
            print cutter_plane[1]
            print cutter_plane[0]
            print obj.Trim.Overloads[Rhino.Geometry.Plane,System.Double](cutter_plane,tol)



if __name__ == "__main__":
    ts = time.time()
    #rs.EnableRedraw(False)
    
    TST()
    
    print "Elapsed time is {:.2f}".format(time.time()-ts)

Anyone?

@ivelin.peychev - is there a model that goes with this?

– Dale

Hi @dale,

Let me check, most likely I created a single box and a couple of surfaces that pass through the box. I probably didn’t even save the model.

UPDATE:
Yes, I didn’t save it.
Use this:
trim_by_box.3dm (44 KB)

This seems to work:

import System
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc

def Test():
    cutter_id = rs.GetObject("Select cutting polysurface", rs.filter.polysurface)
    cutter = rs.coercebrep(cutter_id)
    brep_ids = rs.GetObjects("Select surfaces and polysurfaces to trim", rs.filter.surface | rs.filter.polysurface)
    tol = sc.doc.ModelAbsoluteTolerance
    for brep_id in brep_ids:
        brep = rs.coercebrep(brep_id)
        pieces = brep.Trim.Overloads[Rhino.Geometry.Brep, System.Double](cutter, tol)
        for piece in pieces:
            id = sc.doc.Objects.AddBrep(piece)
            obj = sc.doc.Objects.Find(id)
            obj.Select(True)
    sc.doc.Views.Redraw()

if __name__ == "__main__":
    Test()

What am I missing?

– Dale

Thanks @dale,

I checked my code again and apparently I made one too many checks and printed the trim result instead of assigning it to an object. :man_facepalming:

Now it works

def TST():
    cutter_brep_obj_id = rs.GetObject("select cutting box: ",rs.filter.polysurface)
    cutter_brep_obj = rs.coercebrep(cutter_brep_obj_id)
    to_be_trimmed_ids = rs.GetObjects("select objects to be trimmed: ",rs.filter.surface|rs.filter.polysurface)
    to_be_trimmed = [rs.coercebrep(id) for id in to_be_trimmed_ids if id is not None]
    if cutter_brep_obj_id == None or to_be_trimmed_ids == None: return
    for obj in to_be_trimmed:
        if cutter_brep_obj.Faces.Count > 1:
            #print obj.Trim.Overloads[Rhino.Geometry.Brep,System.Double](cutter_brep_obj,tol)
            trimmed = obj.Trim.Overloads[Rhino.Geometry.Brep,System.Double](cutter_brep_obj,tol)
            for brep in trimmed:
                sc.doc.Objects.AddBrep(brep)