1

With the Trimesh module in Python, I am able to get 2D cross-sections from a STL file, with the code shown below.

mesh = trimesh.load_mesh('MyFile.stl')
slicex = mesh.section(plane_origin=mesh.centroid, plane_normal=[0,30,0])
slice_2D, to_3D = slice.to_planar()

With the 2D Path (Slice_2D), obtained from the above code, I am able to get the polygons in it as a NumPy array and iterate over it with the code below:

for polygon in slice_2D.polygons_closed:
    trimesh.path.polygons.plot_polygon(polygon, show=True)

The above code SHOWS the polygons on the console. However, I would like to know if there is a way to get the properties of the polygon, for example: No. of edges in the polygon; Perimeter and Area of the polygon; Type of Polygon (triangle or square or rectangle or parallelogram or circle, etc.).

Any help in this regard would be much appreciated!

AdCal
  • 19
  • 1

1 Answers1

1

The property "polygons_closed" returns an array of shapely polygons. So to get ie. the area, use:

for polygon in slice_2D.polygons_closed:
    trimesh.path.polygons.plot_polygon(polygon, show=True)
    print(polygon.area)
  • Dear Holger, thank you for the answer. This really helped. Is there a way to count the number of edged or vertices of the polygon? I also want to find out the approx type of the polygon (circle or traingle or square etc.). If there is no direct way, I thought of an indirect way, that is to save the polgon (shown in the console) as a .PNG or :JPEG and then use cv2 module to find number of edges. Could you please also explain how can I save the polgon shown in the console as a picture withour any borders (axes)? Thank you. – AdCal Jun 28 '20 at 10:33
  • 1
    What you get in 'polygon' is an instance of the class 'polygon' from the package 'shapely'. I recommend taking a look at it in the debugger. It is very powerful and will have all the information needed such as a list of vertices and the moments of inertia. – Holger Weiss Jun 29 '20 at 07:54