For usage of the query features see the tutorial: Tutorial for getting data from DXF files
Entity Query String¶
QueryString := EntityQuery ("[" AttribQuery "]" "i"?)*
The query string is the combination of two queries, first the required entity query and second the
optional attribute query, enclosed in square brackets, append
'i' after the closing square bracket
to ignore case for strings.
The entity query is a whitespace separated list of DXF entity names or the special name
'*' means all DXF entities, exclude some entity types by appending their names with a preceding
(e.g. all entities except LINE =
'* !LINE'). All DXF names have to be uppercase.
The optional attribute query is a boolean expression, supported operators are:
not (!): !term is true, if term is false
and (&): term & term is true, if both terms are true
or (|): term | term is true, if one term is true
and arbitrary nested round brackets
append (i) after the closing square bracket to ignore case for strings
Attribute selection is a term: “name comparator value”, where name is a DXF entity attribute in lowercase, value is a integer, float or double quoted string, valid comparators are:
"!="not equal “value”
"<"lower than “value”
"<="lower or equal than “value”
">"greater than “value”
">="greater or equal than “value”
"?"match regular expression “value”
"!?"does not match regular expression “value”
EntityQuery class is the return type of all
EntityQuery contains all DXF entities of the source collection,
which matches one name of the entity query AND the whole attribute query.
If a DXF entity does not have or support a required attribute, the corresponding attribute search term is
LINE[text ? ".*"]: always empty, because the LINE entity has no text attribute.
LINE CIRCLE[layer=="construction"]: all LINE and CIRCLE entities with layer ==
*[!(layer=="construction" & color<7)]: all entities except those with layer ==
"construction"and color <
*[layer=="construction"]i, (ignore case) all entities with layer ==
EntityQueryclass is a result container, which is filled with dxf entities matching the query string. It is possible to add entities to the container (extend), remove entities from the container and to filter the container. Supports the standard Python Sequence methods and protocols.
First entity or
Last entity or
__len__() → int¶
Returns count of DXF entities.
__getitem__(item: int) → DXFEntity¶
Returns DXFEntity at index item, supports negative indices and slicing.
__iter__() → Iterable[ezdxf.entities.dxfentity.DXFEntity]¶
Returns iterable of DXFEntity objects.
extend(entities: Iterable[DXFEntity], query: str = '*', unique: bool = True) → ezdxf.query.EntityQuery¶
EntityQuerycontainer by entities matching an additional query.
remove(query: str = '*') → None¶
Remove all entities from
EntityQuerycontainer matching this additional query.
query(query: str = '*') → ezdxf.query.EntityQuery¶
Returns a new
EntityQuerycontainer with all entities matching this additional query.
raises: ParseException (pyparsing.py)
groupby(dxfattrib: str = '', key: Callable[[DXFEntity], Hashable] = None) → Dict[Hashable, List[DXFEntity]]¶
Returns a dict of entity lists, where entities are grouped by a DXF attribute or a key function.
dxfattrib – grouping DXF attribute as string like
key – key function, which accepts a DXFEntity as argument, returns grouping key of this entity or None for ignore this object. Reason for ignoring: a queried DXF attribute is not supported by this entity