Using matlab to generate complex graphics stl file
3 views (last 30 days)
Show older comments
Hi, I have been able to use matlab to generate cuboid stl files, but the stl files of concave bodies always make mistakes, because there are always problems in the triangle division of concave surface. Could you please tell me how to automatically divide triangles that can generate stl files on concave surface.I would really appreciate it if you could help me.
The generated cuboid stl file

The graph you want to generate

0 Comments
Accepted Answer
DGM
on 2 Apr 2025
Edited: DGM
on 2 Apr 2025
For a simple "sandwich" model composed of two roughly flat and parallell profiled sides connected by a ribbon around their boundary, it's easy enough if you can triangulate one of the flat sides.
% some parameters
d = [4 1 2]; % block dimensions [x y z]
s = [1 1]; % slot dimensions [x z]
os = 1; % slot offset on x
% vertices and faces for the front side
V = [0 0 0; d(1) 0 0; d(1) 0 d(3); s(1)+os 0 d(3); ...
s(1)+os 0 d(3)-s(2); os 0 d(3)-s(2); os 0 d(3); 0 0 d(3)];
F = [6 7 8; 1 6 8; 1 2 6; 6 2 5; 2 3 5; 3 4 5];
% create the back side faces and vertices
nverts = size(V,1);
V = [V; V]; % duplicate (x and z stay the same)
V(nverts+1:end,2) = d(2); % offset y
F = [F; fliplr(F) + nverts]; % flip the winding and offset the indices
% since our object is two parallel faces with the same number of
% boundary vertices, we can just stitch those together (lofting).
% our boundary vertex list is simply sequential
va = (1:nverts).'; % front
vb = va + nverts; % back
vc = circshift(va,-1); % front
vd = vc + nverts; % back
F = [F; va vb vc; vd vc vb]; % add them to the list
% write to file
stlwrite(triangulation(F,V),'testfile.stl')
% display it using patch()
patch('faces',F,'vertices',V, ...
'facecolor','w','edgecolor','k');
view(3); camlight; view(10,33)
axis equal; grid on
xlabel('X'); ylabel('Y'); zlabel('Z')
If you're asking how to do CSG on an existing model, that's something else.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!