Drawings SDK Developer Guide > Working with .dwg Files > Working with Databases > Working with Database Containers > Working with Dictionaries of Objects > Basics of Dictionaries > Getting and Checking Objects and Names
Getting and Checking Objects and Names

The dictionary object has methods for getting and checking objects and names. In the following examples, the pDict variable stores a pointer to the dictionary object. For example, to add two objects:


OdDbColorPtr pObjectA = OdDbColor::createObject();
OdDbObjectId idObjectA = pDict->setAt(L"key_A", pObjectA);

OdDbColorPtr pObjectB = OdDbColor::createObject();
OdDbObjectId idObjectB = pDict->setAt(L"key_B", pObjectB);

Getting a name

To get the name of an object, use the nameAt() method which requires one argument — the OdDbObjectId instance associated with the specified object — and returns the name associated with this object in the dictionary as a value of the OdString type or returns an empty string when the object is not found. For example:


OdString sName = pDict->nameAt(idObjectA);

if(!sName.isEmpty())
  odPrintConsoleString(L"\nObject %s has the name: %s", idObjectA.getHandle().ascii(), sName.c_str());
else
  odPrintConsoleString(L"\nObject is not found);

Getting an object

To get an object of the dictionary, use the getAt() method which has two implementations. This method searches the object using the name and returns the object ID or pointer.

The first implementation of the getAt() method requires two arguments — the name associated with the object as a non-empty OdString value and also the mode for opening the object as a value of the OdDb::OpenMode enumerator (kForRead, kForWrite, or kForNotify) — and returns the smart pointer to the object as a value of the OdDbObjectPtr type if the object with the specified name exists in the dictionary, or it returns NULL if the name is not found or the object cannot be opened in the specified mode. For example:


// Open the object in the read mode
OdDbObjectPtr pObjA = pDict->getAt(L"key_A", OdDb::kForRead);

// Open the object in the write mode
OdDbObjectPtr pObjB = pDict->getAt(L"key_B", OdDb::kForWrite);

For example, to open the object and check the result:


OdString sName = L"key_X";
OdDbObjectPtr pObj = pDict->getAt(sName, OdDb::kForNotify);

if(!pObj.isNull())
  odPrintConsoleString(L"\nName \"%s\" is associated with Object: %s", sName.c_str(), pObj->handle().ascii());
else
  odPrintConsoleString(L"\nObject is not found");

The second implementation of the getAt() method requires two arguments — the name associated with the object as a non-empty OdString value and also the pointer to the variable of the OdResult type in which the method must save the result of this operation — and returns the OdDbObjectId instance associated with the obtained object of the dictionary if the object with the specified name exists in the dictionary, or it returns OdDb::kNull if the name is not found. The second argument is optional. For example:


OdDbObjectId idObjA = pDict->getAt(L"key_A");
odPrintConsoleString(L"\nObject: %s", idObjA.getHandle().ascii());

For example, to get the object and check the result:


OdResult result;
OdString sName = L"key_X";

OdDbObjectId idObj = pDict->getAt(sName, &result);

if(result)
  odPrintConsoleString(L"\nError: %d - %s", result, pDict->appServices()->getErrorDescription(result).c_str());
else
  odPrintConsoleString(L"\nName \"%s\" is associated with Object: %s", sName.c_str(), idObj.getHandle().ascii());

To get a smart pointer to the instance of the obtained object, use the safeOpenObject() method of the OdDbObjectId object; it opens the object associated with the specified ID in the specified mode for the specified erase status, and returns the smart pointer to the opened instance. For example:


OdDbObjectId idObjB = pDict->getAt(L"key_B");

if(!idObjB.isNull())
{
  OdDbObjectPtr pObjB = idObjB.safeOpenObject(OdDb::kForWrite);
  odPrintConsoleString(L"\nObjectB has handle = %s", pObjB->handle().ascii());
}
else
  odPrintConsoleString(L"\nObjectB is not found");

To check whether the name or object exists in the dictionary, use the has() method which has two implementations.

Checking a name

The first implementation of the has() method requires the name as an argument of the OdString type and returns True if the name exists in the dictionary or False when it is absent from the dictionary. For example:


odPrintConsoleString(L"\nObject "key_A\" %s in the dictionary", 
                          ((pDict->has(L"key_A")) ? L"exists" : L"absents"));                // true
odPrintConsoleString(L"\nObject "key_A\" %s in the dictionary", 
                          ((pDict->has(L"key_X")) ? L"exists" : L"absents"));                // false

Checking an object

The second implementation of the has() method requires the existing object ID as an argument of the OdDbObjectId type and returns True if the object exists in the dictionary or False when the object is absent from the dictionary. For example:


odPrintConsoleString(L"\nObject A %s in the dictionary", 
                          ((pDict->has(idObjectA)) ? L"exists" : L"absents"));               // true
odPrintConsoleString(L"\nGlobal material %s in the dictionary", 
                          ((pDict->has(pDb->globalMaterialId())) ? L"exists" : L"absents")); // false

Listing all objects

To display a list of all objects of the dictionary, use an iterator for traversing through objects. To create the iterator, use the newIterator() method of the dictionary object. To move the iterator, use the next() method of the iterator. To check whether the traverse is completed, use the done() method of the iterator. For example:


OdDbDictionaryIteratorPtr itDict = pDict->newIterator();

while(!itDict->done())
{
  odPrintConsoleString(L"\n%s-\"%s\"", itDict->objectId().getHandle().ascii(), itDict->name().c_str());
  itDict->next();
}

See Also

Working with Dictionaries of Objects

Adding, Naming, and Removing Objects

Iterating through Objects of Dictionaries

Example of Using the Dictionary Interface for Selecting Objects

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