trimesh
trimesh copied to clipboard
Area of filled polygons in a 2D slice
I generate DXF files from 2D slices - see minimal example below. I would like to calculate the area of each filled polygon in the slice as I iterate through the polygons in the slice. Is there a command equivalent to entire_slice_area=slice_2D.area
for the filled polygons?
# converts stl slice to DXF
import trimesh
import ezdxf
mesh = trimesh.load_mesh(r'C:...test_file_1.stl')
slice = mesh.section(plane_origin=[0,0,0.5], plane_normal=[0,0,1])
slice_2D, to_3D = slice.to_planar()
print(slice_2D.area)
num_poly = len(slice_2D.discrete)
doc = ezdxf.new("R2000")
msp = doc.modelspace()
hatch = msp.add_hatch(
color=2,
dxfattribs={
"hatch_style": ezdxf.const.HATCH_STYLE_NESTED,
# 0 = nested: ezdxf.const.HATCH_STYLE_NESTED
# 1 = outer: ezdxf.const.HATCH_STYLE_OUTERMOST
# 2 = ignore: ezdxf.const.HATCH_STYLE_IGNORE
},)
for i in range(num_poly):
hatch.paths.add_polyline_path(
slice_2D.discrete[i],
is_closed=True,
flags=ezdxf.const.BOUNDARY_PATH_DEFAULT,)
doc.saveas(r"C:...\slice_068.dxf")