import { type DxfText, type Outline, dxfText, toDxf } from '../shared/svg-dxf'; import { BACKGROUND, Built, FOREGROUND, TextShape } from './dxf-geometry'; /** * A drawing's lines as plain outlines, for an AutoCAD R12 DXF (the oldest * version, which every laser, vinyl and CNC program reads) and for a plot * file (hpgl-viewer/plt-from.ts). LibreDWG writes its DXF in the DWG's own * version only, so the R12 copy is made here from the drawing as this site * draws it: every line, arc, circle, polyline, spline and solid fill as a * polyline (curves traced finely), blocks exploded, each on its own layer. * Text (TEXT, MTEXT, attributes and dimensions' text) goes in as R12 TEXT * entities, one per line, where the drawing shows it, at its height, turn * and colour, on its layer; MTEXT's formatting inside a line (fonts, * colours of single words) is not kept. Hatch patterns are left out. */ /** Millimetres in one drawing unit, by $INSUNITS; 0 when the drawing does not say. */ export function mmPerUnit(units: number): number { const mm: Record = { 1: 25.4, 2: 304.8, 3: 1609344, 4: 1, 5: 10, 6: 1000, 7: 1e6, 8: 2.54e-5, 9: 0.0254, 10: 914.4, 11: 1e-7, 12: 1e-6, 13: 1e-3, 14: 100, 15: 1e4, 16: 1e5, 17: 1e12, 18: 1.495978707e14, 19: 9.4607304725808e18, 20: 3.0856775814913673e19, 21: 304.8006096012192, 22: 25.400050800101603, 23: 914.4018288036576, 24: 1609347.2186944373, }; return mm[units] ?? 0; } /** R12 layer names: letters, digits, - and _ only, at most 31 characters. */ export function r12Layer(name: string): string { return name.replace(/[^A-Za-z0-9_-]/g, '_').slice(0, 31) || '0'; } export function hexColor(c: number): string { return c === FOREGROUND || c < 0 ? '#000000' : '#' + (c & 0xffffff).toString(16).padStart(6, '0'); } /** * The drawing's outlines, with their layers, and how many text items were * left out. Layers in `hidden` (switched off, frozen, or hidden in the * viewer's list; their names as in the drawing) are left out whole. */ export function builtOutlines(built: Built, hidden: ReadonlySet = new Set()): { outlines: Outline[]; text: number; texts: DxfText[] } { const outlines: Outline[] = []; const texts: DxfText[] = []; let text = 0; for (const s of built.shapes) { if (hidden.has(s.layer)) continue; if (s.kind === 'text' && s.text) { text++; texts.push(...textLines(s.text, hexColor(s.color), r12Layer(s.layer))); continue; } if (s.ghost || s.mask || s.segs || s.tint || s.color === BACKGROUND || !s.rings || (s.kind !== 'line' && s.kind !== 'fill')) continue; for (const ring of s.rings) { const points: [number, number][] = []; for (let i = 0; i + 1 < ring.length; i += 2) points.push([ring[i], ring[i + 1]]); if (points.length >= 2) outlines.push({ points, closed: s.kind === 'fill' || !!s.closed, color: hexColor(s.color), layer: r12Layer(s.layer) }); } } return { outlines, text, texts }; } /** * A text shape as R12 TEXT entities, one per line: each line a line-spacing * further down the text's own (turned) direction, as the viewer draws it. */ export function textLines(t: TextShape, color: string, layer: string): DxfText[] { const out: DxfText[] = []; const step = t.height * t.lineSpacing; const sin = Math.sin(t.rotation), cos = Math.cos(t.rotation); const valign = t.baseline === 'alphabetic' ? 'baseline' : t.baseline; const n = t.lines.length; // A block attached by its middle or bottom is placed as a whole, as the viewer draws it (render.ts): its first line moves up. const first = t.baseline === 'middle' ? -((n - 1) * step) / 2 : t.baseline === 'bottom' ? -(n - 1) * step : 0; t.lines.slice(0, 2000).forEach((line, i) => { // A blank line still takes its place. if (!line.trim()) return; const along = first + step * i; // Down the text's own direction: (0, -1) turned by the rotation. const dx = sin * along, dy = -cos * along; out.push({ text: line, x: t.x + dx, y: t.y + dy, height: t.height, rotation: (t.rotation * 180) / Math.PI, widthFactor: t.widthFactor, align: t.align, valign, mirrored: t.mirrored, color, layer }); }); return out; } /** * The R12 DXF in millimetres (a drawing without units is taken as * millimetres), or null when it has no lines. No $INSUNITS: R12 has no such * header variable, and this copy is for the readers that know only R12. */ export function r12Dxf(built: Built, units: number, hidden: ReadonlySet = new Set()): { text: string; textCount: number; textCut: number; unitless: boolean } | null { const { outlines, text, texts } = builtOutlines(built, hidden); if (!outlines.length && !texts.length) return null; const mm = mmPerUnit(units); return { text: toDxf(outlines, mm || 1, { insunits: false, texts }), textCount: text, textCut: texts.filter((x) => dxfText(x.text).cut).length, unitless: !mm }; }