--- name: dxf-sheet-from-code description: Generate a 2D CAD sheet (floor plan, layout, site plan, detail) as a DXF file from Python with ezdxf - layers with colours and lineweights, walls as merged outlines with solid hatch, door and window functions, real associative dimensions, room labels, area schedule and title block - that opens in AutoCAD, CADCraft, BricsCAD, LibreCAD or any DXF reader. Use when the user wants drawings generated from data or code, "plano desde código", repeatable sheets, a plan from a BIM model or brochure dimensions, or documentation as code. --- # DXF sheet from code The CAD program never runs the Python: the script writes a plain-text DXF (group code / value pairs) and any CAD reads it. That makes sheets repeatable, diffable in git and easy for an agent to update. ## Inputs - Geometry source: dimensions from the user, a brochure, a survey, or an IFC/BIM model (read it with IfcOpenShell). - Sheet standard: units (metres or millimetres), paper size and scale, layer naming convention (company or national; ask), text style, title block fields. ## Procedure 1. `pip install ezdxf shapely` (example tested with ezdxf 1.4.5 and shapely 2.x). 2. Start from `references/example_floor_plan.py` (a real A3 1:50 apartment sheet, 346 entities) and keep its structure: 1. Document: `ezdxf.new("R2010", setup=True)`, `doc.units`, text style. 2. Layers dict: name -> (ACI colour, lineweight in 1/100 mm). 3. Walls as rectangles -> `shapely.ops.unary_union` -> one LWPOLYLINE + one SOLID HATCH per outline. 4. `door()` and `window()` functions: write once, call per opening. 5. Furniture/fixtures, then annotation: `room()`, `tag()`, `level()`, `section()`. 6. Dimensions with `msp.add_linear_dim(...).render()` and a DIM override dict (real DIMENSION entities, not exploded lines). 7. Tables, notes, legend, frame and title block; `doc.saveas()`. 3. Put all project numbers in data tables at the top (walls, openings, rooms). Logic stays in small functions; an edit is a data change. 4. Validate: `auditor = doc.audit()`; report `auditor.has_errors`; then render a PNG to look at it (`python -m ezdxf draw file.dxf -o file.png`, needs matplotlib) or open it in a free viewer. Check the overall dimensions against the source before delivering. 5. Compute areas from the same polygons (`shapely` `.area`) instead of typing them, so the schedule cannot drift from the drawing. 6. Deliver the .py and the .dxf. If they need DWG, open the DXF in their CAD and Save As DWG (CADCraft and AutoCAD both do it). ## Output - The DXF, the script, a PNG preview, and a check line: entities, layers, dimensions, audit result, overall dimensions. ## Never - Never invent dimensions to "close" a plan; mark what is missing on the drawing and in the reply. - Never present a 2D sheet as BIM: it has lines and text, not objects with data. - Don't explode dimensions into lines: keep them as DIMENSION entities so they stay editable.