PDA

View Full Version : Count the number of Layers with a specific character in the Layer name


christoph
2007-03-15, 01:56 PM
Hi everyone,

I,m looking for a routine in pure Autolisp to count the number of layers with a specific character in the layer-name. I.e. count all layers beginning with "s-".

Thank you

christoph

Avatart
2007-03-15, 04:27 PM
I had something similar already done, I chopped it about a bit and here is the result:

;; Get Layer List
;; from Drawing
(defun C:S-Layers ( / newelem laylist searchparam listlength searchparamtext)
(setq searchparamtext (getstring T "\nEnter Layer search parameter: "))
(setq searchparam (strcat "*" searchparamtext "*"))
(setq newelem (tblnext "layer" T)); Get first Layer
(if (= (wcmatch (cdr (assoc 2 newelem)) searchparam) T) (setq newelem (cdr (assoc 2 (tblnext "layer" T)))) (setq newelem nil)); Start Layer List
(if (/= nil newelem) (setq laylist (append laylist newelem))); Add to Layer list if appropriate
(while (/= (setq newelem (tblnext "layer")) nil); Start loop for remaining layers
(if (= (wcmatch (cdr (assoc 2 newelem)) searchparam) T) (setq newelem (list (cdr (assoc 2 newelem)))) (setq newelem nil)); Check each Layer
(if (/= nil newelem) (setq laylist (append laylist newelem)))); Add to Layer list if appropriate
(setq laylist (acad_strlsort laylist))
(setq listlength (rtos (length laylist) 2 0))
(alert (strcat "There are " listlength " layers that contain \"" searchparamtext "\""))
)

T.Willey
2007-03-15, 05:49 PM
(
(lambda (String / cnt LayEnt flag)

(setq cnt 0)
(while (setq LayEnt (tblnext "layer" (not flag)))
(if (wcmatch (cdr (assoc 2 LayEnt)) String)
(setq cnt (1+ cnt))
)
(setq flag T)
)
(prompt (strcat "\n " (itoa cnt) " layer(s) found."))
)
"s-*"
)

CAB2k
2007-03-15, 06:25 PM
Nice one Tim.
A suggestion to make it case insensitive.
((lambda (String / cnt LayEnt)
(setq cnt 0
String (strcase String))
(while (setq LayEnt (tblnext "layer" (not LayEnt)))
(if (wcmatch (strcase (cdr (assoc 2 LayEnt))) String)
(setq cnt (1+ cnt))
)
)
(princ (strcat "n " (itoa cnt) " layer(s) found."))
)
"s-*"
)

Avatart
2007-03-15, 06:30 PM
Don't half make my code look cumbersome! :Oops:

T.Willey
2007-03-15, 06:31 PM
Thanks Alan.
But wouldn't you need to 'strcase' the layer name pulled also then?
ie:
(if (wcmatch (cdr (assoc 2 LayEnt)) String)
to
(if (wcmatch (strcase (cdr (assoc 2 LayEnt))) String)

T.Willey
2007-03-15, 06:33 PM
Don't half make my code look cumbersome! :Oops:
Didn't mean to. It's just there are so many way to do anything in lisp, I thought I would show another. :lol:

Avatart
2007-03-15, 06:35 PM
Didn't mean to. It's just there are so many way to do anything in lisp, I thought I would show another. :lol:Tim, your code is light years ahead of mine, I just sort of bludgeon it into submission, yours is elegant, nice one!

CAB2k
2007-03-15, 06:40 PM
Tim,
You're quite right. :Oops: I revised the posted code.

T.Willey
2007-03-15, 06:46 PM
Thanks guys! Now if we could only get the OP to come back and say it works for them.

Avatart
2007-03-15, 07:01 PM
Thanks guys! Now if we could only get the OP to come back and say it works for them.i thought there was something missing here.... :shock:

.T.
2007-03-15, 08:52 PM
Thanks guys! Now if we could only get the OP to come back and say it works for them.

~shrug~ Sometimes they do, sometimes they don't. I don't hold my breath any more.

:greentea: and :icecubes: One lump, or two?

T.Willey
2007-03-15, 09:00 PM
~shrug~ Sometimes they do, sometimes they don't. I don't hold my breath any more.

True, but it would be nice if they came back once in awhile, besides to ask another question.


:greentea: and :icecubes: One lump, or two?
No lumping! I like my head the shape it is now, but I will take the tea. Thanks! :ummmmTea:

christoph
2007-03-19, 01:37 PM
Thank you,

I think you all helped me to go on.