Blocks Section

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

Warning

Blocks are an essential building block of the DXF format. Most blocks are referenced are by name, and renaming or deleting a block is not as easy as it seems, since there is no overall index where all block references appear, and such block references can also reside in custom data or even custom entities, therefore renaming or deleting block definitions can damage a DXF file!

See also

DXF Internals: BLOCKS Section and Block Management Structures

class ezdxf.sections.blocks.BlocksSection
__iter__() → Iterator[ezdxf.layouts.blocklayout.BlockLayout]

Iterable of all BlockLayout objects.

__contains__(name: str) → bool

Returns True if BlockLayout name exist.

__getitem__(name: str) → ezdxf.layouts.blocklayout.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(name: str, default=None) → ezdxf.layouts.blocklayout.BlockLayout

Returns BlockLayout name, returns default if name not exist.

new(name: str, base_point: Union[Sequence[float], ezdxf.math._vector.Vec2, ezdxf.math._vector.Vec3] = Vec3(0.0, 0.0, 0.0), dxfattribs=None) → ezdxf.layouts.blocklayout.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: Union[Sequence[float], ezdxf.math._vector.Vec2, ezdxf.math._vector.Vec3] = Vec3(0.0, 0.0, 0.0)) → ezdxf.layouts.blocklayout.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

Warning

This is a low-level tool and does not rename the block references, so all block references to old_name are pointing to a non-existing block definition!

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

Delete block. Checks if the block is still referenced if safe is True.

Parameters:
  • name – block name (case insensitive)
  • safe – check if the block is still referenced or a special block without explicit references
Raises:
  • DXFKeyError – if block not exists
  • DXFBlockInUseError – if block is still referenced, and safe is True
delete_all_blocks() → None

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

Warning

There could exist references to blocks which are not documented in the DXF reference, hidden in extended data sections or application defined data, which could invalidate a DXF document if these blocks will be deleted.