hibba Posted October 10, 2022 Posted October 10, 2022 (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 October 10, 2022 by hibba Quote
Steven P Posted October 10, 2022 Posted October 10, 2022 (edited) will control+ page down / page up work just as well? WHOOPS - answering the wrong question!! Edited October 10, 2022 by Steven P Quote
hibba Posted October 10, 2022 Author Posted October 10, 2022 i tried and this works to switch between layouts. i need layer switching LISP Quote
Steven P Posted October 10, 2022 Posted October 10, 2022 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 Quote
hibba Posted October 10, 2022 Author Posted October 10, 2022 (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). 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 October 10, 2022 by hibba Quote
mhupp Posted October 10, 2022 Posted October 10, 2022 (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 October 11, 2022 by mhupp Thanks StevenP 1 Quote
Steven P Posted October 10, 2022 Posted October 10, 2022 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. 1 Quote
mhupp Posted October 10, 2022 Posted October 10, 2022 (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 October 10, 2022 by mhupp 1 Quote
hibba Posted October 11, 2022 Author Posted October 11, 2022 i used the lisp and getting this error {{Error : bad function: #<SUBR @00000292a7e3faa8 <>}} Quote
Steven P Posted October 11, 2022 Posted October 11, 2022 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 '<)) Quote
hibba Posted October 11, 2022 Author Posted October 11, 2022 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 ) Quote
hibba Posted October 11, 2022 Author Posted October 11, 2022 (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 October 11, 2022 by hibba Quote
rlx Posted October 11, 2022 Posted October 11, 2022 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)) ) ) ) ) 1 Quote
hibba Posted October 11, 2022 Author Posted October 11, 2022 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? Quote
rlx Posted October 11, 2022 Posted October 11, 2022 thats probably the acad_strlsort function. remove it and see if result is different Quote
hibba Posted October 11, 2022 Author Posted October 11, 2022 i might delete the wrong one (no experience to understand the codes) . can you edit your code in your previous post? Quote
rlx Posted October 11, 2022 Posted October 11, 2022 ;;; 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)) ) ) ) ) Quote
hibba Posted October 11, 2022 Author Posted October 11, 2022 this is working better than the previous but new layers are not in an orders. is it possible to switch layers in selected filter list "WORK"? unless in "all used layers"? so arch layers or unwanted layers will skip Quote
rlx Posted October 11, 2022 Posted October 11, 2022 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? Quote
hibba Posted October 11, 2022 Author Posted October 11, 2022 i appreciate your work and struggle. current LISP is working fine and filter is an optional. Quote
Recommended Posts
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.