The AcDbDatabase class represents an AutoCAD drawing file.

Each AcDbDatabase object contains the various header variables, symbol tables, table records, entities, and objects that make up the drawing. The AcDbDatabase class has member functions to allow access to all the symbol tables, to read and write to DWG files, to get or set database defaults, to execute various database-level operations, and to get or set all header variables.

const database = new AcDbDatabase();
await database.read(dxfData, { readOnly: true });
const entities = database.tables.blockTable.modelSpace.entities;

Hierarchy (View Summary)

Constructors

Properties

events: {
    entityAppended: AcCmEventManager<AcDbEntityEventArgs>;
    entityModified: AcCmEventManager<AcDbEntityEventArgs>;
    headerSysVarChanged: AcCmEventManager<AcDbHeaderSysVarEventArgs>;
    layerAppended: AcCmEventManager<AcDbLayerEventArgs>;
    layerErased: AcCmEventManager<AcDbLayerEventArgs>;
    layerModified: AcCmEventManager<AcDbLayerModifiedEventArgs>;
    openProgress: AcCmEventManager<AcDbProgressdEventArgs>;
} = ...

Events that can be triggered by the database.

These events allow applications to respond to various database operations such as entity modifications, layer changes, and progress updates.

Type declaration

Accessors

  • get aunits(): number
  • Gets the angle units for the database.

    This is the current AUNITS value for the database.

    Returns number

    The angle units value

    const angleUnits = database.aunits;
    
  • set aunits(value: number): void
  • Sets the angle units for the database.

    Parameters

    • value: number

      The new angle units value

    Returns void

    database.aunits = AcDbAngleUnits.DecimalDegrees;
    
  • get celtscale(): number
  • The line type scaling for new objects relative to the ltscale setting. A line created with celtscale = 2 in a drawing with ltscale set to 0.5 would appear the same as a line created with celtscale = 1 in a drawing with ltscale = 1.

    Returns number

  • set celtscale(value: number): void
  • Parameters

    • value: number

    Returns void

  • get currentSpaceId(): string
  • Gets the object ID of the AcDbBlockTableRecord of the current space.

    The current space can be either model space or paper space.

    Returns string

    The object ID of the current space

    const currentSpaceId = database.currentSpaceId;
    
  • set currentSpaceId(value: string): void
  • Sets the current space by object ID.

    Parameters

    • value: string

      The object ID of the block table record to set as current space

    Returns void

    When the specified block table record ID doesn't exist

    database.currentSpaceId = 'some-block-record-id';
    
  • get database(): AcDbDatabase
  • Gets the database in which this object is resident.

    When an object isn't added to a database, this property returns the current working database. After it is added to a database, it will be set automatically. You should never set this value manually.

    Returns AcDbDatabase

    The database this object belongs to

    const db = obj.database;
    
  • set database(db: AcDbDatabase): void
  • Sets the database for this object.

    This is typically set automatically when the object is added to a database. Manual setting should be avoided unless you know what you're doing.

    Parameters

    Returns void

    obj.database = myDatabase;
    
  • get insunits(): number
  • Gets the drawing-units value for automatic scaling of blocks, images, or xrefs.

    This is the current INSUNITS value for the database.

    Returns number

    The insertion units value

    const insertionUnits = database.insunits;
    
  • set insunits(value: number): void
  • Sets the drawing-units value for automatic scaling.

    Parameters

    • value: number

      The new insertion units value

    Returns void

    database.insunits = AcDbUnitsValue.Millimeters;
    
  • get objectId(): string
  • Gets the object ID.

    AutoCAD uses 64-bit integers to represent handles, which exceed the maximum integer value of JavaScript. Therefore, strings are used to represent object handles.

    Returns string

    The object ID as a string

    const id = obj.objectId;
    console.log(`Object ID: ${id}`);
  • set objectId(value: string): void
  • Sets the object ID.

    Parameters

    • value: string

      The new object ID

    Returns void

    obj.objectId = 'new-object-id';
    
  • get ownerId(): string
  • Gets the object ID of the owner of this object.

    Returns string

    The owner object ID

    const ownerId = obj.ownerId;
    
  • set ownerId(value: string): void
  • Sets the object ID of the owner of this object.

    Parameters

    • value: string

      The new owner object ID

    Returns void

    obj.ownerId = 'parent-object-id';
    

Methods

  • Closes the object.

    All changes made to the object since it was opened are committed to the database, and a "closed" notification is sent. This method can be overridden by subclasses to provide specific cleanup behavior.

    Returns void

    obj.close();
    
  • Gets the value of the specified attribute.

    This method will throw an exception if the specified attribute doesn't exist. Use getAttrWithoutException() if you want to handle missing attributes gracefully.

    Parameters

    • attrName: string

      The name of the attribute to retrieve

    Returns any

    The value of the specified attribute

    When the specified attribute doesn't exist

    try {
    const value = obj.getAttr('objectId');
    } catch (error) {
    console.error('Attribute not found');
    }
  • Gets the value of the specified attribute without throwing an exception.

    This method returns undefined if the specified attribute doesn't exist, making it safer for optional attributes.

    Parameters

    • attrName: string

      The name of the attribute to retrieve

    Returns any

    The value of the specified attribute, or undefined if it doesn't exist

    const value = obj.getAttrWithoutException('optionalAttribute');
    if (value !== undefined) {
    // Use the value
    }
  • Read AutoCAD DXF or DWG drawing specified by the URL into the database object. The method automatically detects the file type based on the URL extension:

    • .dxf files are read as text using readAsText()
    • .dwg files are read as binary data using readAsArrayBuffer()

    Parameters

    • url: string

      Input the URL linked to one AutoCAD DXF or DWG file

    • options: AcDbOpenDatabaseOptions

      Input options to read drawing data

    Returns Promise<void>

  • Reads drawing data from a string or ArrayBuffer.

    This method parses the provided data and populates the database with the resulting entities, tables, and objects. The method supports both DXF and DWG file formats.

    Parameters

    • data: string | ArrayBuffer

      The drawing data as a string or ArrayBuffer

      • For DXF files: Pass a string containing the DXF content
      • For DWG files: Pass an ArrayBuffer instance containing the binary DWG data
    • options: AcDbOpenDatabaseOptions

      Options for reading the database

    • fileType: AcDbFileType = AcDbFileType.DXF

      The type of file being read (defaults to DXF)

    Returns Promise<void>

    // Reading a DXF file (string)
    const database = new AcDbDatabase();
    await database.read(dxfString, { readOnly: true }, AcDbFileType.DXF);

    // Reading a DWG file (ArrayBuffer)
    const database = new AcDbDatabase();
    await database.read(dwgArrayBuffer, { readOnly: true }, AcDbFileType.DWG);