I am trying to load a stl file and trying to view the slices of the stl file.
I have tried this:
我正在尝试加载一个stl文件并试图查看该stl文件的片段。我试过这个:
import numpy as np
from stl import mesh
import matplotlib.pyplot as plt
Load the STL file
mesh_data = mesh.Mesh.from_file('MyStl.stl')
Define the slicing axis (X, Y, or Z)
slicing_axis = 'Y' # Replace with the desired axis
Define the number of slices
num_slices = 10 # Adjust the number of slices as needed
Calculate the range along the slicing axis
axis_range = mesh_data.points[:, 2] # Assuming Z-axis slicing
Create evenly spaced slice positions
slice_positions = np.linspace(min(axis_range), max(axis_range), num_slices)
Iterate through the slices and create 2D images
for i, slice_position in enumerate(slice_positions):
# Define a small tolerance for matching vertices
tolerance = 1e-6
# Create a mask to extract the slice
if slicing_axis == 'X':
mask = np.isclose(mesh_data.points[:, 0], slice_position, atol=tolerance)
elif slicing_axis == 'Y':
mask = np.isclose(mesh_data.points[:, 1], slice_position, atol=tolerance)
elif slicing_axis == 'Z':
mask = np.isclose(mesh_data.points[:, 2], slice_position, atol=tolerance)
# Extract the vertices for the slice
sliced_vertices = mesh_data.points[mask]
# Create a 2D plot of the slice (you can use any 2D plotting library)
plt.figure()
plt.scatter(sliced_vertices[:, 0], sliced_vertices[:, 1], s=1)
plt.axis('equal') # Ensure equal aspect ratio for the plot
plt.title(f'Slice {i+1}')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
I am getting the plots but not able to view any thing in the plots meaning I'm just getting empty plots.Help me in viewing the slices .Thanks in advance.
我得到了情节,但不能查看情节中的任何东西,这意味着我只是得到了空的情节。帮助我查看切片。提前谢谢。
更多回答
STL does not have any slices you have to cut the mesh yourself. Some 3D rendering engines and APIs can be configured to do this for example in OpenGL you can simply discard any fragment outside the region of slice with simple if condition in fragment shader ... without the need for any geometric cutting by planes ... You want axis aligned slices so you can even configure camera so it shows only the slice you want even without shaders
STL没有任何切片,您必须自己切割网格。一些3D渲染引擎和API可以配置为这样做,例如在OpenGL中,您只需在片段着色器中使用简单的IF条件就可以丢弃切片区域之外的任何片段。不需要用飞机进行任何几何切割。你想要轴对齐的切片,这样你甚至可以配置相机,这样即使没有着色器,它也只显示你想要的切片
By discarding the fragments will i be able to get 2d slices of my stl file?or are you telling that we will acheive parts of 3d stl file we have?
通过丢弃碎片,我将能够获得我的STL文件的2D切片吗?或者你是在告诉我们我们将获得我们拥有的3D STL文件的部分吗?
if you do just zero thickness slice this way you will get the contour however most likely with holes (like doted lines) ... its better to have "finite thickness" slice instead so the lines will be fully filled ... so I would chose slice thickness to be around pixel size (or some small multiple of it) ....
如果你只用这种方法做零厚度切片,你会得到轮廓,但很可能是有洞的(如虚线)。最好是用“有限厚度”切片,这样线条就会被完全填满。因此,我会选择切片厚度约为像素大小(或它的某个小倍数)。
我是一名优秀的程序员,十分优秀!