---
name: dxf-drafting
description: Draft 2D technical drawings and cut files as real DXF with Python and ezdxf. Use when the user asks for a DXF, a DWG-ready drawing, a dimensioned engineering or shop drawing, orthographic views of a part, or a flat profile for a laser, plasma, waterjet or CNC router.
---

# DXF drafting with ezdxf

A DXF someone opens in AutoCAD has to be right in ways a screenshot never shows: the units, the scale, the layers and whether a dimension is a real dimension. Follow every rule below. Check them at the end with the script in step 8, not by looking.

## 1. Units and scale

- Model space is millimetres at 1:1. A 120 mm edge is 120 drawing units long. Never scale geometry to fit a sheet.
- Create the document with `ezdxf.new("R2000", setup=True)`, then set `doc.units = ezdxf.units.MM`, `doc.header["$INSUNITS"] = 4` and `doc.header["$MEASUREMENT"] = 1`.
- If the user works in inches, use `$INSUNITS = 1` and `$MEASUREMENT = 0`, and keep the geometry in inches.

## 2. Layers

Create these layers and put every entity on one of them. Nothing goes on layer `0`.

| Layer | Colour | Linetype | Holds |
| --- | --- | --- | --- |
| `OUTLINE` | 7 | `CONTINUOUS` | visible edges |
| `HIDDEN` | 8 | `DASHED` | hidden edges |
| `CENTER` | 1 | `CENTER` | centre lines and hole centre marks |
| `DIM` | 3 | `CONTINUOUS` | dimensions |
| `TEXT` | 7 | `CONTINUOUS` | notes and the title block |
| `BORDER` | 7 | `CONTINUOUS` | the sheet border |

For a laser, plasma or waterjet file, also make a `CUT` layer. Put only the closed cut profiles on it, and keep dimensions and text on their own layers so they can be switched off before cutting.

## 3. Geometry

- A part's outer profile is one closed `LWPOLYLINE` (`close=True`). Do not use loose `LINE`s that only look closed. Use bulge values for fillets and rounded corners.
- A round hole is a `CIRCLE` at its true diameter. A slot is one closed polyline with two arcs.
- Every hole gets two short centre lines on `CENTER` that reach about 3 mm past the edge.

## 4. Views

- Lay out the orthographic views by the projection the user asked for. ISO and Europe use first angle, North America uses third angle. If the user did not say, use first angle and write it in the title block.
- Views share their alignment. The side view's heights line up with the front view, so a horizontal line from a feature in one view hits the same feature in the other.
- Leave at least 25 mm between views for dimensions.

## 5. Dimensions

- Every size is a real `DIMENSION` entity: `msp.add_linear_dim(...)`, `add_aligned_dim`, `add_radius_dim` or `add_diameter_dim`, followed by `.render()`. Never draw a dimension as separate lines with a `TEXT` number.
- Let the dimension measure the geometry. Do not override its text with a number you typed, because then the label and the drawing can disagree.
- Dimension each feature once, in the view where it is clearest. Give the overall sizes, every hole diameter and every hole position.
- Use a `dimstyle` with a 3.5 mm text height, so the drawing reads when printed.
- Watch the default style. `ezdxf.new(setup=True)` makes `EZDXF` the default, and it has `dimlfac` 100 because it is meant for drawings in metres. On a millimetre drawing every label then reads 100 times too large: 8000 for an 80 mm edge. Create your own style with `dimlfac` 1, or pass `dimstyle=` on every dimension.

## 6. Sheet, title block and notes

- Draw a rectangular sheet border around everything on a `BORDER` layer, with a 10 mm margin to the nearest view or dimension.
- Put a title block in the bottom right corner on `TEXT`: the part name, the material if known, `UNITS: MM`, `SCALE 1:1`, the projection, and the date. Use plain `TEXT` or `MTEXT`, not an image.
- Add a notes block with at least: the general tolerance (`GENERAL TOLERANCES ISO 2768-m` unless the user gave one), `BREAK ALL SHARP EDGES`, and anything the user said about finish or treatment.
- A fabricator cannot ask you what you meant. If a size was assumed, say so in the notes.

## 7. Save

Save as R2000 (`AC1015`). It opens in every CAD, laser and CNC program in use. Name the file after the part.

## 8. Check before you say it is done

Run this, fix anything it reports, and run it again.

```python
import ezdxf
doc = ezdxf.readfile("part.dxf")
auditor = doc.audit()
msp = doc.modelspace()
assert doc.header["$INSUNITS"] in (1, 4), "units not set"
assert not [e for e in msp if e.dxf.layer == "0"], "entities on layer 0"
dims = msp.query("DIMENSION")
assert dims, "no real dimensions"
print("audit errors:", len(auditor.errors))
for d in dims:
    print(d.dxf.layer, round(d.get_measurement(), 2))
```

Then compare the printed measurements with every size in the request. Each requested size must appear as a dimension with exactly that value. If one is missing or wrong, the geometry is wrong, so fix the geometry rather than the label.

Finally, tell the user what you assumed (any size they did not give), the projection, and that the file is at 1:1 in millimetres.
