Skip to main content

DwgViewer

DwgViewer

AutoCAD DWG drawings converted to DXF in the browser with LibreDWG (WebAssembly), then shown in the CAD viewer

Tags: viewer cad dwg autocad libredwg gpl

DwgViewer converts AutoCAD DWG files to DXF in the browser with LibreDWG (opens in new tab), compiled to WebAssembly, and shows the result in CadViewer. Nothing is uploaded. Files from AutoCAD R14 to 2018 convert.

GPL-3.0. LibreDWG is GPL, so @classic-homes/viewer-dwg is too. It is a separate, opt-in package: the other viewer packages are MIT and never depend on it. An app that bundles it ships GPL code to its users' browsers and must comply with the GPL (for example, by offering the app's source). Check with whoever owns licensing for the app before adding it.

Import​

<script>
  import { DwgViewer } from '@classic-homes/viewer-dwg/svelte';
</script>

Setup​

npm install @classic-homes/viewer-dwg @classic-homes/viewer-svelte dxf-viewer

Configure it once at app start, next to configureCad:

import { configureDwg } from '@classic-homes/viewer-dwg';
import DwgWorker from '@classic-homes/viewer-dwg/worker?worker';
import wasmUrl from '@classic-homes/viewer-dwg/libredwg-web.wasm?url';

configureDwg({ worker: () => new DwgWorker(), wasmUrl });

The WebAssembly file is about 11 MB (2.5 MB compressed) and is downloaded the first time a DWG is opened. Set wasmUrl explicitly: Vite's dependency pre-bundling breaks the library's own lookup.

Basic Usage​

Register it with DocumentViewer:

const viewers: ViewerRegistry = {
  dxf: () => import('@classic-homes/viewer-svelte/cad'),
  dwg: () => import('@classic-homes/viewer-dwg/svelte'),
};

Without the dwg entry, DWG files get a message naming their AutoCAD release and suggesting saving as DXF. To convert without showing anything, call dwgToDxf(bytes) from @classic-homes/viewer-dwg.

Props​

Prop Type Default Description
source LoadedSource required Loaded DWG bytes plus name and MIME hints
kind RenderableKind required 'dwg'
background 'theme' | 'dark' | 'light' 'theme' Canvas background
showLayers boolean false Open the layer panel on load
fonts string[] - Font URLs, instead of the ones from configureCad
hideToolbar boolean false Hide the toolbar
onready () => void - The converted drawing is on screen
onerror (error: ViewerError) => void - Conversion or rendering failed
class string - Classes for the outer element

What it shows​

Everything CadViewer draws, from model space. A note under the toolbar names the AutoCAD release and lists what is not shown: tables, multileaders, multilines, 3D solids and faces, wipeouts, construction lines, images, OLE and proxy objects. Paper-space layouts are not shown. Download saves the original DWG.

Rational spline weights are preserved in the converted DXF but are not rendered by the current CAD engine. Hatch elevation is not exposed by the pinned LibreDWG converter, so elevated hatches are flattened.

Fit frames everything in model space, as AutoCAD's Zoom Extents does: a stray object far from the drawing makes the drawing small. Very large drawings can run out of WebAssembly memory and get a "too large" state with the download button.

On this page