MapCadLayer API
MapCadLayer API
MapCadLayer is the main facade for vjmapext. It implements vjmap’s IControl interface and mounts on the map via map.addControl().
Constructor
new MapCadLayer(options?: Partial<MapCadLayerOptions>)MapCadLayerOptions
| Property | Type | Default | Description |
|---|---|---|---|
locale | 'zh' | 'en' | 'zh' | UI language |
defaultColor | number | 0x00ff00 | Default color for new entities (24-bit RGB integer) |
nudgePixels | number | 1 | Arrow-key nudge distance in pixels |
nudgeRotateDeg | number | 1 | Nudge rotation in degrees |
nudgeScaleFactor | number | 1.02 | Nudge scale factor |
mode | 'edit' | 'browse' | 'edit' | Initial mode |
modeToggleShortcut | string | 'ctrl+shift+e' | Shortcut to toggle mode |
shortcuts | ShortcutMap | — | Custom shortcut map |
drawingDefaults | Partial<DrawingDefaultsData> | — | Drawing defaults overrides |
Example:
import { MapCadLayer } from 'vjmapext';
const cadLayer = new MapCadLayer({
locale: 'zh',
defaultColor: 0xff0000,
mode: 'edit',
});
map.addControl(cadLayer);Read-only members
Use these to reach subsystem instances.
| Property | Type | Description |
|---|---|---|
coordMapper | CoordMapper | Coordinate mapper (geo ↔ CAD) |
eventBus | EventBus | Global event bus |
store | EntityStore | Entity storage |
renderCache | RenderCache | Render cache |
renderPipeline | RenderPipeline | Render pipeline |
previewManager | PreviewManager | Preview manager |
inputManager | InputManager | Input manager |
commandRegistry | CommandRegistry | Command registry |
entityRegistry | EntityRegistry | Entity type registry |
gripManager | GripManager | Grip manager |
undoManager | UndoManager | Undo manager |
pluginManager | PluginManager | Plugin manager |
snapManager | SnapManager | Snap manager |
linetypeManager | LinetypeManager | Linetype manager |
drawingDefaults | DrawingDefaults | Drawing defaults |
animationManager | AnimationManager | Animation manager |
blocks | Blocks | Block definitions |
dimReactorManager | DimReactorManager | Dimension reactor manager |
Lifecycle
onAdd
onAdd(map: vjmap.Map): HTMLElementCalled when the control is added to the map: binds coordinates, creates layers, wires events. Usually invoked by map.addControl(), not manually.
| Parameter | Type | Description |
|---|---|---|
map | vjmap.Map | Map instance |
Returns: HTMLElement — control container.
onRemove
onRemove(): voidCalled when the control is removed; releases resources.
Font loading
loadFont
loadFont(url: string, name?: string): Promise<void>Loads an OpenType font (.ttf / .otf) for text entities.
| Parameter | Type | Description |
|---|---|---|
url | string | Font file URL |
name | string | Optional registered name; inferred from filename if omitted |
Example:
await cadLayer.loadFont('/fonts/simhei.ttf', '黑体');Mode management
getMode
getMode(): 'edit' | 'browse'Returns: Current mode.
setMode
setMode(mode: 'edit' | 'browse'): voidSwitch edit / browse mode.
| Parameter | Type | Description |
|---|---|---|
mode | 'edit' | 'browse' | Target mode |
toggleMode
toggleMode(): voidToggle between edit and browse.
setModeToggleShortcut
setModeToggleShortcut(shortcut: string): voidSet the mode-toggle shortcut.
| Parameter | Type | Description |
|---|---|---|
shortcut | string | Shortcut string, e.g. 'ctrl+shift+e' |
Shortcuts
setShortcuts
setShortcuts(map: ShortcutMap): voidReplace the shortcut map.
| Parameter | Type | Description |
|---|---|---|
map | ShortcutMap | Shortcut → command map |
getShortcuts
getShortcuts(): ShortcutMapReturns: A copy of the current shortcut map.
Command execution
executeCommand
executeCommand(name: string, opts?: Record<string, any>): Promise<void>Run a registered command by name.
| Parameter | Type | Description |
|---|---|---|
name | string | Command name |
opts | Record<string, any> | Optional arguments |
Example:
await cadLayer.executeCommand('line');
await cadLayer.executeCommand('circle', { radius: 50 });repeatLastCommand
repeatLastCommand(): Promise<void>Repeat the last command.
getLastCommandName
getLastCommandName(): string | undefinedReturns: Last command name, or undefined if none.
Entity operations
addEntity
addEntity(entity: EntityBase): voidAdd an entity to the store.
| Parameter | Type | Description |
|---|---|---|
entity | EntityBase | Entity instance |
deleteEntity
deleteEntity(id: string): voidDelete an entity by id.
getEntities
getEntities(): EntityBase[]Returns: All entities.
getSelectedEntities
getSelectedEntities(): EntityBase[]Returns: Currently selected entities.
clearSelection
clearSelection(): voidClear selection.
Draw order
moveToFront
moveToFront(ids?: string[]): voidMove entities to the front. If ids is omitted, affects the selection.
moveToBack
moveToBack(ids?: string[]): voidMove entities to the back.
moveUp
moveUp(ids?: string[]): voidMove entities up one level.
moveDown
moveDown(ids?: string[]): voidMove entities down one level.
Undo / redo
undo
undo(): voidUndo the last operation.
redo
redo(): voidRedo the last undone operation.
Plugins
loadPlugin
loadPlugin(plugin: Plugin): Promise<void>Load and activate a plugin.
| Parameter | Type | Description |
|---|---|---|
plugin | Plugin | Plugin object |
Serialization
toJSON
toJSON(): SerializedDocumentSerialize the drawing to JSON.
Returns: SerializedDocument.
fromJSON
fromJSON(doc: SerializedDocument): voidRestore a drawing from JSON.
| Parameter | Type | Description |
|---|---|---|
doc | SerializedDocument | Serialized document |
Example:
const doc = cadLayer.toJSON();
localStorage.setItem('drawing', JSON.stringify(doc));
// Restore
const saved = JSON.parse(localStorage.getItem('drawing')!);
cadLayer.fromJSON(saved);Object snap
setSnapEnabled
setSnapEnabled(enabled: boolean): voidEnable or disable object snap.
setSnapMode
setSnapMode(mode: number): voidSet snap mode (bitmask). See SnapManager API.
Drawing defaults
getDrawingDefaults
getDrawingDefaults(): DrawingDefaultsDataReturns: A copy of current drawing defaults.
setDrawingDefaults
setDrawingDefaults(opts: Partial<DrawingDefaultsData>): voidUpdate drawing defaults (merged).
View
zoomExtents
zoomExtents(padding?: number): voidZoom to fit all entities.
| Parameter | Type | Description |
|---|---|---|
padding | number | Optional padding in pixels |
getMap
getMap(): vjmap.MapReturns: Associated vjmap map instance.
getMapContainer
getMapContainer(): HTMLElementReturns: Map container DOM element.
refreshDisplayLevel
refreshDisplayLevel(): voidRefresh display-level filtering for entities based on current zoom.
Internationalization
setLocale
setLocale(locale: Locale): voidSwitch UI language.
| Parameter | Type | Description |
|---|---|---|
locale | Locale | 'zh' or 'en' |
UI creation
createUI
createUI(options?: object): voidCreate default UI (toolbar, property panel, command line, etc.).
| Parameter | Type | Description |
|---|---|---|
options | object | Optional UI options |
Example:
cadLayer.createUI();