Writing a .stl file from a plot

9 次查看(过去 30 天)
Willow Miller
Willow Miller 2014-10-27
编辑: DGM 2025-6-29
Hey guys,
I'm working on a project that creates a 3d plot, and presents it in .stl format so it can be 3d printed. For background, the object I am creating is a brake disc.
I have no problem creating a 3d plot, but I am having trouble with the conversion to .stl. When I open the file in Autodesk Inventor it is a 2d circle with no detail. I am using an stl write program that I got from here (<http://www.mathworks.co.uk/matlabcentral/fileexchange/20922-stlwrite-write-binary-or-ascii-stl-file)>.
One of my friends recommended that adding 'triangulation' , 'f', would help, but my data isn't in a suitable grid format.
Could anyone recommend a solution? Either finding a new way to write the stl file or getting my data into grid format.
I would be very appreciative!
Thanks for all your help,
Willow x
I've attached my code below:
axis equal
hold on
inner_radius = 50;
outer_radius = 300;
hole_radius = 20;
disc_thickness = 20
segment_number = 12;
circles = linspace (0 , 2*pi , 100);
zb=zeros(1,100);
zt=ones(1,100);
zt(1:end) = disc_thickness;
angle_to_hole = (pi/segment_number);
x=[];
y=[];
z=[];
for segment = 1:segment_number
curves = linspace( 0, 2*segment*pi/(segment_number), 100);
%Inner curve
xin = inner_radius*cos(curves);
yin = inner_radius*sin(curves);
plot3(xin,yin,zb);
plot3(xin,yin,zt);
x=[x,xin];
y=[y,yin];
z=[z,zb];
x=[x,xin];
y=[y,yin];
z=[z,zt];
%Outer curve
xout = outer_radius*cos(curves);
yout = outer_radius*sin(curves);
plot3(xout,yout,zb)
plot3(xout,yout,zt)
x=[x,xout];
y=[y,yout];
z=[z,zb];
x=[x,xout];
y=[y,yout];
z=[z,zt];
%Cooling Holes
xcirc = hole_radius*cos(circles)+ 175 * cos(angle_to_hole + 2*segment*pi/segment_number);
ycirc = hole_radius*sin(circles)+ 175 * sin(angle_to_hole + 2*segment*pi/segment_number);
plot3(xcirc,ycirc,zb)
plot3(xcirc,ycirc,zt)
x=[x,xcirc];
y=[y,ycirc];
z=[z,zb];
x=[x,xcirc];
y=[y,ycirc];
z=[z,zt];
end
stlwrite('mine.stl',x,y,z);

回答(2 个)

Rahul
Rahul 2025-5-12
There are a few approaches you can consider in this case:
  • Instead of just defining points, consider defining surfaces for the 3D object. This can be done using functions like 'surf', 'patch' or 'delaunayTriangulation'. After this 'slwrite' function can be used.
  • Another function 'surf2stl' available through a MATLAB File Exchange submission could be used in this case. This function however it is not able to export multiple 3D objects at once.
The following MathWorks documentation and File Exchange submission can be referred:
Thanks.

DGM
DGM 2025-6-21
编辑:DGM 2025-6-29
The given code does a ton of things in duplicate. I'm going to start from scratch.
% PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plate parameters
Rpmaj = 300;
Rpmin = 100;
disc_thickness = 20;
% hole parameters
Rhole = 20;
Rpitch = 175;
nholes = 12;
% number of edges for all circles
fn = 24;
% VERTICES & EDGES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% start in 2D. construct vertices in a CCW path around the object boundary
% start with a template circle
th = linsegment(0,2*pi,fn+1).';
V0 = [cos(th) sin(th)];
% get vertices for all the circles
% i'm going to use a cell array here as a generalization
Vc = {Rpmaj*V0; % the outer boundary
Rpmin*flipud(V0)}; % the inner hole
% the cooling holes
thch = linsegment(0,2*pi,nholes) + pi/nholes;
for k = 1:nholes
offset = Rpitch*[cos(thch(k)) sin(thch(k))];
Vc = [Vc; {Rhole*flipud(V0) + offset}]; %#ok<*AGROW>
end
% consolidate the vertex list
V = cell2mat(Vc);
% construct the edge lists in the same direction
% the vertex lists in Vc don't need to be the same length
sz = cellfun(@(x) size(x,1),Vc); % length of each vertex list
szc = [0; cumsum(sz)];
E = zeros(0,2);
for k = 1:numel(Vc)
v = 1+szc(k):szc(k+1);
thisE = [v; circshift(v,-1)].';
E = [E; thisE];
end
% TRIANGULATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% do a constrained triangulation to generate the triangles
%T = delaunayTriangulation(V(:,1:2),E);
%F = T.ConnectivityList(isInterior(T),:);
% or better yet, use mesh2D (FEX #25555)
[V,~,F,~] = refine2(V,E);
% display it using patch()
patch('faces',F,'vertices',V, ...
'facecolor','w','edgecolor','k');
axis equal; grid on
xlabel('X'); ylabel('Y')
% EXTRUSION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% extrude the part into 3D
[F V] = extrude(F,V,disc_thickness);
% 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')
This example as shown uses this tool from the FEX:
Alternative code based on delaunayTriangulation() is included, but commented out. For this object, it will produce poor triangle quality, but it works.
Attached are two support functions used to do the extrusion from 2D-3D.

Community Treasure Hunt

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

Start Hunting!

Translated by