Import data from other DXF drawings

class Importer

Import definitions and entities from other DXF drawings.

  • can import line-, text-, dimension-styles and layer-definitions
  • can import block-definitions
  • can import entities from model-space
  • can’t import layouts
  • can’t import entities from layouts

Compatible Drawings

  • It is always possible to copy from older to newer versions (except R12).
  • It is possible to copy an entity from a newer to an older versions, if the entity is defined for both versions (like LINE, CIRCLE, …), but this can not be granted by default. Enable this feature by Importer(s, t, strict_mode=False).

Incompatible Drawings

The basic DXF structure has been changed with version AC1012 (AutoCAD R13):

  • can’t copy from R12 to newer versions, it’s possible if strict_mode=False, but the target drawing is invalid.
  • can’t copy from newer versions to R12, it’s possible if strict_mode=False, but the target drawing is invalid.
Importer.__init__(source, target, strict_mode=True)
Parameters:
  • source – source drawing of type Drawing
  • target – target drawing of type Drawing
  • strict_mode (bool) – import is only possible, if the drawings are compatible.

Now you can import DXF tables, like layer definitions and dimension style definitions or block definitions from the blocks section or DXF entities from the model-space.

First create an Importer object:

import ezdxf

source_drawing = ezdxf.readfile("Source_DXF_Drawing.dxf")
target_drawing = ezdxf.new(dxfversion=source_drawing.dxfversion)
importer = ezdxf.Importer(source_drawing, target_drawing)

Import Tables

Import line-, text-, dimension-styles and layer-definitions from other DXF drawing.

Importer.import_tables(query='*', conflict='discard')

Import all tables listed by the query string, * means all tables. Valid table names are layers, linetypes, appids, dimstyles, styles, ucs, views, viewports and block_records.

Importer.import_table(name, query='*', conflict='discard')

Import table entries from a specific table, the query string specifies the entries to import, * means all table entries.

Parameters:
  • discard: already existing entries will be preserved
  • replace: already existing entries will replaced by entries from the source drawing

Import Block Definitions

Import block-definitions from other DXF drawings.

Importer.import_blocks(query='*', conflict='discard')

Import block definitions, the query string specifies the blocks to import, * means all blocks.

Parameters:
  • query (str) – is a Name Query String
  • conflict (str) – discard | replace | rename
  • discard: already existing blocks will be preserved
  • replace: already existing blocks will replaced by blocks from the source drawing
  • rename: the imported block gets a new name, existing references in the source drawing will be resolved if possible. Block references in the model-space will be resolved, if they are imported AFTER importing the block definitions.

Import Model-Space Entities

Import entities from model-space of other DXF drawings.

Importer.import_modelspace_entities(query='*')

Import DXF entities from source model-space to the target model-space, select DXF types to import by the query string, * means all DXF types. If called after the import_blocks() method, references to renamed blocks will be resolved.

Parameters:query (str) – is an Entity Query String

Additional Methods

Importer.is_compatible()

True if drawings are compatible, else False.

Importer.import_all(table_conflict='discard', block_conflict='discard')

Import all tables, block-definitions and entities from model-space.