필터 지우기
필터 지우기

How to this convert matlab code to python code?

조회 수: 60 (최근 30일)
SUBHA
SUBHA 2023년 5월 1일
답변: Vilém Frynta 2023년 5월 1일
%clear all previous commands
clear all
close all
clc
%variable you can change
SizeL = 20;
Def = 160;
SFact = (SizeL/2)/pi;
A = SFact*pi;
D = A/Def;
[X,Y,Z] = meshgrid(-A:D:A);
OBJ = sin (Y/SFact) + cos (Y/SFact).* sin (Z/SFact)+ cos (Z/SFact).* sin (X/SFact)+ (0);
T = 0.5;
OBJ = ((OBJ-T).*(OBJ+T));
[F1,V1] = isosurface (X,Y,Z,OBJ,0);
[F2,V2] = isocaps(X,Y,Z,OBJ,0,'below');
F3 = [F1;F2+length(V1(:,1))];
V3 = [V1;V2];
P = patch ('Vertices',V3, 'Faces',F3,'FaceColor','red','EdgeColor','none');
view(3)
camlight
stlWrite(['J:\lattice\MATLAB\MATLAB\Gyroid-network' num2str(1) '.stl'],F3,V3);
axis equal

답변 (1개)

Vilém Frynta
Vilém Frynta 2023년 5월 1일
This requires knowledge of both languages. If you don't know Python, and have nobody to help you, you can try AI.
I've prompted your request to an AI, and this was the result. Keep in mind that this might not work because of some silly errors. It will need to be adjusted, which means you need to Google the commands used etc.
import numpy as np
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from stl import mesh
# clear all previous commands
np.random.seed(0)
np.set_printoptions(precision=3, suppress=True)
# variable you can change
SizeL = 20
Def = 160
SFact = (SizeL/2)/np.pi
A = SFact*np.pi
D = A/Def
X,Y,Z = np.meshgrid(np.arange(-A, A+D, D),
np.arange(-A, A+D, D),
np.arange(-A, A+D, D))
OBJ = np.sin(Y/SFact) + np.cos(Y/SFact) * np.sin(Z/SFact) \
+ np.cos(Z/SFact) * np.sin(X/SFact) + 0
T = 0.5
OBJ = ((OBJ-T)*(OBJ+T))
iso_surface = np.isosurface(OBJ, level=0)
F1, V1 = iso_surface.triangles, iso_surface.vertices
iso_caps = np.isocaps(OBJ, level=0, direction='below')
F2, V2 = iso_caps.triangles, iso_caps.vertices
F3 = np.vstack((F1, F2 + len(V1)))
V3 = np.vstack((V1, V2))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
mesh = Poly3DCollection(V3[F3])
mesh.set_facecolor('red')
ax.add_collection3d(mesh)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_box_aspect([1,1,1])
ax.view_init(30, 30)
plt.show()
mesh = mesh.Mesh(np.zeros(F3.shape[0], dtype=mesh.Mesh.dtype))
for i, f in enumerate(F3):
for j in range(3):
mesh.vectors[i][j] = V3[f[j], :]
mesh.save('Gyroid-network1.stl')
Hope I helped.

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by