Main Content

stlwrite

Create STL file from triangulation

Description

stlwrite(TR,filename) writes a triangulation TR to a binary STL file filename. The triangulation can be either a triangulation object or a 2-D delaunayTriangulation object.

example

stlwrite(TR,filename,fileformat) also specifies a file format for the written file. fileformat can be either 'binary' (default) or 'text'.

example

stlwrite(___,Name,Value) specifies additional options for writing to the STL file using one or more Name,Value pair arguments for either of the previous syntaxes. For example, stlwrite(TR,'stlbinary','Attribute',attributes) also writes a uint16 vector of attributes for each triangle in TR.

Examples

collapse all

Create a triangulation representing two intersecting rings and write the result to an STL file.

To represent the first ring, define a torus surface with the internal radius of 1 and external radius of 5.

R = 5;
r = 1;
angle = linspace(0,2*pi,50);
angle(end) = []; % Omit the repeated point for 0 and 2pi
[theta,omega] = meshgrid(angle,angle);

Define the coordinates x, y, and z to represent a torus.

X = (R+r.*cos(theta)).*cos(omega);
Y = (R+r.*cos(theta)).*sin(omega);
Z = r.*sin(theta);

Create an alphaShape object with an alpha radius of 2.

shp = alphaShape(X(:),Y(:),Z(:),2);

Extract triangulation from the boundary surface of alphaShape. This triangulation represents the first ring.

[tri,xyz] = shp.boundaryFacets;
t1 = triangulation(tri,xyz);

Define the second ring by rotating the triangulation.

t2 = triangulation(tri,xyz*[1 0 0;0 0 1;0 1 0] + [4 0 0]);

Combine the two triangulations.

Points = [t1.Points;t2.Points];
Connectivity = ...
[t1.ConnectivityList;
    t2.ConnectivityList + size(t1.Points,1)];
trCombined = triangulation(Connectivity,Points);

Plot the resulting triangulation.

p = trisurf(trCombined);
p.EdgeAlpha = 0.4;

Figure contains an axes object. The axes object contains an object of type patch.

Create an STL file from the triangulation.

stlwrite(trCombined,"Rings.stl");

Create and plot a 2-D triangulation object.

P = [2.5 8.0;
     6.5 8.0;
     2.5 5.0;
     6.5 5.0;
     1.0 6.5;
     8.0 6.5];
T = [5 3 1;
     3 2 1;
     3 4 2;
     4 6 2];
TR = triangulation(T,P);
triplot(TR)

Figure contains an axes object. The axes object contains an object of type line.

Write the triangulation to a text file named tritext.stl.

stlwrite(TR,"tritext.stl","text")

Input Arguments

collapse all

Triangulation, specified as a triangulation object or a 2-D delaunayTriangulation object.

STL file name, specified as a character vector or scalar string. The file name must end with the .stl or .STL extension.

Example: 'stltextfile.stl'

File format, specified as either 'binary' or 'text'.

Name-Value Arguments

collapse all

Example: stlwrite(TR,'stltext','SolidIndex',solidIDs)

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify the name and value pair arguments in any order as Name1,Value1,Name2,Value2.

Binary attributes, specified as a uint16 vector. When the input file is a binary file, attributes can contain coded information about the triangles. Its length must be equal to the number of triangles in the triangulation. This parameter is not supported when the input file is a text file.

Solid grouping index, specified as a vector of identification numbers. When the input file is a text file, the identification numbers must assign each triangle to a grouping of triangles in the triangulation. The length of the vector must be equal to the number of triangles in the triangulation. This parameter is not supported when the input file is binary.

Extended Capabilities

expand all

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2018b