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 > Getting and Checking Layers
Getting and Checking Layers

The layer table object has the has() method for checking a layer in the table and the getAt() method for getting an existing layer from the table. In the examples below, the pLayers variable stores a pointer to the layer table object. The following example adds three layers in the table and deletes one of them:


OdDbLayerTableRecordPtr pLayer;

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

pLayer = OdDbLayerTableRecord::createObject();
pLayer->setName("ErasedLayer");
pLayers->add(pLayer);
pLayer->erase(true);

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

To check whether the layer exists in the table, use the has() method of the layer table object; it requires either the existing layer name or the ID associated with the existing layer record object as an argument of the method, and the method returns True if the layer exists in the table or False if the layer is absent from the table or is erased. For example:


// Check the unerased layer object by name
odPrintConsoleString(L"\nLayer X %ls in the table", ((pLayers->has("LayerX")) ? L"exists" : L"absents"));

// Check the unerased layer object by ID
odPrintConsoleString(L"\nLayer Y %ls in the table", ((pLayers->has(pLayer->objectId())) ? L"exists" : L"absents"));

// Check the erased layer object (result is absent)
odPrintConsoleString(L"\nErased Layer %ls in the table", ((pLayers->has("ErasedLayer")) ? L"exists" : L"absents"));

To get a layer from the layer table, use the getAt() method of the layer table object; it requires either the name or the ID of the layer record object as a parameter for searching. The getAt() method has two implementations.

The first implementation of the getAt() method returns the smart pointer to the existing layer record object that satisfies the specified parameters. The method has three arguments:

  • The first argument is mandatory and specifies the layer name as a nonempty OdString value that satisfies the requirements for naming objects.
  • The second argument is optional and specifies the mode for opening the layer record object as a value of the OdDb::OpenMode enumerator (kForRead, kForWrite, or kForNotify). The value is OdDb::kForRead by default.
  • The third argument is optional and specifies the erase status as a Boolean value which is True if the layer table object must open an erased layer record object or False when the layer table object must open an unerased layer record object. The value is False by default.

To get a smart pointer to the layer record object, declare a variable of the OdDbLayerTableRecordPtr type and use the first implementation of the getAt() method. For example:


// Get the unerased layer object in the read mode
OdDbLayerTableRecordPtr pLayer1 = pLayers->getAt("LayerX");

// Get the unerased layer object in the write mode
OdDbLayerTableRecordPtr pLayer2 = pLayers->getAt("LayerY", OdDb::kForWrite, false);

// Get the unerased or erased layer object in the notify mode
OdDbLayerTableRecordPtr pLayer3 = pLayers->getAt("ErasedLayer", OdDb::kForNotify, true);

The second implementation of the getAt() method returns the OdDbObjectId instance associated with the layer record object that satisfies the specified name. The method has two arguments:

  • The first argument is mandatory and specifies the layer name as a nonempty OdString value.
  • The second argument is optional, specifies the erase status as a Boolean value, and is False by default.

To get the ID of the layer record object, declare a variable of the OdDbObjectId type and use the second implementation of the getAt() method. For example:


// Get the ID of the unerased layer object
OdDbObjectId idLayer1 = pLayers->getAt("LayerX");

// Get the ID of the unerased layer object
OdDbObjectId idLayer2 = pLayers->getAt("LayerY", false);

// Get the ID of the erased layer object
OdDbObjectId idLayer3 = pLayers->getAt("ErasedLayer", true);

To get the smart pointer to the layer instance with matching ID, use the safeOpenObject() method of the OdDbObjectId object to open the layer record object associated with the specified ID in the specified mode for the specified erase status, and return the a smart pointer to it. For example:


// Get the unerased layer object in the read mode using ID
OdDbLayerTableRecordPtr pLayer1 = idLayer1.safeOpenObject();

// Get the unerased layer object in the write mode using ID
OdDbLayerTableRecordPtr pLayer2 = idLayer2.safeOpenObject(OdDb::kForWrite, false);

// Get the unerased or erased layer object in the notify mode using ID
OdDbLayerTableRecordPtr pLayer3 = idLayer3.safeOpenObject(OdDb::kForNotify, true);

These operations can be combined in one line:


OdDbLayerTableRecordPtr pLayer = pLayers->getAt("LayerY").safeOpenObject(OdDb::kForWrite);

When the erase status is False, the getAt() method gets the layer record object if and only if it is marked as "unerased". When the erase status is True, the getAt() method gets the layer record object of any status, that is, marked as "erased" or "unerased".

The getAt() method returns the ID = kNull or smart pointer = Null when the layer with the specified name is absent from the layer table, is permanently deleted, not opened in the specified mode, or marked as "erased" and the status argument is False. For example:


// When ID = kNull
OdDbObjectId idLayer = pLayers->getAt("LayerN");
if(idLayer.isNull())
  odPrintConsoleString(L"\nThe layer N can not be opened\n");

// When Pointer = Null
OdDbLayerTableRecordPtr pLayer = pLayers->getAt("LayerN", OdDb::kForWrite);
if(pLayer.isNull())
  odPrintConsoleString(L"\nThe layer N can not be opened\n");

Before getting a layer, use the has() method for checking whether the layer with the specified name exists in the layer table.

See Also

Working with Layers

Adding and Naming Layers

Example of Working with the Layer Table Object

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