[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:
- Obtain the database instance through MxCpp. getCurrentMxCAD(). getData();
- Call methods such as getLayerTable(), getBlockTable(), getTextCtyleTable() to access various types of tables;
- Use setCurrentlyLayerName(), setCurrentlyTrueColor(), setCurrentlyTextCtyle(), and other settings to set the current drawing state;
- 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
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
↳
McDbDatabase
Table of contents
Constructors
Properties
Accessors
- blockTable
- currentSpace
- currentSpaceId
- dimStyleTable
- dxf0
- isCurrentModelSpace
- layerTable
- linetypeTable
- modelSpaceId
- objectName
- textStyleTable
- ucsMatrix
- zeroLayerId
Methods
- CreateGroup
- GetGroupDictionary
- SetCurrentlyTextStyleId
- getBlockTable
- getCurrentlyColorIndex
- getCurrentlyDimStyleId
- getCurrentlyDrawColor
- getCurrentlyLayerId
- getCurrentlyLayerName
- getCurrentlyLineTypeName
- getCurrentlyLineTypeScale
- getCurrentlyLineWeight
- getCurrentlyLinetypeId
- getCurrentlyTextStyleId
- getCurrentlyTextStyleName
- getCurrentlyTrueColor
- getDimStyleTable
- getEntitiesInTheGroup
- getImp
- getJson
- getLayerTable
- getLinetypeTable
- getNamedObjectsDictionary
- getTextStyleTable
- handleToIdIndex
- initTempObject
- insert
- isKindOf
- isModifyed
- isNull
- readFile
- resetModificationStatus
- saveAs
- setCurrentlyColorIndex
- setCurrentlyDimStyleId
- setCurrentlyLayerId
- setCurrentlyLayerName
- setCurrentlyLineTypeName
- setCurrentlyLineTypeScale
- setCurrentlyLineWeight
- setCurrentlyLinetypeId
- setCurrentlyTextStyle
- setCurrentlyTrueColor
- setJson
- wblock
Constructors
constructor
• new McDbDatabase(imp?)
Constructor.
Parameters
| Name | Type | Description |
|---|---|---|
imp? | Any | Internal implementation object |
Overrides
Properties
imp
• imp: any = 0
Internal implementation object.
Inherited from
Accessors
blockTable
• get blockTable(): McDbBlockTable
Get block table
Returns
currentSpace
• get currentSpace(): McDbBlockTableRecord
Return the current space block table record
Returns
Block table record
Example
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
Block table record ID
dimStyleTable
• get dimStyleTable(): McDbDimStyleTable
Retrieve the annotation style sheet.
Returns
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
linetypeTable
• get linetypeTable(): McDbLinetypeTable
Get line type table
Returns
modelSpaceId
• get modelSpaceId(): McObjectId
Return the model space block table record ID
Returns
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
ucsMatrix
• get ucsMatrix(): McGeMatrix3d
Obtain the UCS user coordinate system transformation matrix
Returns
• set ucsMatrix(val): void
Set UCS user coordinate system transformation matrix
Parameters
| Name | Type |
|---|---|
val | McGeMatrix3d |
Returns
void
zeroLayerId
• get zeroLayerId(): McObjectId
Return layer 0 ID
Returns
Layer 0 ID
Methods
CreateGroup
▸ CreateGroup(ids, name?): boolean
Create a group from a pile of objects
Parameters
| Name | Type | Default value |
|---|---|---|
ids | McObjectId[] | undefined |
name | string | "" |
Returns
boolean
boolean
Example
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
SetCurrentlyTextStyleId
▸ SetCurrentlyTextStyleId(id): boolean
Set the current text style id
Parameters
| Name | Type |
|---|---|
id | McObjectId |
Returns
boolean
getBlockTable
▸ getBlockTable(): McDbBlockTable
Retrieve the block table.
Returns
Block table.
Example
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
getCurrentlyDrawColor
▸ getCurrentlyDrawColor(): Color
Get the color used in the current drawing
Returns
Color
getCurrentlyLayerId
▸ getCurrentlyLayerId(): McObjectId
Return the current layer ID
Returns
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
getCurrentlyLinetypeId
▸ getCurrentlyLinetypeId(): McObjectId
Return the current line type ID
Returns
getCurrentlyTextStyleId
▸ getCurrentlyTextStyleId(): McObjectId
Return the current text style id
Returns
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
Current CAD color.
getDimStyleTable
▸ getDimStyleTable(): McDbDimStyleTable
Retrieve the annotation style sheet.
Returns
Linear table.
getEntitiesInTheGroup
▸ getEntitiesInTheGroup(id): McObjectId[]
Get all object IDs in the group where the object is located
Parameters
| Name | Type |
|---|---|
id | McObjectId |
Returns
McDbDictionary
getImp
▸ getImp(): any
Retrieve internal implementation objects.
Returns
any
Internal implementation object.
Inherited from
getJson
▸ getJson(): string
Retrieve a string in JSON format.
Returns
string
A string in JSON format.
Inherited from
getLayerTable
▸ getLayerTable(): McDbLayerTable
Retrieve the layer table.
Returns
Layer table.
Example
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
Linear table.
Example
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
Example
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
Text style sheet.
Example
//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
| Name | Type |
|---|---|
sHandle | string |
Returns
Example
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
| Name | Type | Description |
|---|---|---|
| 'imp' | 'any' | Internal implementation object |
Returns
void
Inherited from
insert
▸ insert(database, sBlkName): McObjectId
Insert block
Parameters
| Name | Type |
|---|---|
database | McDbDatabase |
sBlkName | string |
Returns
isKindOf
▸ isKindOf(sObjectName): boolean
Determine object type
Parameters
| Name | Type | Description |
|---|---|---|
| SOrtName | string | Type Name |
Returns
boolean
Return whether the object is of the target type
Inherited from
isModifyed
▸ isModifyed(): boolean
Has the current database been modified
Returns
boolean
Return Boolean value
Example
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
readFile
▸ readFile(sFilePath): boolean
Read files.
Parameters
| Name | Type |
|---|---|
sFilePath | string |
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
| Name | Type | Default value |
|---|---|---|
sFilePath | string | undefined |
sJsonParam | string | "" |
Returns
boolean
setCurrentlyColorIndex
▸ setCurrentlyColorIndex(colorIndex): any
Set the current color index (ColorIndexType).
Parameters
| Name | Type | Description |
|---|---|---|
| ColorIndex | Number | Color Index |
Returns
any
setCurrentlyDimStyleId
▸ setCurrentlyDimStyleId(id): boolean
Set the current annotation style id
Parameters
| Name | Type |
|---|---|
id | McObjectId |
Returns
boolean
setCurrentlyLayerId
▸ setCurrentlyLayerId(id): boolean
Set the current annotation style id
Parameters
| Name | Type |
|---|---|
id | McObjectId |
Returns
boolean
setCurrentlyLayerName
▸ setCurrentlyLayerName(sName): void
Set the current layer name.
Parameters
| Name | Type | Description |
|---|---|---|
| SName | string | layer name |
Returns
void
setCurrentlyLineTypeName
▸ setCurrentlyLineTypeName(sName): void
Set the current line type name.
Parameters
| Name | Type | Description |
|---|---|---|
| SName | string | Line type name |
Returns
void
setCurrentlyLineTypeScale
▸ setCurrentlyLineTypeScale(val): number
Set the current line type scale.
Parameters
| Name | Type | Description |
|---|---|---|
| Val | number | Line proportion |
Returns
number
Current line type scale.
setCurrentlyLineWeight
▸ setCurrentlyLineWeight(lineWeight): void
Set the current line weight used
Parameters
| Name | Type |
|---|---|
lineWeight | LineWeight |
Returns
void
setCurrentlyLinetypeId
▸ setCurrentlyLinetypeId(id): boolean
Set the current annotation style id
Parameters
| Name | Type |
|---|---|
id | McObjectId |
Returns
boolean
setCurrentlyTextStyle
▸ setCurrentlyTextStyle(sName): void
Set the current text style name.
Parameters
| Name | Type | Description |
|---|---|---|
| SName | string | Text style name |
Returns
void
setCurrentlyTrueColor
▸ setCurrentlyTrueColor(color): any
Set the current CAD color.
Parameters
| Name | Type | Description |
|---|---|---|
| Color | [McCMColor] (2d. McCmColor. md) | CAD color |
Returns
any
setJson
▸ setJson(str): boolean
Set a string in JSON format.
Parameters
| Name | Type | Description |
|---|---|---|
| Str | string | JSON formatted string |
Returns
boolean
Is the setting successful.
Inherited from
wblock
▸ wblock(database, aryId, ptBase?, param?): boolean
Write a block
Parameters
| Name | Type |
|---|---|
database | McDbDatabase |
aryId | McObjectId[] |
ptBase? | McGePoint3d |
param? | object |
Returns
boolean
