Close

Relief for ODA Team in Ukraine

Learn more
ODA Drawings SDK
Adding and Naming Text Styles

To create a new text style, declare a variable of the OdDbTextStyleTableRecordPtr type and use the pseudo-constructor of the text style record object. This pseudo-constructor is a static method that creates a new dynamic text style instance and returns a smart pointer to it. For example:


OdDbTextStyleTableRecordPtr pTextStyle = OdDbTextStyleTableRecord::createObject();

The created instance exists independently from the database. Many properties of the text style record object are invalid and the created instance does not get an ID until the text style is added in the database. The text style table object cannot manipulate the new text style without the OdDbObjectId instance associated with the created text style record object. The new text style instance gets an empty name by default. The text style table object cannot identify a text style without a name. Therefore, after creating, the program must assign a name for the new text style, and then, must add it in the text style table of the database.

The text style name is an arbitrary nonempty string that can be up to 255 characters long and can contain letters, digits, blank spaces, underscores, and some special characters, but cannot contain inadmissible letters (see Naming objects). The database contains a predefined text style with a fixed name: "Standard". The text style record object has the getName() method for getting the name and the setName() method for setting the name.

To get the name, use the getName() method of the text style record object; it does not have arguments and returns the text style name as an OdString value. For example:


OdString sName = pTextStyle->getName();
odPrintConsoleString(L"\nText style has the name: %s", sName.c_str());

To set the name, use the setName() method of the text style record object; it requires the name as a non-empty OdString value and does not return a value. For example:


pTextStyle->setName("NewTextStyleX");

If the text style name contains inadmissible letters, the setName() method generates the exception: «37 - Invalid Symbol Table name». If the text style name is an empty string, the setName() method generates the exception: «106 - Empty Record name».

To add a text style in the text style table, use the add() method of the text style table object that requires one argument — the pointer to the text style instance to be added — and returns the OdDbObjectId instance associated with the added text style record object. In the example below, the pTextStyles variable stores a pointer to the text style table object. For example:


OdDbObjectId idTextStyle = pTextStyles->add(pTextStyle);

If the text style table already contains a text style by the same name, the add() and setName() methods generate the exception: «104 - Duplicate Record name object [10]». The exception message contains the handle of the duplicate text style in the square brackets. To check whether the text style table contains the specified name, use the has() method of the text style table object.

To catch exceptions, use the try … catch statement when the program adds the text style record object in the table. The following example demonstrates adding a text style:


void NewTextStyle(OdString sTSName, OdDbTextStyleTablePtr pTextStyles)
{
  if(sTSName.isEmpty())

    odPrintConsoleString(L"\nThe text style name is undefined\n");

  else if(sTSName.findOneOf("<>/\\\":;?,|=`") != -1)

    odPrintConsoleString(L"\nThe text style name contains inadmissible letters\n");

  else if(pTextStyles->has(sTSName))

    odPrintConsoleString(L"\nThe text style with name \"%s\" already exists\n", sTSName.c_str());

  else
  {
    OdDbTextStyleTableRecordPtr pTextStyle = OdDbTextStyleTableRecord::createObject();

    try {
      pTextStyle->setName(sTSName);
      pTextStyles->add(pTextStyle);
    }
    catch(OdError& e)
    {
      odPrintConsoleString(L"\nError: %d - %ls\n", e.code(), e.description());
      pTextStyle->erase();
    }
  }
}

See Also

Working with Text Styles

Getting and Checking Text Styles

Deleting and Recovering Text Styles

Example of Working with the Text Style Table Object

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