File: usage_for_beginners.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 (320 lines) | stat: -rw-r--r-- 10,255 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
.. _arch-usr:

Usage for Beginners
===================

This section shows the intended usage of the `ezdxf` package.
This is just a brief overview for new `ezdxf` users, follow the provided links
for more detailed information.


First import the package::

    import ezdxf

Loading DXF Files
-----------------

`ezdxf` supports loading ASCII and binary DXF documents from a file::

    doc = ezdxf.readfile(filename)

or from a zip-file::

    doc = ezdxf.readzip(zipfilename[, filename])

Which loads the DXF document `filename` from the zip-file `zipfilename` or the
first DXF file in the zip-file if `filename` is absent.

It is also possible to read a DXF document from a stream by the :func:`ezdxf.read`
function, but this is a more advanced feature, because this requires detection
of the file encoding in advance.

This works well with DXF documents from trusted sources like AutoCAD or BricsCAD.
For loading DXF documents with minor or major flaws use the :mod:`ezdxf.recover`
module.

.. seealso::

    Documentation for :func:`ezdxf.readfile`, :func:`ezdxf.readzip` and
    :func:`ezdxf.read`, for more information about file
    management go to the :ref:`dwgmanagement` section. For loading DXF documents
    with structural errors look at the :mod:`ezdxf.recover` module.

Layouts and Blocks
------------------

Layouts are containers for DXF entities like LINE or CIRCLE. The most important
layout is the modelspace labeled as "Model" in CAD applications which represents
the "world" work space. Paperspace layouts represents plottable sheets which
contains often the framing and the tile block of a drawing and VIEWPORT entities
as scaled and clipped "windows" into the modelspace.

The modelspace is always present and can not be deleted. The active paperspace
is also always present in a new DXF document but can be deleted, in that case
another paperspace layout gets the new active paperspace, but you can not delete
the last paperspace layout.

Getting the modelspace of a DXF document::

    msp = doc.modelspace()

Getting a paperspace layout by the name as shown in the tab of a
CAD application::

    psp = doc.paperspace("Layout1")

A block is just another kind of entity space, which can be inserted
multiple times into other layouts and blocks by the INSERT entity also called
block references, this is a very powerful and an important concept of the DXF
format.

Getting a block layout by the block name::

    blk = doc.blocks.get("NAME")


All these layouts have factory functions to create graphical DXF entities for
their entity space, for more information about creating entities see section:
`Create new DXF Entities`_

Query DXF Entities
------------------

As said in the `Layouts and Blocks`_ section, all graphical DXF entities are
stored in layouts, all these layouts can be iterated and do support the index
operator e.g. :code:`layout[-1]` returns the last entity.

The main difference between iteration and index access is, that iteration filters
destroyed entities, but the index operator returns also destroyed entities
until these entities are purged by :code:`layout.purge()`, more about this topic
in section: `Delete Entities`_.

There are two advanced query methods: :meth:`~ezdxf.layouts.BaseLayout.query`
and :meth:`~ezdxf.layouts.BaseLayout.groupby`.

Get all lines of layer ``"MyLayer"``::

    lines = msp.query('LINE[layer=="MyLayer"]')

This returns an :class:`~ezdxf.query.EntityQuery` container, which also provides
the same :meth:`query` and :meth:`groupby` methods.

Get all lines categorized by a DXF attribute like color::

    all_lines_by_color = msp.query("LINE").groupby("color")
    lines_with_color_1 = all_lines_by_color.get(1, [])

The :meth:`groupby` method returns a regular Python :class:`dict` with colors as
key and a regular Python :class:`list` of entities as values
(not an :class:`~ezdxf.query.EntityQuery` container).

.. seealso::

    For more information go to the :ref:`tut_getting_data`

Examine DXF Entities
--------------------

Each DXF entity has a :attr:`dxf` namespace attribute, which stores the named
DXF attributes, some entity attributes and assets are only available from
Python properties or methods outside the :attr:`dxf` namespace like the
vertices of the LWPOLYLINE entity. More information about the DXF attributes of
each entity can found in the documentation of the :mod:`ezdxf.entities` module.

Get some basic DXF attributes::

    layer = entity.dxf.layer  # default is "0"
    color = entity.dxf.color  # default is 256 = BYLAYER

Most DXF attributes have a default value, which will be returned if the DXF
attribute is not present, for DXF attributes without a default value you can
check if the attribute really exist::

    entity.dxf.hasattr("true_color")

or use the :meth:`get` method and provide a default value::

    entity.dxf.get("true_color", 0)

.. seealso::

    - :ref:`Common graphical DXF attributes`
    - Helper class :class:`ezdxf.gfxattribs.GfxAttribs` for building DXF attribute
      dictionaries.

Create a New DXF File
---------------------

Create new document for the latest supported DXF version::

    doc = ezdxf.new()

Create a new DXF document for a specific DXF version, e.g. for DXF R12::

    doc = ezdxf.new("R12")


The :func:`ezdxf.new` function can create some standard resources, such as
linetypes and text styles, by setting the argument `setup` to ``True``::

    doc = ezdxf.new(setup=True)

.. seealso::

    - :ref:`tut_simple_drawings`
    - Documentation for :func:`ezdxf.new`, for more information about file
      management go to the :ref:`dwgmanagement` section.

Create New DXF Entities
-----------------------

The factory methods for creating new graphical DXF entities are located in the
:class:`~ezdxf.layouts.BaseLayout` class and these factory methods are
available for all entity containers:

    - :class:`~ezdxf.layouts.Modelspace`
    - :class:`~ezdxf.layouts.Paperspace`
    - :class:`~ezdxf.layouts.BlockLayout`

The usage is simple::

    msp = doc.modelspace()
    msp.add_line((0, 0), (1, 0), dxfattribs={"layer": "MyLayer"})


A few important/required DXF attributes are explicit method arguments,
most additional DXF attributes are gives as a regular Python
:class:`dict` object by the keyword only argument :attr:`dxfattribs`.
The supported DXF attributes can be found in the documentation of the
:mod:`ezdxf.entities` module.

.. warning::

    Do not instantiate DXF entities by yourself and add them to layouts, always
    use the provided factory methods to create new graphical entities, this is
    the intended way to use `ezdxf`.

.. seealso::

    - :ref:`thematic_factory_method_index`
    - :ref:`tut_simple_drawings`
    - :ref:`tut_dxf_primitives`
    - :ref:`tut_lwpolyline`
    - :ref:`tut_text`
    - :ref:`tut_mtext`
    - :ref:`tut_hatch`

Saving DXF Files
----------------

Save the DXF document with a new name::

    doc.saveas("new_name.dxf")

or with the same name as loaded::

    doc.save()

.. seealso::

    Documentation for :func:`ezdxf.document.Drawing.save` and
    :func:`ezdxf.document.Drawing.saveas`, for more information about file
    management go to the :ref:`dwgmanagement` section.

Create New Blocks
-----------------

The block definitions of a DXF document are managed by the
:class:`~ezdxf.sections.blocks.BlocksSection` object::

    my_block = doc.blocks.new("MyBlock")

.. seealso::

    :ref:`tut_blocks`


Create Block References
-----------------------

A block reference is just another DXF entity called INSERT.
The :class:`~ezdxf.entities.Insert` entity is created by the factory method:
:meth:`~ezdxf.layouts.BaseLayout.add_blockref`::

    msp.add_blockref("MyBlock", (0, 0))


.. seealso::

    See :ref:`tut_blocks` for more advanced features like using
    :class:`~ezdxf.entities.Attrib` entities.


Create New Layers
-----------------

A layer is not an entity container, a layer is just another DXF attribute
stored in the entity and the entity can inherit some properties from this
:class:`~ezdxf.entities.Layer` object.
Layer objects are stored in the layer table which is available as
attribute :code:`doc.layers`.

You can create your own layers::

    my_layer = doc.layers.add("MyLayer")

The layer object also controls the visibility of entities which references this
layer, the on/off state of the layer is unfortunately stored as positive or
negative color value which make the raw DXF attribute of layers useless, to
change the color of a layer use the property :attr:`Layer.color` ::

    my_layer.color = 1

To change the state of a layer use the provided methods of the
:class:`~ezdxf.entities.Layer` object, like
:meth:`~ezdxf.entities.Layer.on`, :meth:`~ezdxf.entities.Layer.off`,
:meth:`~ezdxf.entities.Layer.freeze` or :meth:`~ezdxf.entities.Layer.thaw`::

    my_layer.off()

.. seealso::

    :ref:`layer_concept`

Delete Entities
---------------

The safest way to delete entities is to delete the entity from the layout
containing that entity::

    line = msp.add_line((0, 0), (1, 0))
    msp.delete_entity(line)

This removes the entity immediately from the layout and destroys the entity.
The property :attr:`~ezdxf.entities.DXFEntity.is_alive` returns ``False`` for a
destroyed entity and all Python attributes are deleted, so
:code:`line.dxf.color` will raise an :class:`AttributeError` exception,
because ``line`` does not have a :attr:`~ezdxf.entities.DXFEntity.dxf`
attribute anymore.

`Ezdxf` also supports manually destruction of entities by calling the method
:meth:`~ezdxf.entities.DXFEntity.destroy`::

    line.destroy()

Manually destroyed entities are not removed immediately from entities containers
like :class:`Modelspace` or :class:`EntityQuery`, but iterating such a container
will filter destroyed entities automatically, so a :code:`for e in msp: ...` loop
will never yield destroyed entities. The index operator and the :func:`len`
function do **not** filter deleted entities, to avoid getting deleted entities
call the :func:`purge` method of the container manually to remove deleted
entities.

Further Information
-------------------

- :ref:`basic concepts`: what is the meaning or purpose of ...
- :ref:`tasks`: how to accomplish certain tasks
- :ref:`reference`