The data-model package provides the core classes for interacting with AutoCAD's database and entities. This package mimics AutoCAD ObjectARX's AcDb (Database) classes and implements the drawing database structure that AutoCAD developers are familiar with.
This package contains the core classes for defining and manipulating AutoCAD entities (e.g., lines, circles, blocks), handling entity attributes and geometric data, and storing and retrieving data from the drawing database. It uses the same drawing database structure as AutoCAD ObjectARX, making it easier for AutoCAD developers to build applications based on this SDK.
AcDbNativeDxfConverter, registered by default); extensible converter registration for DWG and alternate DXF readersnpm install @mlightcad/data-model
DXF import is built in via AcDbNativeDxfConverter (registered by default). An optional GPL alternative is @mlightcad/dxf-json-converter. DWG import is provided by converter packages such as @mlightcad/libredwg-converter; register a DWG converter with AcDbDatabaseConverterManager before calling AcDbDatabase.read() on DWG files.
import { AcDbDatabase } from '@mlightcad/data-model';
// Create a new database
const database = new AcDbDatabase();
// Get symbol tables
const layerTable = database.getLayerTable();
const blockTable = database.getBlockTable();
const linetypeTable = database.getLinetypeTable();
import { AcDbLine, AcDbCircle, AcGePoint3d } from '@mlightcad/data-model';
// Create a line entity
const startPoint = new AcGePoint3d(0, 0, 0);
const endPoint = new AcGePoint3d(10, 10, 0);
const line = new AcDbLine(startPoint, endPoint);
// Create a circle entity
const center = new AcGePoint3d(0, 0, 0);
const radius = 5;
const circle = new AcDbCircle(center, radius);
// Set entity properties
line.setColor(1); // Red
line.setLayer('0');
circle.setLinetype('CONTINUOUS');
import { AcDbLayerTableRecord } from '@mlightcad/data-model';
// Create a new layer
const layerRecord = new AcDbLayerTableRecord();
layerRecord.setName('MyLayer');
layerRecord.setColor(2); // Yellow
layerRecord.setLinetype('DASHED');
// Add layer to database
const layerTable = database.getLayerTable();
layerTable.add(layerRecord);
import { AcDbBlockReference, AcGePoint3d } from '@mlightcad/data-model';
// Create a block reference
const insertionPoint = new AcGePoint3d(0, 0, 0);
const blockRef = new AcDbBlockReference(insertionPoint, 'MyBlock');
// Set block properties
blockRef.setScale(2.0);
blockRef.setRotation(Math.PI / 4);
// Add to model space
const modelSpace = database.getModelSpace();
modelSpace.appendEntity(blockRef);
DXF support is registered by default via AcDbNativeDxfConverter (MIT, main-thread
streaming). You only need to register a converter when you want a different DXF
implementation or when reading DWG files.
import {
AcDbDatabase,
AcDbDatabaseConverterManager,
AcDbFileType,
acdbHostApplicationServices,
AcDbOpenDatabaseOptions
} from '@mlightcad/data-model'
import { AcDbLibreDwgConverter } from '@mlightcad/libredwg-converter'
// Optional: replace the default native DXF converter (e.g. GPL worker-based parser)
// import { AcDbDxfConverter } from '@mlightcad/dxf-json-converter'
// AcDbDatabaseConverterManager.instance.register(
// AcDbFileType.DXF,
// new AcDbDxfConverter({
// useWorker: true,
// parserWorkerUrl: './assets/dxf-parser-worker.js'
// })
// )
// DWG still requires an explicit converter registration
AcDbDatabaseConverterManager.instance.register(
AcDbFileType.DWG,
new AcDbLibreDwgConverter({
useWorker: true,
parserWorkerUrl: './assets/libredwg-parser-worker.js'
})
)
// Read a file into the database
const database = new AcDbDatabase()
acdbHostApplicationServices().workingDatabase = database
const buffer = await file.arrayBuffer()
await database.read(buffer, { readOnly: true }, AcDbFileType.DXF)
Fonts referenced by text entities are loaded on demand by the mtext renderer when a
font is first needed. Viewers such as @mlightcad/cad-simple-viewer typically resolve
font metadata from mlightcad/cad-data
(default CDN: https://cdn.jsdelivr.net/gh/mlightcad/cad-data@main/).
To self-host fonts and templates (directory layout, fonts.json, CORS, and baseUrl
configuration), see the
Self Hosted Fonts and Templates
guide in the cad-viewer wiki.
import { AcDbAlignedDimension, AcGePoint3d } from '@mlightcad/data-model';
// Create an aligned dimension
const defPoint1 = new AcGePoint3d(0, 0, 0);
const defPoint2 = new AcGePoint3d(10, 0, 0);
const textPosition = new AcGePoint3d(5, 5, 0);
const dimension = new AcDbAlignedDimension(defPoint1, defPoint2, textPosition);
dimension.setDimensionText('10.0');
dimension.setDimensionStyle('Standard');
import { AcDbLayoutManager } from '@mlightcad/data-model';
// Get layout manager
const layoutManager = database.getLayoutManager();
// Get current layout
const currentLayout = layoutManager.getCurrentLayout();
// Switch to model space
layoutManager.setCurrentLayout('Model');
// Create a new layout
const newLayout = layoutManager.createLayout('MyLayout');
newLayout.setPlotType('Extents');
newLayout.setPlotCentered(true);
DXF reading works out of the box. For DWG, install a converter such as @mlightcad/libredwg-converter. An optional GPL DXF alternative is @mlightcad/dxf-json-converter.
For detailed API documentation, visit the RealDWG-Web documentation.
This package is part of the RealDWG-Web monorepo. Please refer to the main project README for contribution guidelines.