Skip to content

How to use BRep_Tool.CurveOnSurface #1433

@tnakaicode

Description

@tnakaicode

Description:

While using the BRep_Tool.CurveOnSurface function in the Python bindings of OpenCASCADE, there is ambiguity in how to correctly pass and retrieve the parameters, especially for the First and Last values and the theIsStored flag. The documentation and behavior of the function in Python do not align clearly with the C++ API.

Observed Behavior:

  • The function raises a TypeError when called with the expected arguments in Python.
  • The documentation does not clearly explain how to handle the theIsStored flag in Python, as Python does not support pointer-based arguments (bool *).

Expected Behavior:

The function should:

  1. Return a valid Geom2d_Curve handle if the curve exists in the UV space.
  2. Update the First and Last parameters with the parameter range of the curve.
  3. Optionally update the theIsStored flag to indicate whether the curve is stored or dynamically created.
def uv_polygon2d_to_wire_on_surface(surface, uv_points, close=True):
    edges = []
    n = len(uv_points)
    for i in range(n if close else n - 1):
        p1 = gp_Pnt2d(*uv_points[i])
        p2 = gp_Pnt2d(*uv_points[(i + 1) % n])
        seg2d = GCE2d_MakeSegment(p1, p2).Value()
        edge = BRepBuilderAPI_MakeEdge(seg2d,
                                       surface,
                                       seg2d.FirstParameter(),
                                       seg2d.LastParameter()).Edge()
        edges.append(edge)
    wire_maker = BRepBuilderAPI_MakeWire()
    for edge in edges:
        wire_maker.Add(edge)
    return wire_maker.Wire()

def surface_wire_to_uv_wire(face, wire):
    """
    Function to convert a Wire on a Surface to a Wire in the UV plane.
    face: TopoDS_Face (Face containing the Surface)
    wire: TopoDS_Wire (Wire on the Surface)
    Returns: TopoDS_Wire (Wire in the UV plane)
    """
    # List to store edges in the UV plane
    uv_edges = []

    # Retrieve edges within the Wire
    explorer = TopExp_Explorer(wire, TopAbs_EDGE)
    while explorer.More():
        edge = topods.Edge(explorer.Current())
        # Convert the edge on the Surface to a curve in UV space
        first = 0.0
        last = 1.0
        is_stored = True  # Using Python's bool instead of theIsStored

        # Correctly call BRep_Tool.CurveOnSurface
        curve_2d, first, last = BRep_Tool.CurveOnSurface(edge, face, is_stored)

        if curve_2d is not None:
            trimmed_curve = Geom2d_TrimmedCurve(curve_2d, first, last)
            # Create an edge in UV space
            uv_edge = BRepBuilderAPI_MakeEdge(trimmed_curve).Edge()
            uv_edges.append(uv_edge)
        explorer.Next()

    # Create a Wire in the UV plane
    wire_maker = BRepBuilderAPI_MakeWire()
    for uv_edge in uv_edges:
        wire_maker.Add(uv_edge)

    return wire_maker.Wire()


display, start_display, add_menu, add_function_to_menu = init_display()

# Create a cylindrical surface
cylinder = Geom_CylindricalSurface(gp_Ax3(), 5.0)
face = BRepBuilderAPI_MakeFace(cylinder.Cylinder(), 0, 2 * pi, -10, 10).Face()

# Define a rectangle in UV space
uv_poly = [
    (0, 1),
    (pi, 1),
    (pi, 5),
    (pi + pi / 4, 7),
    (pi + pi / 4, 1),
    (2 * pi, 1),
]
wire = uv_polygon2d_to_wire_on_surface(cylinder, uv_poly, False)

# Convert the Wire on the Surface to a Wire in the UV plane
uv_wire = surface_wire_to_uv_wire(face, wire)

# Verify the result
print("UV Wire:", uv_wire)

display.DisplayShape(uv_wire, update=True)

display.FitAll()
start_display()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions