0

My main aim is to get the Drawing views of a STL file. I have tried the SolidWorks Method of converting a STL file to SLDPRT and then take the drawing views, but the drawing views in that case contains a lot of noise and is not accurate. SO, I am trying the Trimesh module. So far, with

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

and by changing the Plane_normal array values, I get the required cross section (which is somewhat similar to the three views), but I do not know how to save the image shown in the Console of Spyder as JPEG or PNG. I need the drawing views for further image analysis.

Any leads on this method or any other method to get the drawing views would be much appreciated! Thank you!

273K
  • 29,503
  • 10
  • 41
  • 64
AdCal
  • 19
  • 1

1 Answers1

0

The best results I've seen for this is using matplotib and saving a scatter plot. You are able to adjust the resolution or save as a vector once it is in that format:

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')

More details on the file format and options are here. This also works well for saving a full 2D mesh or a planar view of a 3D mesh, using the Trimesh.vertices as points.

ALTERNATIVE

If you're wanting to replicate what slice_2D.show() does, you can just borrow from its code (which uses 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')
wwwslinger
  • 936
  • 8
  • 14
  • Thank you very much for the response. I tried the code. Unfortunately, I do not get the image as I require. Instead of straigt lines, I get dots in patches. I wanted to attach pictures to exaplin it more clearly, but I am unable to do it. – AdCal Jun 18 '20 at 14:41
  • @Ad23 I added an update that shows what `slice_2D.show()` actually uses, and you can just save from there. – wwwslinger Jun 22 '20 at 02:06
  • Thank you. This helped me solve my problem. Is there any general way of saving whatever is displayed on the console as a picture? – AdCal Jun 28 '20 at 10:36
  • @Ad23 I would post that as a separate question, but I think it just depends on what tool you're using to display. You can always use `savefig` with matplotlib. Most plots can be replicated in plotly, which good exporting, too. – wwwslinger Jun 28 '20 at 15:33