MLightCAD
    Preparing search index...

    Document manager that handles CAD document lifecycle and provides the main entry point for the CAD viewer.

    This singleton class manages:

    • Document creation and opening (from URLs or file content)
    • View and context management
    • Command registration and execution
    • Font loading for text rendering
    • Event handling for document lifecycle

    The manager follows a singleton pattern to ensure only one instance manages the application state.

    Index

    Properties

    events: {
        documentActivated: AcCmEventManager<AcDbDocumentEventArgs>;
        documentCreated: AcCmEventManager<AcDbDocumentEventArgs>;
        documentToBeOpened: AcCmEventManager<AcDbDocumentEventArgs>;
        workersReady: AcCmEventManager<{ ready: boolean }>;
    } = ...

    Events fired during document lifecycle

    Type Declaration

    • documentActivated: AcCmEventManager<AcDbDocumentEventArgs>

      Fired when a document becomes active

    • documentCreated: AcCmEventManager<AcDbDocumentEventArgs>

      Fired when a new document is created

    • documentToBeOpened: AcCmEventManager<AcDbDocumentEventArgs>

      Fired before a document starts opening

    • workersReady: AcCmEventManager<{ ready: boolean }>

      Fired when a worker readiness check completes

    Accessors

    Methods

    • Returns true when all configured worker files are reachable.

      Uses HEAD requests internally. A successful result is cached on this instance for fast subsequent calls; failures update workersReady to false but can be retried. The underlying URL probe does not cache failures, so transient network errors can recover on a later call.

      Returns Promise<boolean>

    • Protected

      Checks whether the current document's database already has usable content even though the open operation reported failure.

      Returns boolean

      True when the database has recoverable partial content.

    • Loads default fonts for CAD text rendering.

      This method loads either the specified fonts or the configured default font fallback chains (DEFAULT_FONTS_PRESET, currently modern: text hztxt 鈫?simsun, symbol amgdt) if no fonts are provided. The loaded fonts are used for rendering CAD text entities like MText and Text in the viewer.

      It is better to load default fonts when viewer is initialized so that the viewer can render text correctly if fonts used in the document are not available.

      Parameters

      • Optionalfonts: string[]

        Optional array of font names to load. If not provided or null, loads the active FontManager.getFontsToLoad chains

      Returns Promise<void>

      Promise that resolves when all specified fonts are loaded

      // Load the modern default font chain
      await docManager.loadDefaultFonts();

      // Load specific fonts
      await docManager.loadDefaultFonts(['Arial', 'SimSun']);

      // Load no fonts (empty array)
      await docManager.loadDefaultFonts([]);
      • AcApFontLoader.load - The underlying font loading implementation
      • createExampleDoc - Method that uses this for example document creation
    • Loads the specified fonts for text rendering.

      Parameters

      • fonts: string[]

        Array of font names to load

      Returns Promise<void>

      Promise that resolves when fonts are loaded

      await docManager.loadFonts(['Arial', 'Times New Roman']);
      
    • Loads a DWG/DXF file as a read-only overlay (base drawing/reference) rendered alongside the currently open document in the same WCS coordinate system, without replacing it.

      Unlike openDocument, this parses the file into a standalone AcDbDatabase that never becomes curDocument — it isn't touched by undo, the layer panel/table, or selection. Overlay geometry currently covers top-level entities (lines, arcs, polylines, text, hatch, etc.); block (INSERT) expansion, viewports, and dimensions are not yet supported and are skipped.

      Parameters

      • fileName: string

        Input file name, used to determine DWG vs DXF from its extension.

      • content: ArrayBuffer

        Input file content as an ArrayBuffer.

      • options: AcDbOpenDatabaseOptions = {}

        Input options forwarded to the underlying database read.

      Returns Promise<string>

      The generated overlay id, used with removeOverlay and setOverlayVisible.

      const fileContent = await file.arrayBuffer();
      const overlayId = await docManager.loadOverlay('base.dwg', fileContent);
      docManager.setOverlayVisible(overlayId, false); // hide it later
      docManager.removeOverlay(overlayId); // or remove it entirely
    • Search through all of the global and untranslated names in all of the command groups in the command stack starting at the top of the stack trying to find a match with cmdName. If a match is found, the matched AcEdCommand object is returned. Otherwise undefined is returned to indicate that the command could not be found. If more than one command of the same name is present in the command stack (that is, in separate command groups), then the first one found is used.

      The command is only returned if it is compatible with that open mode of the current document. Higher value modes are compatible with lower value modes.

      Parameters

      • cmdName: string

        Input the command name to search for

      Returns AcEdCommand<{}> | undefined

      Return the matched AcEdCommand object if a match is found and compatible with the open mode of the current document. Otherwise, return undefined.

    • Search through all of the local and translated names in all of the command groups in the command stack starting at the top of the stack trying to find a match with cmdName. If a match is found, the matched AcEdCommand object is returned. Otherwise undefined is returned to indicate that the command could not be found. If more than one command of the same name is present in the command stack (that is, in separate command groups), then the first one found is used.

      The command which is compatible with the open mode of the current document is only returned

      Parameters

      • cmdName: string

        Input the command name to search for

      Returns AcEdCommand<{}> | undefined

      Return the matched AcEdCommand object if a match is found and compatible with the open mode of the current document. Otherwise, return undefined.

    • Creates a new CAD document from the default ISO drawing template.

      This method loads the predefined template (acadiso.dxf) from baseUrl and replaces the current document in write mode with default open options.

      Parameters

      Returns Promise<boolean>

      Promise that resolves to true if the document was successfully created

      const success = await docManager.newDocument();
      
    • Protected

      Performs setup operations after a document opening attempt.

      This protected method is called automatically after any document opening operation. If the document was successfully opened, it dispatches the documentActivated event, sets up layout information, and zooms the view to fit the content.

      A large DWG/DXF parse can fail (e.g. a worker timeout) after entities have already been committed to the database in earlier pipeline stages, leaving isSuccess false while the database already has valid, non-empty extents. Without this check the view never initializes and the user is left with a blank canvas despite the data being in memory. When that happens, treat it as a recovered partial open so the view still activates and frames whatever content did land.

      Parameters

      Returns void

    • Opens a CAD document from file content.

      This method loads a document from the provided file content (binary data) and replaces the current document. It handles the complete document lifecycle including before/after open events.

      Parameters

      • fileName: string

        The name of the file being opened (used for format detection)

      • content: ArrayBuffer

        The file content

      • options: AcApOpenDatabaseOptions

        Database opening options including font loader settings

      Returns Promise<boolean>

      Promise that resolves to true if the document was successfully opened, false otherwise

      const fileContent = await file.arrayBuffer();
      const success = await docManager.openDocument('drawing.dwg', fileContent, options);
    • Opens a CAD document from a URL.

      This method loads a document from the specified URL and replaces the current document. It handles the complete document lifecycle including before/after open events.

      Parameters

      • url: string

        The URL of the CAD file to open

      • Optionaloptions: AcApOpenDatabaseOptions

        Optional database opening options. If not provided, default options with font loader will be used

      Returns Promise<boolean>

      Promise that resolves to true if the document was successfully opened, false otherwise

      const success = await docManager.openUrl('https://example.com/drawing.dwg');
      if (success) {
      log.info('Document opened successfully');
      }
    • Registers an already-parsed read-only database as an overlay.

      Prefer this when the caller already loaded the secondary database (e.g. XATTACH extents preview) to avoid reading the same file twice.

      Parameters

      • db: AcDbDatabase

      Returns Promise<string>

    • Resolves colors for creating new entities.

      Returns:

      • entityColor: the resolved RGB color (24-bit) to use for newly created entities.
      • layerColor: the current layer's resolved RGB color (24-bit).

      Returns { entityColor: number; layerColor: number }

    • Fuzzy search for commands by prefix using the command iterator.

      This method iterates through all commands in all command groups and returns those whose global or local names start with the provided prefix. The search is case-insensitive. Only commands which are compatible with that open mode of the current document are returned. Higher value modes are compatible with lower value modes.

      Parameters

      • prefix: string

        The prefix string to search for. Case-insensitive.

      Returns AcEdCommandIteratorItem[]

      An array of objects containing matched commands and their corresponding group names.

    • Executes a command by its string name.

      This method looks up a registered command by name and executes it with the current context. If the command is not registered yet, it attempts to load a lazy plugin whose trigger matches the command name (see AcApPluginManager.loadByTrigger). It checks if the command's required mode is compatible with the document's current mode. If the command is not found or not compatible, an error is thrown.

      Parameters

      • cmdStr: string

        The command string to execute (e.g., 'pan', 'zoom', 'select')

      Returns void

      If the command is not found or if the command's mode is not compatible with the document's mode

      docManager.sendStringToExecute('zoom');
      docManager.sendStringToExecute('pan');
    • Configures layout information for the current view.

      Sets up the active layout block table record ID and model space block table record ID based on the current document's space configuration.

      Returns void

    • Shows or hides a previously loaded overlay without removing it.

      Parameters

      • overlayId: string

        Input the id returned by loadOverlay.

      • visible: boolean

        Input whether the overlay should be visible.

      Returns boolean

      True when an overlay with the given id was found.