get list of layer names based on description

get list of layer names based on description

dwattersAMXH5
Enthusiast Enthusiast
2,440 Views
8 Replies
Message 1 of 9

get list of layer names based on description

dwattersAMXH5
Enthusiast
Enthusiast

i am trying to build a lisp to vplayer  freeze layers based on their description 

 

 

here is where i am now  - 

(defun c:test2 (/ vpl ln ss en)
(vl-load-com)
(setq dsc '("testlayer" "testlayer2")) ; setting descriptions
(vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
(if (member (vla-get-description layer) dsc)
(setq layers '((vla-get-name layer)))
) ;trying to set layers to a list of layer names which descriptions match dsc
(setq ss (ssget "X" (list (cons 0 "VIEWPORT")
(cons 8 "viewport-titleblock")))) ; selecting viewport to run vplayer in
(= (sslength ss) 1)
(setq vp (ssname ss 0)) ;setting vp to the viewport name of the viewport that met selection criteria
(command "_.VPLAYER" "_Freeze" (member layers) "_S" vp "" "") ;trying to run vplayer, freeze, plug in list of layer names, set viewport to the one we want, enter, enter
(prin1)
)

my problem is that vplayer, freeze doesn't want to select the layer names from what i am trying to plug in. 

any assistance on what i am doing wrong here? 

0 Likes
Accepted solutions (3)
2,441 Views
8 Replies
Replies (8)
Message 2 of 9

pbejse
Mentor
Mentor

Not deviating too much from your original code

(defun c:test2 (/ layers dsc ss)
(vl-load-com)
	(setq layers ""
	      dsc '("TESTLAYER" "TESTLAYER2"))
	(vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
	 (if (member (Strcase (vla-get-description layer))  dsc)
	 	(setq layers (strcat (vla-get-name layer) "," layers))
		)
	  )
	(if
	  (and
	    layers
	    (setq ss (ssget "X" '((0 . "VIEWPORT")(8 . "viewport-titleblock")(410 . "~Model"))))
	    (= (sslength ss) 1)
	    )
	 	(command "_.VPLAYER" "_Freeze" layers "_S" (ssname ss 0) "" "")
	  )
	(prin1)
	)

HTH

 

0 Likes
Message 3 of 9

pbejse
Mentor
Mentor

should be 

(setq ss (ssget "X" (list '(0 . "VIEWPORT")'(8 . "viewport-titleblock")(cons 410 (getvar 'ctab)))))

 

0 Likes
Message 4 of 9

Moshe-A
Mentor
Mentor
Accepted solution

dwatters,

 

check this one.

 

enjoy

moshe

 

 

(vl-load-com)

(defun c:test2 (/ dsc LayLst^ LayTblRec ss n elist ename lay LayLst^)
 (setvar "tilemode" 0) ; switch to paper space
  
 (setq dsc "testlayer1,testlayer2") ; constant descriptions

 (setq LayLst^ '())
  
 (vlax-for LayTblRec (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
  (if (wcmatch (strcase (vla-get-description LayTblRec)) (strcase dsc))
   (setq LayLst^ (append LayLst^ (list (vla-get-name LayTblRec)))) ; create layer list
  ) 
 ); vlax-for

  
 (if (setq ss (ssget "X" (list '(0 . "viewport") '(8 . "viewport-titleblock")))) ; select viewports
  (repeat (setq n (sslength ss))
   (setq elist (entget (setq ename (ssname ss (setq n (1- n))))))
    
   (if (> (cdr (assoc '69 elist)) 1) ; skip screen viewport
    (progn
     (if (/= (setq ctab (getvar "ctab")) (cdr (assoc '410 elist)))
      (setvar "ctab" (cdr (assoc '410 elist))) ; set layout
     )

     (command "._mspace") ; enter model psace	
     (setvar "cvport" (cdr (assoc '69 elist)))  ; set viewport

     ; freeze layers
     (foreach lay LayLst^
      (command "_.VPLAYER" "_Freeze" lay "_S" ename "" "")
     )

     (command "._pspace") ; back to paper space
    ); progn
   ); if
  ); repeat
 ); if
   
 (princ)
)
0 Likes
Message 5 of 9

dwattersAMXH5
Enthusiast
Enthusiast
	(setq layers ""
	      dsc '("TESTLAYER" "TESTLAYER2"))
	(vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
	 (if (member (Strcase (vla-get-description layer))  dsc)
	 	(setq layers (strcat (vla-get-name layer) "," layers))
		)
	  )
	(if
	  (and
	    layers
	    (setq ss (ssget "X" '((0 . "VIEWPORT")(8 . "viewport-titleblock")(410 . "~Model"))))
	    (= (sslength ss) 1)
	    )
	 	(command "_.VPLAYER" "_Freeze" layers "_S" (ssname ss 0) "" "")
	  )

ran with this and it works great when there is only one viewport on the  "viewport-titleblock" layer when i copy the layout and have two viewports on "viewport-titleblock" layer it doesn't want to complete the lisp  (no errors or anything it just doesn't viewport freeze the layers) when i turn one of the viewports layers to 0 then re-run the lisp it works fine. 

tried running something like this to make it run in each layout but that didn't really help the issue. 

(vl-load-com)
(foreach layout (layoutlist)
 (setvar "ctab" layout)
	(setq layers ""
	      dsc '("TESTLAYER" "TESTLAYER2"))
	(vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
	 (if (member (Strcase (vla-get-description layer))  dsc)
	 	(setq layers (strcat (vla-get-name layer) "," layers))
		)
	  )
	 (if
	  (and
	    layers
	    (setq ss (ssget "X" '((0 . "VIEWPORT")(8 . "viewport-titleblock")(410 . "~Model"))))
	    (= (sslength ss) 1)
	    )
	 	(command "_.VPLAYER" "_Freeze" layers "_S" (ssname ss 0) "" "")
	  )
	 )
	(prin1)

 

0 Likes
Message 6 of 9

dlanorh
Advisor
Advisor
Accepted solution

Your initial code contains the code line

(= (sslength ss) 1)

This indicates that you only want this to happen if there is only one viewport in the selection set. No where in your description does it mention anything about applying it across multiple viewports in the selection set. Both posted solutions run with this theme.

Perhaps a better description of what you are trying to do is required to obtain the solution you require.

I am not one of the robots you're looking for

0 Likes
Message 7 of 9

pbejse
Mentor
Mentor
Accepted solution

As dlanorh already pointed out in the previous post

 

(= (sslength ss) 1)

This indicates that you only want this to happen if there is only one viewport in the selection set.

 

Below are two modified routines to process more than 1 layout tab

One viewport per layout

(defun c:test219 (/ layers dsc ss i ent e)
(vl-load-com)
	(setq layers ""
	      dsc '("TESTLAYER" "TESTLAYER2"))
	(vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
	 (if (member (Strcase (vla-get-description layer))  dsc)
	 	(setq layers (strcat (vla-get-name layer) "," layers))
		)
	  )
	(if
	  (and
	    layers
	    (setq ss (ssget "X" '((0 . "VIEWPORT")(8 . "viewport-titleblock")(410 . "~Model"))))
	    )
	  (repeat (setq i (sslength ss))
	    	(setq ent (entget (setq e (ssname ss (setq i (1- i))))))
	    
	    	(if (/= (setq thistab (cdr (assoc 410 ent))) (getvar 'ctab))
		  	(setvar 'ctab thistab)
		  	)
	 	(command "_.VPLAYER" "_Freeze" layers "_S" e "" "")
	  )
	  )
	(princ)
	)

 

Multiple layouts and viewports 

(defun c:test423 (/ layers dsc ss i ent e)
(vl-load-com)
	(setq layers ""
	      dsc '("TESTLAYER" "TESTLAYER2"))
	(vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
	 (if (member (Strcase (vla-get-description layer))  dsc)
	 	(setq layers (strcat (vla-get-name layer) "," layers))
		)
	  )
  (foreach lay (layoutlist)    
	(if
	  (and
	    layers
	    (setq ss (ssget "X" (list '(0 . "VIEWPORT")'(8 . "viewport-titleblock")
				      (cons 410 lay))))	    
	    )
	  (progn
	    (setvar 'ctab lay)
		  (repeat (setq i (sslength ss))
		    	(setq ent (entget (setq e (ssname ss (setq i (1- i))))))
		 	(command "_.VPLAYER" "_Freeze" layers "_S" e "" "")
		  )
	    )
	  )
    )
	(princ)
	
  )

 

For both routines, it will switch layout tab only if there are valid viewports found.

HTH

0 Likes
Message 8 of 9

dwattersAMXH5
Enthusiast
Enthusiast
I know my original post didn’t state that, this was an after thought to better understand what was making it behave that way, I’ve just started trying to write my own lisps after having been using ones I found on this forum for about a year extensively, I oviusly just know enough at this point to get myself in trouble with it, and I do try to search extensively for similar case situations to find what I can’t figure out before making a post.
I wasn’t trying to say anyone’s code didn’t work, they all did and were a huge help for part of the lisp I am trying to work through.
But Thanks for your concerns about my post descriptions.
0 Likes
Message 9 of 9

dwattersAMXH5
Enthusiast
Enthusiast
Thank you both for explaining sslength a little better for me( still working through multiple Udemy courses on Lisps).

I will run this later and see what the results are but I’m sure it’s perfect as every other bit of code you posted has been!

Thank you, and everyone else so much for all the help.
0 Likes