Drawings SDK Developer Guide > Working with .dwg Files > Working with Databases > Working with Database Containers > Working with Dictionaries of Objects > Basics of Dictionaries > Creating the Database Dictionary
Creating the Database Dictionary

Similar to other objects, a dictionary is an object of the database. To create a dictionary, declare a variable of the OdDbDictionaryPtr type and use the pseudo-constructor of the dictionary object. The pseudo-constructor is the static method that creates a new dynamic instance of a dictionary and returns the smart pointer to it. For example:


OdDbDictionaryPtr pDict = OdDbDictionary::createObject();

The created instance exists independently from the database. Multiple properties of the created dictionary object are invalid initially and its instance does not get an ID until the dictionary is added in the database. The database cannot manipulate the new dictionary without the OdDbObjectId instance associated with the created dictionary object. Objects added in the dictionary must get the Object ID of the dictionary as an ID of the owner object, so if the owner of an object is invalid, adding objects in the dictionary is invalid. Therefore, after creating, the program must add the new dictionary in the database, which is the same as with other objects. A program can add the new dictionary in the root drawing dictionary, extension dictionary, or another existing dictionary object (tree-shape hierarchical structure).

To add a new dictionary in the database, use the addOdDbObject() method of the database object. This method requires a pointer to the database object, assigns a new handle for this object, associates it with an ID, and returns an OdDbObjectId instance associated with the added object. In the following examples, the pDb variable stores a pointer to the database object. For example:


OdDbObjectId idObj = pDb->addOdDbObject(pDict);

odPrintConsoleString(L"\nNew dictionary gets handle = %s", idObj.getHandle().ascii());

A new dictionary can be added in the root drawing dictionary of the database, which exists initially in the drawing and stores sub-dictionaries. The root drawing dictionary associates a unique name with each sub-dictionary. To add a new dictionary in the root, the program needs to get the smart pointer to the root drawing dictionary, assign a unique name for the new dictionary, check whether the name is absent from the root dictionary, and put the new dictionary object with its assigned name in the root drawing dictionary. To get the root drawing dictionary, use the getNamedObjectsDictionaryId() method of the database object. To open the root dictionary in write mode, use the safeOpenObject() method of the Object ID with the argument OdDb::kForWrite. To check the name, use the has() method of the dictionary object. To put the new dictionary in the root dictionary, use the setAt() method of the dictionary object. For example:


OdDbDictionaryPtr pRoot = pDb->getNamedObjectsDictionaryId().safeOpenObject(OdDb::kForWrite);

if(!pRoot->has("MY_DICT"))
{
  OdDbObjectId idDict = pRoot->setAt("MY_DICT", pDict);

  odPrintConsoleString(L"\n\"MY_DICT\" dictionary gets handle = %s", idDict.getHandle().ascii());
}
else odPrintConsoleString(L"\n\"MY_DICT\" dictionary already exists");

Any object of the database can have an extension dictionary for storing individual data and attached custom objects. The object can use this data for rendering a device, converting between different versions of a file format, or exporting to external applications. The extension dictionary stores data as X-records. To create an extension dictionary, use the createExtensionDictionary() method of the object. To get the ID of the extension dictionary, use the extensionDictionary() method of the object. For example, to create an extension dictionary for layer zero:


OdDbLayerTableRecordPtr pZLayer = pDb->getLayerZeroId().safeOpenObject(OdDb::kForWrite);

pZLayer->createExtensionDictionary();

OdDbObjectId idZDict = pZLayer->extensionDictionary();

odPrintConsoleString(L"\nExtension dictionary of Zero layer has handle = %s", idZDict.getHandle().ascii());

See Also

Working with Dictionaries of Objects

Adding, Naming, and Removing Objects

Getting and Checking Objects and Names

Root Drawing Dictionary

Examples of Using the Record–Table and Dictionary Interfaces

Copyright © 2002 – 2021. Open Design Alliance. All rights reserved.