Close

Relief for ODA Team in Ukraine

Learn more
ODA Drawings SDK
Example of Working with the Text Style Table Object

This example demonstrates working with the text style table object and adding, renaming, deleting, recovering, modifying, and printing of text style record objects. This example uses a console menu that represents the following operations: printing a list of accessible text styles [L], printing specific properties of all text styles contained in the table [P], exposing predefined text styles [E], creating a new text style and adding it in the table [A], renaming an existing text style [N], deleting an existing text style [D], recovering a deleted text style [R], and setting a text style to be active [S].

The ModifyTextStyles() 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 code and uses a switch statement to select whether the case must be performed. The ModifyTextStyles() function uses the PrintTextStyleProperties() function for printing properties of the text style record object, the ModifyTextStyleProperties() function for modifying properties of the selected text style record object, and the EntryRecordName() function for entering the text style name, checking whether the name is correct, and checking whether the name exists or is absent from 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 ModifyTextStyles() function obtains a smart pointer to the text style table object using the getTextStyleTableId() method of the passed database object. Before the loop, the function creates an iterator using the newIterator() method of the text style table object and declares a smart pointer to the text style record object. The ModifyTextStyles() function processes user actions in the loop until the user selects the [Q] operation.

When the user selects [L], the ModifyTextStyles() function prints a list of text style names accessed in the text style table. The function uses an iterator for traversing through the text style table, getRecord() method of the Iterator object for getting the pointer to each text style record object, and getName() method for displaying the name of each text style record object to which the iterator refers. When the user selects [P], the function prints the specific properties of all text style record objects contained in the text style table object, including objects marked as "erased". The function uses the iterator for traversing through the text style table and the PrintTextStyleProperties() function for displaying the specific properties of the text style record objects to which the iterator refers. When 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, or False – the record marked as unerased or erased; the function initializes the step() method of the iterator using parameters: True – traversing to the next record in the table, False – including unerased and erased records. When 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 True – only unerased records. If a text style record object was permanently deleted, the getRecord() method returns NULL, and the function and iterator skip the record.

When the user selects [A], the ModifyTextStyles() function inquires about the name of the new text style record object using the EntryRecordName() function. The name must be absent from the text style table because the table can not contain duplicate records and the function passes the kMustAbsent value as a second argument for checking the new name. If the entered name is correct and absent from the table, the function creates a new text style record object using its static pseudo-constructor. Then, the function sets the specified name for the created text style record object using its setName() method, and adds the new text style in the text style table using the add() method of the text style table object. The function uses a 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 the text style list. When an exception occurs, the function displays an error message and breaks to the console menu.

When the user selects [N], the ModifyTextStyles() function inquires about the name of the existing text style record object to be renamed and inquires about the new name using the EntryRecordName() function for both names. The first name must exist in the text style table, and the function passes the kMustExist value as a second argument for checking the entered name. The second name must be absent from the text style table because the table can not contain duplicate records; the function passes the kMustAbsent value as a second argument for checking the new name. If the first entered name is correct and exists in the table, the function gets a smart pointer to the corresponding text style record object using the getAt() method of the text style table object and opens it in write mode. Then, the function sets the second name as a new name of the opened text style 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 ModifyTextStyles() function inquires about the name of the existing text style record object to be deleted using the EntryRecordName() function. The name must exist in the text style table; the function passes the kMustExist value as a second argument for checking the entered name. If the entered name is correct and exists in the table, the ModifyTextStyles() function gets a smart pointer to the corresponding text style record object using the getAt() method of the text style table object and opens it in write mode. The function passes a False value as the third argument of the getAt() method that identifies the unerased record of the table. If the getAt() method returns a pointer to the specified text style record object, the function calls the erase() method of the text style record object and passes to it a True value as an argument. The erase(true) method marks the text style record object as "erased" and keeps it in the table if and only if the database is using an undo operation, otherwise the object is deleted permanently. After deleting, the function checks the result returned by the erase() method. If the result is zero, the deleting 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 [R], the ModifyTextStyles() function inquires about the name of the deleted text style record object to be recovered using the EntryRecordName() function. The inquired name must be absent from the text style 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 ModifyTextStyles() function tries to get a smart pointer to the corresponding text style record object using the getAt() method of the text style table object and tries to open it in write mode, but the function also passes a True value as a third argument of the getAt() method to identify the erased record of the table. If the getAt() method returns NULL, the text style record object was deleted permanently and recovery is impossible. In this case, the function displays an error message and breaks to the console menu. If the getAt() method returns a pointer to the deleted text style record object, the function calls the erase() method of the text style record object and passes to it a False value as an argument. The erase(false) method marks the text style 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 was 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 [M], the ModifyTextStyles() function inquires about the name of the existing text style record object to be modified using the EntryRecordName() function. The name must exists in the text style's table, and the function passes the kMustExist value as second argument for checking of the entered name. If the entered name is correct and exists in the table, the ModifyTextStyles() function gets the smart pointer to the existing text style record object specified by the inquired name using the getAt() method of the text style table object and opens it in the write mode. Then, the function calls the ModifyTextStyleProperties() function that organizes own console menu for modifying of the specific properties of the specified text style record object.

When the user selects [S], the ModifyTextStyles() function inquires about the name of the existing text style record object which should be active using the EntryRecordName() function. The name must exist in the text style table; the function passes the kMustExist value as a second argument for checking the entered name. If the entered name is correct and exists in the table, the ModifyTextStyles() function gets the Object ID of the text style record object using the getAt() method and passes the Object ID as an argument of the setTEXTSTYLE() method of the database object. The setTEXTSTYLE() method makes the specified text style active.

When the user selects [E], the ModifyTextStyles() function displays predefined text styles using the getTEXTSTYLE() and getTextStyleStandardId() methods of the database object. The getTEXTSTYLE() method returns the Object ID of the current active text style. The getTextStyleStandardId() method returns the Object ID of the standard text style.

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

The ModifyTextStyles() function has the following implementation:


#include "..\Common\DemoMain.h"
#include "DbTextStyleTable.h"
#include "DbTextStyleTableRecord.h"
#include <iostream>
using namespace std;

void PrintTextStyleProperties(OdDbTextStyleTableRecord* pTextStyle);
void ModifyTextStyleProperties(OdDbTextStyleTableRecord* pTextStyle);

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

  OdDbTextStyleTablePtr pTextStyles = pDb->getTextStyleTableId().safeOpenObject(OdDb::kForWrite);
  OdDbSymbolTableIteratorPtr itTextStyle = pTextStyles->newIterator();
  OdDbTextStyleTableRecordPtr pTextStyle;

  wcout << L"\n\nStart modifying of text styles";
  wcout << L"\nTextStyle's Table " << pTextStyles->handle().ascii() 
        << L", Class: " << OdDbTextStyleTable::desc()->name() 
        << L", Record: " << OdDbTextStyleTableRecord::desc()->name()
        << L", Iterator: " << itTextStyle->isA()->name() << L"\n";

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

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


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


      case L'P':          // Print the specific properties of all text styles
      case L'p':
        wcout << L"\nList the specific properties of text styles"; 
        for(itTextStyle->start(true, false) ; !itTextStyle->done() ; itTextStyle->step(true, false))
        {
          pTextStyle = itTextStyle->getRecord(OdDb::kForRead, true);
          if(!pTextStyle.isNull()) PrintTextStyleProperties(pTextStyle.get());
        }
        wcout << L"\n";
        break;


      case L'S':          // Set the current active text style
      case L's':
        wcout << L"\nEntry the text style name to be set active:>" ;
        if((sName = EntryRecordName(pTextStyles, kMustExist)).isEmpty()) break;

        pDb->setTEXTSTYLE(pTextStyles->getAt(sName));
        break;


      case L'E':          // Expose the predefined text styles
      case L'e':
        pTextStyle = pDb->getTEXTSTYLE().safeOpenObject();
        wcout << L"\nCurrent active text style: \"" << pTextStyle->getName() << L"\"";

        pTextStyle = pDb->getTextStyleStandardId().safeOpenObject(OdDb::kForRead);
        wcout << L"\nStandard text style: \"" << pTextStyle->getName() << L"\"\n";
        break;


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

        pTextStyle = pTextStyles->getAt(sName, OdDb::kForWrite);

        wcout << L"\nEntry the new text style name:>";
        if((sName = EntryRecordName(pTextStyles, kMustAbsent)).isEmpty()) break;

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


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

        pTextStyle = pTextStyles->getAt(sName, OdDb::kForWrite, false);

        if(pTextStyle->objectId() == pDb->getTextStyleStandardId())
        {
          wcout << L"\nError: Standard text style can not be deleted\n";
          break;
        }
        if(pTextStyle->objectId() == pDb->getTEXTSTYLE())
        {
          wcout << L"\nError: Current active text style can not be deleted\n";
          break;
        }

        result = pTextStyle->erase(true);

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


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

        pTextStyle = pTextStyles->getAt(sName, OdDb::kForWrite, true);

        if(pTextStyle.isNull())
        {
          wcout << L"\nThe text style \"" << sName << L"\" - was deleted permanently\n";
          break;
        }

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


      case L'M':          // Modify the selected text style
      case L'm':
       	wcout << L"\nEntry the text style name to be modified:>" ;
        if((sName = EntryRecordName(pTextStyles, kMustExist)).isEmpty()) break;

        pTextStyle = pTextStyles->getAt(sName, OdDb::kForWrite);

        ModifyTextStyleProperties(pTextStyle.get());
        break;


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

      default:
        wcout << L"Incorrect operation\n";
    }
    wcout << L"\nL. List the accessible text styles";
    wcout << L"\nP. Print the properties of all text styles";
    wcout << L"\nE. Expose the predefined text styles";
    wcout << L"\nA. Add a new text style";
    wcout << L"\nS. Set the current active text style";
    wcout << L"\nN. Rename the selected text style";
    wcout << L"\nM. Modify the selected text style";
    wcout << L"\nD. Delete the selected text style";
    wcout << L"\nR. Recover the deleted text style";
    wcout << L"\nQ. Quit";
    wcout << L"\nSelect operation:>";
    wcin >> ch;
  }
  while(ch != L'Q' && ch != L'q');

  wcout << L"Stop modifying of text styles\n";
}

Testing gives the following results:


Start modifying of text styles
Text Style's Table 7, Class: AcDbTextStyleTable, Record: AcDbTextStyleTableRecord, Iterator: OdDbSymbolTableIterator

List of the accessible text styles:
"Standard"

L. List the accessible text styles
P. Print the properties of all text styles
E. Expose the predefined text styles
A. Add a new text style
S. Set the current active text style
N. Rename the selected text style
M. Modify the selected text style
D. Delete the selected text style
R. Recover the deleted text style
Q. Quit
Select operation:>

When the operation is A-"Add a new text style":


Entry the name of a new text style:>Style_A
List of the accessible text styles:
"Standard"
"Style_A"

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


Entry the name of a new text style:>Standard
Error: the name "Standard" - already exists

When the operation A-"Add a new text style" and the name is incorrect:


Entry the name of a new text style:>St?l*
Error: the name contains inadmissible letters: <>/\":;?,|=`

When the operation is N-"Rename the selected text style":


Entry the text style name to be renamed:>Style_A
Entry the new text style name:>myStyle

When the operation is N-"Rename the selected text style" and the name is absent from the table:


Entry the text style name to be renamed:>Style_A
Error: the name "Style_A" - is not found

When the operation is N-"Rename the selected text style" and the name is incorrect:


Entry the text style name to be renamed:>myStyle
Entry the new text style name:>St*le?
Error: the name contains inadmissible letters: <>/\":;?,|=`

When the operation is N-"Rename the selected text style" and the new name exists in the table:


Entry the text style name to be renamed:>myStyle
Entry the new text style name:>Standard
Error: the name "Standard" - already exists

Repeat the operation A-"Add a new text style":


Entry the name of a new text style:>Style_B
List of the accessible text styles:
"Standard"
"myStyle"
"Style_B"

When the operation is S-"Set the current active text style":


Entry the text style name to be set active:>Style_B

When the operation is E-"Expose the predefined text styles":


Current active text style: "Style_B"
Standard text style: "Standard"

When the operation is D-"Delete the selected text style":


Entry the text style name to be deleted:>myStyle

When the operation is D-"Delete the selected text style" and the name is absent from the table or if the record is already deleted:


Entry the text style name to be deleted:>myStyle
Error: the name "myStyle" - is not found

When the operation is D-"Delete the selected text style" and the text style is active:


Entry the text style name to be deleted:>Style_B
Error: Current active text style can not be deleted

When the operation is D-"Delete the selected text style" and the text style is predefined:


Entry the text style name to be deleted:>standard
Error: Standard text style can not be deleted

When the operation is M-"Modify the selected text style":


Entry the text style name to be modified:>Style_B
Start modifying of the text style properties

Selected text style: "Style_B"
P. Print specific properties
T. Set the Text|Shape status
D. Set the Direction typeface
S. Set the Letter Side typeface
O. Set the Orientation typeface
U. Set the Uni-Font filename
B. Set the Big-Font filename
W. Set the Width Scale Factor
H. Set the Height Scale Factor
L. Set the Last Height
A. Set the Oblique Angle
Q. Quit
Select operation:>D

Entry the Direction typeface [B-backward|F-forward]:>B
Typeface is set to: Backward

Select operation:>H

Current height scale factor = 0.2
Entry a new factor (text size) [f >= 0]:>1.2
Height Scale Factor is set to: 1.2

Select operation:>U

Uni-font filename: ""
Entry a shx-file name:>complex.shx
Uni-font is set to: "complex.shx"

Select operation:>Q
Stop modifying of the text style properties

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


List the specific properties of text styles
H=11
  Name = "Standard"
  Status: unerased,resident,text
  Uni-font: "txt"
  Big-font: ""
  TypeFace: forward,upside,horizontal
  Width Scale Factor = 1
  Height Scale Factor = 0
  Last Height = 0.2
  Oblique Angle = 0 radians
H=40
  Name = "myStyle"
  Status: erased,resident,text
  Uni-font: ""
  Big-font: ""
  TypeFace: forward,upside,horizontal
  Width Scale Factor = 1
  Height Scale Factor = 0.2
  Last Height = 0.2
  Oblique Angle = 0 radians
H=41
  Name = "Style_B"
  Status: unerased,resident,modified,text
  Uni-font: "complex.shx"
  Big-font: ""
  TypeFace: backward,upside,horizontal
  Width Scale Factor = 1
  Height Scale Factor = 1.2
  Last Height = 0.2
  Oblique Angle = 0 radians

When the operation is L-"List the accessible text styles":


List of the accessible text styles:
"Standard"
"Style_B"

When the operation is R-"Recover the deleted text style":


Entry the text style name to be recovered:>myStyle

When the operation is L-"List the accessible text styles":


List of the accessible text styles:
"Standard"
"myStyle"
"Style_B"

When the operation is R-"Recover the deleted text style" and the name is absent from the table or if the record is permanently deleted:


Entry the text style name to be recovered:>Style_A
The text style "Style_A" - was deleted permanently

Use the Q-"Quit" operation to exit:


Stop modifying of text styles

See Also

Working with Text Styles

Organization of Text Styles

Example of Working with the Text Style Record Object

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

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