Drawings SDK Developer Guide > Working with .dwg Files > Working with Databases > Working with Database Containers > Working with Dictionaries of Objects > Basics of Dictionaries > Example of Selecting a Dictionary from the Root Drawing Dictionary
Example of Selecting a Dictionary from the Root Drawing Dictionary

This example demonstrates selecting a dictionary object from the root drawing dictionary of the database object. The SelectDatabaseDictionary() function requires one argument — the pointer to the database object from which the dictionary must be obtained — and returns the OdDbObjectId instance associated with the dictionary object selected by the user or OdDb::kNull if the user cancels selection. This function is used in the documented examples that require selection of a dictionary.

The SelectDatabaseDictionary() function gets the Object ID of the root drawing dictionary of the specified database object using the getNamedObjectsDictionaryId() method, opens it in read mode using the safeOpenObject() method, and saves the smart pointer to it. Then the function declares the itItem variable as an iterator for traversing through sub-dictionaries of the root dictionary and the keyword variable as an array of wide-characters for entering the sub-dictionary name.

The SelectDatabaseDictionary() function organizes the loop in which it displays the list of sub-dictionaries of the root drawing dictionary, inquires aboutthe dictionary name to be selected, and checks whether the entered name exists in the root drawing dictionary. To display the list of sub-dictionaries, the function creates the iterator using the newIterator() method, moves the iterator to the next sub-dictionary using the next() method, checks whether the traversing is complete using the done() method, obtains the handle of the current sub-dictionary using the objectId() and getHandle() methods, and gets the sub-dictionary name using the name() method. Then the function inquires about the sub-dictionary name to be selected. After user entry, the function checks whether the entered name exists in the root drawing dictionary using the has() method of the dictionary object, which returns True when the specified name exists in the dictionary or False when the name is not found. If the entered name exists in the root dictionary, the function gets the Object ID associated with name using the getAt() method and returns the Object ID of the selected sub-dictionary to the calling function. If the entered name is not found in the root dictionary, the function displays an error message and performs the loop to display the list of sub-dictionaries and inquire about the name. If the entered name is colon ':', the function cancels entry, breaks the loop, and returns the OdDb::kNull instance.

The SelectDatabaseDictionary() function has the following implementation:


OdDbObjectId SelectDatabaseDictionary(OdDbDatabase* pDb)
{
  OdDbDictionaryPtr pRoot = pDb->getNamedObjectsDictionaryId().safeOpenObject(); 
  OdDbDictionaryIteratorPtr itItem; 
  wchar_t keyword[64];

  do {
    wcout << L"\nThe root dictionary of dictionaries:";
    for(itItem = pRoot->newIterator() ; !itItem->done() ; itItem->next())
    {
      wcout << L"\n" << itItem->objectId().getHandle().ascii() 
            << L"-\"" << itItem->name() << L"\"";
    }
    itItem = NULL;

    wcout << L"\nEntry the dictionary name to be selected (or [:]-quit):>";
    wcin.sync();
    wcin.getline(keyword, 64, 10);

    if(keyword[0] == L':') break;

    else if(pRoot->has(keyword)) return (pRoot->getAt(keyword));

    else wcout << L"\nError: the name \"" << keyword << L"\" - is not found\n";
  } 
  while(1);

  return (OdDbObjectId::kNull);
}

The following example demonstrates listing of objects for the dictionary. The PrintDictionaryObjects() function requires one argument — the pointer to the dictionary object whose content must be printed — and does not return a value. To display the list of objects, the function creates the iterator using the newIterator() method, moves the iterator to the next object using the next() method, checks whether the traversing is completed using the done() method, and gets the handle, name, and type of the current object using the objectId(), getHandle(), name(), getObject(), and isA() methods.

The PrintDictionaryObjects() function has the following implementation:


void PrintDictionaryObjects(OdDbDictionary* pDict)
{
  OdDbDictionaryIteratorPtr itItem;

  wcout << L"\nThe list of objects:";
  for(itItem = pDict->newIterator() ; !itItem->done() ; itItem->next())
  {
    wcout << L"\n" << itItem->objectId().getHandle().ascii() 
          << L"-\"" << itItem->name() << L"\"-" 
          << itItem->getObject()->isA()->name();
  }
  itItem = NULL;
}

The DemoBase() function tests the SelectDatabaseDictionary() and PrintDictionaryObjects() functions. The DemoBase() function requires a pointer to the database object and organizes a loop in which it calls the SelectDatabaseDictionary() function for dictionary selection by a user. If the returned Object ID is not null, the function gets the pointer to the dictionary object, calls the PrintDictionaryObjects() function, and passes to it the pointer to the obtained dictionary for listing of its objects.

The DemoBase() function has the following implementation:


void DemoBase(OdDbDatabase* pDb)
{
  OdDbObjectId idDict;
  
  wcout << L"Start testing";
  do {
    idDict = SelectDatabaseDictionary(pDb);
    if(idDict.isNull()) break;
    PrintDictionaryObjects((OdDbDictionary*) idDict.safeOpenObject().get());
  } 
  while(1);
  wcout << L"Stop testing";
}

Testing gives the following results:


Start testing
The root dictionary of dictionaries:
D-"ACAD_GROUP"
E-"ACAD_PLOTSTYLENAME"
17-"ACAD_MLINESTYLE"
19-"ACAD_PLOTSETTINGS"
1A-"ACAD_LAYOUT"
2A-"ACAD_VISUALSTYLE"
3B-"ACAD_MATERIAL"
Entry the name to be selected (or [:]-quit):>ACAD_LAYOUT

The list of objects:
1E-"Layout1"-AcDbLayout
22-"Model"-AcDbLayout
26-"Layout2"-AcDbLayout

Entry the name to be selected (or [:]-quit):>ACAD_MATERIAL

The list of objects:
3C-"ByLayer"-AcDbMaterial
3D-"ByBlock"-AcDbMaterial
3E-"Global"-AcDbMaterial

Entry the name to be selected (or [:]-quit):>ACAD
Error: the name "ACAD" - is not found

Entry the name to be selected (or [:]-quit):>:
Stop testing 

See Also

Working with Dictionaries of Objects

Root Drawing Dictionary

Example of Using the Dictionary Interface for Selecting Objects

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