Jump to content

How to List specific layer names


gsc

Recommended Posts

Hi,

 

I want to List specific existing layer names for viewport freeze.

The layer names always start with "29-barge-*"

The * is a sequenced number starting at 1.

 

How do I do that?

Link to comment
Share on other sites

If you really must use a LISP code,

 

(defun ListLayerByPattern (pat / ly nm rtn)
    (while (setq ly (tblnext "layer" (null ly)))
	(if (wcmatch (setq nm (cdr (assoc 2 ly))) pat)
	    (setq rtn (cons nm rtn))
	    )
	)
    (reverse rtn)
    )

 

so you can call it like:

 

(ListLayerByPattern "29`-barge`-[1-9]*")

 

Or apply a function to every matching layer

 

;; pat - matching pattern by layer name
;; fnc - function accepting one argument that represents the layer name

(defun ApplyToLayerByPattern (pat fnc / ly nm)
    (while (setq ly (tblnext "layer" (null ly)))
	(if (wcmatch (setq nm (cdr (assoc 2 ly))) pat)
	    (eval (list fnc nm))
	    )
	)
    )

 

For example, this one freezes all layers of the matching pattern except for the current layer:

 

(defun c:test ( / cur lys)
    (setq lys (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object))) cur (getvar 'clayer))
    (ApplyToLayerByPattern "29`-barge`-[1-9]*"
	'(lambda (x)
	     (and
		 (not (wcmatch cur "29`-barge`-[1-9]*"))
		 (vla-put-Freeze (vla-item lys x) :vlax-true)
		 )
	     )
	)
    )

 

  • Like 1
Link to comment
Share on other sites

13 hours ago, ronjonp said:

Not sure where this thread is going but are you familiar with layer property filters?

 

2020-07-08_8-06-55.jpg

That is how I'd do it too, and the same suggestion which I made, but later deleted as the OP seemed disinterested in an analog approach.   :beer:

Another analog approach might be to include such layer names, pre-viewport frozen, and Grouped into a filter in the Layer Properties Manager in your customized default QNEW template.

Old school.   :playing:

Looks like @Jonathan Handojo has gotten it sorted out though, cool.    :beer:

 

Link to comment
Share on other sites

11 hours ago, Jonathan Handojo said:

If you really must use a LISP code,

 


(defun ListLayerByPattern (pat / ly nm rtn)
    (while (setq ly (tblnext "layer" (null ly)))
	(if (wcmatch (setq nm (cdr (assoc 2 ly))) pat)
	    (setq rtn (cons nm rtn))
	    )
	)
    (reverse rtn)
    )

 

so you can call it like:

 


(ListLayerByPattern "29`-barge`-[1-9]*")

 

Or apply a function to every matching layer

 


;; pat - matching pattern by layer name
;; fnc - function accepting one argument that represents the layer name

(defun ApplyToLayerByPattern (pat fnc / ly nm)
    (while (setq ly (tblnext "layer" (null ly)))
	(if (wcmatch (setq nm (cdr (assoc 2 ly))) pat)
	    (eval (list fnc nm))
	    )
	)
    )

 

For example, this one freezes all layers of the matching pattern except for the current layer:

 


(defun c:test ( / cur lys)
    (setq lys (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object))) cur (getvar 'clayer))
    (ApplyToLayerByPattern "29`-barge`-[1-9]*"
	'(lambda (x)
	     (and
		 (not (wcmatch cur "29`-barge`-[1-9]*"))
		 (vla-put-Freeze (vla-item lys x) :vlax-true)
		 )
	     )
	)
    )

 

 

Nice solution man, thanx!

 

I guess the ` in (ListLayerByPattern "29`-barge`-[1-9]*") is an escape character for the dash?

 

The [1-9] numbers (layers) are ascending (not infinite) in number during steps in my main routine.

And I actually only want to VP freeze the last 2 numbers (layers 8 and 9 in your example):

number 9 in the Viewport on the last Layout number and number 8 in the Viewport of the second to last Layout number.

 

My layout names have numbers like 01, 02, etc. and are also ascending in number

I list those Viewports with:

(setq vport_1 (ssget "x" (list (cons 0 "VIEWPORT") (cons 410 (nth 0 (reverse (layoutlist)))))))
(setq vport_2 (ssget "x" (list (cons 0 "VIEWPORT") (cons 410 (nth 1 (reverse (layoutlist)))))))

 

 

 

 

 

 


 

Link to comment
Share on other sites

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...