Tickles! Posted March 31, 2020 Posted March 31, 2020 (edited) (defun c:nlayer (/ n1 n2) (setq n1 (GetString "\nSet Layer: ")) (setq n2 (GetString "\nOff Layer: ")) (command "._-LAYER" "SET" n1 "OFF" n2 "") ) As you know I do not know how to code lisp. so i googled some to set next layer as current and turn off the previous current layer my layers are numbers representing elevations, Ex: 900, 902, 904 I was wondering maybe someone could make a routine better? to set a number, and everytime you use the routine, it just automatically add 2 so you can set the next layer and off the precious layer? Edited March 31, 2020 by Tickles! Quote
Jonathan Handojo Posted March 31, 2020 Posted March 31, 2020 (edited) The below will turn off a layer. (defun offlayer (lay / obj state) (if (setq obj (tblobjname "Layer" lay)) (progn (setq state (abs (cdr (assoc 62 (entget obj))))) (entmod (subst (cons 62 (- state)) (assoc 62 (entget obj)) (entget obj) ) ) ) ) ) You can run: (foreach x lst (offlayer x)) where lst is a list of all your layer names to be turned off. Or: ;; If 'first' is your first layer and n is the number of times... (repeat n (offlayer (setq first (itoa (+ 2 (atoi first))))) ) Or: ;; If 'start' is your first layer (integer) and 'end' is your final layer (integer) (while (<= start end) (offlayer (itoa start)) (setq start (+ start 2)) ) ...however you wanna do it. Edited March 31, 2020 by Jonathan Handojo Quote
Jonathan Handojo Posted March 31, 2020 Posted March 31, 2020 As for setting a layer as current: (setvar "CLAYER" lay) But I don't recommend you doing this within a loop. Quote
BIGAL Posted March 31, 2020 Posted March 31, 2020 Maybe ignore code -la s 902 off 901 or (defun c:clax (command "-layer" "S" (getstring) "off" (getstring) "")) Quote
dlanorh Posted March 31, 2020 Posted March 31, 2020 Try this. Will only work with integer layer names. First time run in new session or new drawing will prompt for a start layer to be selected, thereafter it will increment by 2 turning the last layer off. "lyr+" to start. (defun _get_cur_lyr ( / ent lyr) (while (not ent) (setq ent (car (entsel "\nSelect Numeric Layer For Current : ")) lyr (cdr (assoc 8 (entget ent))) ) (cond ( (vl-every '(lambda (x) (< 47 x 58)) (vl-string->list lyr)) (setq lyr (atoi lyr))) (t (alert "Not a Numeric Layer") (setq ent nil)) );end_cond );end_while lyr );end_defun (vl-load-com) (defun c:lyr+ (/ lastlayer n1 n2) (or *c_lyrs* (setq *c_lyrs* (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))) (cond (*curlyr* (setq lastlyr (itoa *curlyr*) *curlyr* (+ *curlyr* 2) );end_setq (cond ( (tblsearch "layer" (itoa *curlyr*)) (setvar 'clayer (itoa *curlyr*)) (vlax-put-property (vla-item *c_lyrs* lastlyr) 'layeron :vlax-false) ) (t (alert (strcat "Layer " (itoa *curlyr*) " Not Found"))) );end_cond ) (t (setq *curlyr* (_get_cur_lyr)) (if *curlyr* (setvar 'clayer (itoa *curlyr*)) (alert "No Numeric Layer Selected"))) );end_cond (princ) );end_defun Quote
Tickles! Posted April 1, 2020 Author Posted April 1, 2020 14 hours ago, dlanorh said: Try this. Will only work with integer layer names. First time run in new session or new drawing will prompt for a start layer to be selected, thereafter it will increment by 2 turning the last layer off. "lyr+" to start. (defun _get_cur_lyr ( / ent lyr) (while (not ent) (setq ent (car (entsel "\nSelect Numeric Layer For Current : ")) lyr (cdr (assoc 8 (entget ent))) ) (cond ( (vl-every '(lambda (x) (< 47 x 58)) (vl-string->list lyr)) (setq lyr (atoi lyr))) (t (alert "Not a Numeric Layer") (setq ent nil)) );end_cond );end_while lyr );end_defun (vl-load-com) (defun c:lyr+ (/ lastlayer n1 n2) (or *c_lyrs* (setq *c_lyrs* (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))) (cond (*curlyr* (setq lastlyr (itoa *curlyr*) *curlyr* (+ *curlyr* 2) );end_setq (cond ( (tblsearch "layer" (itoa *curlyr*)) (setvar 'clayer (itoa *curlyr*)) (vlax-put-property (vla-item *c_lyrs* lastlyr) 'layeron :vlax-false) ) (t (alert (strcat "Layer " (itoa *curlyr*) " Not Found"))) );end_cond ) (t (setq *curlyr* (_get_cur_lyr)) (if *curlyr* (setvar 'clayer (itoa *curlyr*)) (alert "No Numeric Layer Selected"))) );end_cond (princ) );end_defun mister, can i ask? could you turn the current layer on i mean after it sets the next layer as current, could you turn it on as well? Quote
BIGAL Posted April 1, 2020 Posted April 1, 2020 This is what you asked for "set next layer as current and turn off the previous current layer " -la s newlayer not very hard to type. It sounds like your changing your mind as you go along you can have different lisp defuns you can give them names like "1l" "1o" "2o" "2l" "xxxx" you need to define the exact combinations you want 1 at a time. So for last request it would be "2l" got 2 layers down and set current. The core code would be as already posted just multiple defuns.. Quote
Tickles! Posted April 1, 2020 Author Posted April 1, 2020 im sorry if i am not explaining myself clearly, im not good at english.. example: 1. my layer names are all numbers ex: 900 902 904 906 908 910 my current layer is 900 what i have in mind is a routine, with 2 commands... first command is for setting the layer second command is for changing the current layer into the next layer and turning it on, and then turn off the previous layer. so when i type the second command again, it will just add 2 again so that it will "set as current" the next layer, and then it will turn off the previous layer again if im on layer 900, i will type 1st command to set my number to 900. then i will type 2nd command, it will turn off layer 900, then turn on layer 902 and set as current layer 902. if i type 2nd command again, it will turn off layer 902, and turn on layer 904 and set as current layer 904 if i type 2nd command again, it will turn off layer 904, and turn on layer 906 and set as current layer 906 Quote
dlanorh Posted April 1, 2020 Posted April 1, 2020 Try this. Just seen the above post, so added a reset so you can change out. (defun _get_cur_lyr ( / ent lyr) (while (not ent) (setq ent (car (entsel "\nSelect Numeric Layer For Current : ")) lyr (cdr (assoc 8 (entget ent))) ) (cond ( (vl-every '(lambda (x) (< 47 x 58)) (vl-string->list lyr)) (setq lyr (atoi lyr))) (t (alert "Not a Numeric Layer") (setq ent nil)) );end_cond );end_while lyr );end_defun (defun c:lyr+ (/ lastlayer n1 n2 o) (or *c_lyrs* (setq *c_lyrs* (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))) (cond (*curlyr* (setq lastlyr (itoa *curlyr*) *curlyr* (+ *curlyr* 2) );end_setq (cond ( (tblsearch "layer" (itoa *curlyr*)) (if (= :vlax-false (vlax-get-property (setq o (vla-item *c_lyrs* (itoa *curlyr*))) 'layeron)) (vlax-put-property o 'layeron :vlax-true)) (setvar 'clayer (itoa *curlyr*)) (vlax-put-property (vla-item *c_lyrs* lastlyr) 'layeron :vlax-false) ) (t (alert (strcat "Layer " (itoa *curlyr*) " Not Found"))) );end_cond ) (t (setq *curlyr* (_get_cur_lyr)) (if *curlyr* (setvar 'clayer (itoa *curlyr*)) (alert "No Numeric Layer Selected"))) );end_cond (princ) );end_defun (defun c:resetlyr+ (/) (setq *curlyr* nil)) 1 Quote
Tickles! Posted April 1, 2020 Author Posted April 1, 2020 32 minutes ago, dlanorh said: Try this. Just seen the above post, so added a reset so you can change out. (defun _get_cur_lyr ( / ent lyr) (while (not ent) (setq ent (car (entsel "\nSelect Numeric Layer For Current : ")) lyr (cdr (assoc 8 (entget ent))) ) (cond ( (vl-every '(lambda (x) (< 47 x 58)) (vl-string->list lyr)) (setq lyr (atoi lyr))) (t (alert "Not a Numeric Layer") (setq ent nil)) );end_cond );end_while lyr );end_defun (defun c:lyr+ (/ lastlayer n1 n2 o) (or *c_lyrs* (setq *c_lyrs* (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))) (cond (*curlyr* (setq lastlyr (itoa *curlyr*) *curlyr* (+ *curlyr* 2) );end_setq (cond ( (tblsearch "layer" (itoa *curlyr*)) (if (= :vlax-false (vlax-get-property (setq o (vla-item *c_lyrs* (itoa *curlyr*))) 'layeron)) (vlax-put-property o 'layeron :vlax-true)) (setvar 'clayer (itoa *curlyr*)) (vlax-put-property (vla-item *c_lyrs* lastlyr) 'layeron :vlax-false) ) (t (alert (strcat "Layer " (itoa *curlyr*) " Not Found"))) );end_cond ) (t (setq *curlyr* (_get_cur_lyr)) (if *curlyr* (setvar 'clayer (itoa *curlyr*)) (alert "No Numeric Layer Selected"))) );end_cond (princ) );end_defun (defun c:resetlyr+ (/) (setq *curlyr* nil)) yes! this is the one! i thank you so much for this! i tried it and it worked so much more than i expected! Quote
BIGAL Posted April 2, 2020 Posted April 2, 2020 (edited) Dlanorh just an idea if you get the layer table find the 1st layer by name, but get its count number then you can use the count number to edit the layer table directly rather than by name. Yes it will go to alpha layers but that means has got to the end of the layer name pattern. Command: (vlax-dump-object (setq *c_lyrs* (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))) ; IAcadLayers: The collection of all layers in the drawing ; Property values: ; Application (RO) = #<VLA-OBJECT IAcadApplication 00007ff7af791e30> ; Count (RO) = 548 (vla-item (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) 12) Edited April 2, 2020 by BIGAL Quote
BIGAL Posted April 2, 2020 Posted April 2, 2020 Ignore what I said above it may work as the layer table retrieves in creation order but display is in Alphabetical order. So if you added some layers like 999 it will be last layer in list. It may be worthwhile not sure how to do a alphabet sort of the layer table. There is a lot of info connected to a layer. Note "Item" 0 is "0" (setq *c_lyrs* (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))) (vlax-for lay *c_lyrs* (princ (vla-get-name lay)) (princ "\n") ) 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.