Skip to content

stl

Read and Write STL Binary and ASCII Files.

Classes

STLMesh()

Bases: object

Container for STL mesh data.

Source code in pyrevitlib/pyrevit/interop/stl.py
def __init__(self):
    self.triangles = []

Attributes

triangles = [] instance-attribute

Functions

add_triangle(normal, vertices)

Add a triangle to the mesh.

Parameters:

Name Type Description Default
normal

tuple of 3 floats (nx, ny, nz)

required
vertices

list of 3 tuples, each with 3 floats (x, y, z)

required
Source code in pyrevitlib/pyrevit/interop/stl.py
def add_triangle(self, normal, vertices):
    """Add a triangle to the mesh.

    Args:
        normal: tuple of 3 floats (nx, ny, nz)
        vertices: list of 3 tuples, each with 3 floats (x, y, z)
    """
    self.triangles.append({"normal": normal, "vertices": vertices})

Functions

load(filepath)

Load an STL file (binary or ASCII).

Parameters:

Name Type Description Default
filepath

path to the STL file

required

Returns:

Type Description

STLMesh object containing the mesh data

Source code in pyrevitlib/pyrevit/interop/stl.py
def load(filepath):
    """Load an STL file (binary or ASCII).

    Args:
        filepath: path to the STL file

    Returns:
        STLMesh object containing the mesh data
    """
    if not op.exists(filepath):
        raise IOError("File not found: {0}".format(filepath))

    # Try to determine if file is binary or ASCII
    with open(filepath, "rb") as f:
        header = f.read(80)
        # ASCII files start with "solid"
        if header.startswith(b"solid") or header.startswith(b"SOLID"):
            # Could be ASCII, but need to verify
            f.seek(0)
            try:
                # Try reading as ASCII
                first_line = f.readline().decode("ascii", errors="strict")
                if first_line.strip().startswith("solid"):
                    f.seek(0)
                    return _load_ascii(f)
            except (UnicodeDecodeError, ValueError):
                pass

        # If not ASCII, load as binary
        f.seek(0)
        return _load_binary(f)

dump(outputfile, mesh)

Save mesh to binary STL file.

Parameters:

Name Type Description Default
outputfile

path to output file

required
mesh

STLMesh object to save

required
Source code in pyrevitlib/pyrevit/interop/stl.py
def dump(outputfile, mesh):
    """Save mesh to binary STL file.

    Args:
        outputfile: path to output file
        mesh: STLMesh object to save
    """
    with open(outputfile, "wb") as f:
        # Write 80-byte header
        header = b"Binary STL file generated by pyRevit"
        header = header + b" " * (80 - len(header))
        f.write(header[:80])

        # Write triangle count
        f.write(struct.pack("<I", len(mesh.triangles)))

        # Write each triangle
        for triangle in mesh.triangles:
            # Write normal
            f.write(struct.pack("<3f", *triangle["normal"]))

            # Write vertices
            for vertex in triangle["vertices"]:
                f.write(struct.pack("<3f", *vertex))

            # Write attribute byte count (0)
            f.write(struct.pack("<H", 0))