Drawings SDK Developer Guide > Working with .dwg Files > Working with Databases > Working with Database Containers > Working with Predefined Tables of Named Records > Working with Specific Predefined Tables of Named Records > Layers Table > Manipulating of Layers > Deleting and Recovering Layers
Deleting and Recovering Layers

The layer table object does not have a method for deleting a layer. Each layer record object deletes itself. When the layer deletes itself, the database marks it as «erased», and the layer is continued to be stored in the layer table until the database is not saved in a file. This provides the ability to recover layers. Recovery is possible only if the undo process is started for the database, otherwise any objects are deleted permanently. In the following examples, the pLayers variable stores a pointer to the layer table object. For example, add a layer in the table:


OdDbLayerTableRecordPtr pLayer = OdDbLayerTableRecord::createObject();
pLayer->setName("LayerX");
pLayers->add(pLayer);

To start the undo process, use the startUndoRecord() method of the database object. To get the database object from the layer table object, use its database() method. For example:


pLayers->database()->startUndoRecord();

Deleting a layer

To delete a layer, get the smart pointer to the layer record object in write mode and use the erase() method when its argument equals True. The erase() method marks the layer as «erased» and returns the result status as the OdResult value. For example:


pLayer = pLayers->getAt("LayerX", OdDb::kForWrite);
OdResult result = pLayer->erase(true);

The program can use the obtained result value for verification. For example:


if(result) 
  odPrintConsoleString(L"\nError: %d - %ls", result, pLayers->database()->appServices()->getErrorDescription(result)); 

If the layer record object is not opened in write mode, the erase() method generates the exception: «62 - Not opened for write».

If the layer is the current active layer or is zero, the erase() method generates the exception: «116 - Object can't be erased». Therefore, check the layer before deleting it. For example:


if(pLayer->objectId() == pLayers->database()->getLayerZeroId())
  odPrintConsoleString(L"\nError: Zero layer can not be deleted"); 

if(pLayer->objectId() == pLayers->database()->getCLAYER())
  odPrintConsoleString(L"\nError: Current active layer can not be deleted"); 

Recovering a layer

The layer marked as «erased» is stored in the database and can be recovered from the layer table. For recovery, the program must start the undo procedure for the database, otherwise the erase()method deletes the layer permanently. To recover a layer, get the smart pointer to the deleted layer record object in write mode and use the erase() method when its argument equals False. The erase() method marks the layer as «unerased». For example:


pLayer = pLayers->getAt("LayerX", OdDb::kForWrite, true);
if(!pLayer.isNull())
  pLayer->erase(false);

If the layer was permanently deleted, the erase(false) method cannot recover it and generates the exception: «104 - Object was permanently deleted».

Note: The layer table must not be empty because new entities will obtain OdDb::kNull instead of the layer ID by default, and this action creates an invalid situation.

See Also

Working with Layers

Make a Layer Active

Adding and Naming Layers

Getting and Checking Layers

Example of Working with the Layer Table Object

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