pythonocc-core icon indicating copy to clipboard operation
pythonocc-core copied to clipboard

Retrieve Curve from BRepAlgoAPI_Section result

Open Tanneguydv opened this issue 2 years ago • 5 comments

Hello, I don't get the way we could get the result of an intersection of a Plane with a Shape as a Curve, I believe we need to translate the result of BRepAlgoAPI_Section into a curve but I can't find how...

Could you please help me on that point?

As an example here one of the demos code : https://github.com/tpaviot/pythonocc-demos/blob/master/examples/core_topology_boolean.py

def slicer(event=None):
    # Param
    Zmin, Zmax, deltaZ = -100, 100, 5
    # Note: the shape can also come from a shape selected from InteractiveViewer
    if 'display' in dir():
        shape = display.GetSelectedShape()
    else:
        # Create the shape to slice
        shape = BRepPrimAPI_MakeSphere(60.).Shape()
    # Define the direction
    D = gp_Dir(0., 0., 1.)  # the z direction
    # Perform slice
    sections = []
    init_time = time.time()  # for total time computation
    for z in range(Zmin, Zmax, deltaZ):
        # Create Plane defined by a point and the perpendicular direction
        P = gp_Pnt(0, 0, z)
        Pln = gp_Pln(P, D)
        face = BRepBuilderAPI_MakeFace(Pln).Shape()
        # Computes Shape/Plane intersection
        section_shp = BRepAlgoAPI_Section(shape, face)
        if section_shp.IsDone():
            sections.append(section_shp)
    total_time = time.time() - init_time
    print("%.3fs necessary to perform slice." % total_time)

    display.EraseAll()
    display.DisplayShape(shape)
    for section_ in sections:
        display.DisplayShape(section_.Shape())
    display.FitAll()

Tanneguydv avatar Aug 24 '21 10:08 Tanneguydv

I have a beginning of solution thanks to this discussion : https://dev.opencascade.org/content/topo-explorer-iterate-over-any-shape

It goes with this function : from OCC.Core.TopoDS import TopoDS_Iterator

Here the same code with this function and prints to understand, next step would be to only get the TopoDS_Edge of the result :

def slicer(event=None):
    # Param
    Zmin, Zmax, deltaZ = -100, 100, 5
    # Note: the shape can also come from a shape selected from InteractiveViewer
    if 'display' in dir():
        shape = display.GetSelectedShape()
    else:
        # Create the shape to slice
        shape = BRepPrimAPI_MakeSphere(60.).Shape()
    # Define the direction
    D = gp_Dir(0., 0., 1.)  # the z direction
    # Perform slice
    sections = []
    init_time = time.time()  # for total time computation
    for z in range(Zmin, Zmax, deltaZ):
        # Create Plane defined by a point and the perpendicular direction
        P = gp_Pnt(0, 0, z)
        Pln = gp_Pln(P, D)
        face = BRepBuilderAPI_MakeFace(Pln).Shape()
        # Computes Shape/Plane intersection
        section_shp = BRepAlgoAPI_Section(shape, face)
        if section_shp.IsDone():
            sections.append(section_shp)
    total_time = time.time() - init_time
    print("%.3fs necessary to perform slice." % total_time)

    display.EraseAll()
    #display.DisplayShape(shape)
    compounds = []
    for section_ in sections:
        #display.DisplayShape(section_.Shape())
        print(section_.Shape())
        compounds.append(section_.Shape())

    from OCC.Core.TopoDS import TopoDS_Iterator
    edges = []
    for compound in compounds:
        it = TopoDS_Iterator(compound, True, True)  # options: compose subshapes with orientation and location of aShape
        while it.More():
            edges.append(it.Value())
            it.Next()

    for edge in edges:
        print(edge)
        display.DisplayColoredShape(edge, "YELLOW")

    display.FitAll()

Tanneguydv avatar Aug 27 '21 10:08 Tanneguydv

Hi, did you solve the issue? I have somewhat similar problem - Z-axis symmetric shape (but not done by rotation, relatively large STEP file), and would like to cut it using, say ZY (or ZX) plane, and get cut-off curve as set of Z,Y points. Any advices are greatly appreciated

Kri-Ol avatar Aug 03 '22 03:08 Kri-Ol

Hi, did you solve the issue? I have somewhat similar problem - Z-axis symmetric shape (but not done by rotation, relatively large STEP file), and would like to cut it using, say ZY (or ZX) plane, and get cut-off curve as set of Z,Y points. Any advices are greatly appreciated

I used the above function TopoDS_Iterator, but the curve obtained is quite different from the expected, and I don't know how to successfully implement the data transform.

qigong777 avatar Aug 03 '22 07:08 qigong777

@qigong777 Thank you. I just implemented the same logic, and got bunch of edges (of type TopoDS_Edge), and they together looks right in the UI/viewer! So my next question is - could I get just bunch of points from each edge? Is there somewhere hidden parametric representation of the edge? Should be, because how else they are drawn on the screen?

Kri-Ol avatar Aug 04 '22 02:08 Kri-Ol

@qigong777 Thank you. I just implemented the same logic, and got bunch of edges (of type TopoDS_Edge), and they together looks right in the UI/viewer! So my next question is - could I get just bunch of points from each edge? Is there somewhere hidden parametric representation of the edge? Should be, because how else they are drawn on the screen?

You can extract the starting point of each edge. The code is shown below:

line = BRepAlgoAPI_Section(stlshapes, rectShape2.Shape(), True)
display, start_display, add_menu, add_function_to_menu = init_display()
it = TopoDS_Iterator(line.Shape(), True, True)
edges = []
while it.More():
    edges.append(it.Value())
    it.Next()
for i, edge in enumerate(edges):
    [curve_handle, a, b] = BRep_Tool.Curve(edge)
    pt = [curve_handle.Value(a).X(), curve_handle.Value(a).Y(), curve_handle.Value(a).Z()]
    display.DisplayShape(gp_Pnt(pt[0],pt[1],pt[2]), update=True, color=rgb_color(1, 0, 0))
    #display.DisplayColoredShape(edges[j], "YELLOW")
display.DisplayShape(line.Shape(), update=True, color=rgb_color(0, 0, 1))
display.FitAll()
start_display()

image

qigong777 avatar Aug 04 '22 06:08 qigong777