Splitting / Trimming with Python

I have a spiral shaped cut around a cylinder, where I want to remove the strip that is in the spiral shape. I am trying to do this programmatically with python, but I’m not so sure where to begin with the methods for splitting/trimming objects from the brep class. I think this application is a little more tricky too, since I am splitting a cylinder the seam breaks up the cut so the result is more than just piece A and piece B.

The file below shows the objects I start with, and the desired goal that I would like to automate into a routine. In summary, it’s using a closed curve to split a brep surface which is periodic, and the closed curve goes across the surface seam multiple times.

trim_python_acrossSeams.3dm (365.5 KB)

Any general practices for splitting or adding trim curves to a brep would be really helpful, as I couldn’t find much in the McNeel Python Examples.

Below is the intended result; here I have manually picked the surfaces to get rid of.

If the face is first split in two, my scripts, xTrimSrfWithCrvs / xSplitSrfWithCrvs | Food4Rhino , can trim/split those faces to your intended result. Depending on how far you want to automate your workflow, I can help you set up your script to use these scripts.

If you want to develop your own splitting/trimming routine yourself, I (still) suggest splitting the face in two then using BrepFace.Split Method (IEnumerable(Curve), Double)

Dear Steve, that would be really helpful. Getting to this point, the work is done in grasshopper. Will I still be able to call your scripts through the ghPython component ? Maybe I need to dig into how I do that.

I think I must’ve looked at every class nearly except BrepFace … some of the classes, I didn’t understand so well. there was one, for example below, where it talked about getting numbers / indices to identify trimming curves, which you then pass back through later.

It looks possible: Importing Python methods from my own library in Rhino and Grasshopper on macOS?

But I just noticed that though _Trim doesn’t work well on your example, _Split does. I then tested the even higher-level method, Brep.Split Method (IEnumerable(Curve), Double) , and this also works on your example even without splitting the brep first. So, this is my revised recommendation to try first.

Brep.AddTrimCurve is for the low-level construction/modification of a brep.

This is the one you’d recommend ?

1 Like

Hmm… doing this so far in a ghPython gives me the original cylinder. I could upload the file maybe, but I also don’t want to take up too much of your time.

I was trying to get my head around, in the API, how the trimming itself ‘happens’. For example, just choosing some curves to cut presumably isn’t enough. Do I then need to do things pertaining to loop type, and so forth?

Definitely seems like a non trivial topic!

Brep.Split acts similar to _Split; it even returns multiple breps instead of adding faces. Here is a script using it that works on your example:

Python code
from __future__ import absolute_import, print_function, unicode_literals

import Rhino
import Rhino.DocObjects as rd
import Rhino.Input as ri
import scriptcontext as sc

def main():
    
    res, objrefB = ri.RhinoGet.GetOneObject(
        "Select brep",
        acceptNothing=False,
        filter=rd.ObjectType.Brep)
    
    sc.doc.Objects.UnselectAll()
    sc.doc.Views.Redraw()
    
    if res != Rhino.Commands.Result.Success:
        return
    
    res, objrefsC = ri.RhinoGet.GetMultipleObjects(
        "Select curves",
        acceptNothing=False,
        filter=rd.ObjectType.Curve)
    
    sc.doc.Objects.UnselectAll()
    sc.doc.Views.Redraw()
    
    if res != Rhino.Commands.Result.Success:
        return
    
    rgB_In = objrefB.Brep()
    crvs = [o.Curve() for o in objrefsC]
    
    rgBs_Out = rgB_In.Split(
        cutters=Rhino.Collections.CurveList(crvs),
        intersectionTolerance=sc.doc.ModelAbsoluteTolerance)
    
    if len(rgBs_Out) < 2:
        print("Brep was not split.")
        return
    
    print("Brep was split into {} breps.".format(len(rgBs_Out)))
    
    [sc.doc.Objects.AddBrep(rgB) for rgB in rgBs_Out]
    sc.doc.Views.Redraw()

if __name__ == '__main__': main()