Make a Layer Current (.NET)

Dec 16, 2014  |  In-Product View

You are always drawing on the active layer. When you make a layer active, you create new objects on that layer. If you make a different layer active, any new objects you create is assigned that new active layer and uses its color and linetype. You cannot make a layer active if it is frozen.

To make a layer active, use the Clayer property of the Database object or the CLAYER system variable. For example:

Make a layer current through the database

This example sets a layer current through the Database object with the Clayer property.

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("SetLayerCurrent")> _
Public Sub SetLayerCurrent()
    '' Get the current document and database
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
    Dim acCurDb As Database = acDoc.Database

    '' Start a transaction
    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

        '' Open the Layer table for read
        Dim acLyrTbl As LayerTable
        acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _
                                     OpenMode.ForRead)

        Dim sLayerName As String = "Center"

        If acLyrTbl.Has(sLayerName) = True Then
            '' Set the layer Center current
            acCurDb.Clayer = acLyrTbl(sLayerName)

            '' Save the changes
            acTrans.Commit()
        End If

        '' Dispose of the transaction
    End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("SetLayerCurrent")]
public static void SetLayerCurrent()
{
    // Get the current document and database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Layer table for read
        LayerTable acLyrTbl;
        acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
                                        OpenMode.ForRead) as LayerTable;

        string sLayerName = "Center";

        if (acLyrTbl.Has(sLayerName) == true)
        {
            // Set the layer Center current
            acCurDb.Clayer = acLyrTbl[sLayerName];

            // Save the changes
            acTrans.Commit();
        }

        // Dispose of the transaction
    }
}

VBA/ActiveX Code Reference

ThisDrawing.ActiveLayer = ThisDrawing.Layers("Center")

Make a layer current with the CLAYER system variable

This example sets a layer current with the CLAYER system variable.

VB.NET

Application.SetSystemVariable("CLAYER", "Center")

C#

Application.SetSystemVariable("CLAYER", "Center");

VBA/ActiveX Code Reference

ThisDrawing.SetVariable "CLAYER", "Center"

Discuss