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[BlockLayout] ¶
Iterable of all
BlockLayout
objects.
- __contains__(name: str) bool ¶
Returns
True
ifBlockLayout
name exist.
- __getitem__(name: str) BlockLayout ¶
Returns
BlockLayout
name, raisesDXFKeyError
if name not exist.
- __delitem__(name: str) None ¶
Deletes
BlockLayout
name and all of its content, raisesDXFKeyError
if name not exist.
- get(name: str, default=None) BlockLayout ¶
Returns
BlockLayout
name, returns default if name not exist.
- new(name: str, base_point: UVec = NULLVEC, dxfattribs=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: UVec = NULLVEC) 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_nameWarning
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.
Applies some safety checks when safe is
True
. ADXFBlockInUseError
will be raised for:blocks with active references
blocks representing existing layouts
special blocks used internally
- Parameters:
name – block name (case-insensitive)
safe – apply safety checks
- Raises:
DXFKeyError – if block not exists
DXFBlockInUseError – when safe is
True
and block is in use
- 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.