用python写一段用matplotlib画出的长方体导出保存成stl文件的代码
时间: 2023-03-09 12:50:30 浏览: 139
MATLAB导出stl格式文件
5星 · 资源好评率100%
以下是一段可以用来画出长方体并保存为stl文件的python代码:from stl import mesh
import numpy
import matplotlib.pyplot as plt# Create 6 faces of a cube
data = numpy.zeros(6, dtype=mesh.Mesh.dtype)# Top of the cube
data['vectors'][0] = numpy.array([[0, 1, 1],
[1, 0, 1],
[0, 0, 1]])
data['vectors'][1] = numpy.array([[1, 0, 1],
[0, 1, 1],
[1, 1, 1]])
# Front face
data['vectors'][2] = numpy.array([[1, 0, 0],
[1, 0, 1],
[1, 1, 0]])
data['vectors'][3] = numpy.array([[1, 1, 1],
[1, 0, 1],
[1, 1, 0]])
# Left face
data['vectors'][4] = numpy.array([[0, 0, 0],
[1, 0, 0],
[1, 0, 1]])
data['vectors'][5] = numpy.array([[0, 0, 1],
[1, 0, 1],
[0, 0, 0]])# Create the mesh
cube = mesh.Mesh(data.copy())# Visualization of the cube
# Create a figure
fig = plt.figure()
# Add an axes
ax = fig.add_subplot(1, 1, 1)
# Set the title
ax.set_title('3D Test')
# Show the cube
ax.add_collection3d(mesh.Mesh3dCollection(cube.vectors))
# Auto scale to the mesh size
scale = cube.points.flatten(-1)
ax.auto_scale_xyz(scale, scale, scale)
# Show the figure
plt.show()# Save the stl file
cube.save('cube.stl')
阅读全文