Drawings SDK Developer Guide > Working with .dwg Files > Working with Databases > Working with Database Containers > Working with Predefined Tables of Named Records > Basics of Predefined Tables of Named Records > Example of Using the Record-Table Interface for Selecting Objects
Example of Using the Record-Table Interface for Selecting Objects

This example demonstrates selecting a named record object from a predefined table object using the base interface independent of the type of records contained in the table.

The EntryTableRecordObject() function requires two arguments — the OdDbObjectId instance associated with the predefined table object and the OdDbObjectId instance associated with the named record object being used as a default object for selection — and returns the OdDbObjectId instance associated with the record object selected by the user or the default object if the user cancels selection. The first argument is mandatory; the second argument is optional.

The EntryTableRecordObject() 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. The second argument can be null — it indicates an undefined default record object, and the function uses only the first argument. When the second argument is not null, the function gets the smart pointer to the object and checks whether the obtained object is the named record, that is, derived from the OdDbSymbolTableRecord class. If the obtained object has another class, the function sets the smart pointer to NULL, assuming an undefined default record object. The function uses the first argument to get the predefined table object by default.

If the first argument is null but the default record object is obtained from the second argument, the function tries to get the predefined table object using the second argument. The function uses the ownerId() method of the obtained default record object which returns the OdDbObjectId instance associated with its owner object. The predefined table object is the owner for the named record objects that it contains. The function can obtain the table from the specified default record object. If the default record object is not null, the function gets the predefined table object using the ownerId() method and opens it in read mode using the safeOpenObject() method.

If the first argument is not null, the function opens it in read mode using the safeOpenObject() method which returns the smart pointer to it. Then the function checks whether the obtained object is the predefined table, that is, derived from the OdDbSymbolTable class. If the obtained object has another class, the function checks whether the obtained object is the named record, that is, derived from the OdDbSymbolTableRecord class. If the obtained object is the named record, the function takes it as the default record object, ignoring the record object obtained from the second argument, and gets the predefined table object using the ownerId() method and opens it in read mode using the safeOpenObject() method. If the obtained object has another class, the function calls the SelectDatabasePredefinedTable() function for selecting a predefined table and passes it the pointer to the database object using the database() method of the obtained object. The SelectDatabasePredefinedTable() function displays the list of predefined tables and inquires about one. If the user selects a table, the function returns the OdDbObjectId instance associated with selected table, and the EntryTableRecordObject() function takes it as the predefined table object of records and gets the smart pointer to it using the safeOpenObject() method. The function also takes the default record object as undefined and sets its smart pointer to NULL. If the user cancels selection, the EntryTableRecordObject() function returns OdDb::kNull.

To check the class of objects, the function uses the isA() method to return the pointer to the class describing instance and the isDerivedFrom() method of the class describing instance to pass the pointer to the corresponding class describing instance obtained by the desc() static method of the corresponding object.

After selecting a table object, the EntryTableRecordObject() function creates an iterator using the newIterator() method and organizes a loop in which it displays the list of records contained in the table using the iterator, displays information about the selected record object, and inquires about the name of the record object to be selected. To display the list of records, the function sets the iterator to the first record using the start() method, traverses through records of the table using the step() method, checks whether the traverse is completed using the done() method, and displays the name using the getRecord() and getName() methods and gets the handle using the getRecordId() and getHandle() methods for each record to which the iterator refers. To display information about the selected record object, the function uses the handle() and getName() methods of the record object to which the pointer of the selected record refers.

To enter the name of a selected record object, the EntryTableRecordObject() 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 table using the has() method of the predefined table object. If the entered name exists in the table, the function gets the Object ID associated with the name using the getAt() method of the predefined table object and returns it in the calling function. If the entered name is not found in the table, the function displays an error message and performs a new loop to display the list of the record objects and inquire about the name. If the entered name is '?', the function sets a NULL value instead of the currently selected record 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 record or returns OdDb::kNull if the pointer of the current selected record is NULL.

The EntryTableRecordObject() function has the following implementation:


OdDbObjectId EntryTableRecordObject(OdDbObjectId idTable, OdDbObjectId idRecord = OdDbObjectId::kNull)
{
  OdDbObjectId SelectDatabasePredefinedTable(OdDbDatabase* pDb);

  OdDbObjectPtr pRecord;
  
  if(!idRecord.isNull())
  {
    pRecord = idRecord.safeOpenObject(OdDb::kForRead);

    if(!pRecord->isA()->isDerivedFrom(OdDbSymbolTableRecord::desc()))
    {
      pRecord = NULL;
    }
  }

  OdDbObjectPtr pTable;

  if(!idTable.isNull())
  {
    pTable = idTable.safeOpenObject(OdDb::kForRead);

    if(!pTable->isA()->isDerivedFrom(OdDbSymbolTable::desc()))
    {
      if(!pTable->isA()->isDerivedFrom(OdDbSymbolTableRecord::desc()))
      {
        idTable = SelectDatabasePredefinedTable(pTable->database());
        
        if(idTable.isNull()) return (OdDbObjectId::kNull);

        pTable = idTable.safeOpenObject(OdDb::kForRead);
        pRecord = NULL;
      }
      else 
      {  
        pRecord = pTable;
        pTable = pRecord->ownerId().safeOpenObject(OdDb::kForRead);
      }
    }
  }
  else if(!pRecord.isNull())
  {
    pTable = pRecord->ownerId().safeOpenObject(OdDb::kForRead);
  }
  else
  {
    wcout << L"\nError: arguments are incorrect\n";
    return (OdDbObjectId::kNull);
  }

  OdDbSymbolTableIteratorPtr itObject = ((OdDbSymbolTable*) pTable.get())->newIterator();

  wchar_t keyword[64];

  do {
    wcout << L"\nSelect the " << pTable->isA()->name().mid(4, pTable->isA()->name().find(L"Table")-4) << L" object:";

    for(itObject->start() ; !itObject->done() ; itObject->step())
    {
      wcout << L"\n" << itObject->getRecordId().getHandle().ascii() 
            << L"-\"" << itObject->getRecord()->getName() << L"\"";
    }

    if(pRecord.isNull())
      wcout << L"\nCurrent: [Db:kNull]";
    else
      wcout << L"\nCurrent: [" << pRecord->handle().ascii() 
            << L"-\"" << ((OdDbSymbolTableRecord*) pRecord.get())->getName() << 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'?') pRecord = NULL;

    else if(((OdDbSymbolTable*) pTable.get())->has(keyword))
    {
        return ((OdDbSymbolTable*) pTable.get())->getAt(keyword);
    }
    else wcout << L"\nError: the name \"" << keyword << L"\" - is not found\n";
  }
  while(1);

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

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

  1. First argument is the predefined table object; second argument is the record object from this table.
  2. First argument is the predefined table object; second argument is undefined.
  3. First argument is undefined; second argument is the record object from a predefined table.
  4. First argument is the record object from a predefined table; second argument is undefined.
  5. First argument is the object of another type; second argument is undefined.
  6. 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(EntryTableRecordObject(pDb->getLinetypeTableId(), pDb->getLinetypeContinuousId()));

  wcout << AboutDbObject(EntryTableRecordObject(pDb->getBlockTableId()));

  wcout << AboutDbObject(EntryTableRecordObject(OdDbObjectId::kNull, pDb->getTextStyleStandardId()));

  wcout << AboutDbObject(EntryTableRecordObject(pDb->getLayerZeroId()));

  wcout << AboutDbObject(EntryTableRecordObject(pDb->getMaterialDictionaryId()));

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

Testing gives the following results:


Start testing
Select the Linetype object:
14-"ByBlock"
15-"ByLayer"
16-"Continuous"
17-"Dash Line"
18-"Dot Line"
Current: [16-"Continuous"]
Entry the name to be selected (or [?]-null/[:]-quit):>Dash Line
[17-"Dash Line"-Linetype]

Select the Block object:
1A-"*Model_Space"
1B-"*Paper_Space"
1C-"*Paper_Space0"
Current: [Db:kNull]
Entry the name to be selected (or [?]-null/[:]-quit):>*Model_Space
[1A-"*Model_Space"-Block]

Select the TextStyle object:
21-"Standard"
22-"Italic"
23-"Script"
Current: [11-"Standard"]
Entry the name to be selected (or [?]-null/[:]-quit):>Italic
[22-"Italic"-TextStyle]

Select the Layer object:
10-"0"
11-"1"
12-"2"
Current: [10-"0"]
Entry the name to be selected (or [?]-null/[:]-quit):>?
Current: [Db:kNull]
Entry the name to be selected (or [?]-null/[:]-quit):>:
[Db:kNull]

The predefined tables:
U. UCSs table
V. Views table
B. Blocks table
L. Layers table
T. lineTypes table
P. viewPorts table
S. text Styles table
D. Dimension styles table
R. Reg-applications table
Q. Quit (cancel)
Select the operation:>P

Select the Viewport object:
94-"*Active"
95-"*VPort"
Current: [Db:kNull]
Entry the name to be selected (or [?]-null/[:]-quit):>*Active
[94-"*Active"-Viewport]

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

The EntryTableRecordObject() function is used in the documented examples that require entry of a record object from a predefined table.

See Also

Collecting Database Objects using Predefined Tables of Named Records

Example of Selecting a Predefined Table Object

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

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