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 Using the Dictionary Interface for Selecting Objects
Example of Using the Dictionary Interface for Selecting Objects

This example demonstrates selection of an object from a dictionary using the base dictionary interface independent from the type of objects contained in the dictionary.

The EntryDictionaryObject() function requires two arguments — the OdDbObjectId instance associated with the owner container object, that is, the dictionary, and the OdDbObjectId instance associated with an object being used as a default object for selection — and returns the OdDbObjectId instance associated with the object selected by the user or a default object if the user cancels selection. The first argument is mandatory; the second argument is optional.

The EntryDictionaryObject() function checks whether the first passed Object ID and second passed Object ID are associated with objects of the database. When the Object ID is null, it is not associated with an object. If the Object ID is not null and is valid, the function gets the smart pointer to the associated object using the safeOpenObject() method of the Object ID. The first and second arguments cannot be null together. If both arguments are null, the function displays an error message and returns OdDb::kNull instance. The second argument can be null — it indicates an undefined default object. The function uses the first argument to get the dictionary object by default. When the first argument is the database object, the function uses it as the owner object. When the first argument is null, the function tries to get the dictionary object using the second argument. For that, the function uses the ownerId() method of the passed object which returns the OdDbObjectId instance associated with its owner object. If the object is contained in the dictionary, the dictionary object is the owner for this object and the function can use it. If the OdDbObjectId instance obtained form the ownerId() method is null, the function takes the object of the second argument as the owner object.

Then, the EntryDictionaryObject() function checks whether the owner object is a dictionary using the isA() method of the object, which returns the pointer to the class describing instance. The function uses the desc() static method of the dictionary object and the isDerivedFrom() method of the class describing instance to check the class of the owner object. When the owner object is a dictionary, the function creates a loop. When the owner object is not a dictionary, the function calls the SelectDatabaseDictionary() function for selecting a dictionary from the root drawing dictionary and passes it the pointer to the database object obtained from the owner object using the database() method. The SelectDatabaseDictionary() function displays the list of dictionaries and inquires about one. If the user selects a dictionary, the function returns the OdDbObjectId instance associated with selected dictionary. If the user selects a dictionary from the root drawing dictionary, the EntryDictionaryObject() function takes it as the owner object and gets the smart pointer to it using the safeOpenObject() method. If the user cancels selection, the EntryDictionaryObject() function returns OdDb::kNull instance.

After selecting a dictionary, the EntryDictionaryObject() function organizes the loop in which it displays the list of objects contained in the dictionary using an iterator, displays information about the selected object, and inquires about the name of the object to be selected. To display the list of objects, the function creates the iterator using the newIterator() method, traverses it through objects of the dictionary using the next() method, checks whether the traverse is completed using the done() method, and displays the name using the name() method and the handle using the objectId() and getHandle() methods for each object to which the iterator refers. To display information about the selected object, the function uses the nameAt() method of the dictionary object and the handle() and objectId () methods of the object to which the pointer of the selected record refers.

To enter the name of the object selected by the user, the EntryDictionaryObject() function declares a keyword variable as an array of wide-characters and enters the name in it. After entry, the function checks whether the entered name exists in the dictionary using the has() method of the dictionary object, which returns True if the specified name exists in the dictionary or False if the name is not found. If the entered name exists in the dictionary, the function gets the Object ID associated with the name using the getAt() method of the dictionary object and returns it to the calling function. If the entered name is not found in the dictionary, the function displays an error message and performs the loop to display the list of objects and inquire about the name. If the entered name is '?', the function sets a NULL value instead of the currently selected object and performs a new loop. If the entered name is colon ':', the function cancels entry and returns the Object ID specified by the pointer of the selected object or OdDb::kNull if the pointer of the current selected object is NULL.

The EntryDictionaryObject() function has the following implementation:


OdDbObjectId EntryDictionaryObject(OdDbObjectId idOwner, OdDbObjectId idObject = OdDbObjectId::kNull)
{
  OdDbObjectPtr pObject;

  if(!idObject.isNull()) pObject = idObject.safeOpenObject();

  OdDbObjectPtr pOwner;

  if(!idOwner.isNull()) pOwner = idOwner.safeOpenObject();
  
  else if(!pObject.isNull()) 
  {  
    idOwner = pObject->ownerId();
    if(!idOwner.isNull()) 
      pOwner = idOwner.safeOpenObject();
    else 
      pOwner = pObject;
  }
  else 
  {
    wcout << L"\nError: arguments are incorrect\n";
    return (OdDbObjectId::kNull);
  }

  if(!pOwner->isA()->isDerivedFrom(OdDbDictionary::desc()))
  {
    idOwner = SelectDatabaseDictionary(pOwner->database());

    if(idOwner.isNull()) return (OdDbObjectId::kNull);

    pOwner = idOwner.safeOpenObject();
    pObject = NULL;
  }

  OdDbDictionaryIteratorPtr itObject; 
  wchar_t keyword[64];

  do {
    wcout << L"\nList of the objects:";

    itObject = ((OdDbDictionary*) pOwner.get())->newIterator(); 
    while(!itObject->done())
    {
      wcout << L"\n" << itObject->objectId().getHandle().ascii() 
            << L"-\"" << itObject->name() << L"\"";
      itObject->next();
    }
    itObject = NULL;

    if(pObject.isNull())
      wcout << L"\nCurrent: [Db:kNull]";
    else
      wcout << L"\nCurrent: [" << pObject->handle().ascii() 
            << L"-\"" << ((OdDbDictionary*) pOwner.get())->nameAt(pObject->objectId()) << L"\"]";

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

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

    else if(keyword[0] == L'?') pObject = NULL;
 
    else if(((OdDbDictionary*) pOwner.get())->has(keyword))
    {
      return (((OdDbDictionary*) pOwner.get())->getAt(keyword));
    }
    else wcout << L"\nError: the name \"" << keyword << L"\" - is not found\n";
  }
  while(1);

  return ( (pObject.isNull()) ? (OdDbObjectId::kNull) : (pObject->objectId()) );
}

The DemoBase() function tests the EntryDictionaryObject() function. The DemoBase() function requires the pointer to the database object and calls the EntryDictionaryObject() function for the following cases:

  1. First argument is the dictionary object; second argument is the object from this dictionary.
  2. First argument is the dictionary object; second argument is undefined.
  3. First argument is undefined; second argument is the object from a dictionary.
  4. First argument is the object of another type; second argument is undefined.
  5. First argument is undefined; second argument is undefined.

The DemoBase() function uses the AboutDbObject() function to display the result. The DemoBase() function has the following implementation:


void DemoBase(OdDbDatabase* pDb)
{
  wcout << L"\nStart testing";
  wcout << AboutDbObject(EntryDictionaryObject(pDb->getMaterialDictionaryId(), pDb->globalMaterialId()));

  wcout << AboutDbObject(EntryDictionaryObject(pDb->getVisualStyleDictionaryId()));

  wcout << AboutDbObject(EntryDictionaryObject(OdDbObjectId::kNull, pDb->currentLayoutId()));

  wcout << AboutDbObject(EntryDictionaryObject(pDb->getLinetypeTableId()));

  wcout << AboutDbObject(EntryDictionaryObject(OdDbObjectId::kNull, OdDbObjectId::kNull));
  wcout << L"\nStop testing";
}

Testing gives the following results:


Start testing
List of the objects:
97-"ByBlock"
96-"ByLayer"
98-"Global"
99-"Steel"
9A-"Tree"
Current: [98-"Global"]
Entry the name to be selected (or [?]-null/[:]-quit):>Steel
[99-"Steel"-Material]

List of the objects:
9F-"2dWireframe"
A1-"3D Hidden"
A0-"3dWireframe"
9A-"Flat"
9C-"Gouraud"
A3-"Realistic"
Current: [Db:kNull]
Entry the name to be selected (or [?]-null/[:]-quit):>Flat
[9A-"Flat"-VisualStyle]

List of the objects:
59-"Layout1"
5E-"Layout2"
22-"Model"
23-"Common"
Current: [22-"Model"]
Entry the name to be selected (or [?]-null/[:]-quit):>?
Current: [Db:kNull]
Entry the name to be selected (or [?]-null/[:]-quit):>Model
[22-"Model"-Layout]

The root dictionary of dictionaries:
D-"ACAD_GROUP"
E-"ACAD_PLOTSTYLENAME"
17-"ACAD_MLINESTYLE"
19-"ACAD_PLOTSETTINGS"
1A-"ACAD_LAYOUT"
72-"ACAD_MATERIAL"
B6-"ACAD_SCALELIST"
99-"ACAD_VISUALSTYLE"
Entry the name to be selected (or [:]-quit):>ACAD_MLINESTYLE

List of the objects:
190-"Style_A"
191-"Style_B"
18-"Standard"
Current: [Db:kNull]
Entry the name to be selected (or [?]-null/[:]-quit):>standard
[18-"Standard"-MlineStyle]

Error: arguments are incorrect
[Db:kNull]
Stop testing

The EntryDictionaryObject() function is used in the documented examples that require entry of an object from a dictionary.

See Also

Collecting Database Objects using Dictionaries

Example of Selecting a Dictionary from the Root Drawing Dictionary

Example of Using the Record–Table and Dictionary Interfaces for Getting Information about Objects

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