Exemple #1
0
def face_mesh_triangle(comp=TopoDS_Shape(), isR=0.1, thA=0.1):
    # Mesh the shape
    BRepMesh_IncrementalMesh(comp, isR, True, thA, True)
    bild1 = BRep_Builder()
    comp1 = TopoDS_Compound()
    bild1.MakeCompound(comp1)
    bt = BRep_Tool()
    ex = TopExp_Explorer(comp, TopAbs_FACE)
    while ex.More():
        face = topods_Face(ex.Current())
        location = TopLoc_Location()
        facing = bt.Triangulation(face, location)
        tab = facing.Nodes()
        tri = facing.Triangles()
        print(facing.NbTriangles(), facing.NbNodes())
        for i in range(1, facing.NbTriangles() + 1):
            trian = tri.Value(i)
            index1, index2, index3 = trian.Get()
            for j in range(1, 4):
                if j == 1:
                    m = index1
                    n = index2
                elif j == 2:
                    n = index3
                elif j == 3:
                    m = index2
                me = BRepBuilderAPI_MakeEdge(tab.Value(m), tab.Value(n))
                if me.IsDone():
                    bild1.Add(comp1, me.Edge())
        ex.Next()
    return comp1
Exemple #2
0
def triangle_mesh_solid(solid, lin_tol=1e-2, ang_tol=0.5):
    """Computes a triangular mesh for a solid using BRepMesh.
    The resolution or quality of the mesh approximation can be
    adjusted with lin_tol and ang_tol (linear and angular tolerances).
    The computed mesh is returned as a tuple of lists:
       triangles - a list of each triangles' 3x vertices
                   represented as indexes into the vertices list
       vertices - a list of the mesh's 3D vertices
    """
    if isinstance(solid, Solid):
        obj = [solid.wrapped]
    elif isinstance(solid, list):
        obj = [x.wrapped for x in solid]
    else:
        obj = [solid]
    vertices = []
    triangles = []
    for o in obj:
        mesh = BRepMesh_IncrementalMesh(o, lin_tol, False, ang_tol)
        mesh.Perform()
        ms = Shape.cast(mesh.Shape())
        bt = BRep_Tool()
        mesh_faces = ms.Faces()
        for mesh_face in mesh_faces:
            face = mesh_face.wrapped
            location = TopLoc_Location()
            facing = bt.Triangulation(face, location)
            tri = facing.Triangles()
            num_tri = facing.NbTriangles()
            vtx = facing.Nodes()
            txf = face.Location().Transformation()
            rev = (True if face.Orientation()
                   == TopAbs_Orientation.TopAbs_REVERSED else False)
            for i in range(1, num_tri + 1):
                idx = list(tri.Value(i).Get())
                ci = [0, 2, 1] if rev else [0, 1, 2]
                for j in ci:
                    pt = [
                        vtx.Value(idx[j]).Transformed(txf).X(),
                        vtx.Value(idx[j]).Transformed(txf).Y(),
                        vtx.Value(idx[j]).Transformed(txf).Z(),
                    ]
                    if pt not in vertices:
                        vertices.append(pt)
                    idx[j] = vertices.index(pt)
                triangles.append(idx)
    return triangles, vertices
def simple_mesh():
    #
    # Create the shape
    #
    theBox = BRepPrimAPI_MakeBox(200, 60, 60).Shape()
    theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100, 20, 20), 80).Shape()
    shape = BRepAlgoAPI_Fuse(theSphere, theBox).Shape()
    #
    # Mesh the shape
    #
    BRepMesh_IncrementalMesh(shape, 0.8)
    builder = BRep_Builder()
    comp = TopoDS_Compound()
    builder.MakeCompound(comp)

    bt = BRep_Tool()
    ex = TopExp_Explorer(shape, TopAbs_FACE)
    while ex.More():
        face = topods_Face(ex.Current())
        location = TopLoc_Location()
        facing = (bt.Triangulation(face, location)).GetObject()
        tab = facing.Nodes()
        tri = facing.Triangles()
        for i in range(1, facing.NbTriangles() + 1):
            trian = tri.Value(i)
            index1, index2, index3 = trian.Get()
            for j in range(1, 4):
                if j == 1:
                    m = index1
                    n = index2
                elif j == 2:
                    n = index3
                elif j == 3:
                    m = index2
                me = BRepBuilderAPI_MakeEdge(tab.Value(m), tab.Value(n))
                if me.IsDone():
                    builder.Add(comp, me.Edge())
        ex.Next()
    display.EraseAll()
    display.DisplayShape(shape)
    display.DisplayShape(comp, update=True)
Exemple #4
0
# BRep
# ==============================================================================

brep = BRep()
brep.shape = face

# mesh = brep.to_tesselation()

BRepMesh_IncrementalMesh(brep.shape, 0.1, False, 0.1, False)

bt = BRep_Tool()
ex = TopExp_Explorer(brep.shape, TopAbs_FACE)
while ex.More():
    face = topods_Face(ex.Current())
    location = TopLoc_Location()
    facing = (bt.Triangulation(face, location))
    tab = facing.Nodes()
    tri = facing.Triangles()
    for i in range(1, facing.NbTriangles() + 1):
        trian = tri.Value(i)
        index1, index2, index3 = trian.Get()
        # for j in range(1, 4):
        #     if j == 1:
        #         m = index1
        #         n = index2
        #     elif j == 2:
        #         n = index3
        #     elif j == 3:
        #         m = index2
        #     me = BRepBuilderAPI_MakeEdge(tab.Value(m), tab.Value(n))
        #     if me.IsDone():
    def __init__(self, shape):
        from OCC.Core.BRep import BRep_Tool
        from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh
        from OCC.Core.TopAbs import TopAbs_FACE, TopAbs_VERTEX
        from OCC.Core.TopExp import TopExp_Explorer
        from OCC.Core.TopLoc import TopLoc_Location
        from OCC.Core.TopoDS import topods_Face, topods_Vertex, TopoDS_Iterator

        vertices = []  # a (nested) list of vec3
        triangles = []  # a (flat) list of integers
        normals = []
        uv = []

        # Mesh the shape
        linDeflection = 0.8
        BRepMesh_IncrementalMesh(shape, linDeflection)
        bt = BRep_Tool()

        # Explore the faces of the shape
        # each face is triangulated, we need to collect all the parts
        expFac = TopExp_Explorer(shape, TopAbs_FACE)
        while expFac.More():
            face = topods_Face(expFac.Current())
            location = TopLoc_Location()
            facing = (bt.Triangulation(face, location))
            try:
                tri = facing.Triangles()
                nTri = facing.NbTriangles()
                ver = facing.Nodes()
            except:
                tri = None
                nTri = None
                ver = None
            # store origin of the face's local coordinates
            transf = face.Location().Transformation()

            # iterate over triangles and store indices of vertices defining each triangle
            # OCC uses one-based indexing
            for i in range(1, nTri + 1):
                # each triangle is defined by three points
                # each point is defined by its index in the list of vertices
                index1, index2, index3 = tri.Value(i).Get()
                indices = [index1, index2, index3]

                # python uses zero-based indexing
                # for each vertex of a triangle, check whether it is already known
                # then store it (or not) and update the index
                for idx in [0, 1, 2]:
                    # read global coordinates of each point
                    vec3 = [
                        ver.Value(indices[idx]).Transformed(transf).X(),
                        ver.Value(indices[idx]).Transformed(transf).Y(),
                        ver.Value(indices[idx]).Transformed(transf).Z()
                    ]
                    if vec3 not in vertices:
                        vertices.append(vec3)
                    indices[idx] = vertices.index(vec3)
                triangles.extend(indices)
            expFac.Next()

        self.shape = shape
        self.vertices = vertices
        self.triangles = triangles
        self.normals = normals
        self.uv = uv