Get Layouts and Blocks

Layouts and blocks contain all the graphical entities likes LINE, CIRCLE and so on.

Get all paperspace and modelspace layout names in arbitrary order:

layout_names = doc.layout_names()

Get all paperspace and modelspace layout names in tab-order of CAD applications:

layout_names = doc.layout_names_in_taborder()

Modelspace

Each DXF document has one and only one Modelspace layout.

The modelspace() method of the Drawing class returns the Modelspace object.

msp = doc.modelspace()

Paperspace Layouts

Each DXF document has one or more Paperspace layout. DXF R12 supports only one paperspace layout.

Get the active (default) paperspace layout:

psp = doc.paperspace()

Get a paperspace layout by name:

psp = doc.paperspace("Layout0")

The name argument is the name shown in the tabs of CAD applications.

Block Layouts

Blocks are collections of DXF entities which can be placed multiple times as block references in different layouts and other block definitions.

Iterate over all block definitions:

for block in doc.blocks:
    print(block.name)

Get block definition by name:

block = doc.blocks.get("MyBlock")
if block is None:
    print("block not found.")

Count block references:

from ezdxf import blkrefs

...

counter = blkrefs.BlockReferenceCounter(doc)

count = counter.by_name("MyBlock")
print(f"MyBlock is referenced {count} times."

Find unused (unreferenced) block definitions:

Added in version 1.3.5.

from ezdxf import blkrefs

...

for name in blkrefs.find_unreferenced_blocks(doc)
    block = doc.blocks.get(name)