Close

Relief for ODA Team in Ukraine

Learn more
ODA Drawings SDK
Making a Text Style Active

One of the text styles in the text style table of the database can be current active text style. The current active text style is the object that is automatically associated with any new text entity added in the database. For example, the OdDbText and OdDbMText entities have the textStyle() method that returns the ID of the associated text style record object, and the setTextStyle() method that sets the text style record object. When the program creates a new text entity and adds it in the database, this entity is associated with the current active text style by default.

The TEXTSTYLE system variable stores the object ID of the current active text style. This variable has the OdDb::kNull value by default, therefore the program must set an available text style to be active after creating the database. The database object provides access to system variables using get#() and set#() methods, where # is the variable name. The program can obtain the database object either from the host application object or text style table object. In the example below, the pTextStyles variable stores a pointer to the text style table object and the pHost variable stores a pointer to the host application object. For example:


// Create the database object using the host application object
OdDbDatabasePtr pDb = pHost->createDatabase();

// Get the database object from the text style table object
OdDbDatabasePtr pDb = pTextStyles->database();

To get the current active text style, use the getTEXTSTYLE() of the database object; it does not have arguments and returns the OdDbObjectId instance associated with the current active text style record object. For example:


// Get the ID of the current active text style
OdDbObjectId idActiveTextStyle = pDb->getTEXTSTYLE();

To get a smart pointer to the current active text style, declare a variable of the OdDbTextStyleTableRecordPtr type and use the safeOpenObject() method of the obtained OdDbObjectId object. The current active text style cannot be erased, therefore the safeOpenObject() method requires only the open mode of the OdDb::OpenMode type (kForRead, kForWrite, or kForNotify). For example:


// Open the current active text style in the read mode
OdDbTextStyleTableRecordPtr pActiveTextStyle = idActiveTextStyle.safeOpenObject(OdDb::kForRead);

// Open the current active text style in the write mode
OdDbTextStyleTableRecordPtr pActiveTextStyle = idActiveTextStyle.safeOpenObject(OdDb::kForWrite);

These operations can be combined in one line:


OdDbLinetypeTableRecordPtr pActiveTextStyle = pTextStyles->database()->getTEXTSTYLE().safeOpenObject();
odPrintConsoleString(L"\n\nCurrent active text style: \"%ls\"\n", pActiveTextStyle->getName().c_str());

To make a text style active, use setTEXTSTYLE() of the database object; it requires the ID of the text style record object to be set as the active text style as an argument of the OdDbObjectId type, and it does not return a value. For example:


OdDbTextStyleTableRecordPtr pTextStyle = OdDbTextStyleTableRecord::createObject();
pTextStyle->setName("TextStyleX");
OdDbObjectId idTextStyle = pTextStyles->add(pTextStyle);

pDb->setTEXTSTYLE(idTextStyle);

To get the ID of the text style record object that has a pointer to it, use the objectId() method of the text style record object to return the OdDbObjectId instance associated with the text style. For example:


OdDbTextStyleTableRecordPtr pTextStyle = pTextStyles->getAt("TextStyleX", OdDb::kForWrite);
pDb->setTEXTSTYLE(pTextStyle->objectId());

If another text style becomes the current active text style, entities associated with the previous active text style do not change; their association with the previous text style is saved. Changing the current active text style affects new text entities only.

Note: The current active text style cannot be deleted. Change the current active text style before deleting.

See Also

Working with Text Styles

Getting and Checking Text Styles

Example of Working with the Text Style Table Object

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