1. Products
  2.   CAD
  3.   Python
  4.   Ezdxf
 
  

Open Source Python Library for AutoCAD DXF Creation

Free Python CAD library for AutoCAD DXF files creation, Editing & Manipulation. It provides read, write support for ASCII DXF and Binary DXF Models.

Ezdxf is a very powerful open source Python AutoCAD DXF package that enables software developers to work with AutoCAD DXF files inside their own Python applications. The DXF (drawing interchange file) file format is very popular and is a used by AutoCAD to interchange data with other CAD applications. The library makes developers job easy by hiding the complex DXF details from the programmer but at the same time support most capabilities of the DXF format.

The Ezdxf library allows third-party applications to embed application-specific information. There are several important features related to handling DXF documents, such as create DXF files, modify DXF, write existing DXF documents, tested with CPython and pypy3, retains third-party DXF content, optional C-extensions for CPython and many more. There are some additional add-ons are also available which are not automatically installed during the basic setup and need to be installed separately.

The open source Ezdxf library supports various versions of DXF, such as R12, R2000, R2004, R2007, R2010, R2013 and R2018. It also provides read-only support for DXF versions R13/R14 and older DXF versions than R12. The Ezdxf library is available under the MIT-License.

Previous Next

Getting Started with Ezdxf

The easiest way to install Ezdxf package is by using pip. It includes the optional C-extensions from PyPI as binary wheels. Please use the following command for a smooth installation.

Install Ezdxf via pip

pip install ezdxf 

You can download the compiled shared library from Github repository.

Work with Polygon via Python 3D Library

A polygon is a two-dimensional shape with straight sides. Polygonal modeling is an approach for modeling objects by representing or approximating their surfaces using polygon meshes. The Open source Python Library Ezdxf enables software developers to create polygons for each stack point with just a couple of lines of Python code. It is very beneficial in 3d printers whose firmware does not correct for small hole sizes.

AutoCAD DXF File Creation via Python

The Open source Python Ezdxf has included complete supports for reading and writing AutoCAD DXF documents inside Python applications. The library creates the new DXF documents with very little amount of content which means only those resources which are completely necessary are included such as linetypes, text styles, and more. It is also possible to open an existing DXF file and modify its content with ease.

Create a New DXF Drawing via Python Library

import ezdxf
from ezdxf import colors
from ezdxf.enums import TextEntityAlignment

# Create a new DXF document.
doc = ezdxf.new(dxfversion="R2010")

# Create new table entries (layers, linetypes, text styles, ...).
doc.layers.add("TEXTLAYER", color=colors.RED)

# DXF entities (LINE, TEXT, ...) reside in a layout (modelspace, 
# paperspace layout or block definition).  
msp = doc.modelspace()

# Add entities to a layout by factory methods: layout.add_...() 
msp.add_line((0, 0), (10, 0), dxfattribs={"color": colors.YELLOW})
msp.add_text(
    "Test", 
    dxfattribs={
        "layer": "TEXTLAYER"
    }).set_placement((0, 0.2), align=TextEntityAlignment.CENTER)

# Save the DXF document.
doc.saveas("test.dxf")
 

Open & Read AutoCAD DXF Drawings via Python

The Open source Python Ezdxf library enables software developers to open, load and get data from an existing DXF document with just a couple of lines of Python code. Please remember that it works well for DXF drawings from trusted sources like AutoCAD or BricsCAD and will load DXF files with minor or major defects. You can easily access and iterate over all DXF entities and can easily access the attributes of an existing entities.

Loading DXF File via Python Library

import sys
import ezdxf

try:
    doc = ezdxf.readfile("your_dxf_file.dxf")
except IOError:
    print(f"Not a DXF file or a generic I/O error.")
    sys.exit(1)
except ezdxf.DXFStructureError:
    print(f"Invalid or corrupted DXF file.")
    sys.exit(2)

How to Handle Text in DXF Drawings via Python

The Ezdxf Python library has provided full support for handling text inside DXF documents. The library has included various features for text processing, such as adding simple one line text entity, basic text alignment like (top, middle, bottom, and baseline, let center, right), fit text, standard text styles and line-types, add new text style, use 3D text, use of stand fonts and so on.

How to Add Simple One Line Text via Python API

import ezdxf
from ezdxf.enums import TextEntityAlignment

# The TEXT entity is a DXF primitive and is supported in all DXF versions.
# The argument setup=True creates standard linetypes and text styles in the
# new DXF document.
doc = ezdxf.new("R12", setup=True)
msp = doc.modelspace()

# Use method set_placement() to define the TEXT alignment, because the
# relations between the DXF attributes 'halign', 'valign', 'insert' and
# 'align_point' are tricky.
msp.add_text("A Simple Text").set_placement(
    (2, 3),
    align=TextEntityAlignment.MIDDLE_RIGHT
)

# Using a predefined text style:
msp.add_text(
    "Text Style Example: Liberation Serif",
    height=0.35,
    dxfattribs={"style": "LiberationSerif"}
).set_placement((2, 6), align=TextEntityAlignment.LEFT)

doc.saveas("simple_text.dxf")