# stl export for geometry from pointcloud/array

6 views (last 30 days)
Kim on 13 Oct 2020
Commented: Kim on 14 Oct 2020
Hello,
I want to export a geometry to stl. Here I used a very simple example but it's comparable with my application since I have a geometry defined by a pointcloud.
d = [0 1];
[x,y,z] = meshgrid(d,d,d); % a cube
x = [x(:);0];
y = [y(:);0];
z = [z(:);0];
DT = delaunayTriangulation(x,y,z);
tetramesh(DT);
camorbit(20,0)
I found in a forum that you could use this expression:
but it didn't work and according to the Matlab website, stlwrite has to have the following structure:
stlwrite(TR,filename)
where TR is a triangulation object. Following this logic, I tried to implement
but I get the error: "Tetrahedron triangulation is not supported."
The following version "works" in a sense that I get an export but it's just 2D instead of 3D
T = delaunay(x,y);
tri = triangulation(T,x,y,z);
Could you please tell me, where I went wrong and what I have to do to solve this?

Michael Croucher on 13 Oct 2020
I should start this answer with 'I have no idea what I'm doing here'. I found your question interesting and have had a go at it with a combination of googling and experimentation. As such, this may be a terrible way to proceed but I seem to have got somewhere
The first complication is that there are at least two stlwrite functions:
. I think that your forum posts were referring to the FEX version which is older. For what follows, I use the FEX version
d = [0 1];
[x,y,z] = meshgrid(d,d,d); % a cube
x = [x(:);0];
y = [y(:);0];
z = [z(:);0];
I used a free stl viewer to look at the result and the wireframe returned was
I chose ASCII output so we can look at it. Probably want to use binary output (the default) for big files. The contents of the file are
solid Created by stlwrite.m 13-Oct-2020 16:32:19
facet normal 0.0000000E+00 0.0000000E+00 -1.0000000E+00
outer loop
vertex 0.0000000E+00 0.0000000E+00 0.0000000E+00
vertex 0.0000000E+00 1.0000000E+00 0.0000000E+00
vertex 1.0000000E+00 0.0000000E+00 0.0000000E+00
endloop
endfacet
facet normal 0.0000000E+00 -1.0000000E+00 0.0000000E+00
outer loop
vertex 0.0000000E+00 0.0000000E+00 1.0000000E+00
vertex 0.0000000E+00 0.0000000E+00 0.0000000E+00
vertex 1.0000000E+00 0.0000000E+00 0.0000000E+00
endloop
endfacet
facet normal -1.0000000E+00 0.0000000E+00 0.0000000E+00
outer loop
vertex 0.0000000E+00 0.0000000E+00 1.0000000E+00
vertex 0.0000000E+00 1.0000000E+00 0.0000000E+00
vertex 0.0000000E+00 0.0000000E+00 0.0000000E+00
endloop
endfacet
facet normal -1.0000000E+00 -0.0000000E+00 -0.0000000E+00
outer loop
vertex 0.0000000E+00 1.0000000E+00 1.0000000E+00
vertex 0.0000000E+00 1.0000000E+00 0.0000000E+00
vertex 0.0000000E+00 0.0000000E+00 1.0000000E+00
endloop
endfacet
facet normal -0.0000000E+00 -1.0000000E+00 -0.0000000E+00
outer loop
vertex 1.0000000E+00 0.0000000E+00 1.0000000E+00
vertex 0.0000000E+00 0.0000000E+00 1.0000000E+00
vertex 1.0000000E+00 0.0000000E+00 0.0000000E+00
endloop
endfacet
facet normal 0.0000000E+00 0.0000000E+00 1.0000000E+00
outer loop
vertex 0.0000000E+00 1.0000000E+00 1.0000000E+00
vertex 0.0000000E+00 0.0000000E+00 1.0000000E+00
vertex 1.0000000E+00 0.0000000E+00 1.0000000E+00
endloop
endfacet
facet normal 1.0000000E+00 0.0000000E+00 0.0000000E+00
outer loop
vertex 1.0000000E+00 1.0000000E+00 0.0000000E+00
vertex 1.0000000E+00 0.0000000E+00 1.0000000E+00
vertex 1.0000000E+00 0.0000000E+00 0.0000000E+00
endloop
endfacet
facet normal 0.0000000E+00 0.0000000E+00 -1.0000000E+00
outer loop
vertex 0.0000000E+00 1.0000000E+00 0.0000000E+00
vertex 1.0000000E+00 1.0000000E+00 0.0000000E+00
vertex 1.0000000E+00 0.0000000E+00 0.0000000E+00
endloop
endfacet
facet normal 0.0000000E+00 1.0000000E+00 0.0000000E+00
outer loop
vertex 0.0000000E+00 1.0000000E+00 1.0000000E+00
vertex 1.0000000E+00 1.0000000E+00 0.0000000E+00
vertex 0.0000000E+00 1.0000000E+00 0.0000000E+00
endloop
endfacet
facet normal 1.0000000E+00 0.0000000E+00 0.0000000E+00
outer loop
vertex 1.0000000E+00 1.0000000E+00 1.0000000E+00
vertex 1.0000000E+00 0.0000000E+00 1.0000000E+00
vertex 1.0000000E+00 1.0000000E+00 0.0000000E+00
endloop
endfacet
facet normal -0.0000000E+00 1.0000000E+00 0.0000000E+00
outer loop
vertex 0.0000000E+00 1.0000000E+00 1.0000000E+00
vertex 1.0000000E+00 1.0000000E+00 1.0000000E+00
vertex 1.0000000E+00 1.0000000E+00 0.0000000E+00
endloop
endfacet
facet normal -0.0000000E+00 0.0000000E+00 1.0000000E+00
outer loop
vertex 0.0000000E+00 1.0000000E+00 1.0000000E+00
vertex 1.0000000E+00 0.0000000E+00 1.0000000E+00
vertex 1.0000000E+00 1.0000000E+00 1.0000000E+00
endloop
endfacet
endsolid Created by stlwrite.m 13-Oct-2020 16:32:19

#### 1 Comment

Kim on 14 Oct 2020
Thank you very much, Matlab took indeed the build-in function 'stlwrite'. If I call specifically the function of the FEX toolbox, it works just fine.

R2020a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!