AdCal
AdCal

Reputation: 19

Find Properties of Polygons from Path2D, derived from the.to_planar() function of Trimesh Module in Python

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!

Upvotes: 1

Views: 1204

Answers (1)

Holger Weiss
Holger Weiss

Reputation: 51

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)

Upvotes: 1

Related Questions