我为此看到的最好结果是使用 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')