Blocks Section

The BLOCKS section is the home all block definitions (BlockLayout) of a DXF document.

See also

DXF Internals: BLOCKS Section and Block Management Structures

class ezdxf.sections.blocks.BlocksSection
__iter__() → Iterable[BlockLayout]

Iterable of all BlockLayout objects.

__contains__(name: str) → bool

Returns True if BlockLayout name exist.

__getitem__(name: str) → BlockLayout

Returns BlockLayout name, raises DXFKeyError if name not exist.

__delitem__(name: str) → None

Deletes BlockLayout name and all of its content, raises DXFKeyError if name not exist.

get(self, name: str, default=None) → BlockLayout

Returns BlockLayout name, returns default if name not exist.

new(name: str, base_point: Sequence[float] = 0, 0, dxfattribs: dict = None) → BlockLayout

Create and add a new BlockLayout, name is the BLOCK name, base_point is the insertion point of the BLOCK.

new_anonymous_block(type_char: str = 'U', base_point: Sequence[float] = 0, 0) → BlockLayout

Create and add a new anonymous BlockLayout, type_char is the BLOCK type, base_point is the insertion point of the BLOCK.

type_char

Anonymous Block Type

'U'

'*U###' anonymous BLOCK

'E'

'*E###' anonymous non-uniformly scaled BLOCK

'X'

'*X###' anonymous HATCH graphic

'D'

'*D###' anonymous DIMENSION graphic

'A'

'*A###' anonymous GROUP

'T'

'*T###' anonymous block for ACAD_TABLE content

rename_block(old_name: str, new_name: str) → None

Rename BlockLayout old_name to new_name

delete_block(name: str, safe: bool = True) → None

Delete block. If save is True, check if block is still referenced.

Parameters
  • name – block name (case insensitive)

  • safe – check if block is still referenced or special block without explicit references

Raises
  • DXFKeyError – if block not exists

  • DXFBlockInUseError – if block is still referenced, and save is True

delete_all_blocks()

Delete all blocks without references except modelspace- or paperspace layout blocks, special arrow- and anonymous blocks (DIMENSION, ACAD_TABLE).

Warning

There could exist undiscovered references to blocks which are not documented in the DXF reference, hidden in extended data sections or application defined data, which could produce invalid DXF documents if such referenced blocks will be deleted.

Changed in version 0.14: removed unsafe mode

purge()

Delete all unused blocks like delete_all_blocks(), but also removes unused anonymous blocks.

Warning

There could exist undiscovered references to blocks which are not documented in the DXF reference, hidden in extended data sections or application defined data, which could produce invalid DXF documents if such referenced blocks will be deleted.