Jump to content

Recommended Posts

Posted (edited)

Hello Everyone,

i am looking for a lisp that can help me to switch layers next one or previous one back to back. means if i have 5 layers and i want to switch from 1 to 2 so i should press "AS"  or 2 to 1 then "SA" in order to get next or previous layer.

 

thanks 

Edited by hibba
Posted (edited)

will control+ page down / page up work just as well? 

 

WHOOPS - answering the wrong question!!

Edited by Steven P
Posted

i tried and this works to switch between layouts. i need layer switching LISP

Posted

 

 

This will get you a list of all the layers: (https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/get-list-of-all-layers-in-lisp/td-p/822262)

 

(vlax-for lyr (vla-get-layers (vla-get-activedocument (vlax-get-acad-object) ) )
  (setq table (cons (vla-get-name lyr) table))
)

 

 

and this will set a layer to the current to the first in the list (position 0):

 

(command "setvar" "clayer" (nth 0 table))

 

and this gets the current layer

 

(getvar "clayer")

 

So I reckon with a check that you don't go past the end layer in the (nth 0 table) this should help you make something up?

 

 

Remember that what AutoCAD thinks is the first layer might not be what you think it it. you might need to sort the layer list alphabetically to be the same as the layer manager display

Posted (edited)

thanks for the valuable reply. i don't have much knowledge to understand the codes. if you can provide me a single lisp code so i can convert that into lisp to works it.

to understand in a better way please see the list of layers below. i just want to switch from 12 to 25 or 32 based to the layer i want (before drawing anything). 

image.png.d613889cef77d01f62ee5e7ff34cdd00.png

 

this can be done with the mouse selection but this is time consuming. i just want to reduce the time for selection as i need to switch in layers many time 

Edited by hibba
Posted (edited)

building off of @Steven P this should sort the layers into alphabetical order.

 

;;----------------------------------------------------------------------------;;
;; Step Down one layer A-DIM > A-DIM 100
(defun C:LD (/ p laylst)
  (if (and (setq p (vl-position (getvar 'clayer) (layers))) (> (length laylst) 1) (> (- (length laylst) 1) p))
    (setvar 'clayer (nth (+ p 1) laylst))
    (prompt "\nOn Last Layer/Or Only One Layer")
  )
  (princ)
)
;;----------------------------------------------------------------------------;;
;; Step up one layer A-DIM 100 > A-DIM
(defun C:LP (/ p laylst)
  (if (and (setq p (vl-position (getvar 'clayer) (layers))) (> (length laylst) 1) (/= p 0))
    (setvar 'clayer (nth (- p 1) laylst))
    (prompt "\nOn First Layer/Or Only One Layer")
  )
  (princ)
)
(defun layers ()
  (setq laylst (list))
  (vlax-for lyr (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
    (setq laylst (cons (vla-get-name lyr) laylst))
  )
  (setq laylst (vl-sort laylst <)) ;sort to be in alphabetical order
)

 

 

Edited by mhupp
Thanks StevenP
  • Like 1
Posted

Typos!! 

(setq table (vl-sort table <)) in 'Layers' LISP should be

(setq table (vl-sort table <))

 

should line (setvar 'clayer (nth (- 1 l) table)) be swapped in 'LP' LISP: 

(setvar 'clayer (nth (- l 1) table))

 

final one, as it is each iteration adds another set of layer names in layers list. Might be tempted to set 'table' as a blank list in the defun 'layers'

(setq table (list))

 

However, very nice again.

  • Like 1
Posted (edited)

Yeah prob on the best idea to using lowercase L as the variable there to hard to see the difference with the fonts I use.

Edited by mhupp
  • Like 1
Posted

i used the lisp and getting this error {{Error : bad function: #<SUBR @00000292a7e3faa8 <>}}

Posted

when I say "typos" I should really check what I am writing after that so I don't make them myself.... whoops

 

Try this in the LISP 'Layers' there was a ' missing I tihnk

 

(setq table (vl-sort table '<))

 

Posted

thanks for the reply 🙂. see if i did this right?

 

Quote

;;----------------------------------------------------------------------------;;
;; Step Down one layer A-DIM > A-DIM 100
(defun C:LD (/ p laylst)
  (if (and (setq p (vl-position (getvar 'clayer) (layers))) (> (length laylst) 1) (> (- (length laylst) 1) p))
    (setvar 'clayer (nth (+ p 1) table))
    (prompt "\nOn Last Layer/Or Only One Layer")
  )
  (princ)
)
;;----------------------------------------------------------------------------;;
;; Step up one layer A-DIM 100 > A-DIM
(defun C:LP (/ p laylst)
  (if (and (setq p (vl-position (getvar 'clayer) (layers))) (> (length laylst) 1) (/= p 0))
    (setvar 'clayer (nth (- p 1) table))
    (prompt "\nOn First Layer/Or Only One Layer")
  )
  (princ)
)
(defun layers ()
  (setq laylst (list))
  (vlax-for lyr (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
    (setq laylst (cons (vla-get-name lyr) laylst))
  )
  (setq table (vl-sort table '<)) ;sort to be in alphabetical order
)
 

Posted (edited)

i tried the lisp which is working somehow but if i create a new layer later that lisp do not consider it in order and skipping it to the next one.

really thanks if the layer order can be solved too.  

i made a filter layer and selected only those layers which i need for works now getting this error if using the lisp "On Last Layer/Or Only One Layer"

Edited by hibba
Posted

just for fun

 

;;; switch layer Rlx 11-oct-2022
(defun c:swl ( / _laylist lay-list cur-lay pos max-pos done inp)
  (vl-load-com)
  ;;; (setq l (_laylist))
  (defun _laylist ( / d r) (while (setq d (tblnext "layer" (null d)))(setq r (cons (cdr (assoc 2 d)) r))) r)
  (setq lay-list (acad_strlsort (_laylist)) cur-lay (getvar "clayer")
        pos (vl-position cur-lay lay-list) max-pos (1- (length lay-list)))
  
  (princ "\nSwitch layer (Lmouse or - = prev , Rmouse or + = next , anyother key = exit)")
  (princ (strcat "\nCurrent layer is " (getvar "clayer")))
  (setq done nil)
  (while (not done)
    (setq inp (vl-catch-all-apply 'grread (list nil 4 1)))
    (if (vl-catch-all-error-p inp)
      (setq done t )
      (cond
        ;;; - or lmouse
        ((or (equal inp '(2 45)) (= (car inp) 3))
         (if (> pos 0)
           (progn (setq pos (1- pos)) (setvar "clayer" (nth pos lay-list))
             (princ (strcat "\rCurrent layer is now " (getvar "clayer"))))
           (princ "\rYou've reached the first layer")
         )
        )
        ;;; + or rmouse
        ((or (equal inp '(2 43)) (= (car inp) 25))
         (if (< pos max-pos)
           (progn (setq pos (1+ pos)) (setvar "clayer" (nth pos lay-list))
             (princ (strcat "\rCurrent layer is now " (getvar "clayer"))))
           (princ "\rYou've reached the last layer")
         )
        )
        (t (setq done t))
      )
    )
  )
)

 

🐉

  • Like 1
Posted

great!! i liked the concept you used. layers can be switched from mouse right or left clicks for layers up and down. still layer up & down  order is not proper. it might be a drawing issue maybe? 

 

Posted

thats probably the acad_strlsort function. remove it and see if result is different

Posted

i might delete the wrong one (no experience to understand the codes) . can you edit your code in your previous post? 

Posted
;;; switch layer Rlx 11-oct-2022
(defun c:swl ( / _laylist lay-list cur-lay pos max-pos done inp)
  (vl-load-com)
  ;;; (setq l (_laylist))
  (defun _laylist ( / d r) (while (setq d (tblnext "layer" (null d)))(setq r (cons (cdr (assoc 2 d)) r))) r)
  (setq lay-list (_laylist) cur-lay (getvar "clayer")
        pos (vl-position cur-lay lay-list) max-pos (1- (length lay-list)))
  
  (princ "\nSwitch layer (Lmouse or - = prev , Rmouse or + = next , anyother key = exit)")
  (princ (strcat "\nCurrent layer is " (getvar "clayer")))
  (setq done nil)
  (while (not done)
    (setq inp (vl-catch-all-apply 'grread (list nil 4 1)))
    (if (vl-catch-all-error-p inp)
      (setq done t )
      (cond
        ;;; - or lmouse
        ((or (equal inp '(2 45)) (= (car inp) 3))
         (if (> pos 0)
           (progn (setq pos (1- pos)) (setvar "clayer" (nth pos lay-list))
             (princ (strcat "\rCurrent layer is now " (getvar "clayer"))))
           (princ "\rYou've reached the first layer")
         )
        )
        ;;; + or rmouse
        ((or (equal inp '(2 43)) (= (car inp) 25))
         (if (< pos max-pos)
           (progn (setq pos (1+ pos)) (setvar "clayer" (nth pos lay-list))
             (princ (strcat "\rCurrent layer is now " (getvar "clayer"))))
           (princ "\rYou've reached the last layer")
         )
        )
        (t (setq done t))
      )
    )
  )
)

 

Posted

this is working better than the previous but new layers are not in an orders. 

 

image.thumb.png.ac562b8e0976d5afad9d5d4db835cdb6.png

 

is it possible to switch layers in selected filter list "WORK"? unless in "all used layers"? so arch layers or unwanted layers will skip

Posted

not sure , have to look into how filters operate , but looking at the effort to accomplish all this what's left of the profit anymore using the standard layer dialog?

Posted

i appreciate your work and struggle. current LISP is working fine and filter is an optional.  

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...