Drawings SDK Developer Guide > Working with .dwg Files > Working with Databases > Identifying the Objects of Databases > Example of Using the Handle for Selecting Objects
Example of Using the Handle for Selecting Objects

This example demonstrates selecting an object from a database using its handle. The handle provides an unique identification of an object being resident inside a database.

The EntryObjectByHandle() function requires a pointer to the database object in which an object must be searched by handle as the first argument, a pointer to the class description instance to which an object must be belonged as the second argument, and returns the smart pointer of the found object or NULL if the specified handle is not found in the database or the found object does not belong to the specified class.

This function declares a variable of an unsigned 64-bit integer type and inquires a value for a handle as an integer number in the hexadecimal format. If the entered value is incorrect, the function displays an error message. If the entered value is correct, the function calls the getOdDbObjectId() method of the database object and passes to it the entered value. This method returns the object ID. If the returned ID is kNull, the handle is not found and function displays an error message. If the returned ID is correct, the function opens the object associated with this ID in the writing mode using the safeOpenObject() method. If the object is not opened, the function displays an error message. Next, the function checks the class type of the opened object using the isA() method. If classes are different, the function displays an error message.

The EntryObjectByHandle() function has the following implementation:


OdDbObjectPtr EntryObjectByHandle(OdDbDatabase* pDb, OdRxClass *pClass)
{
  OdUInt64 vInt64;
  OdDbObjectId idObj;
  OdDbObjectPtr pObj;

  wcin >> hex >> vInt64 >> dec;
  if(wcin.fail() || wcin.peek() != 10)
  {
    wcin.clear();  
    wcin.sync();  
    wcout << L"Error: Invalid entered value\n";
    pObj = NULL;
  }
  else if((idObj = pDb->getOdDbObjectId(vInt64)).isNull()) 
  { 
    wcout << L"Error: Object is not found\n";
    pObj = NULL;
  }
  else if((pObj = idObj.safeOpenObject(OdDb::kForWrite)).isNull()) 
  { 
    wcout << L"Error: Object cannot be open in write mode\n";
    pObj = NULL;
  }
  else if((pClass != NULL) && (pObj->isA() != pClass)) 
  { 
    wcout << L"Error: Object is not " << pClass->name() << " \n";
    pObj = NULL;
  }
  return pObj;  
}

See Also

Identifying the Objects of Databases

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