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
|
General Document
================
General preconditions:
.. code-block:: python
import sys
import ezdxf
try:
doc = ezdxf.readfile("your_dxf_file.dxf")
except IOError:
print(f"Not a DXF file or a generic I/O error.")
sys.exit(1)
except ezdxf.DXFStructureError:
print(f"Invalid or corrupted DXF file.")
sys.exit(2)
msp = doc.modelspace()
This works well with DXF files from trusted sources like AutoCAD or BricsCAD,
for loading DXF files with minor or major flaws look at the
:mod:`ezdxf.recover` module.
Load DXF Files with Structure Errors
------------------------------------
If you know the files you will process have most likely minor or major flaws,
use the :mod:`ezdxf.recover` module:
.. code-block:: Python
import sys
from ezdxf import recover
try: # low level structure repair:
doc, auditor = recover.readfile(name)
except IOError:
print(f"Not a DXF file or a generic I/O error.")
sys.exit(1)
except ezdxf.DXFStructureError:
print(f"Invalid or corrupted DXF file: {name}.")
sys.exit(2)
# DXF file can still have unrecoverable errors, but this is maybe
# just a problem when saving the recovered DXF file.
if auditor.has_errors:
print(f"Found unrecoverable errors in DXF file: {name}.")
auditor.print_error_report()
For more loading scenarios follow the link: :mod:`ezdxf.recover`
.. _set/get header variables:
Set/Get Header Variables
------------------------
`ezdxf` has an interface to get and set HEADER variables:
.. code-block:: python
doc.header["VarName"] = value
value = doc.header["VarName"]
.. seealso::
:class:`~ezdxf.sections.header.HeaderSection` and online documentation from Autodesk
for available `header variables`_.
.. _set drawing units:
Set DXF Drawing Units
---------------------
The header variable $INSUNITS defines the drawing units for the modelspace and
therefore for the DXF document if no further settings are applied. The most
common units are 6 for meters and 1 for inches.
Use this HEADER variables to setup the default units for CAD applications
opening the DXF file. This setting is not relevant for `ezdxf` API calls,
which are unitless for length values and coordinates and decimal degrees for
angles (in most cases).
Sets drawing units:
.. code-block:: python
doc.header["$INSUNITS"] = 6
For more information see section :ref:`DXF Units`.
Explore the DXF File Structure
------------------------------
DXF files are plain text files, you can open this files with every text editor
which handles bigger files. But it is not really easy to get quick the
information you want.
Use the DXF structure browser:
.. code-block::
# Call as executable script from the command line:
ezdxf browse FILE
# Call as module on Windows:
py -m ezdxf browse FILE
# Call as module on Linux/Mac
python3 -m ezdxf browse FILE
This command requires `PySide6` or `PyQt5` to be installed. It opens a desktop window
with a selection panel for all DXF entities in the document, and handles int the entity
view are links between DXF entities, this simplifies the navigation between the DXF
entities. Read the docs for the :ref:`browse_command` command for more information.
.. figure:: ../gfx/gear-browse.png
.. _calc msp extents:
Calculate Extents for the Modelspace
------------------------------------
Since `ezdxf` v0.16 exist a :mod:`ezdxf.bbox` module to calculate bounding
boxes for DXF entities. This module makes the extents calculation very easy,
but read the documentation for the :mod:`~ezdxf.bbox` module to understand its
limitations.
.. code-block:: Python
import ezdxf
from ezdxf import bbox
doc = ezdxf.readfile("your.dxf")
msp = doc.modelspace()
extents = bbox.extents(msp)
The returned `extents` is a :class:`ezdxf.math.BoundingBox` object.
.. _set msp initial view:
Set Initial View/Zoom for the Modelspace
----------------------------------------
To show an arbitrary location of the modelspace centered in the CAD application
window, set the ``'*Active'`` VPORT to this location. The DXF attribute
:attr:`dxf.center` defines the location in the modelspace, and the :attr:`dxf.height`
specifies the area of the modelspace to view. Shortcut function:
.. code-block:: Python
doc.set_modelspace_vport(height=10, center=(10, 10))
.. seealso::
The :mod:`ezdxf.zoom` module is another way to set the initial modelspace
view.
Setting the initial view to the extents of all entities in the modelspace:
.. code-block:: Python
import ezdxf
from ezdxf import zoom
doc = ezdxf.readfile("your.dxf")
msp = doc.modelspace()
zoom.extents(msp)
Setting the initial view to the extents of just some entities:
.. code-block:: Python
lines = msp.query("LINES")
zoom.objects(lines)
The :mod:`~ezdxf.zoom` module also works for paperspace layouts.
.. Important::
The :mod:`~ezdxf.zoom` module uses the :mod:`~ezdxf.bbox` module to
calculate the bounding boxes for DXF entities. Read the documentation for
the :mod:`~ezdxf.bbox` module to understand its limitations and the
bounding box calculation for large documents can take a while!
Hide the UCS Icon
-----------------
The visibility of the UCS icon is controlled by the DXF
:attr:`~ezdxf.entities.VPort.dxf.ucs_icon` attribute of the
:class:`~ezdxf.entities.VPort` entity:
- bit 0: 0=hide, 1=show
- bit 1: 0=display in lower left corner, 1=display at origin
The state of the UCS icon can be set in conjunction with the initial
:class:`~ezdxf.entities.VPort` of the model space, this code turns off the UCS
icon:
.. code-block:: Python
doc.set_modelspace_vport(10, center=(10, 10), dxfattribs={"ucs_icon": 0})
Alternative: turn off UCS icons for all :class:`VPort` entries in the active
viewport configuration:
.. code-block:: Python
for vport in doc.viewports.get_config("*Active"):
vport.dxf.ucs_icon = 0
Show Lineweights in DXF Viewers
-------------------------------
By default lines and curves are shown without lineweights in DXF viewers.
By setting the header variable $LWDISPLAY to 1 the DXF viewer should display
lineweights, if supported by the viewer.
.. code-block:: Python
doc.header["$LWDISPLAY"] = 1
Add `ezdxf` Resources to Existing DXF Document
----------------------------------------------
Add all `ezdxf` specific resources (line types, text- and dimension styles)
to an existing DXF document:
.. code-block:: Python
import ezdxf
from ezdxf.tools.standards import setup_drawing
doc = ezdxf.readfile("your.dxf")
setup_drawing(doc, topics="all")
Set Logging Level of `ezdxf`
----------------------------
Set the logging level of the `ezdxf` package to a higher level to minimize
logging messages from ezdxf. At level ``ERROR`` only severe errors will be
logged and ``WARNING``, ``INFO`` and ``DEBUG`` messages will be suppressed:
.. code-block:: Python
import logging
logging.getLogger("ezdxf").setLevel(logging.ERROR)
.. _header variables: http://help.autodesk.com/view/OARX/2018/ENU/?guid=GUID-A85E8E67-27CD-4C59-BE61-4DC9FADBE74A
|