Creating and Modifying a Layer Table Record

The following example shows obtaining the layer table for the current database and opening it for writing. It creates a new layer table record (AcDbLayerTableRecord) and sets certain attributes of the layer (name, frozen attribute, on/off, viewport, and locked). Then it creates a color class object and sets the color of the layer to red.

To set the linetype for the layer, this example opens the linetype table for reading and obtains the object ID of the linetype record for the desired linetype (here, “DASHED”). Once it has the object ID for the linetype, it closes the linetype table and sets the linetype for the new layer table record. This example uses the add() function to add the layer table record to the layer table. Finally, it closes the layer table record and the layer table itself.

void
addLayer()
{
    AcDbLayerTable *pLayerTbl;
    acdbHostApplicationServices()->workingDatabase()
        ->getSymbolTable(pLayerTbl, AcDb::kForWrite);
    if (!pLayerTbl->has("ASDK_TESTLAYER")) {
        AcDbLayerTableRecord *pLayerTblRcd
            = new AcDbLayerTableRecord;
        pLayerTblRcd->setName("ASDK_TESTLAYER");
        pLayerTblRcd->setIsFrozen(0);// layer to THAWED
        pLayerTblRcd->setIsOff(0);   // layer to ON
        pLayerTblRcd->setVPDFLT(0);  // viewport default
        pLayerTblRcd->setIsLocked(0);// un-locked
        AcCmColor color;
        color.setColorIndex(1); // set color to red
        pLayerTblRcd->setColor(color);
        // For linetype, we need to provide the object ID of
        // the linetype record for the linetype we want to
        // use. First, we need to get the object ID.
        //
        AcDbLinetypeTable *pLinetypeTbl;
        AcDbObjectId ltId;
        acdbHostApplicationServices()->workingDatabase()
            ->getSymbolTable(pLinetypeTbl, AcDb::kForRead);
        if ((pLinetypeTbl->getAt("DASHED", ltId))
            != Acad::eOk)
        {
            acutPrintf("\nUnable to find DASHED"
                " linetype. Using CONTINUOUS");
            // CONTINUOUS is in every drawing, so use it.
            //
            pLinetypeTbl->getAt("CONTINUOUS", ltId);
        }
        pLinetypeTbl->close();
        pLayerTblRcd->setLinetypeObjectId(ltId);
        pLayerTbl->add(pLayerTblRcd);
        pLayerTblRcd->close();
        pLayerTbl->close();
    } else {
        pLayerTbl->close();
        acutPrintf("\nlayer already exists");
    }
}