Hey guys, Im struggling to think of a way to start this code and I dont usually do much with strings but...
I want to make a lisp routine that can search through all the layers in the drawing and pick out certain strings that can be located anywhere in the layer name. for example: if there were three layers called: 12Road34, Layer1, Layer2 I want the function to search through these layers and select the one with the string "Road" in it or any variation of the string (such as "ROAD"). after that I know how to select all the objects in the layer, just struggling with this first part here.
I would show you what code i currently have, but I have very little idea how to start this and I havent found anything in browsing the forums that gives me something I can workwith.
Any help would be greatly appreciated, Thanks!
Sidenote: I am very new to using lisp, and this is my first post
@chasedowdeswell wrote:
...
I want to make a lisp routine that can search through all the layers in the drawing and pick out certain strings that can be located anywhere in the layer name. for example: if there were three layers called: 12Road34, Layer1, Layer2 I want the function to search through these layers and select the one with the string "Road" in it or any variation of the string (such as "ROAD"). ....
Welcome to the Forums!
The (wcmatch) function is what you need to find a string that contains a specific partial string, with asterisk wildcards representing whatever [if anything] may come before or after that partial string. You can step through the table of Layer definitions and look for a matching name.
(while
(and
(not TheOneYouWant); haven't found it yet
(setq lyrinfo (tblnext "layer" (not lyrinfo)))
; next Layer in table, if any
; [the (not lyrinfo) will be T at the first pass only, forcing it to start at the beginning]
); and
(if (wcmatch (setq lyrname (strcase (cdr (assoc 2 lyrinfo))) "*ROAD*")
; forces Layer name from table to uppercase for comparison
(setq TheOneYouWant lyrname); will stop the (while) loop so it doesn't search further
); if
); while
That should leave the Layer name containing any case variant of "Road" [if there's more than one, only the first one it finds] in the TheOneYouWant variable, so you can use things like
(ssget "_X" (list (cons 8 TheOneYouWant))) to find everything on it, etc.
Here is some old code that I had sitting around. It's not pretty or the most optimized, but it should give you a good start.
(setq lnlist nil) (setq llist (tblnext "layer" T)) (while llist (setq name (cdr (assoc 2 llist))) (if (wcmatch name "*ROAD*") (setq lnlist (append lnlist (list name))) ) (setq llist (tblnext "layer")) ) (if lnlist (setq lnlist (acad_strlsort lnlist)))
You might want to also filter out xref layers, frozen layers, etc. which can be done also. Not shown here.
EDIT: I see Kent is here..... I would listen to what he has to say
Thanks Kent! Ill give your solution a try. I had no idea about the wcmatch function. Im trying to make a lisp that searches for layers containing particular strings, selects all the objects in that layer, and then converts them to a more standardized layer. I know how to do the rest...for the most part, just couldnt figure out that first step.
and thanks for all the replies!
Chase
@chasedowdeswell wrote:
.... Im trying to make a lisp that searches for layers containing particular strings, selects all the objects in that layer, and then converts them to a more standardized layer. ....
Gary's suggestion would be more direct for that than mine, provided you're specific enough about the particular partial string to avoid finding things on more Layers than you want. Or consider using something like mine if you need to narrow it to a specific existing Layer name, and simply RENAME-ing that Layer, rather than moving the objects on it to a different Layer. Or if you might have things on more than one Layer that you want converted to a single standardized Layer, try LAYMRG.