MATLAB Answers

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

조회 수: 46(최근 30일)
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
Tonino Nucifora
Tonino Nucifora 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));

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

채택된 답변

Arpit Bhatia
Arpit Bhatia 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')
  댓글 수: 3
Abdelmadjid Boualleg
Abdelmadjid Boualleg 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.

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

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by