A comprehensive Python package for creating, reading, modifying, and writing DXF (Drawing Exchange Format) documents with support for multiple DXF versions.
npx @tessl/cli install tessl/pypi-ezdxf@1.4.0A comprehensive Python library for creating, reading, modifying, and writing DXF (Drawing Exchange Format) documents. ezdxf provides full support for DXF versions R12 through R2018, enabling seamless manipulation of CAD files while preserving all content including third-party extensions. The library offers both high-level convenience functions and low-level access to DXF structures, making it suitable for everything from simple CAD file processing to complex CAD application development.
pip install ezdxfpip install ezdxf[draw] for rendering support (PySide6, matplotlib, PyMuPDF, Pillow)import ezdxfCommon usage patterns:
# Document operations
from ezdxf import new, read, readfile
# Math utilities
from ezdxf.math import Vec3, Matrix44, BSpline
# Entity classes
from ezdxf.entities import Line, Circle, Text
# Rendering utilities
from ezdxf.render import MeshBuilder
from ezdxf.path import Path
# Colors and enums
from ezdxf.colors import RGB
from ezdxf.enums import TextHAlignimport ezdxf
from ezdxf.math import Vec3
# Create a new DXF document
doc = ezdxf.new('R2018', setup=True)
# Get model space (main drawing area)
msp = doc.modelspace()
# Add basic entities
line = msp.add_line(start=(0, 0), end=(10, 10))
circle = msp.add_circle(center=(5, 5), radius=3)
text = msp.add_text("Hello World", height=2.5).set_placement((0, 0))
# Add a polyline with multiple points
points = [Vec3(0, 0), Vec3(3, 0), Vec3(6, 3), Vec3(6, 6)]
polyline = msp.add_lwpolyline(points)
# Save the document
doc.saveas('example.dxf')
# Read an existing DXF file
doc2 = ezdxf.readfile('existing.dxf')
# Query entities by type
lines = doc2.modelspace().query('LINE')
for line in lines:
print(f"Line from {line.dxf.start} to {line.dxf.end}")
# Audit and repair
auditor = doc2.audit()
if auditor.has_errors:
print(f"Found {len(auditor.errors)} errors")
auditor.print_report()The ezdxf library is built around several key architectural components:
This modular design supports both simple CAD file manipulation and sophisticated CAD application development, with clear separation between high-level convenience functions and low-level DXF structure access.
Core functionality for creating, reading, writing, and managing DXF documents across all supported versions. Includes file I/O, document structure management, and validation.
def new(dxfversion: str = 'R2018', setup: bool = None, units: int = 1): ...
def read(stream, *, errors: str = 'surrogateescape'): ...
def readfile(filename: str, *, encoding: str = None, errors: str = 'surrogateescape'): ...
def readzip(zipfile: str, filename: str = None, *, errors: str = 'surrogateescape'): ...Complete set of entity classes for all DXF entity types from basic geometry (lines, circles, arcs) to complex entities (hatches, meshes, dimensions). Includes entity creation, modification, and property management.
class Line(DXFGraphic):
def __init__(self): ...
@property
def start(self) -> Vec3: ...
@property
def end(self) -> Vec3: ...
class Circle(DXFGraphic):
def __init__(self): ...
@property
def center(self) -> Vec3: ...
@property
def radius(self) -> float: ...Organization and management of drawing content through Model Space and Paper Space layouts. Includes layout creation, entity placement, and viewport management.
class Layout:
def add_line(self, start, end, dxfattribs: dict = None): ...
def add_circle(self, center, radius: float, dxfattribs: dict = None): ...
def add_text(self, text: str, dxfattribs: dict = None): ...
def query(self, query: str = '*'): ...Comprehensive mathematical utilities for 2D/3D geometry, transformations, curve calculations, and geometric constructions. Essential for CAD calculations and entity manipulation.
class Vec3:
def __init__(self, x: float = 0, y: float = 0, z: float = 0): ...
def __add__(self, other): ...
def __mul__(self, scalar: float): ...
def normalize(self): ...
class Matrix44:
def __init__(self): ...
@classmethod
def translate(cls, dx: float, dy: float, dz: float): ...
@classmethod
def scale(cls, sx: float, sy: float = None, sz: float = None): ...Advanced geometry processing including mesh construction, curve tessellation, and entity rendering to basic primitives. Supports both 2D and 3D geometry processing.
class MeshBuilder:
def __init__(self): ...
def add_face(self, vertices): ...
def add_mesh(self, other_mesh): ...
def render_mesh(self, layout, dxfattribs: dict = None): ...
def forms.circle(radius: float = 1, segments: int = None): ...
def forms.ngon(count: int, radius: float = 1): ...Vector path representation and manipulation for advanced geometric operations. Supports path creation from entities, transformations, and conversions between different geometric representations.
class Path:
def __init__(self): ...
def line_to(self, location): ...
def curve3_to(self, location, ctrl): ...
def curve4_to(self, location, ctrl1, ctrl2): ...
def transform(self, matrix): ...
def make_path(entity): ...
def to_lines(paths): ...
def to_polylines3d(paths): ...Color management system supporting RGB colors, AutoCAD Color Index (ACI), transparency, and color conversions. Includes predefined color palettes and utility functions.
class RGB:
def __init__(self, r: int, g: int, b: int): ...
def to_hex(self) -> str: ...
@classmethod
def from_hex(cls, color: str): ...
def int2rgb(value: int): ...
def rgb2int(rgb): ...
def transparency2float(value: int) -> float: ...Specialized utility functions for text processing, pattern management, standards setup, date/time conversion, and various CAD-specific operations.
def setup_linetypes(doc): ...
def setup_styles(doc): ...
def setup_dimstyles(doc): ...
class PatternAnalyser:
def __init__(self, pattern): ...
def analyse(self): ...Extended functionality including import/export utilities, geometry generators, table creation, dimension tools, and specialized format support.
class Importer:
def __init__(self, source_doc, target_doc): ...
def import_entities(self, entities): ...
class MengerSponge:
def __init__(self, level: int = 3, size: float = 1): ...
def render(self, layout): ...# Document and structure types
class Drawing:
"""Main DXF document class"""
class EntityDB:
"""Entity database for efficient storage and retrieval"""
class Auditor:
"""Document validation and error reporting"""
# Vector and matrix types
AnyVec = Union[Vec2, Vec3, Sequence[float]]
UVec = Union[UVec, Vertex]
# Entity base types
class DXFEntity:
"""Base class for all DXF entities"""
class DXFGraphic(DXFEntity):
"""Base class for graphical entities"""
class DXFEntity(DXFEntity):
"""Base class for non-graphical objects"""class DXFError(Exception):
"""Base exception for all DXF-related errors"""
class DXFStructureError(DXFError):
"""DXF document structure errors"""
class DXFVersionError(DXFError):
"""DXF version compatibility errors"""
class DXFAttributeError(DXFError):
"""DXF attribute access errors"""