Пример #1
0
coordinates in the console.
"""

from OCC.Display.SimpleGui import init_display
from OCC.Graphic3d import Graphic3d_Camera
from OCC.TopoDS import topods_Vertex
from OCC.BRep import BRep_Tool
from core_load_step_ap203 import read_step_file

def vertex_clicked(shp, *kwargs):
    """ This function is called whenever a vertex is selected
    """
    for shape in shp:  # this should be a TopoDS_Vertex
        print("Face selected: ", shape)
        v = topods_Vertex(shape)
        pnt = BRep_Tool.Pnt(v)
        print("3d gp_Pnt selected coordinates : X=", pnt.X(),
              "Y=", pnt.Y(), "Z=", pnt.Z())
        # then convert to screen coordinates
        screen_coord = display.View.Convert(pnt.X(), pnt.Y(), pnt.Z())
        print("2d screen coordinates : ", screen_coord)

display, start_display, add_menu, add_function_to_menu = init_display()
# loads  and displays a step file
the_shape = read_step_file('./models/as1_pe_203.stp')

display.SetSelectionModeVertex()  # switch to Vertex selection mode
display.register_select_callback(vertex_clicked)
display.DisplayShape(the_shape, update=True)
start_display()
def build_curve_network(event=None):
    '''
    mimic the curve network surfacing command from rhino
    '''
    root_compound_shape = read_step_file("./models/splinecage.stp")
    topo = Topo(root_compound_shape)

    # approximate the hell out of all surfaces and curves
    # I wanna see it in its full glory
    display.Context.SetDeviationAngle(0.00001)  # 0.001 -> default
    display.Context.SetDeviationCoefficient(0.0001)  # 0.001 -> default

    tangent_constraint_faces = [f for f in topo.faces()]

    # loop through the imported faces
    # associate the length of each of the faces edges to the corresponding face
    _edge_length_to_face, _edge_length_to_edge = hash_edge_lenght_to_face(
        tangent_constraint_faces)

    # loop through the imported curves, avoiding the imported faces
    # when we've got these filtered out, we retrieved the geometry to build the surface from
    filtered_edges = [
        e
        for e in topo._loop_topo(TopAbs_EDGE, root_compound_shape, TopAbs_FACE)
    ]

    filtered_length = {}
    for e in filtered_edges:
        l = round(length_from_edge(e), 3)
        filtered_length[l] = e

    input_edge_face_pairs, edges_no_adjacent_face = [], []
    for l, edg in filtered_length.items():
        if l in _edge_length_to_edge:
            edge_face_pair = (_edge_length_to_edge[l], _edge_length_to_face[l])
            input_edge_face_pairs.append(edge_face_pair)
        else:
            edges_no_adjacent_face.append(edg)

    brep_plate_builder = BRepOffsetAPI_MakeFilling()

    enforce_tangency = True

    if enforce_tangency:
        print("going for surface quality")
        brep_plate_builder.SetConstrParam(
            0.0001, 0.001, 0.01,
            0.01)  # ?!!! Tol2d=1.0, Tol3d=1.0, TolAng=1.0, TolCurv=1.0
        brep_plate_builder.SetApproxParam(8, 240)  # MaxDeg=8, MaxSegments=9
        brep_plate_builder.SetResolParam(
            3, 64, 3)  # Degree=3, NbPtsOnCur=15, NbIter=2, Anisotropie=0
    else:
        print("quick and dirty")

    # illegal instruction 4???
    for i in input_edge_face_pairs:
        display.DisplayShape(i, color=random_color())
        constraint_edg, support_face = i
        if constraint_edg.IsNull() or support_face.IsNull():
            print("OMG null")
        brep_plate_builder.Add(constraint_edg, support_face, GeomAbs_G1)

    # not entirely sure why this fails... how is that different from adding from points?
    # for e in edges_no_adjacent_face:
    #     brep_plate_builder.Add(e, GeomAbs_C0)

    # libc++abi.dylib: terminating with uncaught exception of type Standard_OutOfRange
    for e in edges_no_adjacent_face:
        display.DisplayShape(e)
        for pt in divide_edge_by_nr_of_points(e, 12)[2:-2]:
            brep_plate_builder.Add(pt[1])

    brep_plate_builder.Build()
    if brep_plate_builder.IsDone():
        face = brep_plate_builder.Shape()
        display.DisplayColoredShape(face, "ORANGE")
    else:
        print("constructing the surface failed")
def build_curve_network(event=None):
    '''
    mimic the curve network surfacing command from rhino
    '''
    root_compound_shape = read_step_file("./models/splinecage.stp")
    topo = Topo(root_compound_shape)

    # approximate the hell out of all surfaces and curves
    # I wanna see it in its full glory
    display.Context.SetDeviationAngle(0.00001)  # 0.001 -> default
    display.Context.SetDeviationCoefficient(0.0001)  # 0.001 -> default

    tangent_constraint_faces = [f for f in topo.faces()]

    # loop through the imported faces
    # associate the length of each of the faces edges to the corresponding face
    _edge_length_to_face, _edge_length_to_edge = hash_edge_lenght_to_face(tangent_constraint_faces)

    # loop through the imported curves, avoiding the imported faces
    # when we've got these filtered out, we retrieved the geometry to build the surface from
    filtered_edges = [e for e in topo._loop_topo(TopAbs_EDGE, root_compound_shape, TopAbs_FACE)]

    filtered_length = {}
    for e in filtered_edges:
        l = round(length_from_edge(e), 3)
        filtered_length[l] = e

    input_edge_face_pairs, edges_no_adjacent_face = [], []
    for l, edg in filtered_length.items():
        if l in _edge_length_to_edge:
            edge_face_pair = ( _edge_length_to_edge[l], _edge_length_to_face[l] )
            input_edge_face_pairs.append(edge_face_pair)
        else:
            edges_no_adjacent_face.append(edg)

    brep_plate_builder = BRepOffsetAPI_MakeFilling()

    enforce_tangency = True

    if enforce_tangency:
        print("going for surface quality")
        brep_plate_builder.SetConstrParam(0.0001, 0.001, 0.01, 0.01) # ?!!! Tol2d=1.0, Tol3d=1.0, TolAng=1.0, TolCurv=1.0
        brep_plate_builder.SetApproxParam(8, 240) # MaxDeg=8, MaxSegments=9
        brep_plate_builder.SetResolParam(3, 64, 3) # Degree=3, NbPtsOnCur=15, NbIter=2, Anisotropie=0
    else:
        print("quick and dirty")

    # illegal instruction 4???
    for i in input_edge_face_pairs:
        display.DisplayShape(i, color=random_color())
        constraint_edg, support_face = i
        if constraint_edg.IsNull() or support_face.IsNull():
            print("OMG null")
        brep_plate_builder.Add(constraint_edg, support_face, GeomAbs_G1)

    # not entirely sure why this fails... how is that different from adding from points?
    # for e in edges_no_adjacent_face:
    #     brep_plate_builder.Add(e, GeomAbs_C0)

    # libc++abi.dylib: terminating with uncaught exception of type Standard_OutOfRange
    for e in edges_no_adjacent_face:
        display.DisplayShape(e)
        for pt in divide_edge_by_nr_of_points(e, 12)[2:-2]:
            brep_plate_builder.Add(pt[1])

    brep_plate_builder.Build()
    if brep_plate_builder.IsDone():
        face = brep_plate_builder.Shape()
        display.DisplayColoredShape(face, "ORANGE")
    else:
        print("constructing the surface failed")