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 > Making a Layer Active
Make a Layer Active

A layer in the layer table of the database can be the current active layer. The current active layer is the object that is automatically associated with a new entity when the entity is added in the database. Each entity must belong to one of the layers of the drawing. Each entity has the layer() method that returns the name of the associated layer, the layerId() method that returns the ID of the associated layer record object, and the setLayer() method that sets the layer for the entity.

The layer controls visibility, accessibility, plotability, and default values of properties for entities that are associated with it. If a property of an entity is set to ByLayer, the entity uses the corresponding property of the associated layer record object. When an entity is drawn, it can use the linetype, lineweight, material, color, and transparency of the associated layer. When the program creates an entity and adds it in the database, the new entity gets the current active layer as a default object to set the association with the layer that will control it.

The CLAYER system variable stores the object ID of the current active layer. This variable is set to the predefined zero layer by default, but the program can set the active layer to any of the available layers after it is created in the database. The database object provides access to the 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 layer table object. In the examples below, the pLayers variable stores a pointer to the layer 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 layer table object
OdDbDatabasePtr pDb = pLayers->database();

To get the current active layer, use the getCLAYER() of the database object which does not have arguments and returns the OdDbObjectId instance associated with the current active layer record object. For example:


// Get the ID of the current active layer
OdDbObjectId idActiveLayer = pDb->getCLAYER();

To get the smart pointer to the current active layer, declare a variable of the OdDbLayerTableRecordPtr type and use the safeOpenObject() method of the obtained OdDbObjectId object. The current active layer 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 layer in the read mode
OdDbLayerTableRecordPtr pActiveLayer = idActiveLayer.safeOpenObject(OdDb::kForRead);

// Open the current active layer in the write mode
OdDbLayerTableRecordPtr pActiveLayer = idActiveLayer.safeOpenObject(OdDb::kForWrite);

These operations can be combined in one line:


OdDbLayerTableRecordPtr pActiveLayer = pLayers->database()->getCLAYER().safeOpenObject();
odPrintConsoleString(L"\n\nCurrent active layer: \"%ls\"\n", pActiveLayer->getName().c_str());

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


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

pDb->setCLAYER(idLayer);

To get the ID of the layer record object that has the pointer, use the objectId() method of the layer record object; it returns the OdDbObjectId instance associated with this layer. For example:


OdDbLayerTableRecordPtr pLayer = pLayers->getAt("LayerX", OdDb::kForWrite, false);
pDb->setCLAYER(pLayer->objectId());

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

Note: The current active layer ca not be deleted. Change the active layer before deleting.

See Also

Working with Layers

Getting and Checking Layers

Example of Working with the Layer Table Object

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