用python写一段用matplotlib画出的三维曲面导出保存成stl文件的代码
时间: 2023-03-09 17:50:30 浏览: 158
代码如下:import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from stl import mesh# 定义曲面
X = np.arange(-2,2,0.25)
Y = np.arange(-2,2,0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)# 画出曲面
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')# 导出stl文件
stl_mesh = mesh.Mesh(np.zeros(Z.shape[0]*Z.shape[1], dtype=mesh.Mesh.dtype))
for i in range(Z.shape[0] - 1):
for j in range(Z.shape[1] - 1):
stl_mesh.vectors[i*Z.shape[1] + j] = np.array([[X[i, j], Y[i, j], Z[i, j]],
[X[i, j + 1], Y[i, j + 1], Z[i, j + 1]],
[X[i + 1, j + 1], Y[i + 1, j + 1], Z[i + 1, j + 1]],
[X[i + 1, j], Y[i + 1, j], Z[i + 1, j]]])stl_mesh.save('my_stl_file.stl')
阅读全文