How to convert a figure of a minimal surface into an STL or OBJ file?

13 views (last 30 days)
Hi, I've got this code for making a Schwartz D structure in MATLAB (credits to https://github.com/pttrsnv2/Minimal_Surfaces_Visualization_Code/blob/master/Minimal_Surfaces_Code_Upload_V2.m).
How could I make this into an STL or OBJ file? I need anything I could put into solidworks or fusion 360. I've tried to go through a ton of mathworks answers regarding this, but I can't seem to make stlwrite work no matter what I try.
f_sd = @(x,y,z) sin(x).*sin(y).*sin(z) + sin(x).*cos(y).*cos(z)+cos(x).*sin(y).*cos(z)...
+ cos(x).*cos(y).*sin(z);
% The limits for a 3-D plot
x_sd_l = 0;
x_sd_u = 2*pi;
y_sd_l = 0;
y_sd_u = 2*pi;
z_sd_l = 0;
z_sd_u = pi;
% Mesh density
MD_sd = 50; % Change as needed
% Face color
C_sd = 'red'; % Change as needed
figure
fimplicit3(f_sd, [x_sd_l x_sd_u y_sd_l y_sd_u z_sd_l z_sd_u], 'meshdensity',MD_sd,'facecolor',C_sd)
title('Schwarz D Plot')
xlabel('X')
ylabel('Y')
zlabel('Z')

Answers (2)

Supraja
Supraja on 12 Mar 2023
The github link you mentioned in the question is unfortunately not working. But assuming you want to convert your file into .stl or .obj file, I am attaching the documentation links below which would help you to achieve your goal.
Conversion to .stl file:
Conversion to .obj file:

DGM
DGM on 15 Jul 2025
You already have triangulated data, you just need to write it. That means that surf2stl() won't work for an fimplicit() plot. Unless you're running a version older than R2018b or have some particular special reason, you don't need FEX #20922 either. You already have an STL encoder. Just use it.
Here's an actual answer:
f_sd = @(x,y,z) sin(x).*sin(y).*sin(z) + sin(x).*cos(y).*cos(z)+cos(x).*sin(y).*cos(z)...
+ cos(x).*cos(y).*sin(z);
% The limits for a 3-D plot
x_sd_l = 0;
x_sd_u = 2*pi;
y_sd_l = 0;
y_sd_u = 2*pi;
z_sd_l = 0;
z_sd_u = pi;
% Mesh density
MD_sd = 50; % Change as needed
% Face color
C_sd = 'red'; % Change as needed
hfs = fimplicit3(f_sd, [x_sd_l x_sd_u y_sd_l y_sd_u z_sd_l z_sd_u], ...
'meshdensity',MD_sd,'facecolor',C_sd);
title('Schwarz D Plot')
xlabel('X')
ylabel('Y')
zlabel('Z')
% get the data from the fimplicit object
F = hfs.Triangulation.ConnectivityList;
V = hfs.Triangulation.Points;
% write it to an STL
stlwrite(triangulation(F,V),'test1.stl') % R2018b+
% read it back just for show and tell
T = stlread('test1.stl');
[F V] = t2fv(T);
figure
patch('faces',F,'vertices',V,'facecolor','w','edgecolor','none');
view(3); view(-34,19); camlight;
axis equal; grid on

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!