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.

Properties

events: {
    documentActivated: AcCmEventManager<AcDbDocumentEventArgs>;
    documentCreated: AcCmEventManager<AcDbDocumentEventArgs>;
} = ...

Events fired during document lifecycle

Type declaration

Accessors

  • get avaiableFonts(): AcDbFontInfo[]

    Gets the list of available fonts that can be loaded.

    Note: These fonts are available for loading but may not be loaded yet.

    Returns AcDbFontInfo[]

    Array of available font names

  • get mdiActiveDocument(): AcApDocument

    Gets the currently active document.

    For now, this is the same as curDocument since only one document can be active at a time.

    Returns AcApDocument

    The current active document

Methods

  • Creates an example CAD document with sample content.

    This method asynchronously loads default fonts, creates example drawing content, sets up layout information, and zooms the view to fit the content. The creation is performed after a short delay to ensure proper initialization.

    Returns void

  • Loads default fonts for CAD text rendering.

    This method loads either the specified fonts or falls back to default Chinese fonts (specifically 'simkai') 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, defaults to ['simkai'] for Chinese text support

    Returns Promise<void>

    Promise that resolves when all specified fonts are loaded

    // Load default fonts (simkai)
    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']);
    
  • 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.

    Parameters

    • isSuccess: boolean

      Whether the document was successfully opened

    Returns void

  • Protected

    Performs cleanup operations before opening a new document.

    This protected method is called automatically before any document opening operation. It clears the current view to prepare for the new document content.

    Returns void

  • Opens a CAD document from file content.

    This method loads a document from the provided file content (string or 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: string | ArrayBuffer

      The file content as string or ArrayBuffer

    • options: AcDbOpenDatabaseOptions

      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: AcDbOpenDatabaseOptions

      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) {
    console.log('Document opened successfully');
    }
  • Registers all default commands available in the CAD viewer.

    This method sets up the command system by registering built-in commands including:

    • pan: Pan/move the view
    • select: Select entities
    • zoom: Zoom in/out
    • zoomw: Zoom to window/box
    • csvg: Convert to SVG
    • qnew: Quick new document
    • open: Open document

    All commands are registered under the system command group.

    Returns void

  • 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 found, no action is taken.

    Parameters

    • cmdStr: string

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

    Returns void

    docManager.sendStringToExecute('zoom');
    docManager.sendStringToExecute('pan');
  • Creates the singleton instance with an optional canvas element.

    This method should be called before accessing the instance property if you want to provide a specific canvas element.

    Parameters

    • Optionalcanvas: HTMLCanvasElement

      Optional HTML canvas element for rendering

    Returns undefined | AcApDocManager

    The singleton instance

    const canvas = document.getElementById('my-canvas') as HTMLCanvasElement;
    const docManager = AcApDocManager.createInstance(canvas);