Skip to content

[mxcad_2d API documentation] (../README. md)/[2d] (../modules/2d. md)/McDbDatabase

Class: McDbDatabase ​

2d.McDbDatabase

McDbDatabase is a CAD drawing database object that serves as the primary entry point for all data and object information in the drawing.

Description

This class represents the database container of the current drawing, which stores core data structures such as layer tables, line type tables, text style sheets, block tables, dictionaries, etc. It can accomplish: -Retrieve and manage table objects such as layers, linetypes, styles, block tables, etc; -Set the current layer, current color, current text style, and current line type; -Read and write files, insert blocks, create groups, and process named object dictionaries; -Query and reset the modification status of the current database.

Usage:

  1. Obtain the database instance through MxCpp. getCurrentMxCAD(). getData();
  2. Call methods such as getLayerTable(), getBlockTable(), getTextCtyleTable() to access various types of tables;
  3. Use setCurrentlyLayerName(), setCurrentlyTrueColor(), setCurrentlyTextCtyle(), and other settings to set the current drawing state;
  4. Access the named object dictionary, create groups, or save/read drawing files as needed.

Summary: In practical development, McDbDatabase is the main entrance for the entire drawing data, and almost all layers, blocks, styles, dictionaries, and object management need to start from it.

Example

ts
import { MxCpp, McDbDatabase } from "mxcad";

//Retrieve the current database and perform basic operations.
async function testDatabase() {
  const mxcad = MxCpp.getCurrentMxCAD();
  const database: McDbDatabase = mxcad.getDatabase();

  const layerTable = database.getLayerTable();
Console.log ("Layer Table Object:", layerTable);

  const blockTable = database.getBlockTable();
Console.log ("block table object:", blockTable);

  database.setCurrentlyLayerName("0");
Console. log ("Current Layer:", database. getCurrentlyLayerName());
}

Hierarchy ​

Table of contents ​

Constructors ​

Properties ​

Accessors ​

Methods ​

Constructors ​

constructor ​

• new McDbDatabase(imp?)

Constructor.

Parameters ​

NameTypeDescription
imp?AnyInternal implementation object

Overrides ​

McRxObject.constructor

Properties ​

imp ​

• imp: any = 0

Internal implementation object.

Inherited from ​

McRxObject.imp

Accessors ​

blockTable ​

• get blockTable(): McDbBlockTable

Get block table

Returns ​

McDbBlockTable


currentSpace ​

• get currentSpace(): McDbBlockTableRecord

Return the current space block table record

Returns ​

McDbBlockTableRecord

Block table record

Example

ts
import { MxCpp } from "mxcad";

//Obtain the actual display range of the current drawing
const mxcad = MxCpp.getCurrentMxCAD();
const {minPt, maxPt} = mxcad.getDatabase().currentSpace.getBoundingBox();

currentSpaceId ​

• get currentSpaceId(): McObjectId

Return the record ID of the current space block table

Returns ​

McObjectId

Block table record ID


dimStyleTable ​

• get dimStyleTable(): McDbDimStyleTable

Retrieve the annotation style sheet.

Returns ​

McDbDimStyleTable

Linear table.


dxf0 ​

• get dxf0(): string

Obtain the type name of the object's DXF group code, which is the same as the DXF group code in AutoCAD. For example, the type name of the line is McDbLine, and the group code value for DXF0 is: LINE and DXF0 group code values can be used for type filtering when constructing sets.

Returns ​

string

Inherited from ​

McRxObject.dxf0


isCurrentModelSpace ​

• get isCurrentModelSpace(): boolean

Return layer 0 ID

Returns ​

boolean

Layer 0 ID


layerTable ​

• get layerTable(): McDbLayerTable

Get layer table

Returns ​

McDbLayerTable


linetypeTable ​

• get linetypeTable(): McDbLinetypeTable

Get line type table

Returns ​

McDbLinetypeTable


modelSpaceId ​

• get modelSpaceId(): McObjectId

Return the model space block table record ID

Returns ​

McObjectId

Model block table record ID


objectName ​

• get objectName(): string

Get the object name.

Returns ​

string

Return object name

Inherited from ​

McRxObject.objectName


textStyleTable ​

• get textStyleTable(): McDbTextStyleTable

Get Text Style Sheet

Returns ​

McDbTextStyleTable


ucsMatrix ​

• get ucsMatrix(): McGeMatrix3d

Obtain the UCS user coordinate system transformation matrix

Returns ​

McGeMatrix3d

• set ucsMatrix(val): void

Set UCS user coordinate system transformation matrix

Parameters ​

NameType
valMcGeMatrix3d

Returns ​

void


zeroLayerId ​

• get zeroLayerId(): McObjectId

Return layer 0 ID

Returns ​

McObjectId

Layer 0 ID

Methods ​

CreateGroup ​

▸ CreateGroup(ids, name?): boolean

Create a group from a pile of objects

Parameters ​

NameTypeDefault value
idsMcObjectId[]undefined
namestring""

Returns ​

boolean

boolean

Example

ts
async function Mx_Test_CreateGroup(){
   let ss = new MxCADSelectionSet();
If (! Await ss. userSelect): ")) return;
   if (ss.count() == 0) return;
   let mxcad = MxCpp.getCurrentMxCAD();
   mxcad.getDatabase().CreateGroup(ss.getIds(),"GroupName");

   let idGroup = mxcad.database.GetGroupDictionary().getAt("GroupName");
   let group = idGroup.getMcDbGroup();
   if(group){
       console.log(group.getAllEntityId());
   }
}

GetGroupDictionary ​

▸ GetGroupDictionary(): McDbDictionary

Obtain the group dictionary object of the drawing.

Returns ​

McDbDictionary

McDbDictionary


SetCurrentlyTextStyleId ​

▸ SetCurrentlyTextStyleId(id): boolean

Set the current text style id

Parameters ​

NameType
idMcObjectId

Returns ​

boolean


getBlockTable ​

▸ getBlockTable(): McDbBlockTable

Retrieve the block table.

Returns ​

McDbBlockTable

Block table.

Example

ts
  import { MxCpp, McDbDatabase } from "mxcad"
  let mxcad = MxCpp.App.getCurrentMxCAD();
  let blockTable = mxcad.getDatabase().getBlockTable();
  let aryId = blockTable.getAllRecordId();
  aryId.forEach((id) => {
      let blkRec = id.getMcDbBlockTableRecord();
      if (blkRec === null) return;
      console.log(blkRec);
      console.log("blkRec.name:" + blkRec.name);
      console.log("blkRec.origin:" + blkRec.origin);
  });

getCurrentlyColorIndex ​

▸ getCurrentlyColorIndex(): number

Retrieve the current color index.

Returns ​

number

Current Color Index (ColorIndexType).


getCurrentlyDimStyleId ​

▸ getCurrentlyDimStyleId(): McObjectId

Return the current annotation style id

Returns ​

McObjectId


getCurrentlyDrawColor ​

▸ getCurrentlyDrawColor(): Color

Get the color used in the current drawing

Returns ​

Color


getCurrentlyLayerId ​

▸ getCurrentlyLayerId(): McObjectId

Return the current layer ID

Returns ​

McObjectId


getCurrentlyLayerName ​

▸ getCurrentlyLayerName(): string

Get the current layer name.

Returns ​

string

Current layer name.


getCurrentlyLineTypeName ​

▸ getCurrentlyLineTypeName(): string

Get the current line type name.

Returns ​

string

The current line type name.


getCurrentlyLineTypeScale ​

▸ getCurrentlyLineTypeScale(): number

Get the current line type scale.

Returns ​

number

Current line type scale.


getCurrentlyLineWeight ​

▸ getCurrentlyLineWeight(): LineWeight

Return the current line weight used

Returns ​

LineWeight


getCurrentlyLinetypeId ​

▸ getCurrentlyLinetypeId(): McObjectId

Return the current line type ID

Returns ​

McObjectId


getCurrentlyTextStyleId ​

▸ getCurrentlyTextStyleId(): McObjectId

Return the current text style id

Returns ​

McObjectId


getCurrentlyTextStyleName ​

▸ getCurrentlyTextStyleName(): string

Get the current text style name.

Returns ​

string

The current text style name.


getCurrentlyTrueColor ​

▸ getCurrentlyTrueColor(): McCmColor

Retrieve the current CAD color.

Returns ​

McCmColor

Current CAD color.


getDimStyleTable ​

▸ getDimStyleTable(): McDbDimStyleTable

Retrieve the annotation style sheet.

Returns ​

McDbDimStyleTable

Linear table.


getEntitiesInTheGroup ​

▸ getEntitiesInTheGroup(id): McObjectId[]

Get all object IDs in the group where the object is located

Parameters ​

NameType
idMcObjectId

Returns ​

McObjectId[]

McDbDictionary


getImp ​

▸ getImp(): any

Retrieve internal implementation objects.

Returns ​

any

Internal implementation object.

Inherited from ​

McRxObject.getImp


getJson ​

▸ getJson(): string

Retrieve a string in JSON format.

Returns ​

string

A string in JSON format.

Inherited from ​

McRxObject.getJson


getLayerTable ​

▸ getLayerTable(): McDbLayerTable

Retrieve the layer table.

Returns ​

McDbLayerTable

Layer table.

Example

ts
  import { MxCADUiPrEntity, MxCpp } from "mxcad";
//Hide the layer where the target object is located
  async function Mx_SelectEntitHideLayer() {
     let selEntity1 = new MxCADUiPrEntity();

SelElement1.setMessage ("Select Objects to Hide")
     let id = await selEntity1.go();
     if (!id.isValid()) return;

     let ent = id.getMcDbEntity();
     let mxcad = MxCpp.getCurrentMxCAD();
     let layerTable = mxcad.getDatabase().getLayerTable();
     let layerId = layerTable.get(ent.layer);
     let layerRec = layerId.getMcDbLayerTableRecord();
     if (layerRec === null) return;
     layerRec.isOff = true;
     mxcad.updateLayerDisplayStatus();
     mxcad.updateDisplay()
   }

getLinetypeTable ​

▸ getLinetypeTable(): McDbLinetypeTable

Retrieve the line type table.

Returns ​

McDbLinetypeTable

Linear table.

Example

ts
   import { MxCpp } from "mxcad"
//Get all line types
   let mxcad = MxCpp.App.getCurrentMxCAD();
   let linetypeTable = mxcad.getDatabase().getLinetypeTable();
   let aryId = linetypeTable.getAllRecordId();
   aryId.forEach((id) => {
       let linetypeRec = id.getMcDbLinetypeTableRecord();
       if (linetypeRec === null) return;
       console.log(linetypeRec);
   });

getNamedObjectsDictionary ​

▸ getNamedObjectsDictionary(): McDbDictionary

Obtain the dictionary object of the drawing.

Returns ​

McDbDictionary

McDbDictionary

Example

ts
  import { MxCpp, McDbDatabase } from "mxcad"
//Write extension records
  let mxcad = MxCpp.getCurrentMxCAD();
  let dict = mxcad.getDatabase().getNamedObjectsDictionary();

  let sName = "MyDict";
  let idDict = dict.getAt(sName);
  if (idDict.isNull()) {
      let newDict = new McDbDictionary;
      idDict = dict.addObject(sName, newDict);
   }
   let myDict = idDict.getMcDbDictionary();
   if (myDict) {

       let xrec = new McDbXrecord();
       let data = new MxCADResbuf();
       data.AddString("TestData");
       xrec.setData(data);
       myDict.addObject("MyRecord", xrec);
       console.log("write xrecord ok");
   }

getTextStyleTable ​

▸ getTextStyleTable(): McDbTextStyleTable

Retrieve the text style sheet.

Returns ​

McDbTextStyleTable

Text style sheet.

Example

ts
//Get all text styles
 import { MxCpp } from "mxcad"
 let mxcad = MxCpp.getCurrentMxCAD();
 let textSyleTable = mxcad.getDatabase().getTextStyleTable();
 let aryId = textSyleTable.getAllRecordId();
 aryId.forEach((id) => {
     let textSyleRec = id.getMcDbTextStyleTableRecord();
     if (textSyleRec === null) return;
     let out: any = {};
     out.name = textSyleRec.name;
     out.font = textSyleRec.font();
     out.fileName = textSyleRec.fileName;
     out.bigFontFileName = textSyleRec.bigFontFileName;
     out.textSize = textSyleRec.textSize;
     out.xScale = textSyleRec.xScale;
     console.log(out);
 });

handleToIdIndex ​

▸ handleToIdIndex(sHandle): McObjectId

Return the object ID based on the handle of the object

Parameters ​

NameType
sHandlestring

Returns ​

McObjectId

Example

ts
import { MxCpp, McDbDatabase } from "mxcad"

const dataBase:McDbDatabase = MxCpp.getCurrentMxCAD().getDatabase();
//Pass in the handle and obtain the corresponding object ID
const handle = "ABCD1234"; //  Assuming this is a valid handle
const objectId = dataBase.handleToIdIndex(handle);
//Ensure that the obtained object ID is valid
if (objectId) {
  console.log("success", objectId);
} else {
  console.log("error")
}

initTempObject ​

▸ initTempObject(imp): void

Initialize temporary objects.

Parameters ​

NameTypeDescription
'imp''any'Internal implementation object

Returns ​

void

Inherited from ​

McRxObject.initTempObject


insert ​

▸ insert(database, sBlkName): McObjectId

Insert block

Parameters ​

NameType
databaseMcDbDatabase
sBlkNamestring

Returns ​

McObjectId


isKindOf ​

▸ isKindOf(sObjectName): boolean

Determine object type

Parameters ​

NameTypeDescription
SOrtNamestringType Name

Returns ​

boolean

Return whether the object is of the target type

Inherited from ​

McRxObject.isKindOf


isModifyed ​

▸ isModifyed(): boolean

Has the current database been modified

Returns ​

boolean

Return Boolean value

Example

ts
import { MxCpp, McDbDatabase } from "mxcad"

const dataBase:McDbDatabase = MxCpp.getCurrentMxCAD().getDatabase();
const res = dataBase.isModifyed();
if(res){
Console.log ("Modified")
}else{
Console.log ("unmodified")
}

isNull ​

▸ isNull(): any

Determine if it is an empty object

Returns ​

any

Inherited from ​

McRxObject.isNull


readFile ​

▸ readFile(sFilePath): boolean

Read files.

Parameters ​

NameType
sFilePathstring

Returns ​

boolean


resetModificationStatus ​

▸ resetModificationStatus(): void

Reset the current database modification status to unmodified status

Returns ​

void


saveAs ​

▸ saveAs(sFilePath, sJsonParam?): boolean

Save the file.

Parameters ​

NameTypeDefault value
sFilePathstringundefined
sJsonParamstring""

Returns ​

boolean


setCurrentlyColorIndex ​

▸ setCurrentlyColorIndex(colorIndex): any

Set the current color index (ColorIndexType).

Parameters ​

NameTypeDescription
ColorIndexNumberColor Index

Returns ​

any


setCurrentlyDimStyleId ​

▸ setCurrentlyDimStyleId(id): boolean

Set the current annotation style id

Parameters ​

NameType
idMcObjectId

Returns ​

boolean


setCurrentlyLayerId ​

▸ setCurrentlyLayerId(id): boolean

Set the current annotation style id

Parameters ​

NameType
idMcObjectId

Returns ​

boolean


setCurrentlyLayerName ​

▸ setCurrentlyLayerName(sName): void

Set the current layer name.

Parameters ​

NameTypeDescription
SNamestringlayer name

Returns ​

void


setCurrentlyLineTypeName ​

▸ setCurrentlyLineTypeName(sName): void

Set the current line type name.

Parameters ​

NameTypeDescription
SNamestringLine type name

Returns ​

void


setCurrentlyLineTypeScale ​

▸ setCurrentlyLineTypeScale(val): number

Set the current line type scale.

Parameters ​

NameTypeDescription
ValnumberLine proportion

Returns ​

number

Current line type scale.


setCurrentlyLineWeight ​

▸ setCurrentlyLineWeight(lineWeight): void

Set the current line weight used

Parameters ​

NameType
lineWeightLineWeight

Returns ​

void


setCurrentlyLinetypeId ​

▸ setCurrentlyLinetypeId(id): boolean

Set the current annotation style id

Parameters ​

NameType
idMcObjectId

Returns ​

boolean


setCurrentlyTextStyle ​

▸ setCurrentlyTextStyle(sName): void

Set the current text style name.

Parameters ​

NameTypeDescription
SNamestringText style name

Returns ​

void


setCurrentlyTrueColor ​

▸ setCurrentlyTrueColor(color): any

Set the current CAD color.

Parameters ​

NameTypeDescription
Color[McCMColor] (2d. McCmColor. md)CAD color

Returns ​

any


setJson ​

▸ setJson(str): boolean

Set a string in JSON format.

Parameters ​

NameTypeDescription
StrstringJSON formatted string

Returns ​

boolean

Is the setting successful.

Inherited from ​

McRxObject.setJson


wblock ​

▸ wblock(database, aryId, ptBase?, param?): boolean

Write a block

Parameters ​

NameType
databaseMcDbDatabase
aryIdMcObjectId[]
ptBase?McGePoint3d
param?object

Returns ​

boolean