Importing an AutoCAD .DXF floor plan into a web app sounds easy until you open a real architectural drawing from a surveyor, contractor, or builder.
In a real CAD drawing, a 6-inch exterior wall is almost never a single line. It is drawn as two parallel lines separated by 0.1524 meters (or 6 inches, or 150 mm), scattered across layers like A-WALL-EXTR, A-WALL-INTR, A-FURN, A-ANNO-DIMS, and DEFPOINTS, often with a missing or wrong $INSUNITS header and micro-gaps at wall corners.
If you naively extrude every DXF LINE in Three.js, you get hollow paper-thin double sheets, flickering Z-fighting corners, zero closed room polygons, and broken square-footage takeoffs.
When we built the client-side .DXF importer for TrueDraft CAD (open-source, GNU AGPL-3.0), we wrote a pure-TypeScript pipeline in src/lib/cad/dxfImport.ts with zero native binaries and zero server uploads. Here is how each stage turns messy CAD vectors into an editable 2D/3D architectural floor plan.
1. Parsing ASCII DXF Group-Code Pairs (HEADER + ENTITIES)
ASCII DXF (R12 through R2018) is a flat sequence of (groupCode, value) line pairs. Before allocating geometry, we enforce hard browser-safety bounds (MAX_DXF_TEXT_CHARS = 12 * 1024 * 1024, MAX_SOLVER_SEGMENTS = 4000, MAX_WALLS = 1200) and reject binary DXF headers with a clear export hint.
In a single linear scan over the pairs, we read:
-
HEADER:$INSUNITS(1 = inches,2 = feet,4 = mm,5 = cm,6 = meters) and$MEASUREMENT(0 = Imperial,1 = Metric). -
ENTITIES:LINE,LWPOLYLINE(modern lightweight polylines), legacyPOLYLINE->VERTEX->SEQENDchains, plusTEXTandMTEXTentities for automatic room label extraction.
const UNIT_TO_METERS: Record<DetectedDxfUnit, number> = { inches: 0.0254, feet: 0.3048, mm: 0.001, cm: 0.01, meters: 1 }; const INSUNITS_MAP: Record<number, DetectedDxfUnit> = { 1: 'inches', 2: 'feet', 4: 'mm', 5: 'cm', 6: 'meters' }; Because many exported .DXF files leave $INSUNITS = 0 (unitless), we also run a bounding-box scale heuristic against realistic building spans (3 m to 60 m) and let the user override units live (auto | inches | feet | mm | cm | meters) right inside the import dialog without re-uploading the file.
2. Filtering AIA CAD Layers Without Losing Custom Drawings
Architectural DXF files follow AIA or ISO layer naming conventions (A-WALL, A-WALL-EXTR, A-WALL-INTR, A-FURN, A-ANNO-DIMS, DEFPOINTS). Non-structural clutter like furniture blocks, title blocks, dimension ticks, and hatch patterns will ruin wall detection if kept in the solver.
const NON_WALL_LAYER_PREFIXES = [ 'A-ANNO', 'DIM', 'DEFPOINTS', 'A-FURN', 'FURN', 'A-DOOR', 'A-GLAZ', 'A-ELEC', 'A-PLMB', 'A-AREA', 'TEXT', 'TITLE', 'BORDER', 'HATCH' ]; function isExplicitWallLayer(layer: string): boolean { const u = (layer || '0').trim().toUpperCase(); return u.startsWith('A-WALL') || u.startsWith('WALL') || u.startsWith('ARCH-WALL'); } Our importer returns a layerSummaries array ({ name, segmentCount, isWallLayer, selected }) so users can toggle individual layers on or off interactively in the browser DXF editor when a contractor puts structural walls on a custom layer name.
3. Collapsing Double-Line CAD Walls into Parametric Centerlines
This is the hardest geometric step in converting drafting vectors into parametric BIM/CAD walls. Architectural walls appear in two common forms:
- Closed thin wall-shell polylines (
tryCollapseThinWallShell): 4-vertex rectangles between0.06 mand0.36 mthick (2.4"–14"). We collapse each thin shell directly to its midline segment and preserve its measured width as wall thickness. - Parallel opposing
LINEsegments (collapseParallelSegments): Two lines within3°of parallel, separated perpendicularly by0.06 m–0.36 m, with at least60%projected overlap along their shared direction vector.
const sin3Deg = Math.sin((3 * Math.PI) / 180); // Reject segments that are not parallel within 3 degrees if (angleSinDiff(a.x2 - a.x1, a.y2 - a.y1, b.x2 - b.x1, b.y2 - b.y1) > sin3Deg) { continue; } // Perpendicular separation must match realistic architectural wall thickness const perp = Math.abs((bMidX - a.x1) * nx + (bMidY - a.y1) * ny); if (perp < MIN_SHELL_WIDTH_M || perp > MAX_SHELL_WIDTH_M) continue; // Projected overlap along segment A's unit vector const overlap = Math.min(lenA, bMax) - Math.max(0, bMin); const shorter = Math.min(lenA, lenB); if (overlap < shorter * 0.6 || overlap < 0.15) continue; Pairs are greedily matched by longest overlap first, then closest realistic wall thickness. Mitered corner differences up to 0.45 m are averaged so T-junctions and L-corners line up cleanly, while longer overhanging segments are pushed into a leftovers queue so an exterior wall running past an interior partition never gets truncated.
4. Corner Snapping, Collinear Merging, and Room Polygon Recovery
Raw DXF lines rarely meet at exact floating-point coordinates—especially after collapsing double-line faces to their centerlines. Three deterministic cleanup passes make the wall graph watertight:
-
snapWallEndpoints(walls, 0.06): Clusters endpoints within6 cmand snaps T-junctions onto crossing wall spans. -
mergeCollinearWalls(walls): Fuses segmented wall runs (for example, walls split around old door openings or grid intersections) back into clean continuous parametric walls. -
deduplicateWalls(walls): Removes overlapping duplicate segments left over from CAD block overlays.
Once cleaned, the exact same FloorPlan object feeds our planar half-edge room detector (computePlan()), immediately unlocking:
- Closed room polygons and square footage (matched against cleaned DXF
TEXT/MTEXTroom labels likeKITCHEN,PRIMARY BEDROOM,GARAGE), - Live 3D cutaway walkthrough in Three.js (Blueprint to 3D workflow),
- Live material takeoffs (4'×8' drywall sheets, paint gallons, baseboard linear feet, flooring square footage + 10% waste),
- Round-trip clean exports back to AIA-layered AutoCAD R12
.DXF, true-to-scale1/4" = 1'vector Blueprint PDF sheets (A1.0+A2.0), 3D.OBJ, and watertight 3D-printable.STLscale models.
Try It on Your Own .DXF Floor Plan
You can drag and drop any ASCII .DXF floor plan directly into TrueDraft CAD Studio right in your browser—no desktop install, no account wall, and no monthly CAD subscription. Drafting, .DXF import, 3D walkthrough, and watermarked proof PDFs are free; clean vector/CAD/STL export packs are one-time licenses ($9 Single Project or $29 Pro Lifetime, 14-day refund guarantee).
Full parser source code (GNU AGPL-3.0): github.com/truedraftcad/truedraft-cad.

