Close

Relief for ODA Team in Ukraine

Learn more
ODA Drawings SDK
Getting and Checking Records

A predefined table object provides methods for getting and checking records and names from the table. In the following examples, the pTable variable stores a pointer to the predefined table object. For example, add two records and delete one:


OdDbSymbolTableRecordPtr pRecordA = OdDbRegAppTableRecord::createObject();
pRecordA->setName(L"Record_A");
OdDbObjectId idRecordA = pTable->add(pRecordA);

OdDbSymbolTableRecordPtr pRecordB = OdDbRegAppTableRecord::createObject();
pRecordB->setName(L"Record_B");
OdDbObjectId idRecordB = pTable->add(pRecordB);
pRecordB->erase(true);

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

Checking a name

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


odPrintConsoleString(L"\nRecord_B %s in the table", ((pTable->has(L"Record_B")) ? L"exists" : L"absents"));

Checking a record

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


odPrintConsoleString(L"\nRecord_A %s in the table", ((pTable->has(idRecordA)) ? L"exists" : L"absents"));

If the record object is marked as «erased», the has() method returns False for both implementations.

Getting a record

To get a record from the table, use the getAt() method that has two implementations. This method searches for the record object using its name and returns either its object ID or a smart pointer to it.

The first implementation of the getAt() method requires three arguments — the record name as a non-empty OdString value, the mode for opening the record object as a value of the OdDb::OpenMode enumerator (kForRead, kForWrite, or kForNotify), and the erase status as a Boolean value (true – "erased" or false – "unerased") — and returns the smart pointer to the record object as a value of the OdDbSymbolTableRecordPtr type if the record object with the specified name exists in the table, or the method returns NULL if the name is not found or if the record object cannot be opened in the specified mode. The third argument is optional and is False by default. For example:


// Open the unerased record object in the read mode
OdDbSymbolTableRecordPtr pRecA = pTable->getAt("Record_A", OdDb::kForRead, false);

// Open the erased record object in the write mode
OdDbSymbolTableRecordPtr pRecB = pTable->getAt("Record_B", OdDb::kForWrite, true);

The second implementation of the getAt() method requires two arguments — the record name as a non-empty OdString value and the erase status as a Boolean value (true – "erased" or false – "unerased") — and returns the OdDbObjectId instance associated with the obtained record object when the specified name exists in the table, or the method returns an OdDb::kNull instance when the name is not found. The second argument is optional and is False by default. For example:


// Get the unerased record object
OdDbObjectId idObjA = pTable->getAt("Record_A", false);

// Get the erased record object
OdDbObjectId idObjB = pTable->getAt("Record_B", true);

To get a smart pointer to the instance of the obtained record, use the safeOpenObject() method of the OdDbObjectId object that 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:


// Open the unerased record object in the read mode
OdDbObjectPtr pObjA = idObjA.safeOpenObject(OdDb::kForWrite);

// Open the erased record object in the write mode
OdDbObjectPtr pObjB = idObjB.safeOpenObject(OdDb::kForWrite);

These operations can be combined in one line:


// Open the unerased record object in the read mode
OdDbSymbolTableRecordPtr pRecA = pTable->getAt("Record_A", false).safeOpenObject(OdDb::kForRead);

// Open the erased record object in the write mode
OdDbSymbolTableRecordPtr pRecB = pTable->getAt("Record_B", true).safeOpenObject(OdDb::kForWrite);

To get the record object and check the result, use the returned smart pointer or object ID. For example:


OdDbObjectPtr pObj = pTable->getAt("Record_A", OdDb::kForNotify);

if(!pObj.isNull())
  odPrintConsoleString(L"\nRecord_A has handle: %s", pObj->handle().ascii());
else
  odPrintConsoleString(L"\nRecord_A is not found");

OdDbObjectId idObj = pTable->getAt("Record_B");

if(!idObj.isNull())
  odPrintConsoleString(L"\nRecord_B has handle: %s", idObj.getHandle().ascii());
else
  odPrintConsoleString(L"\nRecord_B is not found");

The erase status argument of the getAt() method is optional. If the erase status is False, the getAt() method gets the record object if and only if it is not deleted, that is, it is marked as «unerased». If the record object is marked as «erased» or the specified name is absent from the table, the getAt() method returns an OdDb::kNull instance or a NULL pointer. If the erase status is True, the getAt() method gets the object if it exists in the table independent of its status, that is, it can be marked «erased» or «unerased».

Listing all objects

To display the list of accessible records for the table, create the iterator using the newIterator() method and use the step() method to traverse to the next record in the table until the done() method returns a false. To get the record object on which the iterator refers, use the getRecord() method of the iterator that returns the smart pointer to the current object. For example:


OdDbSymbolTableIteratorPtr itObj = pTable->newIterator();
while(!itObj.done())
{
  odPrintConsoleString(L"\nRecord \"%ls\"", itObj->getRecord()->getName().c_str());
  itObj->step();
}

To display a list of all records for the table including "erased", set the iterator at the beginning of the table and specify the traversing through the erased records using a True value for the first argument and a False value for the second argument of the start() and step() methods of the iterator. For example:


for(!itObj->start(true, false) ; !itObj->done() ; itObj->step(true, false))
  odPrintConsoleString(L"\nRecord \"%ls\"", itObj->getRecord(OdDb::kForRead, true)->getName().c_str());

See Also

Working with Predefined Tables of Named Records

Adding, Naming, Deleting, and Recovering Records

Iterating through Records

Example of Using the Record-Table Interface for Selecting Objects

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