Drawings SDK Developer Guide > Working with .dwg Files > Introduction > Basics of Database Operations and Database Objects > Working with Database Objects > Accessing Database Object
Accessing Database Object

Since an object is owned by the database, it can accessed for reading or modifying. Drawings SDK provides two options to access database residents by using special objects: object Id and object handle.

Object ID object is a unique "name" of an object during the current session. This name unambiguously identifies an object through several opened database, but exists only in the memory while the database is loaded.

Object handle is permanent because it's written to the drawing file and identifies database object uniquely only within the database, but not between all loaded databases.

You are able to use either option to access objects depending on specific objectives.

Programmatically, Object IDs are represented by objects of the OdDbObjectId class. The following paragraphs describe the ways you can obtain an Object ID of different types of objects.

Obtaining the Object ID of newly created objects

Create an object and append it to the database. Since the object is put to the database, the database gives the object an ID and returns it. For example, the created Line object is assigned an ID after it's appended to the database (to the Model Space block):


OdDbLinePtr pLine = OdDbLine::createObject();
OdDbObjectId objId = pModelSpace->appendOdDbEntity(pLine);

Obtaining the Object ID of predefined objects

To obtain the Object ID of the predefined objects (that are created automatically when a database is created), such as the fixed set of symbol tables and the named object dictionary, use the database interface. Here is the list of methods for getting ObjectId objects for symbol tables:

  • Block Table — getBlockTableId()
  • Layer Table — getLayerTableId()
  • View Table — getViewTableId()
  • Linetype Table — getLinetypeTableId()
  • Viewport Table — getViewportTableId()
  • Text style Table — getTextStyleTableId()
  • Dimension style Table — getDimStyleTableId()
  • Registered application Table — getRegAppTableId()
  • UCS's Table — getUCSTableId()

For example, to get an Object ID for the Block Table:


OdDbObjectId blockTableID = pDb->getBlockTableId();

Obtaining the Object ID of associated objects

Certain classes define objects that own or refer to other objects. Using class-specific interfaces, you can obtain Object IDs of the owned objects, associated objects, or the object that is the owner object. For example, the Block Table object contains a number of Block Table Record objects with referenced Block Reference objects:

In this case you can get the Object ID of the Model Space and Paper Space blocks for this block table:


OdDbObjectId modelSpaceId = pDb->getModelSpaceId();
OdDbObjectId paperSpaceId = pDb->getPaperSpaceId();

The array of Object IDs of all block references for a specific block table record:


OdDbObjectIdArray bRefArray;
block->getBlockReferenceIds(bRefArray);

And so on.

Using iterators

To step through a list or set of objects contained in various kinds of container objects, the OdDb library provides a number of special iterator objects such as: OdDbDictionaryIterator, OdDbSymbolTableIterator, etc. The symbol table iterator object is used for the overview below.

To traverse through stored records, tables use specific bidirectional iterators which also are typified for each type of object. Iterators allow processing of records independently from the internal structure of the predefined table and have the semantics and behavior of a pointer while providing additional functionality. Iterators provide a way to traverse through records of tables and access each stored record object.

The iterator can be used multiple times for traversing through records from the beginning to the end or from the end to the beginning of the predefined table. Working with iterators includes the following operations: creating an iterator, setting an iterator to the beginning or end of the table, moving an iterator to the next or previous record in the table, checking whether the traversing is completed, searching a record, and getting a record from the table.

The following function illustrates how to access records from the layer table and get information about each layer.


void dumpLayers(OdDbDatabase* pDb)
{
  /**********************************************************************/
  /* Get a SmartPointer to the LayerTable                               */
  /**********************************************************************/
  OdDbLayerTablePtr pTable = pDb->getLayerTableId().safeOpenObject();

  /**********************************************************************/
  /* Get a SmartPointer to a new SymbolTableIterator                    */
  /**********************************************************************/
  OdDbSymbolTableIteratorPtr pIter = pLayers->newIterator();

  /**********************************************************************/
  /* Step through the LayerTable                                        */
  /**********************************************************************/
  for (pIter->start(); !pIter->done(); pIter->step())
  {
    /********************************************************************/
    /* Open the LayerTableRecord for Reading                            */
    /********************************************************************/
    OdDbLayerTableRecordPtr pRecord = pIter->getRecordId().safeOpenObject();

    /********************************************************************/
    /* Dump the LayerTableRecord                                       */
    /********************************************************************/
    printf("Name: %ls\n", toString(pRecord->getName()).c_str());
    printf("On: %ls\n", toString(!pRecord->isOff()).c_str());
    printf("Locked: %ls\n", toString(pRecord->isLocked()).c_str());
    printf("Color: %ls\n", toString(pRecord->color()).c_str());
    printf("Linetype: %ls\n", toString(pRecord->linetypeObjectId()).c_str());
    printf("\n");
  }
}
Copyright © 2002 – 2021. Open Design Alliance. All rights reserved.