Code covered by the BSD License  

Highlights from
stlwrite - Write binary or ascii STL file

5.0

5.0 | 8 ratings Rate this file 73 Downloads (last 30 days) File Size: 3.73 KB File ID: #20922
image thumbnail

stlwrite - Write binary or ascii STL file

by Sven

 

30 Jul 2008 (Updated 24 Nov 2011)

Export a variety of inputs (patch, surface) to an STL triangular mesh

| Watch this File

File Information
Description

** Big speed boost courtesy of Oliver Woodford. Recommended to update to latest version **

  STLWRITE(FILE,fv) writes a stereolithography (STL) file to FILE for a triangulated
  patch defined by FV (a structure with fields 'vertices' and 'faces').

  STLWRITE(FILE,FACES,VERTICES) takes faces and vertices separately, rather than in an FV struct

  STLWRITE(FILE,X,Y,Z) creates an STL file from surface data in X, Y, and Z. STLWRITE triangulates
  this gridded data into a triangulated surface using triangulations options specified below. X, Y
  and Z can be two-dimensional arrays with the same size. If X and Y are vectors with length equal
  to SIZE(Z,2) and SIZE(Z,1), respectively, they are passed through MESHGRID to create gridded
  data. If X or Y are scalar values, they are used to specify the X and Y spacing between grid
  points.

  STLWRITE(...,'PropertyName',VALUE,'PropertyName',VALUE,...) writes an STL file using the
  following property values:

  MODE - File is written using 'binary' (default) or 'ascii'.

  TITLE - Header text (max 80 characters) written to the STL file.

  TRIANGULATION - When used with gridded data, TRIANGULATION is either:
                    'delaunay' - (default) Delaunay triangulation of X, Y
                    'f' - Forward slash division of grid quadrilaterals
                    'b' - Back slash division of quadrilaterals
                    'x' - Cross division of quadrilaterals
                  Note that 'f', 'b', or 't' triangulations require FEX entry 28327, "mesh2tri".

  FACECOLOR - (not currently implemented) When used with face/vertex input, specifies the
                  colour of each triangle face. If users request this feature, I will attempt to
                  implement it.

  Example 1:
      % Write binary STL from face/vertex data
      tmpvol = zeros(20,20,20); % Empty voxel volume
      tmpvol(8:12,8:12,5:15) = 1; % Turn some voxels on
      fv = isosurface(tmpvol, 0.99); % Create the patch object
      stlwrite('test.stl',fv) % Save to binary .stl

  Example 2:
      % Write ascii STL from gridded data
      [X,Y] = deal(1:40); % Create grid reference
      Z = peaks(40); % Create grid height
      stlwrite('test.stl',X,Y,Z,'mode','ascii')

  Original idea adapted from surf2stl by Bill McDonald. Huge speed
  improvements implemented by Oliver Woodford. Non-Delaunay triangulation
  of quadrilateral surface input requires mesh2tri by Kevin Moerman.

Note: This entry was originally titled "patch2stl", however has been renamed to "stlwrite" after more general input was added.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
surf2stl, mesh2tri
This submission has inspired the following:
stlwrite - Write binary or ascii STL file

MATLAB release MATLAB 7.13 (2011b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (13)
02 Aug 2008 Scott Burnside

Nice companion to cad2matdemo.m. Very useful.

28 Apr 2009 Georg Stillfried

Worked fine!

02 Jul 2009 Holger Roth

Great! That's what I looked for...

13 Sep 2010 Oliver Woodford

Works well, though very slow (in binary mode at least).

13 Oct 2011 Sven

@Oliver
The latest update has made significant speed improvements with vectorised face normal calculation and combining adjacent fwrite calls. Hope it's useful.

28 Nov 2011 Oliver Woodford

Binary output is now very fast! :)

04 Jan 2012 W Thielicke

Works nicely. For my old Matlab version, I had to remove one line to make it work:
narginchk(2, inf)

14 Jan 2012 ava

Faces are not connected in the result stl. ,Does anyone have any idea?

Thanks

14 Jan 2012 Sven

@ava: This is the first I've encountered such an issue. Does the same thing happen when you run the example in the stlwrite help? If not, try using the splitFV FEX entry to see if the original faces/vertices are indeed connected. If the stlwrite example itself comes out unconnected, what software do you use to read the .stl?

15 Jan 2012 ava

@Sven:I am using STL viewer and also MiniMagics 2.o to see the result file but all the faces are disconnected.

03 Feb 2012 Sven

@ava: glad to hear that the issue (nodes indices) was resolved

11 Mar 2012 Romain W  
04 May 2012 Joachim  
Please login to add a comment or rating.
Updates
11 Oct 2011

Vectorised normal direction calculations and streamlined fprint/fwrite calls. 75% faster writing speeds.

17 Oct 2011

Allowed for more general input types, and added capability for more features to be written to STL file.

24 Nov 2011

Huge speed improvements courtesy of Oliver Woodford. Reduction in write-time is proportional to file size.

Tag Activity for this File
Tag Applied By Date/Time
data import Sven 22 Oct 2008 10:12:38
data export Sven 22 Oct 2008 10:12:38
stl Sven 22 Oct 2008 10:12:38
mesh Sven 22 Oct 2008 10:12:38
trimesh Sven 22 Oct 2008 10:12:38
tri Sven 22 Oct 2008 10:12:38
patch Sven 22 Oct 2008 10:12:38
stl ben harrison 20 Jul 2009 03:20:13
surface Sven 18 Oct 2011 12:08:20

Contact us at files@mathworks.com