Drawings SDK Developer Guide > Working with .dwg Files > Working with Entities > Working with Specific Entitites > Working with Tables > Working with Table Templates
Working with Table Templates

A table template is used for storing settings of an existing table and applying them to new tables.

Creating a table template

To create a table template object, one of two methods can be used: OdDbTable::createTemplate() or OdDbTableTemplate::capture(). For example,


OdDbTableTemplatePtr pTemplate = OdDbTableTemplate::createObject();
OdDbObjectId templateId = pDb->addOdDbObject(pTemplate);
pTable->createTemplate(pTemplate, (OdDb::TableCopyOption)(OdDb::kTableCopyOptionRowHeight |OdDb::kTableCopyOptionColumnWidth));

//or

OdDbTableTemplatePtr pTemplate = OdDbTableTemplate::createObject();
OdDbObjectId templateId = pDb->addOdDbObject(pTemplate);
pTemplate->capture(pTable, (OdDb::TableCopyOption)(OdDb::kTableCopyOptionRowHeight|OdDb::kTableCopyOptionColumnWidth));

In these two code fragments, pTemplate is a pointer to the template object, and pTable is the pointer to the source table entity. The second parameter is the OdDb::TableCopyOption enumeration which specifies copy options. For example, a new table template can have a structure as an existing table with or without its content, or if the content is a formula it cannot be copied, etc. The image above shows the source table and its copy created from the source table template without content.

Storing a table template

A table template can be stored in an existing or new table style in the table style dictionary. The sample below shows how to store a table template in a new table style.

// Create a new table style 
OdDbTableStylePtr pStyle = OdDbTableStyle::createObject();
OdString styleName(OD_T("MyTableStyle"));
// Open the table style dictionary
OdDbDictionaryPtr pDictionary = pDb->getNamedObjectsDictionaryId().safeOpenObject(OdDb::kForRead);
OdDbObjectId tblId1 = pDictionary->getAt(OdString(OD_T("ACAD_TABLESTYLE")));
OdDbDictionaryPtr pTableStyle =  tblId1.safeOpenObject(OdDb::kForWrite);
// If the new table style with the name is in the ACAD_TABLESTYLE dictionary, remove it
if(pTableStyle->has(styleName))
{
	pTableStyle->upgradeOpen();
	pTableStyle->remove(styleName);
}
pTableStyle->upgradeOpen();
// Add the new table style to the dictionary
OdDbObjectId tableStyleId = pTableStyle->setAt(styleName, pStyle);
// Store the new template in the table style
pStyle->setTemplate(templateId, OdDb::MergeCellStyleOption::kMergeCellStyleNone);

Note: If a table style with a template is applied to a new table using the OdDbTable::setTableStyle() method, the new table may not have the same view as the source table used for creating the template.

Applying a table template to a new table

To apply a template to a new table, the OdDbTableTemplate::createTable() method is used. In the second parameter of this method you can specify the copy options for applying the template. For example, the sample code below shows that the new table will have the template's column width, but the row height in this case will be set to the default value of the table style applied to the table.

// Create new table entity
OdDbTablePtr pNewTable = OdDbTable::createObject();
// Apply a template to the table
pTemplate->createTable(pNewTable, (OdDb::TableCopyOption)(OdDb::kTableCopyOptionColumnWidth));
// Set table style 
pNewTable->setTableStyle(pStyle->objectId()); 

See Also

Overview of Tables

Specific Table Properties

Editing a Table

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