MATLAB to CAD file

조회 수: 10 (최근 30일)
ABDUL SAMAD
ABDUL SAMAD 2020년 5월 16일
편집: DGM 2025년 6월 23일
MATLAB issue.
I have MATLAB code:
clear all
close all
clc
x_period = 4;
y_period = 4;
z_period = 4;
[x,y,z] = meshgrid(0:0.1:x_period*3.14159265359, 0:0.1:y_period*3.14159265359, 0:0.1:z_period*3.14159265359);
f = cos(x).*sin(y) + cos(y).*sin(z) + cos(z).*sin(x);
figure(1)
isosurface(x,y,z,f)
axis equal
xlabel('x')
ylabel('y')
zlabel('z')
fv = isosurface(x,y,z,f)
I want to save file as .xyz or .stl of that gyroid figure, please help me, if it is possible.
If it is not possible then which file can I save so that I can use in solidworks or any CAD software?

답변 (2개)

Chukwugozie Ejeh
Chukwugozie Ejeh 2020년 7월 22일
편집: DGM 2025년 6월 23일
I am researching on triply periodic minimal surface lattice structures. I have developed a code to save the isosurface data to stl format.
% Initialize Matlab to clear stored data and gain computing speed.
close all; clear all; clc;
% Setting meshgrid
%%%%%%%%%%%%%%%%%%%%%
% generates a row vector L of 100 points linearly spaced between and including 0 and 1.
n = 1; % domain size in the x-direction
L_x = linspace(0, n, 100);
L_y = linspace(0, 1, 100);
L_z = linspace(0, 1, 100);
[X,Y,Z] = meshgrid(L_x, L_y, L_z);
%%%%%%%%%%%%%%%%%%%%%%
a = 2; % constant that controls the periodicity
% Paramertizing the variables
x = a*pi.*X;
y = a*pi.*Y;
z = a*pi.*Z;
c = 0;
% Define the Level-set approimation equation for gyroid (G) and diamond (D)
G = cos(x).*sin(y) + cos(y).*sin(z) + cos(z).*sin(x) - c;
D = cos(0.5.*x).*cos(0.5.*y).*cos(0.5.*z) - sin(0.5.*x).*sin(0.5.*y).*sin(0.5.*z) - c;
% Plotting isosuface
figure (1);
isosurface(X,Y,Z,D,0); % for diamond
% isosurface(X,Y,Z,G,0); % for gyroid
%Writing sheet-network data to stl format by first storing the isovalues (facet and vertex data)
[f,v]=isosurface(X,Y,Z,D,0); stlwrite('sheet.stl',f,v); % for diamond
% [f,v]=isosurface(X,Y,Z,D,0); stlwrite('sheet.stl',f,v); for gyroid
%%% MAKE SURE TO SAVE YOUR STL FILE IN THE SAME DIRECTORY WERE YOUR SAVED THE MATLAB CODE.
  댓글 수: 3
Anupreet Singh
Anupreet Singh 2022년 2월 2일
HI, I am facing the same issue. Did you find a solution?
DGM
DGM 2025년 6월 23일
The example above uses stlwrite() from FEX #20922.
Since R2018b, MATLAB has a built-in stlwrite(). They are not the same.
Unless you want to use some of its particular functionality, there isn't a need to complicate things by using FEX #20922. The problem with recommending it is that it will shadow the built-in stlwrite() that's already available in modern versions. If you want to use it, I recommend renaming it to avoid problems.
% ... all the plotting stuff
% you can either call isosurface() again,
% or you can get the existing F,V data from the figure.
[F V] = isosurface(x,y,z,f);
% FEX #20922 accepts multiple input formats
% it can accept F,V lists, an FV struct, or simple gridded data
stlWrite('Gyroid1.stl',F,V) % i renamed this to prevent name collision
% built-in stlwrite() (R2018b or newer) accepts triangulation objects
T = triangulation(F,V);
stlwrite(T,'Gyroid2.stl') % you already have this

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


DGM
DGM 2025년 6월 23일
편집: DGM 2025년 6월 23일
See also:
An example of saving a gyroid as a solid volume (using both isosurface() and isocaps())
An example of saving a gyroid as a surface of zero thickness (just using isosurface())

카테고리

Help CenterFile Exchange에서 STL (STereoLithography)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by