Esempio n. 1
0
def split_compound(compound):
    all_faces = get_faces(compound)
    planar_faces = list(filter(lambda x: Face(x).is_planar(), all_faces))

    p1, v1 = gp_Pnt(50, 50, 25), gp_Vec(0, 0, -1)
    fc1 = make_face(gp_Pln(p1, vec_to_dir(v1)), -1000, 1000, -1000,
                    1000)  # limited, not infinite plane

    bo = BOPAlgo_Builder()
    bo.AddArgument(copy.deepcopy(compound))
    # bo.AddArgument(fc1)

    # display.DisplayShape(fc1, transparency=0.7)
    for f in planar_faces:
        gprop = BRepGProp_Face(f)
        normal_point = gp_Pnt(0, 0, 0)
        normal_vec = gp_Vec(0, 0, 0)
        gprop.Normal(0, 0, normal_point, normal_vec)
        big_face = make_face(gp_Pln(normal_point,
                                    vec_to_dir(normal_vec)), -1000, 1000,
                             -1000, 1000)  # limited, not infinite plane
        bo.AddArgument(big_face)
        # display.DisplayShape(big_face, transparency=0.7)

    bo.Perform()
    # print("error status: {}".format(bo.ErrorStatus()))

    top = Topo(bo.Shape())
    result = [s for s in top.solids()]
    return result
Esempio n. 2
0
    def Shape(self):
        ball_vecs = []
        for i in self.balls:
            ball_vecs.append(gp_Vec(i.getPnt().XYZ()))
        v_ball_to_ball = ball_vecs[1] - ball_vecs[0]
        ax = gp_Ax2(gp_Pnt(ball_vecs[0].XYZ()), gp_Dir(v_ball_to_ball.XYZ()))
        mcyl = BRepPrimAPI_MakeCylinder(ax, self.d_o / 2.0,
                                        v_ball_to_ball.Magnitude())
        cyl = mcyl.Shape()
        mch = BRepFilletAPI_MakeChamfer(cyl)

        endFaces = []
        for face in Topo(cyl).faces():
            adaptor = BRepAdaptor_Surface(face)
            if adaptor.GetType() == GeomAbs_Plane:
                endFaces.append(face)
                for edge in Topo(face).edges():
                    mch.Add(self.chamfer_distance, edge, face)
        try:
            chamferedCyl = mch.Shape()
        except:
            chamferedCyl = cyl
            print("chamfer on ForceTransferCylinder failed!")
        mc = BRepAlgoAPI_Cut(chamferedCyl, self.balls[0].Shape())
        mc = BRepAlgoAPI_Cut(mc.Shape(), self.balls[1].Shape())
        return mc.Shape()
Esempio n. 3
0
def test_iges_importer_happy_topology():
    r"""import iges file containing a box and test topology"""
    importer = IgesImporter(path_from_file(__file__, "./models_in/box.igs"))

    topo = Topo(importer.compound)
    assert topo.number_of_faces() == 6
    assert topo.number_of_edges() == 24  # 12 edges * 2 possible orientations ?
Esempio n. 4
0
def test_step_importer_2_boxes():
    r"""Import an step file containing 2 distinct boxes and test topology"""
    importer = StepImporter(path_from_file(__file__, "./models_in/2_boxes_203.stp"))
    assert len(importer.shapes) == 1
    assert importer.shapes[0].ShapeType() == TopAbs.TopAbs_COMPOUND

    topo = Topo(importer.shapes[0])
    assert topo.number_of_compounds() == 1
    assert topo.number_of_comp_solids() == 0
    assert topo.number_of_solids() == 2
    assert topo.number_of_shells() == 2
Esempio n. 5
0
def test_iges_importer_2_boxes():
    r"""Import an iges file containing 2 distinct boxes and test topology

    Notes
    -----
    This shows the current limitations of the IgesImporter as 2 boxes cannot be distinguished from one another

    """
    importer = IgesImporter(path_from_file(__file__, "./models_in/2_boxes.igs"))
    topo = Topo(importer.compound)
    assert topo.number_of_faces() == 6 * 2
    assert topo.number_of_edges() == 24 * 2
Esempio n. 6
0
def test_step_importer_happy_topology():
    r"""import step file containing a box and test topology"""
    importer = StepImporter(path_from_file(__file__, "./models_in/box_203.stp"))
    assert len(importer.shapes) == 1

    assert isinstance(importer.shapes[0], TopoDS.TopoDS_Shape)
    assert importer.shapes[0].ShapeType() == TopAbs.TopAbs_SOLID

    topo = Topo(importer.shapes[0])
    assert topo.number_of_compounds() == 0
    assert topo.number_of_comp_solids() == 0
    assert topo.number_of_solids() == 1
    assert topo.number_of_shells() == 1
Esempio n. 7
0
def get_vertex_normal(vertex, shape):
    #a vertex belongs to multiple faces on the shape
    #can only choose one face when calculating vertex normal
    #preference tells which face is preferred (preferred direction, x+,y-,z+)
    distance = sweeper.get_nearest(vertex, shape)
    #always use the first solution
    show_log("get_vertex_normal", "0010")
    #SupportOnShape1, 2 is 1 based, not 0 based
    nearest_shape = distance.SupportOnShape2(1)
    topo_nearest_shape = Topo(nearest_shape)
    topo_shape = Topo(shape)
    face = None
    show_log("get_vertex_normal", "0020")
    if nearest_shape.ShapeType() == TopAbs_VERTEX:
        face = topo_shape.faces_from_vertex(nearest_shape).next()
    if nearest_shape.ShapeType() == TopAbs_EDGE:
        face = topo_shape.faces_from_edge(nearest_shape).next()
    if nearest_shape.ShapeType() == TopAbs_WIRE:
        face = topo_shape.faces_from_wire(nearest_shape).next()
    if nearest_shape.ShapeType() == TopAbs_FACE:
        wire = topo_shape.wires_from_face(nearest_shape).next()
        face = topo_shape.faces_from_wire(wire).next()
    assert face, "unhandled nearest_shape type"
    n = sweeper.get_face_normal(face)
    return n
Esempio n. 8
0
def test_step_exporter_overwrite(box_shape):
    r"""Happy path with a subclass of TopoDS_Shape"""
    filename = path_from_file(__file__, "./models_out/box.stp")
    exporter = StepExporter(filename)
    solid = shape_to_topology(box_shape)
    assert isinstance(solid, TopoDS.TopoDS_Solid)
    exporter.add_shape(solid)
    exporter.write_file()
    initial_timestamp = os.path.getmtime(filename)
    assert os.path.isfile(filename)

    # read the written box.stp
    importer = StepImporter(filename)
    topo_compound = Topo(importer.compound)
    assert topo_compound.number_of_faces() == 6
    assert len([i for i in topo_compound.faces()]) == 6
    assert topo_compound.number_of_edges() == 12

    # add a sphere and write again with same exporter
    sphere = BRepPrimAPI.BRepPrimAPI_MakeSphere(10)
    exporter.add_shape(sphere.Shape())
    exporter.write_file()  # this creates a file with a box and a sphere
    intermediate_timestamp = os.path.getmtime(filename)
    assert intermediate_timestamp >= initial_timestamp

    # check that the file contains the box and the sphere
    importer = StepImporter(filename)
    assert len([i for i in Topo(importer.compound).faces()]) == 7  # 6 from box + 1 from sphere
    assert len([i for i in Topo(importer.compound).solids()]) == 2

    # create a new exporter and overwrite with a box only
    filename = path_from_file(__file__, "./models_out/box.stp")
    exporter = StepExporter(filename)
    solid = shape_to_topology(box_shape)
    exporter.add_shape(solid)
    exporter.write_file()
    assert os.path.isfile(filename)
    last_timestamp = os.path.getmtime(filename)
    assert last_timestamp >= intermediate_timestamp

    # check the file only contains a box
    importer = StepImporter(filename)
    assert len([i for i in Topo(importer.compound).faces()]) == 6  # 6 from box
    assert len([i for i in Topo(importer.compound).solids()]) == 1
Esempio n. 9
0
def test_stl_importer_happy_topology():
    r"""import iges file containing a box and test topology"""

    # binary STL
    importer = StlImporter(path_from_file(__file__, "./models_in/box_binary.stl"))
    topo = Topo(importer.shape)
    # assert len(topo.solids()) == 1
    assert len([i for i in topo.shells()]) == 1
    assert topo.shells().next().Closed() is True  # direct method on TopoDS_Shell
    assert len([i for i in topo.faces()]) == 108
    assert len([i for i in topo.edges()]) == 162

    # ascii STL
    importer = StlImporter(path_from_file(__file__, "./models_in/box_ascii.stl"))
    topo = Topo(importer.shape)
    # assert len(topo.solids) == 1
    assert len([i for i in topo.shells()]) == 1
    assert topo.shells().next().Closed() is True
    assert len([i for i in topo.faces()]) == 108
    assert len([i for i in topo.edges()]) == 162
Esempio n. 10
0
def test_stl_exporter_overwrite(box_shape):
    r"""Happy path with a subclass of TopoDS_Shape"""
    filename = path_from_file(__file__, "./models_out/box.stl")
    exporter = StlExporter(filename)
    solid = shape_to_topology(box_shape)
    assert isinstance(solid, TopoDS.TopoDS_Solid)
    exporter.set_shape(solid)
    exporter.write_file()
    assert os.path.isfile(filename)

    # read the written box.stl
    importer = StlImporter(filename)
    topo = Topo(importer.shape)
    assert topo.number_of_shells() == 1

    # set a sphere and write again with same exporter
    sphere = BRepPrimAPI.BRepPrimAPI_MakeSphere(10)
    exporter.set_shape(sphere.Shape())
    exporter.write_file()  # this creates a file with a sphere only, this is STL specific

    # check that the file contains the sphere only
    importer = StlImporter(filename)
    topo = Topo(importer.shape)
    assert topo.number_of_shells() == 1

    # create a new exporter and overwrite with a box only
    filename = path_from_file(__file__, "./models_out/box.stl")
    exporter = StlExporter(filename)
    solid = shape_to_topology(box_shape)
    exporter.set_shape(solid)
    exporter.write_file()
    assert os.path.isfile(filename)

    # check the file only contains a box
    importer = StlImporter(filename)
    topo = Topo(importer.shape)
    assert topo.number_of_shells() == 1
Esempio n. 11
0
def test_stl_importer_2_boxes():
    r"""Import an iges file containing 2 distinct boxes and test topology

    Notes
    -----
    This shows the current limitations of the IgesImporter as 2 boxes cannot be distinguished from one another

    """
    # binary STL
    importer = StlImporter(path_from_file(__file__, "./models_in/2_boxes_binary.stl"))

    topo = Topo(importer.shape)
    assert len([i for i in topo.shells()]) == 2
    assert topo.shells().next().Closed() is True
    assert [i for i in topo.shells()][1].Closed() is True
    assert topo.number_of_faces() == 108 * 2
    assert topo.number_of_edges() == 162 * 2

    # ascii STL
    importer = StlImporter(path_from_file(__file__, "./models_in/2_boxes_ascii.stl"))

    topo = Topo(importer.shape)
    assert len([i for i in topo.shells()]) == 2
    assert topo.shells().next().Closed() is True
    assert [i for i in topo.shells()][1].Closed() is True
    assert topo.number_of_faces() == 108 * 2
    assert topo.number_of_edges() == 162 * 2
Esempio n. 12
0
p3 = gp_Pnt(10, 10, 0)
p4 = gp_Pnt(10, 0, 0)
rect = make_closed_polygon(p1, p2, p3, p3)

display, start_display, add_menu, add_function_to_menu = init_display()
#my_box = BRepPrimAPI_MakeBox(10., 20., 30.).Shape()

stl_reader = StlAPI_Reader()
stl_box = TopoDS_Shape()
stl_reader.Read(stl_box, './models/box.stl')

axe = gp_Ax2(gp_Pnt(-10, 1, 1), gp_Dir(0, 0, 1))
box = BRepPrimAPI_MakeBox(axe, 50, 15, 15).Shape()
CommonSurface = BRepAlgoAPI_Common(box, stl_box).Shape()

topo = Topo(CommonSurface)
display.EraseAll()
x_mid_max = -100
front_face = None
for face in topo.faces():
    bbox = Bnd_Box()
    OCC.BRepBndLib.brepbndlib_Add(face, bbox)
    xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get()
    x_mid = (xmin + xmax) / 2
    if x_mid > x_mid_max:
        x_mid_max = x_mid
        front_face = face

t_face = Topo(front_face)
OCCUtils.Topology.dumpTopology(front_face)
wires = t_face.wires()
Esempio n. 13
0
from __future__ import print_function

from random import random

from OCC.AIS import AIS_ColoredShape
from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCC.Display.OCCViewer import color
from OCC.Display.SimpleGui import init_display

from OCCUtils import Topo

display, start_display, add_menu, add_function_to_menu = init_display()

my_box = BRepPrimAPI_MakeBox(10., 20., 30.).Shape()

ais = AIS_ColoredShape(my_box)

for fc in Topo(my_box).faces():
    # set a custom color per-face
    ais.SetCustomColor(fc, color(random(), random(), random()))

display.Context.Display(ais.GetHandle())
display.FitAll()

start_display()
Esempio n. 14
0
        trsf.SetTransformation(ax_final, gp_Ax3())

        mt = BRepBuilderAPI_Transform(self.shape, trsf)
        return mt.Shape()


def loadBRep(filename):
    builder = BRep_Builder()
    shape = TopoDS_Shape()
    breptools_Read(shape, filename, builder)
    return shape


profile = loadBRep("inputGeom/5_segment_wire.brep")

for i in Topo(profile).wires():
    wire = i

profile = loadBRep("inputGeom/circ.brep")
for i in Topo(profile).wires():
    wire_1 = i

body = makeEllipticalAnnularSolid(70, 55, 40, 25, 0, 30)
cavityCutter = makeEllipticalAnnularSolid(65, 50, 45, 30, 5, 31)
mc = BRepAlgoAPI_Cut(body, cavityCutter)
part = mc.Shape()

pieSlice = makePieSlice(100, 0, pi / 4.0, -1, 31)

ball = makeBall(10)
stringer = makeStringerWithContinuousSlot()