DEV Community

Cover image for Generating Valid AutoCAD R12 (AC1009) .DXF Files from Scratch in Zero-Dependency TypeScript
Clara Sutton
Clara Sutton

Posted on Originally published at plancaliber.hashnode.dev

Generating Valid AutoCAD R12 (AC1009) .DXF Files from Scratch in Zero-Dependency TypeScript

When we built the browser-based CAD drafting engine behind PlanCaliber, we needed to export clean, layered architectural .DXF files from Cloudflare Workers without running AutoCAD, ODA converters, or native C++ binaries on the server.

If you have ever tried writing a .DXF serializer by hand in TypeScript, you quickly discover that DXF looks deceptively simple—just alternating integer group codes and string values—until AutoCAD refuses to open your file with a cryptic Invalid or incomplete DXF input -- drawing discarded error.

Here are the exact structural rules and subtle geometry pitfalls we solved in our production zero-dependency TypeScript serializer, along with a live downloadable R12 .DXF specimen you can inspect in AutoCAD, QCAD, LibreCAD, or SketchUp right now.


1. Why AutoCAD R12 (AC1009) Beats Newer DXF Versions for Programmatic Export

Modern DXF versions like AC1027 (R2013) or AC1032 (R2018) require a strict relational graph of hexadecimal entity handles (group code 5), owner soft-pointer IDs (330), CLASSES, OBJECTS, DICTIONARY, LAYOUT, and APPID tables. If a single handle or dictionary reference is missing or out of order, AutoCAD aborts the import entirely.

By contrast, AutoCAD R12 (AC1009) requires only four lightweight sections:

  1. HEADER — version, units, and drawing extents
  2. TABLES — line types (LTYPE) and CAD layers (LAYER)
  3. BLOCKS — empty section is valid when emitting flat primitives
  4. ENTITIES — LINE, ARC, CIRCLE, and TEXT geometry

Every major CAD tool—AutoCAD, AutoCAD LT, BricsCAD, QCAD, LibreCAD, Rhino, Chief Architect, and SketchUp Pro—parses AC1009 cleanly.


2. Setting Architectural Units and Extents in HEADER

At minimum, emit $ACADVER, $INSUNITS, $LUNITS, $LUPREC, $EXTMIN, and $EXTMAX in inches so imperial drawings open at true 1:1 real-world scale and frame cleanly when the user opens the file:

const lines: string[] = [];
const push = (code: number, val: string | number): void => {
  lines.push(String(code), String(val));
};

push(0, 'SECTION');
push(2, 'HEADER');
push(9, '$ACADVER');
push(1, 'AC1009');
push(9, '$INSUNITS');
push(70, 1); // 1 = Inches
push(9, '$LUNITS');
push(70, 4); // 4 = Architectural (feet & inches)
push(9, '$LUPREC');
push(70, 4);
push(9, '$EXTMIN');
push(10, fmt(minX - 60));
push(20, fmt(-60));
push(30, '0');
push(9, '$EXTMAX');
push(10, fmt(maxX + 60));
push(20, fmt(depthIn + 60));
push(30, fmt(ceilingHeightIn));
push(0, 'ENDSEC');
Enter fullscreen mode Exit fullscreen mode

Setting $INSUNITS = 1 (inches) and emitting every coordinate in real-world inches avoids the classic 12× scaling mismatch when a drafter imports your file into an architectural template.


3. Defining AIA Standard Layers and Line Types in TABLES

Contractors and drafters expect geometry separated onto clean AIA-style layers so they can toggle electrical overlays, furniture, or dimensions independently. Define both LTYPE (CONTINUOUS and DASHED) and LAYER tables with AutoCAD Color Index (ACI) integers in group code 62:

Layer Purpose ACI Color
A-WALL Double-line trimmed wall faces 7 (White/Black)
A-DOOR Door panels, jambs, swing arcs 30 (Orange)
A-GLAZ Windows & glazing mullions 4 (Cyan)
A-CABN Kitchen cabinets, islands, appliances 8 (Dark Gray)
A-FURN Beds, sofas, fixtures 9 (Light Gray)
E-POWR NEC outlets, GFCI, switches, lights 2 (Yellow)
A-ROOM Room labels, dimensions & GLA 3 (Green)
A-DIMS Exterior dimension strings & ticks 6 (Magenta)
const layerDefs = [
  { name: 'A-WALL', color: 7 },
  { name: 'A-DOOR', color: 30 },
  { name: 'A-GLAZ', color: 4 },
  { name: 'A-CABN', color: 8 },
  { name: 'A-FURN', color: 9 },
  { name: 'E-POWR', color: 2 },
  { name: 'A-ROOM', color: 3 },
  { name: 'A-DIMS', color: 6 }
];

push(0, 'TABLE');
push(2, 'LAYER');
push(70, layerDefs.length);
for (const lyr of layerDefs) {
  push(0, 'LAYER');
  push(2, lyr.name);
  push(70, 0);
  push(62, lyr.color);
  push(6, 'CONTINUOUS');
}
push(0, 'ENDTAB');
Enter fullscreen mode Exit fullscreen mode

4. Pitfall #1: Screen +Y Down vs. CAD North-Up +Y and Backwards Door-Swing Arcs

HTML Canvas and SVG place +Y pointing down, while CAD space puts +Y pointing up (North). Mapping every Y coordinate through flipY = (y) => maxY - y handles lines and circles easily—but it breaks door-swing arcs if you aren't careful.

In DXF, an ARC entity always sweeps counter-clockwise from group code 50 (startAngle in degrees) to group code 51 (endAngle in degrees). Because flipping Y mirrors orientation, naively passing your screen angles produces a 270° backwards arc on half your doors.

The robust fix is to compute the two hinge-to-leaf and hinge-to-jamb angles directly in flipped CAD space, normalize both to [0, 360), and pick the ordering whose counter-clockwise span is <= 180°:

const addArcFromVectors = (
  layer: string,
  hx: number,
  hy: number,
  radius: number,
  v1: Point2D,
  v2: Point2D
): void => {
  const a1 = (Math.atan2(flipY(hy + v1.y) - flipY(hy), v1.x) * 180) / Math.PI;
  const a2 = (Math.atan2(flipY(hy + v2.y) - flipY(hy), v2.x) * 180) / Math.PI;
  const normDeg = (d: number): number => ((d % 360) + 360) % 360;
  const n1 = normDeg(a1);
  const n2 = normDeg(a2);
  const ccwSpan = normDeg(n2 - n1);
  const startDeg = ccwSpan <= 180 ? n1 : n2;
  const endDeg = ccwSpan <= 180 ? n2 : n1;

  push(0, 'ARC');
  push(8, layer);
  push(10, fmt(hx));
  push(20, fmt(flipY(hy)));
  push(30, '0');
  push(40, fmt(radius));
  push(50, fmt(startDeg));
  push(51, fmt(endDeg));
};
Enter fullscreen mode Exit fullscreen mode

That eliminates flipped 270° arcs across every wall orientation, mirror flip, and swing direction.


5. Pitfall #2: Centered TEXT Entities Jumping to (0, 0)

This catches almost everyone implementing DXF text for the first time. In AutoCAD R12, as soon as you set horizontal alignment 72 or vertical alignment 73 to anything non-zero (for example 72 = 1 centered, 73 = 2 middle), AutoCAD ignores the primary insertion point (10, 20, 30) and reads the second alignment point (11, 21, 31) instead.

If you omit codes 11, 21, and 31, every room label collapses into a pile at the origin (0, 0):

push(0, 'TEXT');
push(8, layer);
push(10, fmt(x));
push(20, fmt(flipY(y)));
push(30, '0');
push(40, fmt(heightIn));
push(1, sanitizeDxfText(text));
if (centered) {
  push(72, 1); // Horizontal center
  push(73, 2); // Vertical middle
  push(11, fmt(x));
  push(21, fmt(flipY(y)));
  push(31, '0');
}
Enter fullscreen mode Exit fullscreen mode

Also remember to strip raw \r and \n characters from any TEXT string—a stray newline breaks the strict two-line (code, value) DXF parser alignment for the rest of the file.


6. Trim Wall Faces Around Openings Instead of Exporting Centerlines

Many web floor-plan tools export single centerlines on one layer. For real remodel drafting, emit double-line wall faces offset by ±thickness / 2 along the wall normal and split each wall into pier segments trimmed cleanly around every door and window jamb—so doors and glazing sit cleanly inside wall gaps when a contractor opens the .DXF.


Inspect Real Output or Test the Browser Studio

You can inspect live files generated by this exact TypeScript engine with no login required:

Note: PlanCaliber produces preliminary space-planning and drafting documents (not licensed architectural or engineering services). Always verify dimensions and local building codes on site.

Top comments (0)