usingRhino;usingRhino.DocObjects;usingRhino.Commands;usingRhino.Input;usingRhino.Geometry;namespace examples_cs
{publicclassSplitBrepsWithPlaneCommand:Rhino.Commands.Command{publicoverridestringEnglishName{get{return"csSplitBrepsWithPlane";}}protectedoverrideResultRunCommand(RhinoDoc doc,RunMode mode){//First, collect all the breps to splitObjRef[] obj_refs;var rc =RhinoGet.GetMultipleObjects("Select breps to split",false,ObjectType.Brep,out obj_refs);if(rc !=Result.Success|| obj_refs ==null)return rc;// Get the final planePlane plane;
rc =RhinoGet.GetPlane(out plane);if(rc !=Result.Success)return rc;//Iterate over all object referencesforeach(var obj_ref in obj_refs){var brep = obj_ref.Brep();var bbox = brep.GetBoundingBox(false);//Grow the boundingbox in all directions//If the boundingbox is flat (zero volume or even zero area) //then the CreateThroughBox method will fail.var min_point = bbox.Min;
min_point.X-=1.0;
min_point.Y-=1.0;
min_point.Z-=1.0;
bbox.Min= min_point;var max_point = bbox.Max;
max_point.X+=1.0;
max_point.Y+=1.0;
max_point.Z+=1.0;
bbox.Max= max_point;var plane_surface =PlaneSurface.CreateThroughBox(plane, bbox);if(plane_surface ==null){//This is rare, it will most likely not happen unless either the plane or the boundingbox are invalidRhinoApp.WriteLine("Cutting plane could not be constructed.");}else{var breps = brep.Split(plane_surface.ToBrep(), doc.ModelAbsoluteTolerance);if(breps ==null|| breps.Length==0){RhinoApp.Write("Plane does not intersect brep (id:{0})", obj_ref.ObjectId);continue;}foreach(var brep_piece in breps){
doc.Objects.AddBrep(brep_piece);}
doc.Objects.AddSurface(plane_surface);
doc.Objects.Delete(obj_ref,false);}}
doc.Views.Redraw();returnResult.Success;}}}
fromRhinoimport*fromRhino.DocObjectsimport*fromRhino.Commandsimport*fromRhino.Inputimport*fromRhino.Geometryimport*from scriptcontext import doc
defRunCommand():#First, collect all the breps to split
rc, obj_refs =RhinoGet.GetMultipleObjects("Select breps to split",False,ObjectType.Brep)if rc <>Result.Successor obj_refs ==None:return rc
# Get the final plane
rc, plane =RhinoGet.GetPlane()if rc <>Result.Success:return rc
#Iterate over all object referencesfor obj_ref in obj_refs:
brep = obj_ref.Brep()
bbox = brep.GetBoundingBox(False)#Grow the boundingbox in all directions#If the boundingbox is flat (zero volume or even zero area) #then the CreateThroughBox method will fail.
min_point = bbox.Min
min_point.X-=1.0
min_point.Y-=1.0
min_point.Z-=1.0
bbox.Min= min_point
max_point = bbox.Max
max_point.X+=1.0
max_point.Y+=1.0
max_point.Z+=1.0
bbox.Max= max_point
plane_surface =PlaneSurface.CreateThroughBox(plane, bbox)if plane_surface ==None:#This is rare, it will most likely not happen unless either the plane or the boundingbox are invalidRhinoApp.WriteLine("Cutting plane could not be constructed.")else:
breps = brep.Split(plane_surface.ToBrep(), doc.ModelAbsoluteTolerance)if breps ==Noneor breps.Length==0:RhinoApp.Write("Plane does not intersect brep (id:{0})", obj_ref.ObjectId)continuefor brep_piece in breps:
doc.Objects.AddBrep(brep_piece)
doc.Objects.AddSurface(plane_surface)
doc.Objects.Delete(obj_ref,False)
doc.Views.Redraw()returnResult.Successif __name__ =="__main__":RunCommand()
ImportsRhinoImportsRhino.DocObjectsImportsRhino.CommandsImportsRhino.InputImportsRhino.GeometryNamespace examples_vb
PublicClassSplitBrepsWithPlaneCommandInheritsRhino.Commands.CommandPublicOverridesReadOnlyPropertyEnglishName()AsStringGetReturn"vbSplitBrepsWithPlane"EndGetEndPropertyProtectedOverridesFunctionRunCommand(doc AsRhinoDoc, mode AsRunMode)AsResult'First, collect all the breps to splitDim obj_refs AsObjRef()=NothingDim rc =RhinoGet.GetMultipleObjects("Select breps to split",False,ObjectType.Brep, obj_refs)If rc <>Result.SuccessOrElse obj_refs IsNothingThenReturn rc
EndIf' Get the final planeDim plane AsPlane
rc =RhinoGet.GetPlane(plane)If rc <>Result.SuccessThenReturn rc
EndIf'Iterate over all object referencesForEach obj_ref AsObjRefIn obj_refs
Dim brep = obj_ref.Brep()Dim bbox = brep.GetBoundingBox(False)'Grow the boundingbox in all directions'If the boundingbox is flat (zero volume or even zero area) 'then the CreateThroughBox method will fail.Dim min_point = bbox.Min
min_point.X-=1.0
min_point.Y-=1.0
min_point.Z-=1.0
bbox.Min= min_point
Dim max_point = bbox.Max
max_point.X+=1.0
max_point.Y+=1.0
max_point.Z+=1.0
bbox.Max= max_point
Dim plane_surface =PlaneSurface.CreateThroughBox(plane, bbox)If plane_surface IsNothingThen'This is rare, it will most likely not happen unless either the plane or the boundingbox are invalidRhinoApp.WriteLine("Cutting plane could not be constructed.")ElseDim breps = brep.Split(plane_surface.ToBrep(), doc.ModelAbsoluteTolerance)If breps IsNothingOrElse breps.Length=0ThenRhinoApp.Write("Plane does not intersect brep (id:{0})", obj_ref.ObjectId)ContinueForEndIfForEach brep_piece AsBrepIn breps
doc.Objects.AddBrep(brep_piece)Next
doc.Objects.AddSurface(plane_surface)
doc.Objects.Delete(obj_ref,False)EndIfNext
doc.Views.Redraw()ReturnResult.SuccessEndFunctionEndClassEndNamespace