Selection Tools

The ezdxf.select module provides entity selection capabilities, allowing users to select entities based on various shapes such as windows, points, circles, polygons, and fences.

The selection functions bbox_inside() and bbox_outside() work similarly to the inside and outside selection tools in CAD applications but the selection is based on the bounding box of the DXF entities rather than their actual geometry.

The bbox_overlap() function works similarly to crossing selection in CAD applications. Entities that are outside the selection shape but whose bounding box overlapps the selection shape are included in the selection. This is not the case with crossing selection in CAD applications.

The selection functions accept any iterable of DXF entities as input and return an ezdxf.query.EntityQuery container, that provides further selection tools based on entity type and DXF attributes.

Usage

Select all entities from the modelspace inside a window defined by two opposite vertices:

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)

Selection Functions

The following selection functions are implemented:

ezdxf.select.bbox_inside(shape: SelectionShape, entities: Iterable[DXFEntity], *, cache: Cache | None = None) EntityQuery

Selects entities whose bounding box lies withing the selection shape.

Parameters:
  • shape – seclection shape

  • entities – iterable of DXFEntities

  • cache – optional ezdxf.bbox.Cache instance

ezdxf.select.bbox_outside(shape: SelectionShape, entities: Iterable[DXFEntity], *, cache: Cache | None = None) EntityQuery

Selects entities whose bounding box is completely outside the selection shape.

Parameters:
  • shape – seclection shape

  • entities – iterable of DXFEntities

  • cache – optional ezdxf.bbox.Cache instance

ezdxf.select.bbox_overlap(shape: SelectionShape, entities: Iterable[DXFEntity], *, cache: Cache | None = None) EntityQuery

Selects entities whose bounding box overlaps the selection shape.

Parameters:
  • shape – seclection shape

  • entities – iterable of DXFEntities

  • cache – optional ezdxf.bbox.Cache instance

ezdxf.select.bbox_chained(start: DXFEntity, entities: Iterable[DXFEntity], *, cache: Cache | None = None) EntityQuery

Selects elements that are directly or indirectly connected to each other by overlapping bounding boxes. The selection begins at the specified starting element.

Warning: the current implementation has a complexity of O(n²).

Parameters:
  • start – first entity of selection

  • entities – iterable of DXFEntities

  • cache – optional ezdxf.bbox.Cache instance

ezdxf.select.bbox_crosses_fence(vertices: Iterable[UVec], entities: Iterable[DXFEntity], *, cache: Cache | None = None) EntityQuery

Selects entities whose bounding box intersects an open polyline.

All entities are projected on the xy-plane.

A single point can not be selected by a fence polyline by definition.

Parameters:
  • vertices – vertices of the selection polyline

  • entities – iterable of DXFEntities

  • cache – optional ezdxf.bbox.Cache instance

ezdxf.select.point_in_bbox(location: UVec, entities: Iterable[DXFEntity], *, cache: Cache | None = None) EntityQuery

Selects entities where the selection point lies within the bounding box. All entities are projected on the xy-plane.

Parameters:
  • point – selection point

  • entities – iterable of DXFEntities

  • cache – optional ezdxf.bbox.Cache instance

Selection Shapes

The following selection shapes are implemented:

class ezdxf.select.Window(p1: UVec, p2: UVec)

This selection shape tests entities against a rectangular and axis-aligned 2D window. All entities are projected on the xy-plane.

Parameters:
  • p1 – first corner of the window

  • p2 – second corner of the window

class ezdxf.select.Circle(center: UVec, radius: float)

This selection shape tests entities against a circle. All entities are projected on the xy-plane.

Parameters:
  • center – center of the circle

  • radius – radius of the circle

class ezdxf.select.Polygon(vertices: Iterable[UVec])

This selection shape tests entities against an arbitrary closed polygon. All entities are projected on the xy-plane. Complex concave polygons may not work as expected.