stlwrite

Writes a triangulation to an STL file.

Category: Mathematics > Computational Geometry

Syntax

stlwrite(TR, filename)

Writes the triangulation in binary STL format.

stlwrite(TR, filename, 'text')

Writes human-readable ASCII STL instead.

Description

Every triangle is written with its three vertices and its unit normal, computed from the vertex order by the right-hand rule. The vertex order therefore decides which side counts as outside, and a closed solid should have its triangles ordered consistently.

Binary files are compact: 84 bytes of header plus 50 bytes per triangle. ASCII files are several times larger but can be read in a text editor.

Input arguments

TR
A triangulation object of triangles in 3-D space.
filename
Output file name, usually ending in .stl. An existing file is overwritten.
'text'
Switch to the ASCII format. Binary is the default.

Example

Binary and text files

P = [0 0 0; 1 0 0; 0 1 0; 0 0 1];
T = [1 3 2; 1 2 4; 1 4 3; 2 3 4];
tr = triangulation(T, P);
stlwrite(tr, 'tet_bin.stl');
stlwrite(tr, 'tet_txt.stl', 'text');
d1 = dir('tet_bin.stl');
disp(d1.bytes)
Output
   284

Notes

stlwrite takes a triangulation object, as in MATLAB. Passing faces and vertices separately raises an error.

See also

stlread triangulation faceNormal trisurf