Close

Relief for ODA Team in Ukraine

Learn more
ODA Drawings SDK
Example of Working with the Registered Application Table Object

This example demonstrates working with the registered application table object for adding, renaming, deleting, recovering, modifying, and printing registered application record objects. This example uses a console menu for the following operations: printing the list of accessible applications [L], printing specific properties of all applications contained in the table [P], registering a new application in the table [A], renaming an existing application [N], deleting an existing application [D], and recovering a deleted application [R].

The DemoBase() function requires a pointer to the database object and implements a console menu for the listed operations. The function creates a loop that inquires about the operation and uses the switch statement to select whether the case must be performed. The DemoBase() function uses the PrintRegAppProperties() function for displaying the specific properties of the registered application record object, and EntryRecordName() function for entering an application name, checking whether the name is correct, and for checking whether the name exists or is absent form the table. The EntryRecordName() function returns the entered name as an OdString value when the name is correct for the specified options, or it returns an empty string when the name is incorrect or is in conflict with other names. The DemoBase() function obtains a smart pointer to the registered application table object using the getRegAppTableId() method of the passed database object. Before the loop, the function creates an iterator using the newIterator() method of the registered application table object and declares the smart pointer to the registered application record object. The DemoBase() function processes user actions in the loop until the user selects the [Q] operation.

The PrintRegAppProperties() function displays the listed properties of the passed application and requires the pointer to an existing registered application record object. The PrintRegAppProperties() function has the following implementation:


void PrintRegAppProperties(OdDbRegAppTableRecord* pRegApp)
{
  wcout << L"\nH=" << pRegApp->handle().ascii() 
        << L"\n  Name = \"" << pRegApp->getName()
        << L"\n  Status: " << ((pRegApp->isErased()) ? L"erased" : L"unerased")
                           << ((pRegApp->isDBRO()) ? L",resident" : L"")
                           << ((pRegApp->isModified()) ? L",modified" : L"")
                           << ((pRegApp->isDependent()) ? L",XRef-dependent" : L"");
}

When the user selects [L], the DemoBase() function prints the list of application names accessed in the table. The function uses the iterator for traversing through the registered application table, getRecord() method of the Iterator object for getting the pointer to each registered application record object, and getName() method for displaying the name of each registered application record object to which the iterator refers. When the user selects [P], the function prints the specific properties of all registered application record objects contained in the registered application table object, including objects marked as "erased". The function uses the iterator for traversing through the table and the PrintRegAppProperties() function for displaying specific properties of the registered application record objects to which the iterator refers. For the [P] operation, the function initializes the start() method of the iterator using parameters: True – beginning with the first record to the end record of the table, False – the record marked as unerased or erased, and uses the step() method of the iterator using parameters: True – traversing to the next record in the table, False – including unerased and erased records. For the [L] operation, the function initializes the arguments of the start() and step() methods using default values:  True – beginning with the first record to the next record, and False – only unerased records. When a registered application record object has been permanently deleted, the getRecord() method returns NULL and the function and iterator skip the record.

When the user selects [A], the DemoBase() function inquires about the name of a new registered application record object using the EntryRecordName() function. The name must be absent from the table because the table cannot contain duplicate records; the function passes the kMustAbsent value as the second argument for checking the new name. If the entered name is correct and absent from the table, the function creates a new registered application record object using its static pseudo-constructor. Then, the function sets the specified name for the created registered application record object using its setName() method, and adds the new registered application in the table using the add() method of the registered application table object. The function uses the try…catch statement to catch exceptions when the object is added. If the addition is successful, the function goes to the [L] operation for printing of the registered application list. When an exception occurs, the function displays an error message and breaks to the console menu.

When the user selects [N], the DemoBase() function inquires about the name of the existing registered application record object to be renamed and the new name using the EntryRecordName() function for both names. The first name must exist in the table; the function passes the kMustExist value as a second argument for checking the entered name. The second name must be absent from the table because the table cannot contain duplicate records; the function passes the kMustAbsent value as the second argument for checking the new name. If the first entered name is correct and exists in the table, the function gets the smart pointer to the existing registered application record object specified by the first name using the getAt() method of the registered application table object and opens it in write mode. Then, the function sets the second name as a new name of the opened registered application record object using its setName() method. The function uses the try…catch statement to catch exceptions when the object changes the name. When an exception occurs, the function displays an error message and breaks to the console menu.

When the user selects [D], the DemoBase() function inquires about the name of the existing registered application record object to be deleted using the EntryRecordName() function. The name must exist in the table; the function passes the kMustExist value as the second argument for checking the entered name. If the entered name is correct and exists in the table, the DemoBase() function gets a smart pointer to the corresponding registered application record object using the getAt() method of the registered application table object and opens it in write mode. The function passes a False value for the third argument of the getAt() method to identify the unerased record of the table. If the getAt() method returns a pointer to the specified registered application record object, the function calls the erase() method of the registered application record object and passes to it a True value as an argument. The erase(true) method marks the registered application record object as "erased" and keeps it in the table if and only if the database is performing an undo operation, otherwise the object is deleted permanently. After deletion, the function checks the result returned by the erase() method. If the result is zero, the deletion is successful, and the function breaks to the console menu. If the result is non-zero, the function displays an error message using the getErrorDescription() method of the host object.

When the user selects [R], the DemoBase() function inquires about the name of the deleted registered application record object to be recovered using the EntryRecordName() function. The inquired name must be absent from the table because the deleted record is not accessible in the table; the function passes the kMustAbsent value as a second argument for checking the entered name. If the entered name is correct and is absent from the table, the DemoBase() function tries to get a smart pointer to the corresponding registered application record object using the getAt() method of the registered application table object and open it in write mode; the function passes a True value for the third argument of the getAt() method to identify the erased record of the table. If the getAt() method returns NULL, the registered application record object was deleted permanently and recovery is impossible. In this case, the function displays an error message and breaks to the the console menu. If the getAt() method returns a pointer to the deleted registered application record object, the function calls the erase() method of the registered application record object and passes to it a False value as an argument. The erase(false) method marks the registered application record object as "unerased" and the record becomes accessible in the table. After recovery, the function checks the result returned by the erase() method. If the result is zero, the recovery is successful, and the function breaks to the the console menu. If the result is non-zero, the function displays an error message using the getErrorDescription() method of the host object.

When the user selects [Q], the function ends the loop and returns to the calling function.

The DemoBase() function has the following implementation:


#include "DemoRegApps.h"
#include <iostream>
using namespace std;

void DemoBase(OdDbDatabase* pDb)
{
  wchar_t ch = L'L';
  OdString sName;
  OdResult result;

  OdDbRegAppTablePtr pRegApps = pDb->getRegAppTableId().safeOpenObject(OdDb::kForWrite);
  OdDbSymbolTableIteratorPtr itRegApp = pRegApps->newIterator();
  OdDbRegAppTableRecordPtr pRegApp;

  wcout << L"\n\nStart testing of the registered applications";
  wcout << L"\nRegApp's Table " << pRegApps->handle().ascii() 
        << L", Class: " << OdDbRegAppTable::desc()->name()
        << L", Record: " << OdDbRegAppTableRecord::desc()->name()
        << L", Iterator: " << itRegApp->isA()->name() << L"\n";

  do {
    switch(ch)
    {
      case L'A':          // Add a new registered application
      case L'a':
        wcout << L"\nEntry the name of a new registered application:>";
        if((sName = EntryRecordName(pRegApps, kMustAbsent)).isEmpty()) break;

        pRegApp = OdDbRegAppTableRecord::createObject();
        
        try {
          pRegApp->setName(sName);     
          pRegApps->add(pRegApp);
        }
        catch(OdError& e)
        {
          wcout << L"\nError: " << e.code() << L" - " << e.description() << L"\n";
          pRegApp->erase();
          break;
        }


      case L'L':          // List the accessible registered applications
      case L'l':
        wcout << L"\nList of the accessible registered application:\n"; 
        for(itRegApp->start() ; !itRegApp->done() ; itRegApp->step())
        {
          wcout << "\"" << itRegApp->getRecord()->getName() << L"\"\n";
        }
        break;


      case L'P':          // Print the specific properties of all registered applications
      case L'p':
        wcout << L"\nList the specific properties of registered applications:"; 
        for(itRegApp->start(true, false) ; !itRegApp->done() ; itRegApp->step(true, false))
        {
          pRegApp = itRegApp->getRecord(OdDb::kForRead, true);
          PrintRegAppProperties(pRegApp.get());
        }
        wcout << L"\n";        
        break;


      case L'N':          // Rename the selected registered application
      case L'n':
        wcout << L"\nEntry the name of the registered application to be renamed:>" ;
        if((sName = EntryRecordName(pRegApps, kMustExist)).isEmpty()) break;

        pRegApp = pRegApps->getAt(sName, OdDb::kForWrite);

        wcout << L"\nEntry the new name of the registered application:>";
        if((sName = EntryRecordName(pRegApps, kMustAbsent)).isEmpty()) break;

        try {
          pRegApp->setName(sName);     
        }
        catch(OdError& e) { wcout << L"\nError: " << e.code() << L" - " << e.description() << L"\n"; }
        break;


      case L'D':          // Delete the selected registered application
      case L'd':
        wcout << L"\nEntry the name of the registered application to be deleted:>" ;
        if((sName = EntryRecordName(pRegApps, kMustExist)).isEmpty()) break;

        pRegApp = pRegApps->getAt(sName, OdDb::kForWrite, false);

        result = pRegApp->erase(true);

        if(result) wcout << L"\nError: " << result << L" - " << pDb->appServices()->getErrorDescription(result) << L"\n"; 
        break;


      case L'R':          // Recover the deleted registered application
      case L'r':
        wcout << L"\nEntry the name of the registered application to be recovered:>" ;
        if((sName = EntryRecordName(pRegApps, kMustAbsent)).isEmpty()) break;

        pRegApp = pRegApps->getAt(sName, OdDb::kForWrite, true);

        if(pRegApp.isNull())
        {
          wcout << L"\nThe registered application \"" << sName << L"\" - was deleted permanently\n";
          break;
        }

        result = pRegApp->erase(false);
				
        if(result) wcout << L"\nError: " << result << L" - " << pDb->appServices()->getErrorDescription(result) << L"\n"; 
        break;


      case L'\0':         // Skip an action
        break;

      default:
        wcout << L"Error: Incorrect operation\n";
    }
    wcout << L"\nL. List the accessible registered applications";
    wcout << L"\nP. Print the specific properties of all registered applications";
    wcout << L"\nA. Add a new registered application";
    wcout << L"\nN. Rename the registered application";
    wcout << L"\nD. Delete the registered application";
    wcout << L"\nR. Recover the registered application";
    wcout << L"\nQ. Quit";
    wcout << L"\nSelect operation:>";
    wcin >> ch;
  }
  while(ch != L'Q' && ch != L'q');

  wcout << L"Stop testing of the registered applications\n";
}

Testing gives the following results:


Start testing of the registered applications
RegApp's Table: 2, Class: AcDbRegAppTable, Record: AcDbRegAppTableRecord, Iterator: OdDbSymbolTableIterator

List of the accessible registered applications:
"ACAD"

L. List the accessible registered applications
P. Print the specific properties of all registered applications
A. Add a new registered application
N. Rename the registered application
D. Delete the registered application
R. Recover the registered application
Q. Quit
Select operation:>

When the operation is A-"Add a new registered application":


Entry the name of a new registered application:>Test
List of the accessible registered applications:
"0"
"Test"

When the operation is A-"Add a new registered application" and the name exists in the table:


Entry the name of a new registered application:>Test
Error: the name "Test" - already exists

When the operation is A-"Add a new registered application" and the name is incorrect:


Entry the name of a new registered application:>T?e*s:t
Error: the name contains inadmissible letters: <>/\":;?,|=`

When the operation is N-"Rename the registered application":


Entry the name of the registered application to be renamed:>Test
Entry the new name of the registered application:>ODA

When the operation is N-"Rename the registered application" and the name is absent from the table:


Entry the name of the registered application to be renamed:>Test
Error: the name "Test" - is not found

When the operation is N-"Rename the registered application" and the name is incorrect:


Entry the name of the registered application to be renamed:>ODA
Entry the new name of the registered application:>T?e*s:t
Error: the name contains inadmissible letters: <>/\":;?,|=`

When the operation is N-"Rename the registered application" and the new name exists in the table:


Entry the name of the registered application to be renamed:>ODA
Entry the new name of the registered application:>ACAD
Error: the name "ACAD" - already exists

Repeat the operation A-"Add a new registered application":


Entry the name of a new registered application:>AppX
List of the accessible registered applications:
"ACAD"
"ODA"
"AppX"

When the operation is D-"Delete the registered application":


Entry the name of the registered application to be deleted:>ODA

When the operation is D-"Delete the registered application" and the name is absent from the table or the record is already deleted:


Entry the name of the registered application to be deleted:>ODA
Error: the name "ODA" - is not found

When the operation is P-"Print the specific properties of all registered applications":


List the specific properties of registered applications:
H=20
  Name = "ACAD"
  Status: unerased,resident
H=2F
  Name = "ODA"
  Status: erased,resident
H=30
  Name = "AppX"
  Status: unerased,resident

When the operation is L-"List the accessible registered applications":


List of the accessible registered applications:
"ACAD"
"AppX"

When the operation is R-"Recover the registered application":


Entry the name of the registered application to be recovered:>ODA

When the operation is L-"List the accessible registered applications":


List of the accessible registered applications:
"ACAD"
"ODA"
"AppX"

Use the Q-"Quit" operation to exit:


Stop testing of the registered applications

See Also

Working with Registered Applications

Example of Working with Extended Data

Example of Using the Record–Table and Dictionary Interfaces for Entering Names

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