@cadview/dwg

中文文档
0.2.0发布于 2026/02/19GPL-3.0-onlyTypeScriptESM + CJS零依赖近 30 天下载 3164

DWG file format support for @cadview/core via LibreDWG WASM

原始使用文档
文档目录16 节
  1. License
  2. Installation
  3. Quick Start
  4. Standalone Usage
  5. WASM Preloading
  6. API
  7. dwgConverter: FormatConverter
  8. isDwg(buffer: ArrayBuffer): boolean
  9. getDwgVersion(buffer: ArrayBuffer): string | null
  10. getDwgReleaseName(version: string): string | null
  11. convertDwgToDxf(buffer: ArrayBuffer, options?: ConvertOptio…
  12. initWasm(options?: WasmOptions): Promise<void>
  13. isWasmReady(): boolean
  14. DWG_VERSIONS: Record<string, string>
  15. Building the WASM Module
  16. License

@cadview/dwg

DWG file format support for @cadview/core via LibreDWG compiled to WebAssembly.

Converts DWG files to DXF entirely in the browser — no server required.

License

GPL-3.0-only — This package uses LibreDWG which is GPL-licensed. Using @cadview/dwg in your application makes the combined work subject to GPL-3.0 terms. If you cannot comply with the GPL, do not use this package.

@cadview/core and the framework wrappers (@cadview/react, @cadview/svelte, @cadview/vue) remain MIT-licensed and have zero dependency on this package.

Installation

npm install @cadview/dwg @cadview/core

Quick Start

Register the dwgConverter with CadViewer to automatically handle DWG files alongside DXF:

import { CadViewer } from '@cadview/core';
import { dwgConverter } from '@cadview/dwg';

const viewer = new CadViewer(canvas, {
  formatConverters: [dwgConverter],
});

// loadFile() and loadBuffer() now handle both DXF and DWG
await viewer.loadFile(file);

With framework wrappers:

// React
import { CadViewer } from '@cadview/react';
import { dwgConverter } from '@cadview/dwg';

<CadViewer file={file} formatConverters={[dwgConverter]} />
<!-- Svelte -->
<script>
  import { CadViewer } from '@cadview/svelte';
  import { dwgConverter } from '@cadview/dwg';
</script>

<CadViewer {file} formatConverters={[dwgConverter]} />
<!-- Vue -->
<script setup>
import { CadViewer } from '@cadview/vue';
import { dwgConverter } from '@cadview/dwg';
</script>

<CadViewer :file="file" :format-converters="[dwgConverter]" />

Standalone Usage

You can use the conversion functions directly without CadViewer:

import { convertDwgToDxf, isDwg, getDwgVersion } from '@cadview/dwg';
import { parseDxf } from '@cadview/core';

// Check if a buffer is a DWG file
const buffer = await file.arrayBuffer();
if (isDwg(buffer)) {
  const version = getDwgVersion(buffer); // e.g. "AC1032"
  const dxfString = await convertDwgToDxf(buffer);
  const doc = parseDxf(dxfString);
}

WASM Preloading

The LibreDWG WASM module (~2-5 MB) is lazy-loaded on the first DWG conversion. To avoid the delay when the user first opens a DWG file, preload it on app startup:

import { initWasm } from '@cadview/dwg';

// Preload on app startup
initWasm();

// Or from a CDN
initWasm({ wasmUrl: 'https://cdn.example.com/libredwg.wasm' });

API

dwgConverter: FormatConverter

Pre-configured format converter for use with CadViewer's formatConverters option. Detects DWG files by magic bytes and converts via LibreDWG WASM.

isDwg(buffer: ArrayBuffer): boolean

Detect DWG format by checking magic bytes (AC10 prefix + two ASCII digits). Returns false on any error.

getDwgVersion(buffer: ArrayBuffer): string | null

Extract the 6-character DWG version string (e.g. "AC1032"). Returns null for non-DWG buffers.

getDwgReleaseName(version: string): string | null

Get the human-readable AutoCAD release name for a version string (e.g. "AC1032" → "R2018").

convertDwgToDxf(buffer: ArrayBuffer, options?: ConvertOptions): Promise<string>

Convert a DWG buffer to a DXF string. Lazily initializes the WASM module on first call.

Options:

OptionTypeDefaultDescription
timeoutnumber30000Conversion timeout in ms (0 = no timeout)
wasmUrlstring—Custom URL for the .wasm file (e.g. CDN)

initWasm(options?: WasmOptions): Promise<void>

Pre-initialize the WASM module. Safe to call multiple times. Automatically retries on failure.

isWasmReady(): boolean

Check if the WASM module is loaded and ready.

DWG_VERSIONS: Record<string, string>

Map of known DWG version strings to AutoCAD release names:

VersionRelease
AC1009R11/R12
AC1012R13
AC1014R14
AC1015R2000
AC1018R2004
AC1021R2007
AC1024R2010
AC1027R2013
AC1032R2018

Minimum supported version: AC1012 (R13).

Building the WASM Module

The WASM module must be compiled from LibreDWG using Emscripten before DWG conversion will work at runtime.

Prerequisites:

# Add LibreDWG as a git submodule (one-time)
git submodule add https://github.com/LibreDWG/libredwg.git packages/dwg/wasm/libredwg/

# Build the WASM module
pnpm --filter @cadview/dwg build:wasm

This generates dist/libredwg.js and dist/libredwg.wasm.

License

GPL-3.0-only. See LICENSE.

版本与发布记录

@cadview/dwg 共发布 1 个版本,首次发布于 2026/02/19;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.2.0latest2026/02/198 个月前

关于 @cadview/dwg 的常见问题

@cadview/dwg 是什么?

DWG file format support for @cadview/core via LibreDWG WASM

如何安装 @cadview/dwg?

在项目目录运行 npm install @cadview/dwg,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@cadview/core(>=0.2.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@cadview/dwg 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cadview/dwg 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@cadview/dwg 可以商用吗?

@cadview/dwg 使用 GPL-3.0-only 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以 GPL-3.0 兼容许可开源,并附带专利与反锁定条款。仅内部使用不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@cadview/dwg 的下载量有多少?

根据 npm 官方下载统计,@cadview/dwg 在 2026/10/09 – 2026/11/07 这 30 天内被下载 3,164 次,日均约 105 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cadview/dwg 还在维护吗?

最新版本 0.2.0 发布于 2026/02/19(8 个月前);至今共发布 1 个版本,其中过去 12 个月发布了 1 个;首次发布于 2026/02/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。