Database
In mxcad, you can obtain various data from converted drawings (.mxweb format files) using the mxcad npm package, such as layers, text styles, blocks, and data within graphic objects, among others.
We often need to retrieve this data, make modifications to it, and then apply these modified data to the displayed drawings. mxcad provides a database specifically for handling this data.
How to Retrieve and Modify Data
Each open drawing has an instance of McDbDatabase, which is the database.
To obtain the current drawing's database, you can use the MxCpp.App.getCurrentMxCAD()
method to get the current control instance and then call the getDatabase()
method on that instance.
Within the database instance, you can access various tables through methods, and these tables provide ID objects McObjectId that serve as references to specific data. By calling the corresponding methods, you can retrieve the actual data.
Let's take layer data as an example:
import { McCmColor, MxCpp, createMxCad } from "mxcad"
// Get the current control instance
const mxcad = MxCpp.App.getCurrentMxCAD();
// Get the layer table from the database
const layerTable = mxcad.getDatabase().getLayerTable();
// Get ID objects for all layers
const aryId = layerTable.getAllRecordId();
const layers = aryId.map((id) => {
// Get the layer data object for the corresponding ID
let layerRec = id.getMcDbLayerTableRecord();
if (layerRec === null) return;
return layerRec
})
// Set the color of the first layer
const layer = layers[0]
if (!layer) return
const color = new McCmColor()
color.setRGB(255, 0, 0)
layer.color = color
// After updating the display, you will see that all graphics on layer 0 with color-by-layer are now red
mxcad.updateDisplay()
As you can see, once we have obtained the data and modified the color property, the graphics on the drawing that belong to that layer will turn red when the display is updated.
Graphic Data
Any class instance that inherits from McDbObject is considered data within the database. Graphic data includes instances that inherit from McDbEntity.
In Parametric Interactive Drawing, all graphics like McDbText, McDbLine, and others are representations of corresponding data within the database. After instantiation, they become graphic data. By modifying the properties of these objects, you can make changes to the displayed drawing.
In Selecting Graphics, there is a code example that intuitively demonstrates the characteristics of graphic data.
Usually, database data is accessed through objects of type McObjectId.
Adding and Deleting Layer Data
Here's an example of how to add, delete, and modify layer data:
import { McCmColor, MxCpp, McDbLayerTableRecord, McDb } from "mxcad"
// Get the current control instance
const mxcad = MxCpp.App.getCurrentMxCAD();
// Instantiate a layer data object and set some properties for this layer
const layer = new McDbLayerTableRecord()
layer.color = new McCmColor(0, 0, 0)
layer.isFrozen = true
layer.isLocked = true
layer.isOff = true
layer.lineWeight = McDb.LineWeight.kLnWt018
layer.name = "testLayer1"
// Get the layer table from the current control's database
const layerTable = mxcad.getDatabase().getLayerTable();
// Adding the layer data object to the layer table will return an object ID that identifies this layer data
const objId = layerTable.add(layer)
// You can perform bulk add, delete, and modify operations on some layers by serializing and deserializing the layer table using JSON
const layerJsonString = layerTable.getJson()
const layerJson = JSON.parse(layerJsonString)
// Here, we keep only the layers with the following names
const keepLayerNames = ["0", "排水", "testLayer1"]
const keepLayers = layerJson.filter((layerJsonObj) => {
return keepLayerNames.includes(layerJsonObj.name)
})
const keepLayersJsonString = JSON.stringify(keepLayers)
layerTable.setJson(keepLayersJsonString)
// Finally, you can use the `has` method to check if a layer exists and the `get` method to retrieve the corresponding object ID by providing the layer name
console.log("Is layer '0' present:", layerTable.has("0"))
console.log("Object ID for layer '0':", layerTable.get("0"))
There are also other data tables such as McDbLinetypeTable for linetypes, McDbTextStyleTable for text styles, McDbBlockTable for blocks, and more. They are used in a similar way to the layer table. For more information, you can click on the respective names to learn about their specific properties and methods.
These tables have similar methods for performing operations, and the specific details are not covered here. You can refer to the respective methods for more information.