iterdxf

This add-on allows iterating over entities of the modelspace of really big (> 5GB) DXF files which do not fit into memory by only loading one entity at the time. Only ASCII DXF files are supported.

The entities are regular DXFGraphic objects with access to all supported DXF attributes, this entities can be written to new DXF files created by the IterDXF.export() method. The new add_foreign_entity() method allows also to add this entities to new regular ezdxf drawings (except for the INSERT entity), but resources like linetype and style are removed, only layer will be preserved but only with default attributes like color 7 and linetype CONTINUOUS.

The following example shows how to split a big DXF files into several separated DXF files which contains only LINE, TEXT or POLYLINE entities.

from ezdxf.addons import iterdxf

doc = iterdxf.opendxf('big.dxf')
line_exporter = doc.export('line.dxf')
text_exporter = doc.export('text.dxf')
polyline_exporter = doc.export('polyline.dxf')
try:
    for entity in doc.modelspace():
        if entity.dxftype() == 'LINE':
            line_exporter.write(entity)
        elif entity.dxftype() == 'TEXT':
            text_exporter.write(entity)
        elif entity.dxftype() == 'POLYLINE':
            polyline_exporter.write(entity)
finally:
    line_exporter.close()
    text_exporter.close()
    polyline_exporter.close()
    doc.close()

Supported DXF types:

3DFACE, ARC, ATTDEF, ATTRIB, CIRCLE, DIMENSION, ELLIPSE, HATCH, HELIX, IMAGE, INSERT, LEADER, LINE, LWPOLYLINE, MESH, MLEADER, MLINE, MTEXT, POINT, POLYLINE, RAY, SHAPE, SOLID, SPLINE, TEXT, TRACE, VERTEX, WIPEOUT, XLINE

Transfer simple entities to another DXF document, this works for some supported entities, except for entities with strong dependencies to the original document like INSERT look at add_foreign_entity() for all supported types:

newdoc = ezdxf.new()
msp = newdoc.modelspace()
# line is an entity from a big source file
msp.add_foreign_entity(line)
# and so on ...
msp.add_foreign_entity(lwpolyline)
msp.add_foreign_entity(mesh)
msp.add_foreign_entity(polyface)

Transfer MESH and POLYFACE (dxftype for POLYFACE and POLYMESH is POLYLINE!) entities into a new DXF document by the MeshTransformer class:

from ezdxf.render import MeshTransformer

# mesh is MESH from a big source file
t = MeshTransformer.from_mesh(mesh)
# create a new MESH entity from MeshTransformer
t.render(msp)

# polyface is POLYFACE from a big source file
t = MeshTransformer.from_polyface(polyface)
# create a new POLYMESH entity from MeshTransformer
t.render_polyface(msp)

Another way to import entities from a big source file into new DXF documents is to split the big file into smaller parts and use the Importer add-on for a more safe entity import.

ezdxf.addons.iterdxf.opendxf(filename: Path | str, errors: str = 'surrogateescape') IterDXF

Open DXF file for iterating, be sure to open valid DXF files, no DXF structure checks will be applied.

Use this function to split up big DXF files as shown in the example above.

Parameters:
  • filename – DXF filename of a seekable DXF file.

  • errors

    specify decoding error handler

    • ”surrogateescape” to preserve possible binary data (default)

    • ”ignore” to use the replacement char U+FFFD “�” for invalid data

    • ”strict” to raise an UnicodeDecodeError exception for invalid data

Raises:
  • DXFStructureError – invalid or incomplete DXF file

  • UnicodeDecodeError – if errors is “strict” and a decoding error occurs

ezdxf.addons.iterdxf.modelspace(filename: Path | str, types: Iterable[str] | None = None, errors: str = 'surrogateescape') Iterable[DXFGraphic]

Iterate over all modelspace entities as DXFGraphic objects of a seekable file.

Use this function to iterate “quick” over modelspace entities of a DXF file, filtering DXF types may speed up things if many entity types will be skipped.

Parameters:
  • filename – filename of a seekable DXF file

  • types – DXF types like ['LINE', '3DFACE'] which should be returned, None returns all supported types.

  • errors

    specify decoding error handler

    • ”surrogateescape” to preserve possible binary data (default)

    • ”ignore” to use the replacement char U+FFFD “�” for invalid data

    • ”strict” to raise an UnicodeDecodeError exception for invalid data

Raises:
  • DXFStructureError – invalid or incomplete DXF file

  • UnicodeDecodeError – if errors is “strict” and a decoding error occurs

ezdxf.addons.iterdxf.single_pass_modelspace(stream: BinaryIO, types: Iterable[str] | None = None, errors: str = 'surrogateescape') Iterable[DXFGraphic]

Iterate over all modelspace entities as DXFGraphic objects in a single pass.

Use this function to ‘quick’ iterate over modelspace entities of a not seekable binary DXF stream, filtering DXF types may speed up things if many entity types will be skipped.

Parameters:
  • stream – (not seekable) binary DXF stream

  • types – DXF types like ['LINE', '3DFACE'] which should be returned, None returns all supported types.

  • errors

    specify decoding error handler

    • ”surrogateescape” to preserve possible binary data (default)

    • ”ignore” to use the replacement char U+FFFD “�” for invalid data

    • ”strict” to raise an UnicodeDecodeError exception for invalid data

Raises:
  • DXFStructureError – Invalid or incomplete DXF file

  • UnicodeDecodeError – if errors is “strict” and a decoding error occurs

class ezdxf.addons.iterdxf.IterDXF
export(name: Path | str) IterDXFWriter

Returns a companion object to export parts from the source DXF file into another DXF file, the new file will have the same HEADER, CLASSES, TABLES, BLOCKS and OBJECTS sections, which guarantees all necessary dependencies are present in the new file.

Parameters:

name – filename, no special requirements

modelspace(types: Iterable[str] | None = None) Iterable[DXFGraphic]

Returns an iterator for all supported DXF entities in the modelspace. These entities are regular DXFGraphic objects but without a valid document assigned. It is not possible to add these entities to other ezdxf documents.

It is only possible to recreate the objects by factory functions base on attributes of the source entity. For MESH, POLYMESH and POLYFACE it is possible to use the MeshTransformer class to render (recreate) this objects as new entities in another document.

Parameters:

types – DXF types like ['LINE', '3DFACE'] which should be returned, None returns all supported types.

close()

Safe closing source DXF file.

class ezdxf.addons.iterdxf.IterDXFWriter
write(entity: DXFGraphic)

Write a DXF entity from the source DXF file to the export file.

Don’t write entities from different documents than the source DXF file, dependencies and resources will not match, maybe it will work once, but not in a reliable way for different DXF documents.

close()

Safe closing of exported DXF file. Copying of OBJECTS section happens only at closing the file, without closing the new DXF file is invalid.