How to Create 2D DXF Drawings from CAD Geometry in text-to-cad

The text-to-cad project provides a Python-to-DXF pipeline where you implement a gen_dxf() function that returns an ezdxf Document inside a dictionary envelope, then run the python -m dxf CLI to generate flat-pattern files viewable in the integrated CAD Viewer.

The earthtojake/text-to-cad repository offers a complete workflow for converting Python-defined CAD geometry into industry-standard 2D DXF flat patterns. This system uses the ezdxf library to construct geometry programmatically and bundles a specialized CLI tool to validate and export DXF files. By following the three-stage pipeline—definition, generation, and visualization—you can automate the creation of manufacturing-ready drawings from parametric code.

Define a DXF Generator Function

The foundation of the workflow is a Python generator function named gen_dxf() that constructs geometry using the ezdxf library. According to the envelope validation logic in viewer/packages/cadpy/src/cadpy/metadata.py, this function must return a DXF envelope containing exactly one required key: document, which maps to an ezdxf.Document object.

Create a new Python file and implement the function by instantiating a document, accessing the modelspace, adding geometric entities, and returning the envelope structure.


# square.py

import ezdxf

def gen_dxf():
    """Return a DXF envelope with a 100mm x 50mm rectangle."""
    doc = ezdxf.new("R2010")
    msp = doc.modelspace()
    
    # Add lightweight polyline defining the rectangle perimeter

    msp.add_lwpolyline(
        [(0, 0), (100, 0), (100, 50), (0, 50)], 
        close=True
    )
    
    return {"document": doc}

The document field is the only strictly required member of the returned dictionary. Additional metadata may be included but will be ignored by the validation logic in the cadpy package.

Generate DXF Files Using the CLI Tool

Once your generator function is defined, use the dxf command-line interface to discover, validate, and export the geometry. The CLI entry point resides in skills/dxf/scripts/dxf/cli.py, which delegates processing to cadpy.generation.generate_dxf_targets implemented in viewer/packages/cadpy/src/cadpy/generation.py.

The tool parses the supplied Python file, validates that the returned envelope contains the required document field, normalizes the payload, writes the DXF file to disk, and updates the asset catalog.

Generate a DXF next to the source file:

python -m dxf square.py

Specify an explicit output path:

python -m dxf square.py -o /output/path/square.dxf

The CLI outputs confirmation of the generated file path and handles validation errors by referencing the schema enforcement in viewer/packages/cadpy/src/cadpy/metadata.py.

Visualize 2D Flat Patterns in the CAD Viewer

The text-to-cad Viewer automatically detects generated .dxf companion files and renders them as interactive 2D flat patterns with optional 3D bend previews. The rendering pipeline uses viewer/src/client/components/DxfViewer.js, which parses DXF data via viewer/packages/cadjs/src/lib/dxf/parseDxf.js and constructs preview meshes using viewer/packages/cadjs/src/lib/dxf/buildPreviewMesh.js.

The user interface, defined in viewer/src/client/components/workbench/DxfFileSheet.js, exposes a dedicated "DXF" tab with controls for adjusting material thickness and bend direction. Load your model by pointing the Viewer to the directory containing both your Python source and the generated DXF:


http://localhost:3000/?dir=/path/to/models&file=square.dxf

The Viewer displays the flat pattern immediately and allows real-time tweaking of bend direction and sheet thickness parameters.

Summary

Frequently Asked Questions

What fields are required in the DXF envelope returned by gen_dxf()?

The envelope must contain a single required key named document that holds an ezdxf.Document instance. The validation logic in viewer/packages/cadpy/src/cadpy/metadata.py checks for this field specifically; additional keys may be included but are ignored by the core generation pipeline in cadpy.generation.generate_dxf_targets.

How do I specify the DXF version when creating the document?

Pass the version string to ezdxf.new() when constructing the document inside your gen_dxf() function. The text-to-cad examples typically use "R2010" for broad compatibility with manufacturing software, though ezdxf supports versions from R12 through R2018.

Can I generate multiple DXF files from a single Python file?

The CLI discovers the gen_dxf() function by name, so a single Python file should define one primary generator. To create multiple variants, implement conditional logic within that function to generate different geometries based on parameters, or maintain separate Python files for distinct parts and run the CLI against each file individually.

Where does the CLI save the generated DXF if I don't specify an output path?

When the -o flag is omitted, the CLI writes the DXF file adjacent to the source Python file using the same base filename with a .dxf extension. The cadpy.generation.generate_dxf_targets function resolves this default path before writing the file and updating the internal asset catalog.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →