ezdxf

repository·master·Indexed 23 days ago

https://github.com/mozman/ezdxf

A Python package for creating, reading, modifying, and writing DXF documents. It supports various DXF versions, ASCII and Binary formats, and includes a command-line interface for inspecting, viewing, and converting files. The library features specialized add-ons such as r12writer for high-performance writing, a drawing layer for rendering via matplotlib or PyQt5, and the ezdxf.acis sub-package for managing ACIS data structures embedded in DXF and DWG files.

Tokens
202K
Snippets
357
Records
1.1K
Agent score
77%

What's inside ezdxf

  1. Overview of ezdxf capabilities

    master

    ezdxf is a Python package designed for programmers to create new DXF documents and read, modify, or write existing ones. It is OS-independent and supports both ASCII and Binary DXF formats.

    Key Specifications:

    • Python Requirement: At least Python 3.10.
    • DXF Version Support (Read/Write): R12, R2000, R2004, R2007, R2010, R2013, and R2018.
    • Legacy Support: Read-only support for R13/R14 (upgraded to R2000) and older versions (upgraded to R12).
    • License: MIT-License.
  2. What is ezdxf

    master

    ezdxf is a Python interface to the DXF (drawing interchange file) format. It allows developers to read, modify, and create new DXF documents. While it aims to hide complex DXF details, a basic understanding of the DXF format is recommended to effectively use its capabilities.

    Note: ezdxf is a replacement for the outdated dxfwrite and dxfgrabber packages, but it uses different APIs.

  3. Overview of ezdxf.render utilities

    master

    The ezdxf.render subpackage provides utilities for generating complex geometric forms and entities within a DXF document. It is primarily used to convert mathematical or complex curves into standard DXF entities that can be easily rendered or manipulated by CAD software.

    Key capabilities include:

    • Complex Meshes: Creating complex meshes as ezdxf.entities.Mesh entities.
    • Curve Rendering: Rendering complex curves (such as Bezier curves, Euler spirals, or splines) as ezdxf.entities.Polyline entities.
    • Vertex Generation: Generating vertices for simple and complex forms like circles, ellipses, or Euler spirals.
  4. Use math construction tools in ezdxf

    master

    The ezdxf.math module provides specialized tools for geometric construction and mathematical shape definitions. These tools can be used to calculate or define complex geometries before converting them into DXF entities.

    Available construction classes include:

    • Lines and Rays: ConstructionRay, ConstructionLine
    • Circular/Curved Shapes: ConstructionCircle, ConstructionArc, ConstructionEllipse
    • Polygonal/Complex Shapes: ConstructionPolyline, ConstructionBox, Shape2d
    • Splines and Curves: BSpline, Bezier4P, Bezier3P, Bezier, and EulerSpiral
  5. Use the Geo Interface for geospatial data in ezdxf

    master

    The ezdxf.addons.geo module provides a bridge between DXF entities and geospatial data formats via the __geo_interface__ mapping. It is intended to be used alongside dedicated geospatial libraries like Shapely, pyproj, or GDAL rather than replacing them.

    Key capabilities include:

    • Converting between DXF entities and __geo_interface__ compatible mappings.
    • Projecting coordinates using the WSG84 EPSG:3395 World Mercator projection.
    • Supporting standard GeoJSON types like Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, GeometryCollection, Feature, and FeatureCollection.
  6. Exchange meshes using the MeshExchange add-on

    master

    The ezdxf.addons.meshex module allows you to import and export meshes between DXF and several other 3D mesh formats.

    Supported Formats:

    • STL: Import/Export (supports only triangles as faces).
    • OFF: Import/Export (supports ngons, more compact than STL).
    • OBJ: Import/Export (supports ngons, can contain multiple meshes).
    • PLY: Export only (supports ngons).
    • OpenSCAD: Export as polyhedron (supports ngons).
    • IFC4: Export only (supports ngons).

    Important Limitations:

    • The source or target object must be an ezdxf.render.MeshBuilder instance.
    • Only vertices and faces are exchanged; colors, textures, and explicit face/vertex normals are lost.
    • This is a simple exchange tool and not a full-featured file format interface.
    • Warning: Meshes created via the ezdxf.addons.pycsg add-on may violate the vertex-to-vertex rule (a vertex of a face lying on the edge of another face), making them unsuitable for export in some formats.
    import ezdxf
    from ezdxf.addons import meshex
    
    # Example: Convert STL to DXF
    try:
        mesh = meshex.stl_readfile("your.stl")
    except (meshex.ParsingError, IOError) as e:
        print(str(e))
    
    doc = ezdxf.new()
    mesh.render_mesh(doc.modelspace())
    doc.saveas("your.dxf")
  7. Use ACIS tools for data management

    master

    The ezdxf.acis sub-package provides tools for loading, parsing, creating, and exporting simple and known ACIS data structures.

    Important Limitations:

    • It is not intended to load or edit arbitrary existing ACIS structures.
    • It cannot replace the official ACIS SDK as ezdxf does not provide an ACIS kernel.
    • The tools are primarily designed for use with ACIS data embedded in DXF and DWG files.
    • You can extract geometries consisting only of flat polygonal faces (polyhedrons) from ACIS data. Exporting these polyhedrons works for SAT data (DXF R2000-R2010) and SAB data (DXF R2013-R2018) in Autodesk products or BricsCAD.
  8. Use TablePainter to draw tables from DXF primitives

    master

    The TablePainter add-on allows you to draw tables using DXF primitives (lines, text, blocks) instead of the complex ACAD_TABLE entity. It supports TextCell (multi-line text) and BlockCell (block references with attributes). Cells are addressed using zero-based row and column indices.

    Important: This add-on does not create ACAD_TABLE entities; it creates a collection of standard DXF entities that visually represent a table.

    from ezdxf.addons import TablePainter
    
    table = TablePainter(
        insert=(0, 0), 
        nrows=4, 
        ncols=4, 
        cell_width=6.0, 
        cell_height=2.0
    )
  9. Use r12writer for fast DXF R12 creation

    master

    The ezdxf.addons.r12writer module provides a fast file/stream writer that creates simple DXF R12 drawings. Unlike standard ezdxf workflows, it writes entities directly as strings to a stream without building an in-memory drawing model, making it extremely efficient for high-volume entity generation.

    Key Characteristics:

    • Minimal Structure: Only contains an ENTITIES section. HEADER, TABLES, and BLOCKS sections are omitted unless fixed_tables=True is used.
    • Limited Entity Support: Only supports LINE, CIRCLE, ARC, TEXT, POINT, SOLID, 3DFACE, and POLYLINE.
    • No Blocks: Because the BLOCKS section is missing, BLOCK and INSERT entities cannot be used.
    • Layer Limitations: Layers use default color 7 (black/white) and linetype 'Continuous'. If fixed_tables=True is used, linetypes can be customized using predefined types.
    • Text Styles: If fixed_tables=True is used, you can use predefined styles like OpenSans or OpenSansCondensed-Light. Otherwise, the style is always 'STANDARD'.
  10. Use the ezdxf.math.clustering module for spatial analysis

    master
    The ezdxf.math.clustering module provides algorithms for grouping points or entities based on spatial proximity. It includes implementations for common clustering algorithms like K-Means and DBSCAN, as well as utility functions to analyze the resulting clusters.
  11. Select entities using the ezdxf.select module

    master

    The ezdxf.select module allows you to select DXF entities based on geometric shapes like windows, circles, polygons, and fences.

    Selection functions (like bbox_inside) operate on the bounding box of entities rather than their precise geometry. This means an entity is selected if its bounding box meets the criteria relative to the selection shape.

    Key behaviors:

    • bbox_inside: Selects entities whose bounding boxes are entirely within the shape.
    • bbox_outside: Selects entities whose bounding boxes are entirely outside the shape.
    • bbox_overlap: Similar to 'crossing selection' in CAD; selects entities if their bounding box overlaps the selection shape.

    All selection functions accept an iterable of entities (such as a layout like msp) and return an ezdxf.query.EntityQuery object, which can be used for further filtering by entity type or attributes.

    import ezdxf
    from ezdxf import select
    
    doc = ezdxf.readfile("your.dxf")
    msp = doc.modelspace()
    
    # Define a window for selection
    window = select.Window((0, 0), (10, 10))
    
    # Select entities inside the window from modelspace
    selected_entities = select.bbox_inside(window, msp)
    
    # Iterate over selected entities
    for entity in selected_entities:
        print(entity)
  12. Create banded lines using ezdxf.render.trace

    master
    The ezdxf.render.trace module provides tools to generate banded lines (similar to LWPOLYLINE with width information) by rendering paths as quadrilaterals. This is useful for creating entities like ezdxf.entities.Trace, ezdxf.entities.Solid, or ezdxf.entities.Face3d which represent paths with a specific thickness or width.