1

我的主要目标是获取 STL 文件的图纸视图。我尝试了将 STL 文件转换为 SLDPRT 的 SolidWorks 方法,然后获取工程视图,但在这种情况下,工程视图包含很多噪音并且不准确。所以,我正在尝试 Trimesh 模块。到目前为止,与

slicex = meshx.section(plane_origin=meshx.centroid, plane_normal=[0,0,30])
slice_2D, to_3D = slicex.to_planar()
slice_2D.show()

并通过更改 Plane_normal 数组值,我得到了所需的横截面(这有点类似于三个视图),但我不知道如何将 Spyder 控制台中显示的图像保存为 JPEG 或 PNG。我需要绘图视图以进行进一步的图像分析。

任何有关此方法或任何其他获取图纸视图的方法的线索将不胜感激!谢谢!

4

1 回答 1

0

我为此看到的最好结果是使用 matplotib 并保存散点图。一旦采用该格式,您就可以调整分辨率或另存为矢量:

import matplotlib.pylab as plt
slicex = meshx.section(plane_origin=meshx.centroid, plane_normal=[0,0,30])
slice_2D, to_3D = slicex.to_planar()

fig, ax = plt.subplots(figsize=(16,8))
ax.set_aspect('equal')
_ = ax.scatter(slice_2D.vertices[:,0], slice_2D.vertices[:,1], color='lightgray')
ax.axis('off')
plt.savefig('meshx_slice.png')

有关文件格式和选项的更多详细信息,请参见此处。这也适用于保存完整的 2D 网格或 3D 网格的平面视图,使用 Trimesh.vertices 作为点。

选择

如果你想复制它的代码slice_2D.show(),你可以借用它的代码(它使用 matplotlib):

import matplotlib.pyplot as plt
# keep plot axis scaled the same
plt.axes().set_aspect('equal', 'datalim')
# hardcode a format for each entity type
eformat = {'Line0': {'color': 'g', 'linewidth': 1},
       'Line1': {'color': 'y', 'linewidth': 1},
       'Arc0': {'color': 'r', 'linewidth': 1},
       'Arc1': {'color': 'b', 'linewidth': 1},
       'Bezier0': {'color': 'k', 'linewidth': 1},
       'Bezier1': {'color': 'k', 'linewidth': 1},
       'BSpline0': {'color': 'm', 'linewidth': 1},
       'BSpline1': {'color': 'm', 'linewidth': 1}}
for entity in slice_2D.entities:
    # if the entity has it's own plot method use it
    if hasattr(entity, 'plot'):
        entity.plot(slice_2D.vertices)
        continue
    # otherwise plot the discrete curve
    discrete = entity.discrete(slice_2D.vertices)
    # a unique key for entities
    e_key = entity.__class__.__name__ + str(int(entity.closed))

    fmt = eformat[e_key].copy()
    if hasattr(entity, 'color'):
        # if entity has specified color use it
        fmt['color'] = entity.color
    plt.plot(*discrete.T, **fmt)
    plt.savefig('meshx_slice.png')
于 2020-06-17T20:42:11.320 回答