How to write multiple object surfaces/solids to a single .stl file?

6 次查看(过去 30 天)
I have multiple object surfaces (refer to the image attached below) in Matlab defined by X,Y,Z coordinate data and Triangle connectivity (T). I am using stlwrite function to create a stl file from triangulation object. It can write only one triangulation object surface to a stl file. So if I have 100's of objects, I will have to create that many stl files. But, Is there a way to write all object surfaces into a single stl file? Thank you.

回答(2 个)

Pratyush Swain
Pratyush Swain 2024-4-18
Hi Vinit,
I understand you want to write multiple object surfaces to a single stl file. I believe this MATLAB answer thread will be helpful for your usecase: https://www.mathworks.com/matlabcentral/answers/701737-how-could-i-generate-several-ellipsoids-in-a-single-stl-file#comment_1227172.
In the above case , the user has successfully written two ellipsoid figures into an stl file. He creates triangulation object and applies alphaShape function for each ellipsoid after which the boundary faces are extracted and new set of triangulation is established,then creates a structure to store faces and vertices of each ellipsoid and a final triangulation of all figures is forged and written to file.
I am hopeful going through above thread will provide you with helpful insights for your task.

DGM
DGM 2025-7-15
编辑:DGM 2025-7-17
If you already have a set of triangulation objects, or multiple sets of triangulated F,V data, the rest is fairly simple. It's little more than concatenation and some index offsets.
% you already have an array of triangulation objects
% so let's just make one for testing
p0 = 2.8; p = [1/p0 1 p0]; os = 2.5;
Tc = cell(numel(p),1);
for k = 1:numel(p)
[X Y Z] = superquad(p(k),1,48);
[F V] = surf2patch(X + os*(k-1),Y,Z,'triangles');
Tc{k} = triangulation(F,V);
end
% combine everything into one triangulation object
T = tricat(Tc); % attached
% write it
stlwrite(T,'test.stl')
% read it back for show and tell
T = stlread('test.stl');
[F V] = t2fv(T);
patch('faces',F,'vertices',V,'facecolor',[0.2 0.8 1]*0.8,'edgecolor','none');
view(3); view(-35,15); camlight;
axis equal; grid on
If you had (e.g.) a cell array of F,V lists instead of a cell array ot triangulation objects, the same process applies. Get F and V for each part, combine them with the output lists.
Depending on the context, you might want to prune redundant vertices from the F,V data after it's consolidated. In this case, there are none.
EDIT: I updated this example to use tricat() to combine the data. That will work for triangulation objects, F,V lists, or a number of other types of data. The output vertex list is automatically pruned (again, that doesn't matter in this example).

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by