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)
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.
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)