import { ACI } from './aci'; import { FOREGROUND } from './dxf-geometry'; import { decodeDxf, readDxf } from './dxf-read'; import { dwgToDxf, isDwg } from './dwg'; import { Scene, buildScene } from './scene'; /** * A DWG or DXF file opened all the way to what the page draws: converted (a * DWG), read, and built into the model and its layouts. The same steps run in * the worker (dxf.worker.ts), on the page when a worker cannot start, and for * each file of a batch. */ export interface LayerInfo { /** RGB, or FOREGROUND for ACI 7. */ rgb: number; /** Shown when the file opens: neither switched off nor frozen. */ visible: boolean; } export interface Opened { scene: Scene; /** "AutoCAD 2018 DXF" and so on. */ version: string; /** $INSUNITS. */ units: number; lwDisplay: boolean; layers: Map; /** For a DWG: the DXF it was converted to, which can be saved. */ dxf: Uint8Array | null; } /** A failure to show the reader as it is: the message is written for them. */ export class DrawingError extends Error { /** The converter itself would not load or start, rather than the file being at fault. */ constructor(message: string, readonly converter = false) { super(message); } } export type Progress = (stage: string) => void; export async function openDrawing(bytes: Uint8Array, name: string, progress: Progress = () => {}): Promise { const head = new TextDecoder('latin1').decode(bytes.subarray(0, 22)); const dwg = isDwg(bytes); let dwgLayers: Map | null = null; let dwgViewports: Map | null = null; if (dwg) { progress('convert'); try { const converted = await dwgToDxf(bytes); bytes = converted.dxf; dwgLayers = converted.layers; dwgViewports = converted.viewports; } catch (err) { if ((err as Error)?.name === 'ConverterLoad') throw new DrawingError('The DWG converter could not be downloaded or started. Check the connection and try again; once it has loaded, it works offline while the page stays open.', true); throw new DrawingError(`${name} could not be converted. It may be from a DWG version the converter does not handle, or damaged. If it opens in a CAD program, saving it as DXF from there will work here.`); } } else if (head.startsWith('AutoCAD Binary DXF')) { throw new DrawingError(`${name} is a binary DXF, a rarely used variant this viewer does not read. Saving it again as an ASCII DXF will open here.`); } progress('read'); let drawing; try { drawing = readDxf(decodeDxf(bytes)); } catch (err) { if ((err as Error)?.message === 'Not a DXF file') throw new DrawingError(`${name} is not a DWG or DXF file this viewer can read.`); throw err; } if (dwgLayers?.size) { for (const [layerName, layer] of drawing.layers) { const real = dwgLayers.get(layerName); if (real) { layer.off = real.off; layer.frozen = real.frozen; } else { layer.off = false; } } } if (dwgViewports?.size) { const vps = [...drawing.entities, ...[...drawing.blocks.values()].flatMap((b) => b.entities)].filter((e) => e.type === 'VIEWPORT'); for (const v of vps) { const h = v.pairs.find(([c]) => c === 5)?.[1].toUpperCase(); const state = h ? dwgViewports.get(h) : undefined; if (state && !v.pairs.some(([c]) => c === 331)) for (const f of state.frozen) v.pairs.push([331, f]); } } progress('build'); const scene = buildScene(drawing); const model = scene.sheets[0].space; if (!model.shapes.length && scene.sheets.length === 1) { const solids = (model.skipped.get('3DSOLID') ?? 0) + (model.skipped.get('BODY') ?? 0) + (model.skipped.get('REGION') ?? 0); throw new DrawingError(solids ? `${name} is a 3D model made of solids, which this 2D viewer cannot draw.` : `${name} has nothing in it to draw.`); } const layers = new Map(); const names = new Set(); for (const s of scene.sheets) for (const n of s.space.layerCounts.keys()) names.add(n); for (const n of names) { const l = drawing.layers.get(n); const aci = l?.color ?? 7; layers.set(n, { rgb: l?.trueColor ?? (aci === 7 ? FOREGROUND : ACI[aci] ?? FOREGROUND), visible: !(l?.off || l?.frozen) }); } return { scene, version: acadVersion(drawing.version), units: drawing.units, lwDisplay: drawing.lwDisplay, layers, dxf: dwg ? bytes : null }; } function acadVersion(v: string): string { const map: Record = { AC1009: 'R12', AC1012: 'R13', AC1014: 'R14', AC1015: '2000', AC1018: '2004', AC1021: '2007', AC1024: '2010', AC1027: '2013', AC1032: '2018', }; return map[v] ? `AutoCAD ${map[v]} DXF` : v ? `DXF (${v})` : 'DXF'; } export { mmPerUnit } from './r12';