MLightCAD
    Preparing search index...

    Module @mlightcad/cad-simple-viewer - v1.5.8

    CAD Simple Viewer

    License: MIT npm version

    This package provides the high-performance core components of a CAD viewer such as document management, command handling, and collaboration between the UI and rendering engines. It's designed for optimal performance when handling large CAD files.

    This module doesn't depend on any UI framework and doesn't provide any UI except canvas. If you want to integrate a high-performance CAD viewer into a web application with your own UI, this module is the correct choice.

    • Document management optimized for large files
    • Efficient command stack and undo/redo operations
    • Optimized integration with rendering engines
    • Performance-focused settings and context management
    • Framework-agnostic design for maximum flexibility

    Use cad-simple-viewer if you need core CAD logic only (document management, command stack, rendering engine integration) without any UI framework dependencies. This package is ideal if:

    • You want to build your own custom UI or integrate CAD functionality into a non-Vue or non-web environment.
    • You require maximum flexibility and performance for handling large CAD files, and plan to connect the logic to your own rendering or UI layer.
    • You want a framework-agnostic solution that provides only the essential CAD operations and canvas rendering.

    Recommended for: Custom integrations, headless CAD processing, or advanced users building highly tailored CAD solutions.

    • src/app/ – Document/context management, settings
    • src/command/ – Command implementations (open, zoom, select, etc.)
    • src/editor/ – Command stack, input handling, global functions
    • src/service/ – Layer/entity services and UI layer store
    • src/view/ – Layout and scene management
    • src/util/ – Utilities
    npm install @mlightcad/cad-simple-viewer
    

    Please refer to cad-simple-viewer-example on basic usage and advanced usage.

    While cad-simple-viewer doesn't support saving drawings to DWG/DXF files, it provides comprehensive support for modifying drawings in real-time. You can add, edit, and delete entities within the drawing, and the viewer will automatically update to reflect these changes.

    When you modify entities, you're working directly with the underlying drawing database. The viewer automatically detects these changes and updates the display accordingly. This real-time synchronization ensures that:

    • All modifications are immediately visible
    • The command stack properly tracks changes for undo/redo operations. This will be implemented soon.

    This capability makes cad-simple-viewer suitable for applications that need to not only display CAD files but also allow users to interact with and modify the drawing content.

    Important Note: The usage patterns in cad-simple-viewer are very similar to AutoCAD RealDWG. If you're familiar with AutoCAD RealDWG development, you'll find the API structure and workflow nearly identical. The main difference is that we use the realdwg-web API instead of the native RealDWG libraries.

    In cad-simple-viewer-example it demonstrates how to create one drawing with realdwg-web API.

    Layer table mutations are centralized in AcApLayerService. UI integrations should use AcApDocument.layerStore, which observes that document's layer table and delegates mutations to its layer service.

    AcApLayerService.setLayerOn accepts { switchCurrentLayer?: boolean }:

    • CLI commands leave the default false. Batch helpers skip the current layer instead.
    • UI callers (AcApLayerStore, Vue useLayers) pass true so hiding or freezing the active layer moves CLAYER to another visible layer first.

    The LockAndFade isolation keyword matches AutoCAD naming but the viewer locks non-isolated layers only. It does not apply a visual fade; users are notified when selecting this mode in the LAYISO command.

    The viewer loads worker scripts for DWG parsing and MTEXT rendering. DXF is parsed by the built-in converter in @mlightcad/data-model (no separate worker). Host applications must deploy the DWG/MTEXT worker files and point to them via webworkerFileUrls in AcApDocManager.createInstance().

    Before calling openDocument(), verify that the workers are reachable. Use the built-in readiness API rather than downloading worker bodies with a plain GET request (the LibreDWG worker alone is ~12 MB):

    import {
    AcApDocManager,
    LIBREDWG_PARSER_WORKER_FILE,
    MTEXT_RENDERER_WORKER_FILE
    } from '@mlightcad/cad-simple-viewer'

    const workerUrls = {
    dwgParser: `./workers/${LIBREDWG_PARSER_WORKER_FILE}`,
    mtextRender: `./workers/${MTEXT_RENDERER_WORKER_FILE}`
    }

    // Option 1: check before creating the manager
    const ready = await AcApDocManager.checkWebworkerReadiness(workerUrls)
    if (!ready) {
    throw new Error('CAD worker scripts are missing or blocked')
    }

    const manager = AcApDocManager.createInstance({ webworkerFileUrls: workerUrls })

    // Option 2: check on an existing manager instance
    if (!(await manager.areWorkersReady())) {
    throw new Error('CAD worker scripts are missing or blocked')
    }

    areWorkersReady() and checkWebworkerReadiness() use HEAD requests internally. Successful URL probes are cached for the current page lifecycle; failures are not cached at the probe layer, so a transient network error can succeed on a later areWorkersReady() call. After each check, manager.workersReady is true or false (null only before the first check).

    You can also enable automatic checks during initialization:

    AcApDocManager.createInstance({
    webworkerFileUrls: workerUrls,
    checkWorkersOnInit: true
    })

    manager.events.workersReady.addEventListener(({ ready }) => {
    if (!ready) console.error('CAD workers are not reachable')
    })
    • AcApContext - Main application context
    • AcApDocManager - Document management
    • AcApDocument - Individual document handling (includes layerService and layerStore)
    • AcApSettingManager - Settings management
    • AcApLayerService - Layer table mutations with undo
    • AcApLayerStore - Cached layer rows and UI-friendly mutations (via AcApDocument.layerStore)
    • AcApEntityService - Entity selection, transform, and edit helpers
    • acapRunServiceEdit - Undo-wrapped edits outside command transactions
    • AcApOpenCmd - Open file command
    • AcApZoomCmd - Zoom command
    • AcApPanCmd - Pan command
    • AcApSelectCmd - Selection command

    SVG export (csvg) is provided by the optional @mlightcad/cad-svg-plugin package.

    • AcEditor - Main editor class
    • AcEdCommandStack - Command stack management
    • AcEdSelectionSet - Selection handling
    • AcEdInputPoint - Input point management
    • AcTrScene - Scene management
    • AcTrLayoutView - Layout view handling
    • AcTrView2d - 2D view management

    This package acts as the core logic layer, connecting the frontend UI with the rendering engines and managing all document-related operations.

    MIT

    Enumerations

    AcApOpenViewMode
    AcEdCorsorType
    AcEdOpenMode
    AcEdPromptStatus
    AcEdViewMode
    MTextParagraphAlignment

    Classes

    AcApAboutCmd
    AcApAnnotation
    AcApArcCmd
    AcApBaseRevCmd
    AcApBlockInsertSession
    AcApBlockPreviewConvertor
    AcApCacheFontCmd
    AcApCircleCmd
    AcApCircleJig
    AcApClearMeasurementsCmd
    AcApContext
    AcApConvertToDxfCmd
    AcApConvertToPngCmd
    AcApCopyCmd
    AcApDimJig
    AcApDimLinearCmd
    AcApDocManager
    AcApDocument
    AcApDxfConvertor
    AcApEllipseCmd
    AcApEntityPreviewCmd
    AcApEntityPreviewConvertor
    AcApEntityService
    AcApEraseCmd
    AcApExamplePlugin
    AcApFontUtil
    AcApHatchCmd
    AcApHideObjectsCmd
    AcApI18n
    AcApImageAttachCmd
    AcApInsertCmd
    AcApLayerCloseCmd
    AcApLayerCmd
    AcApLayerCurCmd
    AcApLayerDelCmd
    AcApLayerFreezeCmd
    AcApLayerIsoCmd
    AcApLayerLockCmd
    AcApLayerOnCmd
    AcApLayerPCmd
    AcApLayerService
    AcApLayerStore
    AcApLayerThawCmd
    AcApLayerUnisoCmd
    AcApLayerUnlockCmd
    AcApLayoffCmd
    AcApLineCmd
    AcApLineJig
    AcApLogCmd
    AcApMeasureAngleCmd
    AcApMeasureArcCmd
    AcApMeasureAreaCmd
    AcApMeasureDistanceCmd
    AcApMeasureDistanceJig
    AcApMemoryProfiler
    AcApMLineCmd
    AcApMLineJig
    AcApMoveCmd
    AcApMTextCmd
    AcApOffsetCmd
    AcApOpenCmd
    AcApOpenFileProfiler
    AcApPanCmd
    AcApPluginManager
    AcApPngConvertor
    AcApPointCmd
    AcApPolygonCmd
    AcApPolylineCmd
    AcApPolylineJig
    AcApProgress
    AcApQNewCmd
    AcApRayCmd
    AcApRectCmd
    AcApRectJig
    AcApRedoCmd
    AcApRegenCmd
    AcApRevCircleCmd
    AcApRevCloudCmd
    AcApRevCloudJig
    AcApRevRectCmd
    AcApRevVisibilityCmd
    AcApRotateCmd
    AcApSelectCmd
    AcApSettingManager
    AcApSketchCmd
    AcApSketchJig
    AcApSplineCmd
    AcApSplineJig
    AcApSwitchBgCmd
    AcApSysVarCmd
    AcApUndoCmd
    AcApUnisolateObjectsCmd
    AcApXAttachCmd
    AcApXLineCmd
    AcApXrefManager
    AcApZoomCmd
    AcEdBaseView
    AcEdBatchedPreview
    AcEdCommand
    AcEdCommandLine
    AcEdCommandStack
    AcEdConditionWaiter
    AcEdCursorManager
    AcEdGripEditSession
    AcEdGripHandle
    AcEdGripManager
    AcEdGripPreviewJig
    AcEdHoverController
    AcEdInputManager
    AcEditor
    AcEdKeyword
    AcEdKeywordCollection
    AcEdMarker
    AcEdMarkerManager
    AcEdMTextEditor
    AcEdOsnapResolver
    AcEdPreviewJig
    AcEdPromptAngleOptions
    AcEdPromptBoxOptions
    AcEdPromptBoxResult
    AcEdPromptDistanceOptions
    AcEdPromptDoubleOptions
    AcEdPromptDoubleResult
    AcEdPromptEntityOptions
    AcEdPromptEntityResult
    AcEdPromptIntegerOptions
    AcEdPromptIntegerResult
    AcEdPromptKeywordOptions
    AcEdPromptNumericalOptions
    AcEdPromptNumericalResult
    AcEdPromptOptions
    AcEdPromptPointOptions
    AcEdPromptPointResult
    AcEdPromptResult
    AcEdPromptSelectionOptions
    AcEdPromptSelectionResult
    AcEdPromptStateMachine
    AcEdPromptStringOptions
    AcEdSelectionFilter
    AcEdSelectionSet
    AcEdSelectionStaticPreviewJig
    AcEdSelectionTransformPreviewJig
    AcEdViewKeyHandler
    AcTrInheritedLayerMaterialMapper
    AcTrLayerAppearanceController
    AcTrScene
    AcTrView2d
    AcUiAboutDialog
    AcUiDialog
    MTextColor
    ShxParserFont

    Interfaces

    AcApBlockInsertSessionOptions
    AcApCacheFontOptions
    AcApCloneAndTransformOptions
    AcApCommandServices
    AcApCreateLayersResult
    AcApDocManagerOptions
    AcApLaydelEntitySnapshot
    AcApLayerInfo
    AcApLayerIsolateLayersResult
    AcApLayerIsolateResult
    AcApLayerIsoLayerSnapshot
    AcApLayerIsoLayerState
    AcApLayerIsoSnapshot
    AcApLayerPreviousSnapshot
    AcApLayerStoreChangedEventArgs
    AcApLayerSummary
    AcApLazyPluginRegistration
    AcApMemoryBucketRow
    AcApMemoryCategoryRow
    AcApMemoryDataModelStats
    AcApMemoryFontRow
    AcApMemoryFontStats
    AcApMemoryGeometryLayerRow
    AcApMemoryHeapStats
    AcApMemorySnapshot
    AcApMemorySpatialLayoutRow
    AcApMoveToCurrentLayerResult
    AcApOpenDatabaseOptions
    AcApOpenFileDialogOptions
    AcApPlugin
    AcApProgressOptions
    AcApResolveLayersResult
    AcApResolveSelectedOptions
    AcApResolveSelectedResult
    AcApSetLayerOnOptions
    AcApSetLayersVisibilityOptions
    AcApSettingManagerEventArgs
    AcApSettings
    AcApWebworkerFiles
    AcApXrefAttachOptions
    AcApXrefSession
    AcApXrefTransform
    AcDbDocumentEventArgs
    AcDbSysVarEventArgs
    AcEdCommandEventArgs
    AcEdCommandGroup
    AcEdFontNotLoadedInfo
    AcEdGripEditTarget
    AcEdHoverHost
    AcEdInputModifiers
    AcEdInputToggles
    AcEdKeywordPromptFormat
    AcEdLayerInfo
    AcEdMissedData
    AcEdMissedXref
    AcEdMouseEventArgs
    AcEdMTextEditorCurrentFormatObservable
    AcEdMTextEditorOptions
    AcEdMTextEditorResult
    AcEdOsnapResolveOptions
    AcEdPromptState
    AcEdSelectionEventArgs
    AcEdSelectionModifierState
    AcEdSelectionPreviewEntry
    AcEdSelectionPreviewTransform
    AcEdSpatialQueryResultItem
    AcEdSpatialQueryResultItemEx
    AcEdViewHoverEventArgs
    AcEdViewRenderFrameEventArgs
    AcEdViewResizedEventArgs
    AcTrSceneStats
    AcTrView2dOptions
    AcUiDialogOptions
    FontInfo
    FontLoadStatus
    HatchSettings
    MTextToolbarColorPickerContext
    MTextToolbarColorPickerInstance
    ShxFontData

    Type Aliases

    AcApDeleteLayerResult
    AcApEntityPreviewCaptureResult
    AcApEntityPreviewExportFailure
    AcApEntityPreviewExportResult
    AcApFontMapping
    AcApLayerByEntityResult
    AcApLayerIsolationMode
    AcApLocale
    AcApOpenDocumentDefaultsResolver
    AcEdBusyIndicatorEventArgs
    AcEdCalculateSizeCallback
    AcEdEvents
    AcEdGripHandleState
    AcEdMarkerType
    AcEdMessageType
    AcEdMTextEditorActiveInputBox
    AcEdMTextEditorActiveInputBoxChangeListener
    AcEdMTextEditorCurrentFormatChangeListener
    AcEdOsnapPoint
    AcEdPromptStateStep
    AcEdSelectionAction
    AcEdSelectionMode
    AcEdUiTheme
    AcEdUndoRedoShortcut
    AcTrEntityPreviewScope
    FontType
    MTextToolbarColorPickerFactory

    Variables

    ACAP_UNTITLED_DOC_TITLE
    ACGI_DARK_THEME_FOREGROUND
    ACGI_LIGHT_THEME_FOREGROUND
    ACGI_MODEL_SPACE_BACKGROUND
    ACGI_PAPER_SPACE_BACKGROUND
    AcTrGeometryUtil
    DEFAULT_VIEW_2D_OPTIONS
    DWG_CONVERTER_PACKAGE
    DWG_PARSER_WORKER_FILE
    ENTITY_EDIT_LABEL
    eventBus
    LAYER_EDIT_LABEL
    LAYER_FROZEN_FLAG
    LAYER_LOCKED_FLAG
    LIBREDWG_CONVERTER_PACKAGE
    LIBREDWG_PARSER_WORKER_FILE
    ML_UI_COMPACT_MAX_WIDTH
    ML_UI_COMPACT_MEDIA_QUERY
    ML_UI_MOBILE_MAX_WIDTH
    ML_UI_MOBILE_MEDIA_QUERY
    MLIGHTCAD_BRAND_NAME
    MLIGHTCAD_DOCS_URL
    MLIGHTCAD_ICON_SVG
    MLIGHTCAD_REPO_URL
    MLIGHTCAD_WEBSITE_URL
    MTEXT_RENDERER_PACKAGE
    MTEXT_RENDERER_WORKER_FILE
    POLARMODE_ADDITIONAL_ANGLES
    POLARMODE_POLAR_TRACKING

    Functions

    acapBindCommandServices
    acapCommandServices
    acapCreateMlightcadBrandMark
    acapCreateMlightcadIcon
    acapInstallOpenFileDialog
    acapNotifyUndoStackChanged
    acapRunDatabaseEdit
    acapRunServiceEdit
    acapUninstallOpenFileDialog
    acapUpdateOpenFileDialogOptions
    acapWithSecondaryDatabase
    acedAlert
    acgiContrastingForegroundColor
    acgiForegroundColorForBackground
    acgiIsLightBackground
    applySessionHiddenObjectState
    applyUiTheme
    canvasBackgroundFromColor
    clearAllMeasurements
    cmdDescription
    collectMemorySnapshot
    colorToCssAlpha
    constrainToOrtho
    constrainToTracking
    createRotationMatrix
    cssColor
    cursorColorForBackground
    foregroundColorFromColorTheme
    formatMemoryBytes
    geBox2dToThreeBox2d
    geBox2dToThreeBox3d
    geBox3dToGeBox2d
    geBox3dToThreeBox3d
    getDrawingExportBaseName
    getLayerIsoState
    getPolaraddang
    getPolarang
    getPolarmode
    hideObjects
    isCompactUiLayout
    isEffectiveSpatialQueryHit
    isInsertableBlockName
    isLightColorTheme
    isMobileUiLayout
    isModelSpaceDatabase
    isOpenFileProgressComplete
    isOrthoModeEnabled
    isPolarTrackingEnabled
    isSameLayerIsoState
    layoutBackgroundSysVar
    makeBadge
    makeDot
    makeLiveBadge
    makeLiveDot
    makeOverlayCanvas
    makeSnapIndicator
    measurementColor
    readLayoutBackgroundColor
    registerMeasurementCleanup
    resetExportDownloadNamesForTests
    resolveExportDownloadName
    resolvePointerSelectionAction
    resolveSelectedEntities
    resolveSelectedIds
    resolveSelectionActionFromEvent
    resolveUiTheme
    scaleCopyDisplacement
    sysCmdDescription
    threeBox2dToGeBox2d
    threeBox3dToGeBox2d
    threeBox3dToGeBox3d
    toggleBlackWhiteBackgroundColor
    togglePolarTracking
    unionSpatialQueryItems
    unisolateObjects
    userCmdDescription

    References

    AcGiTextParagraphAlignment → MTextParagraphAlignment