Document Model
About 4 min
Document Model
CadDocument represents a CAD document and is the container for all drawing data. It manages all document-level data including layers, blocks, styles, and more.
Overview
Document Basic Information
import { Engine, CadDocument } from 'vjcad';
// Get current document
const doc: CadDocument = Engine.currentDoc;
// Basic properties
console.log('Document name:', doc.name); // File name
console.log('Document path:', doc.path); // Full path
console.log('Document ID:', doc.docId); // Unique identifier
console.log('Modified:', doc.DBMOD === 1); // Modification status
// Quick access via Engine
console.log('Drawing name:', Engine.DWGNAME); // Same as doc.name
console.log('Drawing prefix:', Engine.DWGPREFIX); // Same as doc.path
console.log('Named drawing:', Engine.DWGTITLED); // path !== ""Document Properties
| Property | Type | Description |
|---|---|---|
name | string | Document name (file name) |
path | string | Document full path |
docId | number | Document unique identifier |
DBMOD | number | Modification status (0=unmodified, 1=modified) |
CLAYER | string | Current layer name |
LTSCALE | number | Global line type scale |
DIMSCALE | number | Dimension global scale |
TEXTSIZE | number | Default text height |
Document Environment Variables
import { Engine, CadDocument } from 'vjcad';
const doc = Engine.currentDoc;
// Layer related
doc.CLAYER = 'Dimension'; // Set current layer
console.log('Current layer:', doc.CLAYER);
// Can also use setCLAYER method
doc.setCLAYER('Layer1', true); // Second parameter indicates whether to update UI
// Line type scale
doc.LTSCALE = 100; // Global line type scale
console.log('Global line type scale:', doc.LTSCALE);
// Dimension scale
doc.DIMSCALE = 50; // Dimension global scale
console.log('Dimension scale:', doc.DIMSCALE);
// Text height
doc.TEXTSIZE = 2.5; // Default text heightLayer Collection (Layers)
The layer collection manages all layers in the document.
Accessing Layers
import { Engine, Layer } from 'vjcad';
const doc = Engine.currentDoc;
const layers = doc.layers;
// Get all layers
console.log('Layer count:', layers.items.length);
// Iterate layers
for (const layer of layers.items) {
console.log(`Layer: ${layer.name}, Color: ${layer.color}, On: ${layer.layerOn}`);
}
// Get layer by name
const layer0 = layers.itemByName('0');
console.log('Layer 0 color:', layer0?.color);
// Get layer by index
const firstLayer = layers.itemByIndex(0);Creating Layers
import { Engine } from 'vjcad';
const layers = Engine.currentDoc.layers;
// Method 1: Auto naming
const newLayer1 = layers.makeLayer();
console.log('New layer name:', newLayer1.name); // "Layer1", "Layer2", ...
// Method 2: Specify name
const newLayer2 = layers.addlayer('Dimension', true, 3); // Name, on, color
newLayer2.lineType = 'CONTINUOUS';
// Method 3: Check and create
if (!layers.has('MyLayer')) {
const myLayer = layers.addlayer('MyLayer', true, 1);
}Layer Properties
import { Engine, Layer } from 'vjcad';
const layer = Engine.currentDoc.layers.itemByName('Layer1');
if (layer) {
// Basic properties
layer.name = 'NewName'; // Layer name
layer.color = 1; // Layer color (1=red)
layer.lineType = 'HIDDEN'; // Layer line type
layer.lineWeight = 0.25; // Layer line weight (mm)
// State control
layer.layerOn = true; // Layer on/off (true=visible)
layer.frozen = false; // Frozen state (true=frozen)
layer.locked = false; // Locked state (true=locked)
layer.plottable = true; // Plottable (true=plottable)
// Transparency
layer.transparency = 0; // Transparency (0-100)
}Layer Operations
import { Engine } from 'vjcad';
const layers = Engine.currentDoc.layers;
// Delete layer
const layerToDelete = layers.itemByName('TempLayer');
if (layerToDelete && layerToDelete.name !== '0') {
layers.delete(layerToDelete);
}
// Check if layer exists
const exists = layers.has('Dimension');
// Get layer index
const index = layers.indexOf('Layer1');Block Collection (Blocks)
The block collection manages all block definitions, including model space and paper space.
Accessing Blocks
import { Engine, BlockDefinition } from 'vjcad';
const doc = Engine.currentDoc;
const blocks = doc.blocks;
// Get model space
const modelSpace = blocks.itemByName('*Model');
console.log('Model space entity count:', modelSpace?.items.length);
// Get paper space
const paperSpace = blocks.itemByName('*Paper');
// Get current space
const currentSpace: BlockDefinition = doc.currentSpace;
// Iterate all block definitions
for (const block of blocks.items) {
if (!block.name.startsWith('*')) { // Exclude system blocks
console.log(`Block: ${block.name}, Entity count: ${block.items.length}`);
}
}Creating Blocks
import { Engine, BlockDefinition, LineEnt, CircleEnt, Point2D } from 'vjcad';
const blocks = Engine.currentDoc.blocks;
// Create new block definition
const newBlock = new BlockDefinition();
newBlock.name = 'MyBlock';
newBlock.origin = new Point2D(0, 0); // Block base point
// Add entities to block
const line = new LineEnt(new Point2D(0, 0), new Point2D(100, 0));
const circle = new CircleEnt(new Point2D(50, 50), 20);
newBlock.add(line);
newBlock.add(circle);
// Add to block collection
blocks.add(newBlock);Block Properties
import { BlockDefinition, Point2D } from 'vjcad';
const block: BlockDefinition = /* ... */;
// Basic properties
block.name = 'BlockName'; // Block name
block.origin = new Point2D(0, 0); // Block base point
// Entity operations
const entities = block.items; // Get all entities
block.add(entity); // Add entity
block.erase([entity]); // Delete entity
// Space properties
const zoom = block.zoom; // Current zoom level
const center = block.viewCenter; // View centerCurrent Space
The current space is the space the user is editing (model space or paper space of a layout).
import { Engine, BlockDefinition, EntityBase } from 'vjcad';
// Get current space
const currentSpace: BlockDefinition = Engine.currentSpace;
// Same as Engine.currentDoc.currentSpace
// Access entities in space
const entities: EntityBase[] = currentSpace.items;
console.log('Current space entity count:', entities.length);
// Iterate entities
for (const entity of entities) {
console.log(`${entity.type}: ${entity.id}, Layer: ${entity.layer}`);
}
// Space view properties
console.log('Zoom level:', currentSpace.zoom);
console.log('View center:', currentSpace.viewCenter);Other Data Collections
Text Styles
import { Engine } from 'vjcad';
const textStyles = Engine.currentDoc.textStyles;
// Iterate text styles
for (const style of textStyles.items) {
console.log(`Style: ${style.name}, Font: ${style.fontName}`);
}
// Get style
const standard = textStyles.itemByName('Standard');Dimension Styles
import { Engine } from 'vjcad';
const dimStyles = Engine.currentDoc.dimensionStyles;
// Iterate dimension styles
for (const style of dimStyles.items) {
console.log(`Style: ${style.name}`);
}Image Collection
import { Engine } from 'vjcad';
const images = Engine.currentDoc.images;
// Iterate images
for (const image of images.items) {
console.log(`Image: ${image.name}, Path: ${image.path}`);
}Document Serialization
Export to JSON
import { Engine, CadDocument } from 'vjcad';
const doc = Engine.currentDoc;
// Export to database format object
const dbDoc = doc.toDb();
// Convert to JSON string
const jsonStr = JSON.stringify(dbDoc);
// Save to file (example)
// await saveToFile('drawing.json', jsonStr);Import from JSON
import { CadDocument } from 'vjcad';
// Restore from JSON
const jsonStr = /* Read file content */;
const dbData = JSON.parse(jsonStr);
const newDoc = new CadDocument();
await newDoc.fromDb(dbData);
// Set as current document
Engine.setActiveDocument(newDoc);Document Lifecycle
Document Operations
import { Engine, CadDocument } from 'vjcad';
// Create new document
const newDoc = new CadDocument();
newDoc.name = 'NewDrawing';
// Add to document collection
Engine.docs.items.push(newDoc);
// Set as active document
Engine.setActiveDocument(newDoc);
// Clean up resources when closing document
Engine.disposeDocument(newDoc);Multi-Document Management
import { Engine, CadDocument } from 'vjcad';
// Get all open documents
const docs = Engine.docs.items;
console.log('Open document count:', docs.length);
// Iterate documents
for (const doc of docs) {
console.log(`${doc.name} - ${doc.DBMOD === 1 ? 'Modified' : 'Unmodified'}`);
}
// Switch document
const targetDoc = docs.find(d => d.name === 'DrawingA');
if (targetDoc) {
Engine.setActiveDocument(targetDoc);
}Document Collection Summary
| Collection | Type | Description |
|---|---|---|
layers | Layers | Layer collection, manages all layers |
blocks | Blocks | Block definition collection, includes model space and paper space |
layouts | Layouts | Layout collection, manages print layouts |
images | Images | Image collection, external image references |
textStyles | TextStyles | Text style collection |
dimensionStyles | DimStyles | Dimension style collection |
lineTypes | LineTypes | Line type definition collection |
plotStyles | PlotStyles | Plot style collection |
Next Steps
- Coordinate System - Learn about coordinate transformation
- Undo/Redo - Learn about the undo mechanism
- Layer Management - Detailed layer operations
- Block Entities - Block definitions and insertion