Layers and Properties
About 3 min
Layers and Properties
Layer management, color, linetype, extended data, and more.
Layer Management
Create Layers
const { Engine } = vjcad;
// Method 1: use Engine.createLayer (recommended)
const layer = Engine.createLayer("AnnotationLayer", {
color: 1, // Color index
lineType: "CONTINUOUS",
layerOn: true, // Whether visible
isFrozen: false, // Whether frozen
isLocked: false // Whether locked
});
// Method 2: use doc.layers.addlayer
const doc = Engine.currentDoc;
if (!doc.layers.itemByName("ConstructionLayer")) {
doc.layers.addlayer("ConstructionLayer", true, 3); // Name, visible, color
}
// Method 3: auto-named layer
const autoLayer = Engine.createLayer(); // Automatically named "Layer1", "Layer2", etc.Switch Layers
// Get current layer
const currentLayer = Engine.getCurrentLayer();
// Switch to specified layer
Engine.setCurrentLayer("LayerA");
// Access current layer name
const layerName = Engine.currentDoc.CLAYER;Get Layers
// Get all layers
const layers = Engine.getLayers();
layers.forEach(layer => {
console.log(`${layer.name}: color=${layer.color}, visible=${layer.layerOn}`);
});
// Get layer by name
const layer = Engine.getLayerByName("AnnotationLayer");
// Check whether layer exists
const exists = !!Engine.currentDoc.layers.itemByName("BuildingLayer");Layer Visibility
const layer = Engine.getLayerByName("LayerName");
// Turn layer off/on
layer.layerOn = false; // Off (invisible, but still participates in regeneration)
layer.layerOn = true; // On
Engine.regen(true); // Must re-renderLayer Freezing
const layer = Engine.getLayerByName("LayerName");
// Freeze/unfreeze layer
layer.isFrozen = true; // Frozen (invisible, does not participate in regeneration)
layer.isFrozen = false; // Unfrozen
Engine.regen(true);
// Note: layer 0 and the current layer cannot be frozenLayer Locking
const layer = Engine.getLayerByName("LayerName");
// Lock/unlock layer
layer.isLocked = true; // Locked (visible but not selectable or editable)
layer.isLocked = false; // UnlockedLayer State Comparison
| State | Visible | Selectable | Editable | Participates in Regeneration |
|---|---|---|---|---|
| Normal | ✓ | ✓ | ✓ | ✓ |
Off (layerOn=false) | ✗ | ✗ | ✗ | ✓ |
Frozen (isFrozen=true) | ✗ | ✗ | ✗ | ✗ |
Locked (isLocked=true) | ✓ | ✗ | ✗ | ✓ |
Get Entities by Layer
// Get all entities on a specified layer
const buildingEntities = Engine.getEntities(ent => ent.layer === "BuildingLayer");
// Get entities on multiple layers
const multiLayerEntities = Engine.getEntities(ent =>
ent.layer === "BuildingLayer" || ent.layer === "AnnotationLayer"
);Color Settings
Color Index
const line = new LineEnt([0, 0], [100, 0]);
line.setDefaults(); // Must be called first
// ACI color index
line.color = 1; // Red
line.color = 2; // Yellow
line.color = 3; // Green
line.color = 4; // Cyan
line.color = 5; // Blue
line.color = 6; // Magenta
line.color = 7; // White
// Special values
line.color = 256; // ByLayer
line.color = 0; // ByBlock
Engine.addEntities(line);RGB True Color
const { ColorConverter } = vjcad;
// Method 1: use hexadecimal directly (with marker bit)
line.color = 0x1000000 + 0xFF8000; // Orange
// Method 2: use ColorConverter
line.color = ColorConverter.createRgbColorIndex(0x800080); // Purple
// Method 3: use RGB components
line.color = ColorConverter.createRgbColorIndexFromComponents(255, 192, 203); // Pink
// Method 4: use hex string
line.color = ColorConverter.createRgbColorIndexFromHexStr("#FFD700"); // GoldCurrent System Color
// Set current drawing color
Engine.CECOLOR = 1; // Red
// Entity uses current color
const line = new LineEnt([0, 0], [100, 0]);
line.setDefaults();
line.color = Engine.CECOLOR;Linetype Settings
Common Linetypes
const line = new LineEnt([0, 0], [100, 0]);
line.setDefaults();
line.lineType = "CONTINUOUS"; // Continuous (default)
line.lineType = "HIDDEN"; // Dashed
line.lineType = "CENTER"; // Center line
line.lineType = "PHANTOM"; // Phantom line
line.lineType = "DASHED"; // Short dashed line
// Linetype scale
line.lineTypeScale = 1.0; // Default
line.lineTypeScale = 0.5; // Denser
line.lineTypeScale = 2.0; // SparserCurrent System Linetype
// Set current linetype
Engine.CELTYPE = "HIDDEN";
// Set current linetype scale
Engine.CELTSCALE = 1.5;Lineweight Settings
const line = new LineEnt([0, 0], [100, 0]);
line.setDefaults();
// Lineweight values (unit: 0.01mm)
line.lineWeight = 0; // Default
line.lineWeight = 15; // 0.15mm
line.lineWeight = 30; // 0.30mm
line.lineWeight = 50; // 0.50mm
line.lineWeight = 100; // 1.00mm
// Special values
line.lineWeight = -1; // ByLayer
line.lineWeight = -2; // ByBlockTransparency Settings
const circle = new CircleEnt([50, 50], 25);
circle.setDefaults();
// Set transparency (0-100, 0=opaque, 100=fully transparent)
circle.transpMgr.setTp100(50);
// ByLayer transparency
circle.transpMgr.setValue(-1);Default Properties
Set System Default Properties
// Set default values
Engine.CECOLOR = 1; // Current color
Engine.CELTYPE = "HIDDEN"; // Current linetype
Engine.CELTSCALE = 1.5; // Current linetype scaleApply Default Properties to Entity
// setDefaults() applies the following system defaults:
// - Layer (CLAYER)
// - Color (CECOLOR)
// - Linetype (CELTYPE)
// - Linetype scale (CELTSCALE)
const line = new LineEnt([0, 0], [100, 0]);
line.setDefaults(); // Apply system defaults
// Important: set custom properties after setDefaults()
line.color = 3; // Override default color
Engine.addEntities(line);Extended Data (XData)
Store custom data on entities.
Set Extended Data
const line = new LineEnt([0, 0], [100, 0]);
line.setDefaults();
// Set extended data (supports any JSON object)
line.setExtData("SENSOR_DATA", {
name: "Temperature Sensor",
value: 25.5,
unit: "℃",
isActive: true,
lastUpdate: "2026-01-29",
config: {
minValue: 0,
maxValue: 100
}
});
Engine.addEntities(line);Read Extended Data
// Check whether extended data exists
if (line.hasExtData()) {
// Get application name
const appName = line.extDataAppName; // "SENSOR_DATA"
// Get extended data object (JSON already parsed automatically)
const data = line.extData;
console.log(data.name); // "Temperature Sensor"
console.log(data.value); // 25.5
}Modify Extended Data
// Method 1: replace completely
line.setExtData("SENSOR_DATA", {
name: "Temperature Sensor",
value: 30.2,
unit: "℃"
});
// Method 2: modify based on existing data
const currentData = line.extData;
if (currentData) {
currentData.value = 30.2;
currentData.lastUpdate = "2026-01-30";
line.setExtData("SENSOR_DATA", currentData);
}Clear Extended Data
line.clearExtData();Batch Query Entities with Extended Data
// Query all entities with extended data
const allEntities = Engine.getEntities();
const entitiesWithXData = allEntities.filter(ent => ent.hasExtData?.());
// Group by AppName
const byAppName = {};
entitiesWithXData.forEach(ent => {
const appName = ent.extDataAppName;
if (!byAppName[appName]) byAppName[appName] = [];
byAppName[appName].push(ent);
});
// Query entities of a specific AppName
const sensorEntities = allEntities.filter(
ent => ent.hasExtData?.() && ent.extDataAppName === "SENSOR_DATA"
);Raw XData Format
// Get raw format (for debugging)
const rawXData = line.xdataRaw;
// Return format: { "1001": "AppName", "1000": "JSON string" }Note: Extended data can be exported to DWG and loaded again; the data will be preserved.
File Operations
Open Local File
// Execute command
await Engine.editor.executerWithOp('OPENFROMLOCAL');Save Local File
// Save to IndexedDB
await Engine.editor.executerWithOp('SAVELOCAL');
// Quick save and download .webcad file
await Engine.editor.executerWithOp('QSAVE');
// Save as
await Engine.editor.executerWithOp('SAVEAS');Open Server File
await Engine.editor.executerWithOp('OPENFROMSERVER');Save Server File
await Engine.editor.executerWithOp('SAVESERVER');Import/Export DWG
// Import DWG
await Engine.editor.executerWithOp('IMPORTDWG');
// Export DWG
await Engine.editor.executerWithOp('EXPORTDWG');