dxf2code

Translate DXF entities and structures into Python source code.

Short example:

import ezdxf
from ezdxf.addons.dxf2code import entities_to_code, block_to_code

doc = ezdxf.readfile('original.dxf')
msp = doc.modelspace()
source = entities_to_code(msp)

# create source code for a block definition
block_source = block_to_code(doc.blocks['MyBlock'])

# merge source code objects
source.merge(block_source)

with open('source.py', mode='wt') as f:
    f.write(source.import_str())
    f.write('\n\n')
    f.write(source.code_str())
    f.write('\n')
ezdxf.addons.dxf2code.entities_to_code(entities: Iterable[DXFEntity], layout: str = 'layout', ignore: Iterable[str] = None)Code

Translates DXF entities into Python source code to recreate this entities by ezdxf.

Parameters
  • entities – iterable of DXFEntity

  • layout – variable name of the layout (model space or block) as string

  • ignore – iterable of entities types to ignore as strings like ['IMAGE', 'DIMENSION']

Returns

Code

ezdxf.addons.dxf2code.block_to_code(block: BlockLayout, drawing: str = 'doc', ignore: Iterable[str] = None)Code

Translates a BLOCK into Python source code to recreate the BLOCK by ezdxf.

Parameters
  • block – block definition layout

  • drawing – variable name of the drawing as string

  • ignore – iterable of entities types to ignore as strings like [‘IMAGE’, ‘DIMENSION’]

Returns

Code

ezdxf.addons.dxf2code.table_entries_to_code(entities: Iterable[DXFEntity], drawing='doc')Code
class ezdxf.addons.dxf2code.Code

Source code container.

code

Source code line storage, store lines without line ending \\n

imports

source code line storage for global imports, store lines without line ending \\n

layers

Layers used by the generated source code, AutoCAD accepts layer names without a LAYER table entry.

linetypes

Linetypes used by the generated source code, these linetypes require a TABLE entry or AutoCAD will crash.

styles

Text styles used by the generated source code, these text styles require a TABLE entry or AutoCAD will crash.

dimstyles

Dimension styles used by the generated source code, these dimension styles require a TABLE entry or AutoCAD will crash.

blocks

Blocks used by the generated source code, these blocks require a BLOCK definition in the BLOCKS section or AutoCAD will crash.

code_str(indent: int = 0) → str

Returns the source code as a single string.

Parameters

indent – source code indentation count by spaces

import_str(indent: int = 0) → str

Returns required imports as a single string.

Parameters

indent – source code indentation count by spaces

merge(code: ezdxf.addons.dxf2code.Code, indent: int = 0) → None

Add another Code object.

add_import(statement: str) → None

Add import statement, identical import statements are merged together.

add_line(code: str, indent: int = 0) → None

Add a single source code line without line ending \n.

add_lines(code: Iterable[str], indent: int = 0) → None

Add multiple source code lines without line ending \n.