lisp for Next-Layer and Previous-Layer

lisp for Next-Layer and Previous-Layer

zasanil
Advocate Advocate
838 Views
2 Replies
Message 1 of 3

lisp for Next-Layer and Previous-Layer

zasanil
Advocate
Advocate

I found a lisp in this thread located here: visual-lisp-autolisp-and-general/possible-lisp that allowes me to switch to the next layer and freeze all the others. The lisp code is:

 

(defun c:NextLayer (/ ActDoc Layers CurLay OldLay LayList tmpPos)

 (vl-load-com)
 (command "cmdecho" (getvar "cmdecho"))
 (setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
 (setq Layers (vla-get-Layers ActDoc))
 (setq CurLay (getvar "clayer"))
 (setq OldLay (vla-item Layers CurLay))
 (vlax-for Lay Layers
 (setq LayList (cons (vla-get-Name Lay) LayList))
 (if (/= (vla-get-Name Lay) CurLay)
 (vla-put-LayerOn Lay :vlax-false)
 )
 )
 (setq LayList (vl-sort LayList '<))
 (setq tmpPos (vl-position CurLay LayList))
 (if (= (1+ tmpPos) (length LayList))
 (progn
 (setq tmpPos -1)
 (prompt "\n Reached end of layers, starting over again.")
 )
 )
 (setq CurLay (vla-Item Layers (nth (1+ tmpPos) LayList)))
 (vla-put-Freeze CurLay :vlax-false)
 (vla-put-Lock CurLay :vlax-false)
 (vla-put-LayerOn CurLay :vlax-true)
 (setvar "clayer" (nth (1+ tmpPos) LayList))
 (vla-put-LayerOn OldLay :vlax-false)
 (vla-Regen ActDoc acActiveViewport)
 (princ)
 )

 This works great for going to the next layer, but I couldn't find a similiar lisp to go to the previous layer. Does anyone know of a lisp that can do this?

As a note, I have used laywalk, but with this program I can hotkey it to just cycle through layers as I wish.

Thanks!

Dan Nicholson C.I.D.
PCB Design Engineer
0 Likes
Accepted solutions (1)
839 Views
2 Replies
Replies (2)
Message 2 of 3

paullimapa
Mentor
Mentor
Accepted solution

This should work for Previous Layer. 

But note that currently both NextLayer & PrevLayer does not check for xref layer names which cannot be made current so the routine would fail at this point.

 

(defun c:PrevLayer (/ ActDoc Layers CurLay OldLay LayList tmpPos)

 (vl-load-com)
 (command "cmdecho" (getvar "cmdecho"))
 (setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
 (setq Layers (vla-get-Layers ActDoc))
 (setq CurLay (getvar "clayer"))
 (setq OldLay (vla-item Layers CurLay))
 (vlax-for Lay Layers
 (setq LayList (cons (vla-get-Name Lay) LayList))
 (if (/= (vla-get-Name Lay) CurLay)
 (vla-put-LayerOn Lay :vlax-false)
 )
 )
 (setq LayList (vl-sort LayList '<))
 (setq tmpPos (vl-position CurLay LayList))
 (if (< (1- tmpPos) 0)
 (progn
 (setq tmpPos (length LayList))
 (prompt "\n Reached begginning of layers, starting at end.")
 )
 )
 (setq CurLay (vla-Item Layers (nth (1- tmpPos) LayList)))
 (vla-put-Freeze CurLay :vlax-false)
 (vla-put-Lock CurLay :vlax-false)
 (vla-put-LayerOn CurLay :vlax-true)
 (setvar "clayer" (nth (1- tmpPos) LayList))
 (vla-put-LayerOn OldLay :vlax-false)
 (vla-Regen ActDoc acActiveViewport)
 (princ)
 )

 

Area Object Link | Attribute Modifier | Dwg Setup | Feet-Inch Calculator
Layer Apps | List on Steroids | VP Zoom Scales |Exchange App Store


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 3

zasanil
Advocate
Advocate

That works great! I currently don't use xrefs so It shouldn't be a bother for me in that area.

Thank you very much!

Dan Nicholson C.I.D.
PCB Design Engineer
0 Likes