Visual Lisp selecting layers by description

Visual Lisp selecting layers by description

dwattersAMXH5
Enthusiast Enthusiast
3,014 Views
10 Replies
Message 1 of 11

Visual Lisp selecting layers by description

dwattersAMXH5
Enthusiast
Enthusiast

i would like to build a visual lisp to select all layers with a specific description so that i can then turn all of those layers off/on/freeze/thaw/no-plot whatever. 

the basic modifying the layer isnt going to be any problem but i was wondering if anyone could point me in a direction for selecting all layers with a defined description using VLisp

thanks! 

0 Likes
Accepted solutions (1)
3,015 Views
10 Replies
Replies (10)
Message 2 of 11

doaiena
Collaborator
Collaborator

This code will browse through all the layers in the drawing and will alert you the name of the layer if it has a desctiption "TEST".

(vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
(if (equal (vla-get-description layer) "TEST") (alert (vla-get-name layer)))
)
0 Likes
Message 3 of 11

scot-65
Advisor
Advisor

This is from my library of snippets gathered throughout the years of browsing these forums.

Give credit to phanaem...

;phanaem 10-13-2014

(defun c:stdlayer ( / acdoc layers layerlist)
  (vl-load-com)
  (setq acdoc  (vla-get-activedocument (vlax-get-acad-object))
        layers (vla-get-layers acdoc)       
        layerlist
         '(("A-COLS" 1 "Continuous" 25 "Columns")
           ("A-BEAM" 2 "Continuous" 30 "Beams")
          )
  )
  (foreach x layerlist
    (setq layer (vla-add layers (car    x)))
    (vla-put-color       layer  (cadr   x))
    (vla-put-linetype    layer  (caddr  x))
    (vla-put-lineweight  layer  (cadddr x))
    (vla-put-description layer  (last   x))
  )
  (princ)
)

I think there are enough hints here to get you thinking...

 

???

 


Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes
Message 4 of 11

dwattersAMXH5
Enthusiast
Enthusiast

thank you! 
so would something like this turn off a layer 

say i had specific layers that a permitting agency didn't want on the drawings (for construction or something)
setting those layers description to "non-permitting"  
something like this should work right? 

(vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
(if (equal (vla-get-description layer) "non-permitting") (command "layer" "off" "*" "" ""))

since this is just saying "if the layer description ="non-permitting" then turn the layer off" 

0 Likes
Message 5 of 11

doaiena
Collaborator
Collaborator
Accepted solution

I think this is what you are looking for:

(vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
(if (equal (vla-get-description layer) "non-permitting") (vla-put-layerOn layer :vlax-false))
)
0 Likes
Message 6 of 11

dwattersAMXH5
Enthusiast
Enthusiast

This Seems to work perfect - 
just a few more questions, if i want to have the vlisp search for multiple descriptions how would that look?

and say i wanted it to turn the layer back on with those descriptions i would just set vlax-false to vlax-true?  

0 Likes
Message 7 of 11

dwattersAMXH5
Enthusiast
Enthusiast
(defun c:adb-player ()
 (vl-load-com)
 (setq str "ADB-PLayers Complete"
       des1 "construction"
       des2 "DOT"
 );setq
 (vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
 (cond
    ((equal (vla-get-description layer) des1) (vla-put-layerOn layer :vlax-false))
    ((equal (vla-get-description layer) des2) (vla-put-layerOn layer :vlax-false))
  ));cond
 (alert str)
)

answered my own question as far as multiple descriptions 

changed the if statement to a cond 

0 Likes
Message 8 of 11

ВeekeeCZ
Consultant
Consultant

@dwattersAMXH5 wrote:
(defun c:adb-player ()
 (vl-load-com)
 (setq str "ADB-PLayers Complete"
       des1 "construction"
       des2 "DOT"
 );setq
 (vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
 (cond
    ((equal (vla-get-description layer) des1) (vla-put-layerOn layer :vlax-false))
    ((equal (vla-get-description layer) des2) (vla-put-layerOn layer :vlax-false))
  ));cond
 (alert str)
)

answered my own question as far as multiple descriptions 

changed the if statement to a cond 


If you put them into a list (as we're using the AutoLISP), you can use the MEMBER function.

 

  (setq dsc '("construction" "DOT"))
  
  (if (member (vla-get-description layer) dsc)
     (vla-put-layerOn layer :vlax-false))

 

Message 9 of 11

dwattersAMXH5
Enthusiast
Enthusiast

oh that's fancy,

now how would i go about turning the layers back on? turn that last 

vlax-false

to 

vlax-true 

 ?

0 Likes
Message 10 of 11

dwattersAMXH5
Enthusiast
Enthusiast

here's what i ended up making from this - 
could easily be set up to user input for dsc variable (will probably be my next step later)
feel free to modify for your needs 

  ; layer-dstatus - Turns Layers On and Off by Description by Derek Watters, 2018
  (defun c:layer-dstatus ()
    (vl-load-com)
    (initget 1 "On Off")
    (setq strOff "Layer-DStatus Complete : All Layers Not Required for Permitting Have Been Turned Off. Please Run Layer-DStatus Command Again to Turn Layers On"
          strOn "Layer-DStatus Complete : All Layers Not Required for Permitting Have Been Turned On. Please Run Layer-DStatus Command Again to Turn Layers Off"
          dsc '("DESCRIPTION1" "DESCRIPTION2" "DESCRIPTION3")
          rspns (getkword "Turn Layers On or Off by Discription (ON/OFF): ")
    )appload
    (if (= rspns "Off")
      (vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
        (if (member (vla-get-description layer) dsc)
        (vla-put-layerOn layer :vlax-false))
      )
      (alert strOn)
    );ifOff
    (if (= rspns "On")
      (vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
        (if (member (vla-get-description layer) dsc)
        (vla-put-layerOn layer :vlax-true))
      )
      (alert strOff)
    );IfOn
    (princ)
    );
0 Likes
Message 11 of 11

ronjonp
Advisor
Advisor

FWIW .. here's a way to toggle the on off status without user input:

(defun c:layer-dstatus nil
  (vl-load-com)
  (vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
    (cond ((wcmatch (strcase (vla-get-description layer)) "DESCRIPTION[1-3]")
	   (vlax-put layer 'layeron (boole 8 0 (vlax-get layer 'layeron)))
	  )
    )
  )
  (princ)
)

 

0 Likes