Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Help using Autolisp to find certain strings within a larger string

5 REPLIES 5
Reply
Message 1 of 6
chasedowdeswell
897 Views, 5 Replies

Help using Autolisp to find certain strings within a larger string

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

5 REPLIES 5
Message 2 of 6


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

Kent Cooper, AIA
Message 3 of 6
rkmcswain
in reply to: chasedowdeswell

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 Smiley Wink

R.K. McSwain     | CADpanacea | on twitter
Message 4 of 6

you could search the layer table for names and wcmatch them... but unless there is something that you are doing in your code that requires the layer names beyond wanting them for an ssget call you can simply use a wildcard in your properties filter for the ssget call itself:

(setq ss (ssget "_X" (list (cons 8 "*ROAD*"))))

-Gary
Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 5 of 6

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

Message 6 of 6


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

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost