def draw_shape_mpl(shape):
    """
    Draw a TopoDS_Shape with matplotlib
    """

    tess = ShapeTesselator(shape)
    tess.Compute()

    triangles = []
    edges = []

    # get the triangles
    triangle_count = tess.ObjGetTriangleCount()
    for i_triangle in range(0, triangle_count):
        i1, i2, i3 = tess.GetTriangleIndex(i_triangle)
        triangles.append(
            [tess.GetVertex(i1),
             tess.GetVertex(i2),
             tess.GetVertex(i3)])

    # get the edges
    edge_count = tess.ObjGetEdgeCount()
    for i_edge in range(0, edge_count):
        vertex_count = tess.ObjEdgeGetVertexCount(i_edge)
        edge = []
        for i_vertex in range(0, vertex_count):
            vertex = tess.GetEdgeVertex(i_edge, i_vertex)
            edge.append(vertex)
        edges.append(edge)

    # plot it
    fig = plt.figure()
    ax = Axes3D(fig)

    ax.add_collection3d(Poly3DCollection(triangles, linewidths=0.2, alpha=0.5))
    ax.add_collection3d(Line3DCollection(edges, colors='w', linewidths=1.0))

    ax.get_xaxis().set_visible(True)
    ax.get_yaxis().set_visible(True)
    ax.set_autoscale_on(True)
    plt.show()
Beispiel #2
0
    def parse_shape(self, geometry):
        # compute the tessellation
        tess = ShapeTesselator(geometry)
        tess.Compute(compute_edges=True)
        # tess.Compute(compute_edges=False, mesh_quality=1.0, parallel=True)

        # get the vertices
        vertices = []
        vertex_count = tess.ObjGetVertexCount()
        for i_vertex in range(0, vertex_count):
            i1, i2, i3 = tess.GetVertex(i_vertex)
            vertices.append(i1)
            vertices.append(i2)
            vertices.append(i3)

        # get the normals
        normals = []
        normals_count = tess.ObjGetNormalCount()
        for i_normal in range(0, normals_count):
            i1, i2, i3 = tess.GetNormal(i_normal)
            normals.append(i1)
            normals.append(i2)
            normals.append(i3)

        # get the triangles
        triangles = []
        triangle_count = tess.ObjGetTriangleCount()
        for i_triangle in range(0, triangle_count):
            i1, i2, i3 = tess.GetTriangleIndex(i_triangle)
            triangles.append(i1)
            triangles.append(i2)
            triangles.append(i3)

        # get the edges
        edges = []
        edge_count = tess.ObjGetEdgeCount()
        for i_edge in range(0, edge_count):
            vertex_count = tess.ObjEdgeGetVertexCount(i_edge)
            # edge = []
            for i_vertex in range(0, vertex_count):
                vertex = tess.GetEdgeVertex(i_edge, i_vertex)
                # edge.append(vertex)
                # edges.append(i_vertex)
                edges.append(vertex[0])
                edges.append(vertex[1])
                edges.append(vertex[2])
            # edges.append(edge)

        return vertices, normals, triangles, edges
Beispiel #3
0
    def to_tesselation(self) -> Mesh:
        """Create a tesselation of the shape for visualisation.

        Returns
        -------
        :class:`~compas.datastructures.Mesh`

        """
        tesselation = ShapeTesselator(self.shape)
        tesselation.Compute()
        vertices = [
            tesselation.GetVertex(i)
            for i in range(tesselation.ObjGetVertexCount())
        ]
        triangles = [
            tesselation.GetTriangleIndex(i)
            for i in range(tesselation.ObjGetTriangleCount())
        ]
        return Mesh.from_vertices_and_faces(vertices, triangles)
Beispiel #4
0
    def to_tesselation(self):
        """Convert the surface to a triangle mesh.

        Returns
        -------
        :class:`~compas.datastructures.Mesh`

        """
        from OCC.Core.Tesselator import ShapeTesselator

        tess = ShapeTesselator(self.occ_shape)
        tess.Compute()
        vertices = []
        triangles = []
        for i in range(tess.ObjGetVertexCount()):
            vertices.append(tess.GetVertex(i))
        for i in range(tess.ObjGetTriangleCount()):
            triangles.append(tess.GetTriangleIndex(i))
        return Mesh.from_vertices_and_faces(vertices, triangles)
Beispiel #5
0
    face = BRepBuilderAPI_MakeFace(brep.Face()).Face()
    builder.Add(shell, face)

# ==============================================================================
# Tesselation
# ==============================================================================

tess = ShapeTesselator(shell)
tess.Compute()

vertices = []
triangles = []

for i in range(tess.ObjGetVertexCount()):
    xyz = tess.GetVertex(i)
    vertices.append(xyz)

for i in range(tess.ObjGetTriangleCount()):
    a, b, c = tess.GetTriangleIndex(i)
    triangles.append([a, b, c])

mesh = Mesh.from_vertices_and_faces(vertices, triangles)

# ==============================================================================
# Viz
# ==============================================================================

print(mesh.number_of_vertices())
print(mesh.number_of_faces())