The ezdxf.select module allows you to select DXF entities based on geometric shapes like windows, circles, polygons, and fences.
Selection functions (like bbox_inside) operate on the bounding box of entities rather than their precise geometry. This means an entity is selected if its bounding box meets the criteria relative to the selection shape.
Key behaviors:
bbox_inside: Selects entities whose bounding boxes are entirely within the shape.bbox_outside: Selects entities whose bounding boxes are entirely outside the shape.bbox_overlap: Similar to 'crossing selection' in CAD; selects entities if their bounding box overlaps the selection shape.
All selection functions accept an iterable of entities (such as a layout like msp) and return an ezdxf.query.EntityQuery object, which can be used for further filtering by entity type or attributes.
import ezdxf
from ezdxf import select
doc = ezdxf.readfile("your.dxf")
msp = doc.modelspace()
# Define a window for selection
window = select.Window((0, 0), (10, 10))
# Select entities inside the window from modelspace
selected_entities = select.bbox_inside(window, msp)
# Iterate over selected entities
for entity in selected_entities:
print(entity)