Turn 3D co-ordinates into a solid and export to STL file

조회 수: 15 (최근 30일)
nathan welch
nathan welch 2020년 3월 4일
댓글: DGM 2025년 7월 18일
I've looked and I cannot find anything that solves the following problem:
Given a shape that is described by points in 3D space, create an STL file that will allow a CAD package to read in this 3D solid shape.
The bit that I'm confused with is that all the use-cases and examples are obsessed with creating infinitely thin, open 2D surfaces and writing these out to an STL file. Why? An open, infinitely thin 2D surface is an impossible object that cannot exist, why would you want to import it into a practical piece of software like a CAD package?
Surely the primary use-case is creating a real-world 3D object that is described by a closed surface.
As an example:
I have a unit cube given by 8 points in 3D space:
x = [0, 1, 1, 0, 0, 1, 1, 0];
y = [0, 0, 1, 1, 1, 1, 0, 0];
z = [0, 0, 0, 0, 1, 1, 1, 1];
How do I turn this into an STL that say Solidworks can import to create this unit cube?
Note that I will want to create more complex shapes in future, so I don't want to create a 2D surface and extrude or similar. Instead, as above, I can create a set of 3D points that define the object and then use convex hull or similar to describe the shape. Similarly, creating 6 separate open 2D surfaces and gluing them together probably isn't a very extensible or generally applicable method for other shapes.
Any help would be greatly appreciated, additionally, as I appear to missing something - any discussion on why the above isn't the main use-case and work flow would be great to hear.
  댓글 수: 10
alan cheville
alan cheville 2022년 6월 14일
Try this out and see if it helps with what you are trying to do.
% create a cube for 3D printing
clf
% set vertices of cube
V = [1 1 1;1 1 0;1 0 1;1 0 0;0 1 1;0 1 0;0 0 1;0 0 0];
% plot the vertices to help with face identification
bogus = size(V);
for m = 1:bogus(1);
a = text(V(m,1),V(m,2),V(m,3),num2str(m));
end
axis([-1 2 -1 2 -1 2])
grid
rotate3d on
xlabel('X')
ylabel('Y')
zlabel('Z')
pause
% define faces as triangles with surface vector pointing out using RHR
f = [2 8 6
2 4 8
4 2 1
4 1 3
2 6 5
2 5 1
8 5 6
8 7 5
8 4 3
8 3 7
7 3 1
7 1 5]; % correct normals using RHR to all be out, check!
% set up struct variable and write the file
fv = struct('faces',f,'vertices',V);
% this is a free function on Matlab named STLWRITE.m, I renamed it
write_stl_file('ascii_cube.txt',fv,'mode','ascii')
% Look in real time what the demo surface looks like.
axis([-1 2 -1 2 -1 2])
hold on
for m = 1:max(size(fv.faces));
fp = fv.faces(m,:);
xx = colormap;
fc = xx(5*ceil(m/80),:);
a = patch('Faces',fp,'Vertices',fv.vertices);
set(a,'FaceAlpha',.25) %transparent
set(a,'EdgeColor',fc)
pause(.5)
end
hold off
DGM
DGM 2025년 7월 18일
@alan cheville's example uses FEX #20922.
In modern versions, it's not necessary. You already have stlread() and stlwrite() built-in.
% use a triangulation object
T = triangulation(f,v);
% use the native encoder
stlwrite(T,'cube.stl')
As an aside, I don't think it's a good idea to get in the habit of using ASCII encoding unless you know that you need it. It wastes a colossal amount of space, and in order to mitigate the cost of using ASCII, some encoders will reduce data precision when using ASCII encoding instead of binary.
Also, you're writing an STL file, not a text file. Use the correct extension unless you're trying to confuse people.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 STL (STereoLithography)에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by