numpy-stl topological information of the stl fle read

741 views Asked by At

Using the Python numpy-stl package: reading the .stl files we can get the geometrical information from the triangular mesh, vertex data, normals, areas etc, but can we access the topological information, the connectivity information? I am a newcomer to Python and everything... but there was once this question was asked and did not get any response.

I need to read a triangular stl mesh and plot some of the elements using Pyvista such as the one in the sample!

# Vertices
vertices = np.array([[0, 0, 0],
    [1, 0, 0],
    [1, 1, 0],
    [0, 1, 0],
    [0.5, 0.5, -1]])

-> This I can

# mesh faces
faces = np.hstack([[4, 0, 1, 2, 3],  # square
    [3, 0, 1, 4],                    # triangle
    [3, 1, 2, 4]])                   # triangle

-> This I cannot.

Is it necessary to define the data structure to generate the connectivity information??

Thank you for reading!

1

There are 1 answers

1
mmusy On

This test gives me the expected result:

from vedo import Mesh
vertices = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0.5, 0.5, -1]]
faces = [[0, 1, 2, 3], [0, 1, 4], [1, 2, 4]]
Mesh([vertices, faces]).cmap('viridis', range(3), on='cells').show()

import numpy as np 
from pyvista import PolyData
vertices = np.array(vertices)
faces = np.hstack([[4, 0, 1, 2, 3],  # square
                   [3, 0, 1, 4],     # triangle
                   [3, 1, 2, 4]])    # triangle
PolyData(vertices, faces).plot(scalars=np.arange(3))

what error message do you get?