Skip to content

hertzforge.io.stl

stl

Read and write STL triangle meshes, with the standard library only.

An STL file is already tessellated by the CAD program that exported it, so importing one is reading the triangles and giving them to the geometry as face elements. The format has no units, so the object imports unit-less and the units pane asks for them, as for any other source without units. The binary form also has no part names, so a binary file becomes one element group named for the file. ASCII STL can hold several solid <name> sections, and each becomes its own named group, but CAD programs rarely write it; 3MF (threemf) is the format that keeps an assembly's parts. STL is read because nearly every CAD program can export it.

Vertices are repeated for each triangle in the file; identical coordinates are merged on import so that triangles with a shared edge share its nodes.

Functions:

Name Description
sniff

True for a path ending in .stl, in any case.

mesh_geometry

A Geometry of triangle face elements from [(name, (n, 3, 3) corner

load

Read an STL file as a geometry of triangle face elements.

faces_as_triangles

[(element group id, (n, 3, 3) corners)] from the face elements.

handles

True for a Geometry. save refuses one with no face elements, which

save

Write the geometry's face elements as binary STL (32-bit floats).

Classes

Functions:

sniff

sniff(path: str | PathLike) -> bool

True for a path ending in .stl, in any case.

The suffix is the only check: a binary STL opens with an arbitrary 80-byte header, so there is no signature to look for.

mesh_geometry

mesh_geometry(parts: list[tuple[str, ndarray]], length_unit: str | None = None) -> Geometry

A Geometry of triangle face elements from [(name, (n, 3, 3) corner arrays)]: one named element group per part, with vertices merged where their coordinates are identical.

length_unit declares the unit of the coordinates; without it the geometry is unit-less.

load

load(path: str | PathLike, length_unit: str | None = None) -> Geometry

Read an STL file as a geometry of triangle face elements.

Parameters:

Name Type Description Default
path str or path - like

The .stl file, binary or ASCII.

required
length_unit str

The unit of the file's coordinates, which STL cannot state. Without it the geometry imports unit-less and the units can be defined later.

None

Returns:

Type Description
Geometry

The triangles as face elements: one element group per ASCII solid, named as the file names it, or one group named for the file. A solid with no name is also named for the file.

Raises:

Type Description
ValueError

When the file holds no triangles.

faces_as_triangles

faces_as_triangles(geometry: Geometry) -> list[tuple[int, ndarray]]

[(element group id, (n, 3, 3) corners)] from the face elements.

Quads are split along a diagonal, higher-order faces are taken by their corner nodes, and line and volume elements are left out.

handles

handles(obj: Any) -> bool

True for a Geometry. save refuses one with no face elements, which this does not check.

save

save(obj: Geometry, path: str | PathLike, unit_system: Any = None) -> None

Write the geometry's face elements as binary STL (32-bit floats).

STL cannot name parts or declare units, so every element group is written into one solid. Without unit_system the coordinates are written as stored, which is meters when the geometry's units are defined. With one, defined coordinates are converted from meters to its length unit; undefined ones are written as they stand. A geometry with no face elements is refused with ValueError, since STL holds triangles and nothing else.