Skip to content
On this page

Layer table

In the DWG database, layers are stored in McDbLayerTable(), and each record in the layer table is called the layer table record object McDbLayerTableRecord(). Each layer table record object corresponds to a layer, and you can set the color, line, close/open, freeze and other properties, which DWG database default always has a "0" layer, the layer can not be deleted.

We can get the current control by calling MxCpp.getCurrentMxCAD() in mxcad and then call the getDatabase() method of the control instance to get the database instance McDbDatabase(). By calling the getLayerTable() method in this database instance, we get the tier table McDbLayerTable().

Click on the database layer table McDbLayerTable(), the layer table record object McDbLayerTableRecord, the database instance McDbDatabase() to check the detailed description attributes and methods.

ts
import { MxCpp} from "mxcad"
// Get current control
const mxcad = MxCpp.getCurrentMxCAD();
// Get the database layer table of the current control
const layerTable = mxcad.getDatabase().getLayerTable();

Current layer

The current layer represents the default layer of the DWG database. When an object is added to the database, it is placed on this layer by default. The property is stored with the diagram. We can get the name of the current layer by calling the getCurrentlyLayerName() method in the database instance McDbDatabase() object and the getCurrentlyLayerId() method to get the layer id.

Click on McDbDatabase getCurrentlyLayerId() View detailed property and method descriptions.

ts
import { MxCpp} from "mxcad" 

const mxcad = MxCpp.getCurrentMxCAD();
const layerName = mxcad.getDatabase().getCurrentlyLayerName();
const layerId = mxcad.getDatabase().getCurrentlyLayerId();
console.log("Current layer",layerName, layerId);

Add layer

We can add a custom layer by calling the addLayer() method in the mxcad instance object and setting the drawLayer property to set the added layer to the drawn layer.

Click McObject addLayer() to check the properties and methods in detail.

ts
import { McApp } from "mxcad"

const mxcad = McApp.getCurrentMxCAD()
mxcad.addLayer(" Layer Name ")
mxcad.drawLayer = "Layer name"

We can also instantiate a layer table record object McDbLayerTableRecord(), set the layer color, line, close/open, freeze and other properties, and then call the add() method to add to the layer table.

ts
import { McCmColor, MxCpp, McDbLayerTableRecord, McDb } from "mxcad"

// Get the current control
const mxcad = MxCpp.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 = "layer name"
// Get the database layer table of the current control
const layerTable = mxcad.getDatabase().getLayerTable();
// Adding a layer data object to the layer table yields an object ID that identifies the layer data
const objId = layerTable.add(layer)
// Update the display
mxcad.updateDisplay()

Go through all layers

We can get all layer ids by calling the getAllRecordId() method in the database layer table McDbLayerTable(), Then call getMcDbLayerTableRecord() method to return the layer table record object McDbLayerTableRecord() to get all the layer data.

ts
import { MxCpp } from "mxcad"

let layerTable = MxCpp.getCurrentMxCAD().getDatabase().getLayerTable();
let aryId = layerTable.getAllRecordId();
aryId.forEach((id) => {
    let layerRec = id.getMcDbLayerTableRecord();
    if (layerRec === null) return;
    console.log(layerRec);
    console.log("layerRec.color:" + layerRec.color.getColorString());
    console.log("layerRec.name:" + layerRec.name);
});

Delete the layer

After we get the target layer table record object McDbLayerTableRecord, we can call the erase() method of the object instance to delete the object.

ts
import { MxCpp } from "mxcad"

let layerTable = MxCpp.getCurrentMxCAD().getDatabase().getLayerTable()
let layerId = layerTable.get(" layer name ")
let layerRec = layerId.getMcDbLayerTableRecord()
layerRec.erase()
// Update the display
mxcad.updateDisplay()

In addition, layers can be removed by JSON serialization and deserialization of the layer table.

ts
import { McCmColor, MxCpp, McDbLayerTableRecord, McDb } from "mxcad"

const layerTable = mxcad.getDatabase().getLayerTable();
const layerJsonString = layerTable.getJson()
const layerJson = JSON.parse(layerJsonString)
// Just keep 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, we can use the has method to check whether the layer exists and the get method to get the corresponding object ID
console.log("testLayer1", layerTable.has("testLayer1"))
console.log("testLayer1", layerTable.get("testLayer1"))

Tip

Before deleting the layer, it is best to determine whether there is a CAD drawing object on the layer to avoid deleting the content of the layer by mistake.

Modify the layer

The basic operation of layer modification in mxcad is to obtain the database layer table object, and then obtain the layer table record object according to the layer name, and set the corresponding attribute value of the layer table record. Let the user select an object on the CAD drawing, and then manipulate the layer where the object is:

  1. Get the target layer
ts
import { MxCADSelectionSet, MxCADUiPrPoint } from "mxcad"

const ss = new MxCADSelectionSet();
const getPoint = new MxCADUiPrPoint()
getPoint.setMessage("Please select the target object")
const point = await getPoint.go()
if (!point) return
const index = ss.pointSelect(point.x, point.y)
const ent = ss.item(index).getMcDbEntity()
if(!ent) return
const layerId = ent.layerId
let layerRec = layerId.getMcDbLayerTableRecord()
console.log(layerRec)
  1. Get or set whether the layer is off: The layer entity on which the layer is off cannot participate in editing operations.
ts
const offVal = layerRec.isOff
// Close the layer
layerRec.isOff = true
// Update the display
mxcad.updateDisplay()
  1. Get or set whether the layer is frozen: the frozen layer entity does not participate in the generation of display data, which can speed up the display, and the speed of opening the drawing, compared with the closed property, the frozen layer is not only invisible, and does not participate in the memory display.
ts
const frozenVal = layerRec.isFrozen
// Freeze the layer
layerRec.isFrozen = true
// Update the display
mxcad.updateDisplay()
  1. Get or set whether the layer is locked: the entities on the locked layer cannot be edited, but can be selected.
ts
const lockVal = layerRec.isLocked
// Lock the layer
layerRec.isLocked = true
// Update the display
mxcad.updateDisplay()
  1. Get or set the layer color.
ts
const colorVal = layerRec.color
// Set the layer color to red
layerRec.color = new McCmColor(255,0,0)
// Update the display
mxcad.updateDisplay()

Get all objects on the layer

We get objects by constructing the selection set MxCADSelectionSet, and use the MxCADResbuf filter to set the layer name filter to get all objects on a certain layer.

tap MxCADSelectionSet , [MxCADResbuf] (https://www.mxdraw3d.com/mxcad_docs/api/classes/2d.MxCADResbuf.html#class-mxcadresbuf) to check the detailed description attributes and methods.

ts
import { MxCADResbuf, MxCADSelectionSet, MxCpp } from "mxcad"

// Create a filtered data link object
const filter = new MxCADResbuf();
// Add the layer name to the filter, 8 is the DXF group code, it represents layerName means yes is a layer name.
const layerName = "0";
filter.AddString(layerName, 8);
// Define a selection set object
let ss = new MxCADSelectionSet();
// Select all objects on the graph
ss.allSelect(filter);
// Go through all objects and set the object highlight
ss.forEach(id=>{
    const ent = id.getMcDbEntity();
    ent.highlight(true);
});
MxCpp.getCurrentMxCAD().updateDisplay();

Set the layer where the CAD entity object is located

Each entity object has a layer property, which can be directly modified to set the layer on which the object resides.

Click McDbEntity layer to check the properties and methods in detail.

ts
import { MxCpp, MxCADUiPrEntity } from "mxcad";
// Modify object layer
async function Mx_Test_ChangeEntityLayer() {
  let newLayerName = "WALL";
  let mxcad = MxCpp.getCurrentMxCAD();
  // Determines if the target layer already exists in the layer table "WALL"
  if (!mxcad.database.layerTable.has(newLayerName)) {
    console.log("no have layer:" + newLayerName)
  }
  // Get target object
  let getEntity = new MxCADUiPrEntity();
  getEntity.setMessage("Target object");
  let id = await getEntity.go();
  if (!id.isValid()) return;

  let ent = id.getMcDbEntity();
  if (!ent) return;
  // Set object layer
  ent.layer = newLayerName;
}