Skip to content
On this page

Dictionary

In the CAD drawing there is a global dictionary object McDbDictionary(), used to store the global information in the drawing, developers can store their own global data by storing extended records McDbXrecord() in the dictionary, the dictionary has built in some data, such as: Groups, multi-line styles, layout information, picture definitions, etc.

Click on dictionary object McDbDictionary(), extension record McDbXrecord() to check the detailed description attributes and methods.

Walk through the dictionary

The dictionary can contain a dictionary (nested store), or it can store an extended record McDbXrecord(). We can recursively iterate through all the data in the dictionary.

ts
import { McDbDictionary, McDbXrecord } from "mxcad"
//Iterate over the recursive functions used by the dictionary
function MxTest_DictionaryData(dict: McDbDictionary) {
  // Gets all the object names in the dictionary
  let aryName = dict.getAllObjectName();
  // Go to each dictionary and fetch the name.
  aryName.forEach((name) => {
    console.log(name);
    let id = dict.getAt(name);
    let obj = id.getMcDbObject();
    if (obj instanceof McDbDictionary) {
      let dict: McDbDictionary = obj;
      console.log(dict);
      MxTest_DictionaryData(dict);
    }
    else if (obj instanceof McDbXrecord) {
      let xrec: McDbXrecord = obj;
      let data = xrec.getData()
      data.PrintData();
    }
  })
}

function MxTest_GetNamedObjectsDictionary() {
  // Get the current cad object
  let mxcad = MxCpp.getCurrentMxCAD();
  // Get the global dictionary
  let dict = mxcad.getDatabase().getNamedObjectsDictionary();
  // Gets all the object names in the dictionary
  let aryName = dict.getAllObjectName();
  // Go to each dictionary and fetch the name.
  aryName.forEach((name) => {
    console.log(name);
    let id = dict.getAt(name);
    let obj = id.getMcDbObject();
    if (obj instanceof McDbDictionary) {
      let dict: McDbDictionary = obj;
      console.log(dict);
      MxTest_DictionaryData(dict);
    }
  })
}
// Invoke the traversal dictionary method
MxTest_GetNamedObjectsDictionary()

Write extension record

We can write custom global data to DWG graphs through extended logging. Write extension record the basic steps of first instance through mxcad object call getDatabase() method gets data object, then call getNamedObjectsDictionary() method to get the drawing of a dictionary object, Finally, the addObject() method of the dictionary object is called to put a custom dictionary in the object, so that you can write an extended record data in the dictionary.

ts
import { MxCpp, McDbDictionary, McDbXrecord, MxCADResbuf } from "mxcad"
// Get the current cad object
let mxcad = MxCpp.getCurrentMxCAD();
// Get the global dictionary
let dict = mxcad.getDatabase().getNamedObjectsDictionary();

// Get a user-defined dictionary. sName is the name of the dictionary
let sName = "MyDict";
let idDict = dict.getAt(sName);
if (idDict.isNull()) {
    // If not, add a dictionary.
    let newDict = new McDbDictionary;
    idDict = dict.addObject(sName, newDict);
}
let myDict = idDict.getMcDbDictionary();
if (myDict) {
    // Create an extension record
    let xrec = new McDbXrecord();
    // Create a linked data list
    let data = new MxCADResbuf();
    // Set the value
    data.AddString("TestData");
    // Set the data in the extension record
    xrec.setData(data);
    // Dictionary writes extended data
    myDict.addObject("MyRecord", xrec);
    console.log("write xrecord ok");
}

Read extended data

We can get the extended data of the entity object by calling its getxData() method.

ts
import { MxCADUiPrEntity,  } from "mxcad"

  // Read extended data.
  async function MxTest_ReadxData() {
  // Select the target object
  let selEntity = new MxCADUiPrEntity();
  selEntity.setMessage(" Select object ");
  let id = await selEntity.go();
  if (! id.isValid()) return;
  // Get the entity object
  let ent = id.getMcDbEntity();
  if (ent === null) return;
  // Get a linked list of the data in the record.
  let data = ent.getxData();
  data.forEach((val, type, dxf) => {
    console.log(JSON.stringify({ val: val, type: type, dxf: dxf }));
  })
  
  //let data = ent.getxDataString("DataName");
  //MxFun.acutPrintf(data.val + "\n");
}

Delete extended data

We can delete extended data in an entity object by calling its deleteXData() method.

ts
ent.deletexData("Target extended data name")