Making a Layer Active

When you create new entities in AutoCAD, they're placed on the current or active layer. Therefore, to draw entities on a specific layer, you must first make that layer active.

■ Note You can't make a layer active if it's frozen. However, you can check for this condition before you attempt to make a layer active. For more details, see the section "Setting a Layer to Be Frozen or Thawed."

The ActiveLayer property is a member of the Document object. To make a specific layer active, assign the Layer object to the ActiveLayer.

DocumentObject.ActiveLayer = LayerObject

The following code makes the layer named "Walls" the active layer for the current Document:

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

New entities will now be placed on the "Walls" layer until another layer is made active. You can change the layer of existing entities by altering the Layer property of that particular entity. The Layer property is a string that corresponds to the name of a Layer object. The following example lets the user pick a drawing entity and specify a new layer name for that entity:

Public Sub ChangeEntityLayer()

On Error Resume Next ' handle exceptions inline

Dim objEntity As AcadEntity

Dim varPick As Variant

Dim strLayerName As String

Dim objLayer As AcadLayer

ThisDrawing.Utility.GetEntity objEntity, varPick, "Select an entity" If objEntity Is Nothing Then

MsgBox "No entity was selected" Exit Sub ' exit if no entity picked End If strLayerName = InputBox("Enter a new Layer name: ")

If "" = strLayerName Then Exit Sub ' exit if no name entered

Set objLayer = ThisDrawing.Layers(strLayerName) If objLayer Is Nothing Then

MsgBox "Layer was not recognized" Exit Sub ' exit if layer not found End If objEntity.Layer = strLayerName ' else change entity layer

End Sub

You can determine if a specific layer is active by comparing the string to the ActiveLayer.Name property:

If ThisDrawing.ActiveLayer.Name = "Walls" Then ...

This is a pretty common operation, so make it a function. You'll also want to allow for various combinations of upper- and lowercase letters, as AutoCAD does:

Public Function IsLayerActive(strLayerName As String) As Boolean IsLayerActive = False 'assume failure

If 0 = StrComp(ThisDrawing.ActiveLayer.Name, strLayerName, _ vbTextCompare) Then IsLayerActive = True End If End Function

This IsLayerActive function checks whether a particular string corresponds to the active layer name. True will be returned if the layer is active, otherwise False will be returned. The following gets a layer name from the user and employs the new function to see if it's active:

Public Sub LayerActive()

Dim strLayerName As String strLayerName = InputBox("Name of the Layer to check: ")

If IsLayerActive(strLayerName) Then

MsgBox & strLayerName & "' is active"


MsgBox & strLayerName & "' is not active"

End If End Sub

+1 0


  • Atso
    How to make a layer autocad 2006?
    8 years ago

Post a comment