Importer

Note

This module exists for compatibility reasons only and is superseded by the newer and more robust core module ezdxf.xref. For information on how to use it, see the Importing Data and Resources section.

This add-on is no longer maintained and will not receive updates or bug fixes.

This add-on is meant to import graphical entities from another DXF drawing and their required table entries like LAYER, LTYPE or STYLE.

Because of complex extensibility of the DXF format and the lack of sufficient documentation, I decided to remove most of the possible source drawing dependencies from imported entities, therefore imported entities may not look the same as the original entities in the source drawing, but at least the geometry should be the same and the DXF file does not break.

Removed data which could contain source drawing dependencies: Extension Dictionaries, AppData and XDATA.

Warning

DON’T EXPECT PERFECT RESULTS!

The Importer supports following data import:

  • entities which are really safe to import: LINE, POINT, CIRCLE, ARC, TEXT, SOLID, TRACE, 3DFACE, SHAPE, POLYLINE, ATTRIB, ATTDEF, INSERT, ELLIPSE, MTEXT, LWPOLYLINE, SPLINE, HATCH, MESH, XLINE, RAY, DIMENSION, LEADER, VIEWPORT

  • table and table entry import is restricted to LAYER, LTYPE, STYLE, DIMSTYLE

  • import of BLOCK definitions is supported

  • import of paper space layouts is supported

Import of DXF objects from the OBJECTS section is not supported.

DIMSTYLE override for entities DIMENSION and LEADER is not supported.

Example:

import ezdxf
from ezdxf.addons import Importer

sdoc = ezdxf.readfile('original.dxf')
tdoc = ezdxf.new()

importer = Importer(sdoc, tdoc)

# import all entities from source modelspace into modelspace of the target drawing
importer.import_modelspace()

# import all paperspace layouts from source drawing
importer.import_paperspace_layouts()

# import all CIRCLE and LINE entities from source modelspace into an arbitrary target layout.
# create target layout
tblock = tdoc.blocks.new('SOURCE_ENTS')
# query source entities
ents = sdoc.modelspace().query('CIRCLE LINE')
# import source entities into target block
importer.import_entities(ents, tblock)

# This is ALWAYS the last & required step, without finalizing the target drawing is maybe invalid!
# This step imports all additional required table entries and block definitions.
importer.finalize()

tdoc.saveas('imported.dxf')
class ezdxf.addons.importer.Importer(source: Drawing, target: Drawing)

The Importer class is central element for importing data from other DXF documents.

Parameters:
  • source – source Drawing

  • target – target Drawing

source

source DXF document

target

target DXF document

used_layers

Set of used layer names as string, AutoCAD accepts layer names without a LAYER table entry.

used_linetypes

Set of used linetype names as string, these linetypes require a TABLE entry or AutoCAD will crash.

used_styles

Set of used text style names, these text styles require a TABLE entry or AutoCAD will crash.

used_dimstyles

Set of used dimension style names, these dimension styles require a TABLE entry or AutoCAD will crash.

finalize() None

Finalize the import by importing required table entries and BLOCK definitions, without finalization the target document is maybe invalid for AutoCAD. Call the finalize() method as last step of the import process.

import_block(block_name: str, rename=True) str

Import one BLOCK definition from source document.

If the BLOCK already exist the BLOCK will be renamed if argument rename is True, otherwise the existing BLOCK in the target document will be used instead of the BLOCK in the source document. Required name resolving for imported block references (INSERT), will be done in the Importer.finalize() method.

To replace an existing BLOCK in the target document, just delete it before importing data: target.blocks.delete_block(block_name, safe=False)

Parameters:
  • block_name – name of BLOCK to import

  • rename – rename BLOCK if a BLOCK with the same name already exist in target document

Returns: (renamed) BLOCK name

Raises:

ValueError – BLOCK in source document not found (defined)

import_blocks(block_names: Iterable[str], rename=False) None

Import all BLOCK definitions from source document.

If a BLOCK already exist the BLOCK will be renamed if argument rename is True, otherwise the existing BLOCK in the target document will be used instead of the BLOCK from the source document. Required name resolving for imported BLOCK references (INSERT), will be done in the Importer.finalize() method.

Parameters:
  • block_names – names of BLOCK definitions to import

  • rename – rename BLOCK if a BLOCK with the same name already exist in target document

Raises:

ValueError – BLOCK in source document not found (defined)

import_entities(entities: Iterable[DXFEntity], target_layout: BaseLayout | None = None) None

Import all entities into target_layout or the modelspace of the target document, if target_layout is None.

Parameters:
  • entities – Iterable of DXF entities

  • target_layout – any layout (modelspace, paperspace or block) from the target document

Raises:

DXFStructureErrortarget_layout is not a layout of target document

import_entity(entity: DXFEntity, target_layout: BaseLayout | None = None) None

Imports a single DXF entity into target_layout or the modelspace of the target document, if target_layout is None.

Parameters:
  • entity – DXF entity to import

  • target_layout – any layout (modelspace, paperspace or block) from the target document

Raises:

DXFStructureErrortarget_layout is not a layout of target document

import_modelspace(target_layout: BaseLayout | None = None) None

Import all entities from source modelspace into target_layout or the modelspace of the target document, if target_layout is None.

Parameters:

target_layout – any layout (modelspace, paperspace or block) from the target document

Raises:

DXFStructureErrortarget_layout is not a layout of target document

import_paperspace_layout(name: str) Layout

Import paperspace layout name into the target document.

Recreates the source paperspace layout in the target document, renames the target paperspace if a paperspace with same name already exist and imports all entities from the source paperspace into the target paperspace.

Parameters:

name – source paper space name as string

Returns: new created target paperspace Layout

Raises:
  • KeyError – source paperspace does not exist

  • DXFTypeError – invalid modelspace import

import_paperspace_layouts() None

Import all paperspace layouts and their content into the target document. Target layouts will be renamed if a layout with the same name already exist. Layouts will be imported in original tab order.

import_shape_files(fonts: set[str]) None

Import shape file table entries from the source document into the target document. Shape file entries are stored in the styles table but without a name.

import_table(name: str, entries: str | Iterable[str] = '*', replace=False) None

Import specific table entries from the source document into the target document.

Parameters:
  • name – valid table names are “layers”, “linetypes” and “styles”

  • entries – Iterable of table names as strings, or a single table name or “*” for all table entries

  • replaceTrue to replace the already existing table entry else ignore existing entries

Raises:

TypeError – unsupported table type

import_tables(table_names: str | Iterable[str] = '*', replace=False) None

Import DXF tables from the source document into the target document.

Parameters:
  • table_names – iterable of tables names as strings, or a single table name as string or “*” for all supported tables

  • replaceTrue to replace already existing table entries else ignore existing entries

Raises:

TypeError – unsupported table type

recreate_source_layout(name: str) Layout

Recreate source paperspace layout name in the target document. The layout will be renamed if name already exist in the target document. Returns target modelspace for layout name “Model”.

Parameters:

name – layout name as string

Raises:

KeyError – if source layout name not exist