Importer

This rewritten Importer class from ezdxf v0.10 is not compatible to previous ezdxf versions, but previous implementation was already broken.

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 new 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 drawings.

Parameters
Variables
  • source – source drawing

  • target – target drawing

  • used_layer – 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 import by importing required table entries and block definition, without finalization the target drawing is maybe invalid fore AutoCAD. Call finalize() as last step of the import process.

import_block(block_name: str, rename=True) → str

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

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

Parameters
  • block_name – name of block to import

  • rename – rename block if exists in target drawing

Returns: block name (renamed)

Raises

ValueError – source block not found

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

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

Parameters
  • block_names – names of blocks to import

  • rename – rename block if exists in target drawing

Raises

ValueError – source block not found

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

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

Parameters
  • entities – Iterable of DXF entities

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

Raises

DXFStructureErrortarget_layout is not a layout of target drawing

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

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

Parameters
  • entity – DXF entity to import

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

Raises

DXFStructureErrortarget_layout is not a layout of target drawing

import_modelspace(target_layout: BaseLayout = None) → None

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

Parameters

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

Raises

DXFStructureErrortarget_layout is not a layout of target drawing

import_paperspace_layout(name: str) → Layout

Import paperspace layout name into target drawing. Recreates the source paperspace layout in the target drawing, renames the target paperspace if already a paperspace with same name exist and imports all entities from source paperspace into 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 target drawing. Target layouts will be renamed if already a layout with same name exist. Layouts will be imported in original tab order.

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

Import specific table entries from source drawing into target drawing.

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

  • replace – True to replace already existing table entry else ignore existing entry

Raises

TypeError – unsupported table type

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

Import DXF tables from source drawing into target drawing.

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

  • replace – True 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 drawing. The layout will be renamed if name already exist in the target drawing. Returns target modelspace for layout name “Model”.

Parameters

name – layout name as string

Raises

KeyError – if source layout name not exist