How to generate a .stl file starting from a 3d figure

136 views (last 30 days)
Hi everyone,
I'm dealing with the following problem:
I need to generate an .stl of the 3d figure that I have achieved with the following code (the one shown here below). I saw that a possible oprion is to use the function "surf2stl" however I'm unable to implement its inputs.
Does anyone know how to write the inputs of this function?
Any other suggestions are really appreciate.
Thanks for the attenction.
Vcub=[0 0 0; 1 0 0; 0 1 0; 0 0 1;1 1 1; 0 1 1; 1 0 1; 1 1 0];
%Vcub(:,2)=2*Vcub(:,2);
N=100;
seeds=1.5*rand(N,3)-0.25;
[Ver,Cel,C_tst]=voronoi3d_cuboid(seeds,Vcub);
figure
hold on
axis('equal')
view([-36 27])
for k = 1:length(Cel)
if ~isempty(Cel{k})
col= rand(1,3);
Vk = Ver(Cel{k},:);
Fk = convhull(Vk);
if exist('mergeCoplanarFaces.m','file')==2
[Vk, Fk] = mergeCoplanarFaces(Vk, Fk);
for i=1:length(Fk)
patch('Vertices',Vk,'Faces',Fk{i},'FaceColor',col,'FaceAlpha',0,'LineWidth',3,'EdgeAlpha',1,'EdgeColor','k')
end
else
trisurf(Fk,Vk(:,1),Vk(:,2),Vk(:,3),'FaceColor',col, ...
'FaceAlpha', 1,'EdgeAlpha',1,'EdgeColor','k');
end
end
end
xlabel('X');
ylabel('Y');
zlabel('Z');
  2 Comments
Tonino Nucifora
Tonino Nucifora on 23 Oct 2020
The error the software gives to me is the following:
Error using stlwrite (line 25)
Input argument must be a triangulation object.
Error in demo_voronoi3d_cuboid (line 42)
stlwrite('mytriangulation.stl',struct('faces',Fk, 'vertices',Vk));
What is wrong?
Thank you
Vcub=[0 0 0; 1 0 0; 0 1 0; 0 0 1;1 1 1; 0 1 1; 1 0 1; 1 1 0];
%Vcub(:,2)=2*Vcub(:,2);
N=100;
seeds=1.5*rand(N,3)-0.25;
[Ver,Cel,C_tst]=voronoi3d_cuboid(seeds,Vcub);
figure
hold on
axis('equal')
view([-36 27]) %3D
% scatter3(seeds(:,1),seeds(:,2),seeds(:,3),25, ...
% 'Marker','o','MarkerFaceColor',[1 0 0], 'MarkerEdgeColor','k');
% scatter3(Ver(:,1),Ver(:,2),Ver(:,3),25, ...
% 'Marker','o','MarkerFaceColor',[0 1 0], 'MarkerEdgeColor','r');
for k = 1:length(Cel)
if ~isempty(Cel{k})
col= rand(1,3);
Vk = Ver(Cel{k},:);
Fk = convhull(Vk);
if exist('mergeCoplanarFaces.m','file')==2
[Vk, Fk] = mergeCoplanarFaces(Vk, Fk);
for i=1:length(Fk)
h=patch('Vertices',Vk,'Faces',Fk{i},'FaceColor',col,'FaceAlpha',0.6,'LineWidth',3,'EdgeAlpha',1,'EdgeColor','k');
end
else
trisurf(Fk,Vk(:,1),Vk(:,2),Vk(:,3),'FaceColor',col, ...
'FaceAlpha', 0.5,'EdgeAlpha',1,'EdgeColor','k','LineWidth',5);
end
end
end
xlabel('X');
ylabel('Y');
zlabel('Z');
stlwrite('mytriangulation.stl',struct('faces',h.Fk, 'vertices',h.Vk));

Sign in to comment.

Accepted Answer

Arpit Bhatia
Arpit Bhatia on 27 Oct 2020
Hi Tonino,
You need to download the stlwrite.m file from the following link: https://in.mathworks.com/matlabcentral/fileexchange/20922-stlwrite-write-ascii-or-binary-stl-files
(Please note that this file has not been created by or maintained by MathWorks)
With the stlwrite.m file in the same folder as the file with the above code, add the following command to the end of your code to generate the required stl file.
stlwrite('mytriangulation.stl',struct('faces', Fk, 'vertices',Vk));
The reason you were earlier facing the error “Input argument must be a triangulation object” is that you were calling the default MATLAB stlwrite function which requires a triangulation as an input argument. To use that function, you will need to first create a triangulation as specified below.
TO = triangulation(Fk,Vk(:,1),Vk(:,2),Vk(:,3));
trisurf(TO,'FaceColor',col, ...
'FaceAlpha', 1,'EdgeAlpha',1,'EdgeColor','k');
stlwrite(TO,'test.stl')
  4 Comments
Abdelmadjid Boualleg
Abdelmadjid Boualleg on 16 Mar 2021
Edited: Abdelmadjid Boualleg on 16 Mar 2021
Hi Arpit Bhatia
Thank you for your help above regarding the provided information about Stlwrite for Voronoi 3D, but when I used that function, it gives me Stl file with triangulation edges, not Voronoi edges as you can see in the attached picture, therefore I kindly ask you to help me for getting the Stl of Voronoi not triangulation. Thank you very much in advance.
Akshay Kumar Pakala
Akshay Kumar Pakala on 10 Mar 2022
Hey Arpita Bhatia
I would like to know if the stlwrite function works to create the entire voronoi cuboid if i store the Vk and Fk matrices at every loop into 2 seperate cells or in two different multi-dimensional dimensional matrix respectively?
Thank you

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!