File: query.rst

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (296 lines) | stat: -rw-r--r-- 9,780 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
.. Task section

.. module:: ezdxf.query
    :noindex:

.. _query entities:

Query Entities
==============

DXF entities can be selected from layouts or arbitrary entity-sequences based on their 
DXF type and attributes.  Create new queries be the :func:`new` function or by the 
:meth:`~ezdxf.layouts.BaseLayout.query` methods implemented by all layouts.

.. seealso::

    - Tutorial: :ref:`tut_getting_data`
    - Reference: :mod:`ezdxf.query` module

Entity Query String
-------------------

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.


.. _query result:

Query Result
------------

The :class:`EntityQuery` class is the return type of all :meth:`query` methods.
:class:`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 ``False``.

Select all LINE and CIRCLE entities with layer  == "construction"::

    result = msp.query('LINE CIRCLE[layer=="construction"]')

This result is always empty, because the LINE entity has no text attribute::

    result = msp.query('LINE[text ? ".*"]')

Select all entities except those with layer  == "construction" and color < 7::

    result = msp.query('*[!(layer=="construction" & color<7)]')

Ignore case, selects all entities with layer == "construction", "Construction", "ConStruction" ...::

    result = msp.query('*[layer=="construction"]i')

.. _extended query features:

Extended EntityQuery Features
-----------------------------

The :class:`EntityQuery` container supports the full Sequence protocol::

    result = msp.query(...)
    first = result[0]
    last = result[-1]

Slices return a new :class:`EntityQuery` container::

    sequence = result[1:-2]

The :meth:`__getitem__` function accepts also a DXF attribute name and returns all 
entities which support this attribute, this is the base for supporting queries by 
relational operators. More on that later.

The :meth:`__setitem__` method assigns a DXF attribute to all supported
entities in the :class:`EntityQuery` container:

.. code-block:: Python

    result = msp.query(...)
    result["layer"] = "MyLayer"

Entities which do not support an attribute are silently ignored:

.. code-block:: Python

    result = msp.query(...)
    result["center"] = (0, 0)  # set center only of CIRCLE and ARC entities

The :meth:`__delitem__` method discards DXF attributes from all entities in
the :class:`EntityQuery` container:

.. code-block:: Python

    result = msp.query(...)
    # reset the layer attribute from all entities in container result to the
    # default layer "0"
    del result["layer"]

Descriptors for DXF Attributes
------------------------------

For some basic DXF attributes exist descriptors in the :class:`EntityQuery` class:

- :attr:`layer`: layer name as string
- :attr:`color`: :ref:`ACI`, see :mod:`ezdxf.colors`
- :attr:`linetype`: linetype as string
- :attr:`ltscale`: linetype scaling factor as float value
- :attr:`lineweight`: :ref:`Lineweights`
- :attr:`invisible`: 0 if visible 1 if invisible, 0 is the default value
- :attr:`true_color`: true color as int value, see :mod:`ezdxf.colors`, has no default value
- :attr:`transparency`: transparency as int value, see :mod:`ezdxf.colors`, has no default value

A descriptor simplifies the attribute access through the :class:`EntityQuery`
container and has auto-completion support from IDEs:

.. code-block:: Python

    result = msp.query(...)
    # set attribute of all entities in result
    result.layer = "MyLayer"
    # delete attribute from all entities in result
    del result.layer
    # and for selector usage, see following section
    assert len(result.layer == "MyLayer") == 1

.. _relational selection operators:

Relational Selection Operators
------------------------------

The attribute selection by :meth:`__getitem__` allows further selections by
relational operators:

.. code-block:: Python

    msp.add_line((0, 0), (1, 0), dxfattribs={"layer": "MyLayer})
    lines = msp.query("LINE")
    # select all entities on layer "MyLayer"
    entities = lines["layer"] == "MyLayer"
    assert len(entities) == 1

    # or select all entities except the entities on layer "MyLayer"
    entities = lines["layer"] != "MyLayer"

These operators work only with real DXF attributes, for instance the :attr:`rgb`
attribute of graphical entities is not a real DXF attribute either the
:attr:`vertices` of the LWPOLYLINE entity.

The selection by relational operators is case insensitive by default, because
all names of DXF table entries are handled case insensitive. But if required
the selection mode can be set to case sensitive:

.. code-block:: Python

    lines = msp.query("LINE")
    # use case sensitive selection: "MyLayer" != "MYLAYER"
    lines.ignore_case = False
    entities = lines["layer"] == "MYLAYER"
    assert len(entities) == 0

    # the result container has the default setting:
    assert entities.ignore_case is True

Supported selection operators are:

  - ``==`` equal "value"
  - ``!=`` not equal "value"
  - ``<`` lower than "value"
  - ``<=`` lower or equal than "value"
  - ``>`` greater than "value"
  - ``>=`` greater or equal than "value"

The relational operators <, >, <= and >= are not supported for vector-based
attributes such as `center` or `insert` and raise a :class:`TypeError`.

.. note::

    These operators are selection operators and not logic operators, therefore
    the logic operators ``and``, ``or`` and ``not`` are **not** applicable.
    The methods :meth:`~EntityQuery.union`, :meth:`~EntityQuery.intersection`,
    :meth:`~EntityQuery.difference` and :meth:`~EntityQuery.symmetric_difference`
    can be used to combine selection. See section `Query Set Operators`_ and
    `Build Custom Filters`_.

.. _regular expression selection:

Regular Expression Selection
----------------------------

The :meth:`EntityQuery.match` method returns all entities where the selected DXF
attribute matches the given regular expression. This methods work only on string
based attributes, raises :class:`TypeError` otherwise.

From here on I use only descriptors for attribute selection if possible.

.. code-block:: Python

    msp.add_line((0, 0), (1, 0), dxfattribs={"layer": "Lay1"})
    msp.add_line((0, 0), (1, 0), dxfattribs={"layer": "Lay2"})
    lines = msp.query("LINE")

    # select all entities at layers starting with "Lay",
    # selection is also case insensitive by default:
    assert len(lines.layer.match("^Lay.*")) == 2

.. _build custom filters:

Build Custom Filters
--------------------

The method :class:`EntityQuery.filter` can be used to build operators for
none-DXF attributes or for complex logic expressions.

Find all MTEXT entities in modelspace containing "SearchText".
All :class:`~ezdxf.entities.MText` entities have a :attr:`text` attribute, no
need for a safety check:

.. code-block:: Python

    mtext = msp.query("MTEXT").filter(lambda e: "SearchText" in e.text)

This filter checks the non-DXF attribute :attr:`rgb`. The filter has to
check if the :attr:`rgb` attributes exist to avoid exceptions, because not all
entities in modelspace may have the :attr:`rgb` attribute e.g. the
:class:`DXFTagStorage` entities which preserve unknown DXF entities:

.. code-block:: Python

    result = msp.query().filter(
        lambda e: hasattr(e, "rgb") and e.rgb == (0, 0, 0)
    )

Build 1-pass logic filters for complex queries, which would require otherwise
multiple passes:

.. code-block:: Python

    result = msp.query().filter(lambda e: e.dxf.color < 7 and e.dxf.layer == "0")

Combine filters for more complex operations. The first filter passes only
valid entities and the second filter does the actual check:

.. code-block:: Python

    def validator(entity):
        return True  # if entity is valid and has all required attributes

    def check(entity):
        return True  # if entity passes the attribute checks

    result = msp.query().filter(validator).filter(check)

.. _query set operators:

Query Set Operators
-------------------

The ``|`` operator or :meth:`EntityQuery.union` returns a new
:class:`EntityQuery` with all entities from both queries. All entities are
unique - no duplicates. This operator acts like the logical ``or`` operator.

.. code-block:: Python

    entities = msp.query()
    # select all entities with color < 2 or color > 7
    result = (entities.color < 2 ) | (entities.color > 7)

The ``&`` operator or :meth:`EntityQuery.intersection` returns a new
:class:`EntityQuery` with entities common to `self` and `other`. This operator
acts like the logical ``and`` operator.

.. code-block:: Python

    entities = msp.query()
    # select all entities with color > 1 and color < 7
    result = (entities.color > 1) & (entities.color < 7)

The ``-`` operator or :meth:`EntityQuery.difference` returns a new
:class:`EntityQuery` with all entities from `self` that are not in `other`.

.. code-block:: Python

    entities = msp.query()
    # select all entities with color > 1 and not layer == "MyLayer"
    result = (entities.color > 1) - (entities.layer != "MyLayer")

The ``^`` operator or :meth:`EntityQuery.symmetric_difference` returns a new
:class:`EntityQuery` with entities in either `self` or `other` but not both.

.. code-block:: Python

    entities = msp.query()
    # select all entities with color > 1 or layer == "MyLayer", exclusive
    # entities with color > 1 and layer == "MyLayer"
    result = (entities.color > 1) ^ (entities.layer == "MyLayer")