AutoCAD

Turn on all layers, then turn layers back to previous state

Ease of Use

Easy

Version tested with

2004, 2005 

Submitted by:

malik641

Description:

When used first, it will turn on all layers (TurnedOn = True & Freeze = False) and unlock all layers. When used a second time, all layers are restored back to their original state. 

Discussion:

When working with layers in templates at my job, I usually have to turn on all the layers in order to make proper changes to the template. But it is a pain to have to reset all the layers back to their original state when I am done with the template. So I developed this to do it for me. 

Code:

instructions for use

			

Option Explicit Public varLay() As Variant Public i As Integer Public blnlayerstate As Boolean 'False is all layers restored, True is all layers on Sub ToggleLayers() 'In case there is a purged layer before the switch back to normal 'or a switch in a drawing occured (not the same drawing when originally used). On Error Resume Next Dim objLayer As AcadLayer Dim intJ As Integer If blnlayerstate = False Then i = 0 'Iterates through all the layers and stores 'their visibility and locked values. Then turns 'on all layers. For Each objLayer In ThisDrawing.Layers If objLayer.Name <> "0" Then ReDim Preserve varLay(0 To 3, i) varLay(0, i) = objLayer.Name varLay(1, i) = objLayer.Freeze varLay(2, i) = objLayer.LayerOn varLay(3, i) = objLayer.Lock objLayer.Freeze = False objLayer.LayerOn = True objLayer.Lock = False i = i + 1 End If Next ThisDrawing.Utility.Prompt vbCrLf & "All layers are on, thawed & unlocked." Else 'Restores all layers to previous state For intJ = 0 To i - 1 ThisDrawing.Layers.Item(varLay(0, intJ)).Freeze = varLay(1, intJ) ThisDrawing.Layers.Item(varLay(0, intJ)).LayerOn = varLay(2, intJ) ThisDrawing.Layers.Item(varLay(0, intJ)).Lock = varLay(3, intJ) Next intJ ThisDrawing.Utility.Prompt vbCrLf & "All layers are restored to previous state." End If 'Boolean switch to determine the state of the layers (All on / Restored) If blnlayerstate = False Then blnlayerstate = True Else blnlayerstate = False ThisDrawing.Regen acAllViewports End Sub

How to use:

  1. Open the VBE (Alt+F11)
  2. Insert a new module (or use an existing if you prefer) Insert --> Module
  3. Copy and Paste code into the new module (be sure no duplicates of "Option Explicit" exist)
  4. Save the project
  5. Close the VBE
 

Test the code:

  1. In AutoCAD, choose Tools --> Macro --> Macros... (or Alt+F8) and choose "ToggleLayers" to Activate the code
  2. Repeat step 1 to restore the previous layer settings
  3. If you like, create a user defined button and in the Button Properties under "Macro Associated with this Button" paste the following (without quotes): "-vbarun ToggleLayers"
 

Sample File:

No Attachment 

Approved by lucas


This entry has been viewed 56 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express