Jump to content

Using wildcards (*) in lisp routine


OMEGA-ThundeR

Recommended Posts

Hi,

 

I want to make an layer rename lisp. But pretty quickly and an extensive search on the interwebz it seemed to be that the Wildcard (*) is not accepted.

 

For instance;

 

i want to rename:

 

LAYER-B5884GG1-SUBNAME-G01 to NEWLAYER-SUBNAME

 

In this case the subname would be know and always be the same. All the rest of the layer is random.

 

So i would try ("-rename" "la" "*SUBNAME*" "NEWLAYER-SUBNAME")

 

But i tried it by hand directly in AutoCAD and it gives an 'invalid layer name' error.

 

Anyone can give me some tips? I only know these simple lisp commands, and VBA or any other language is not know to me... :(

Link to comment
Share on other sites

Here is a very simplistic Visual LISP program:

(defun c:layname ( )
   (vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
       (if (wcmatch (strcase (vla-get-name layer)) "*SUBNAME*")
           (vl-catch-all-apply 'vla-put-name (list layer "NEWLAYER-SUBNAME"))
       )
   )
   (princ)
)
(vl-load-com) (princ)

Note that since the replacement name is static, the program will only rename the first layer matching the given pattern, else duplicate layer names will arise (hence the need for the clumsy vl-catch-all-apply expression).

  • Like 1
Link to comment
Share on other sites

Hot-damn! i don't understand any of the code, but it works like a charm. You made my life a lot easier!

 

Thanx!

Link to comment
Share on other sites

You're most welcome - however, I would suggest looking at the code in more detail, as it will be of benefit should you need to add supplementary conditions for other layer names.

Link to comment
Share on other sites

  • 3 years later...

I'm trying to do the same here... but it's not working for me... any help? what am i doing wrong here?

 

(defun c:layname ( )
(vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
(if (wcmatch (strcase (vla-get-name layer)) "*BAS*")
(vl-catch-all-apply 'vla-put-name (list layer "I-BASEWALL"))
(vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
(if (wcmatch (strcase (vla-get-name layer)) "*SUR*")
(vl-catch-all-apply 'vla-put-name (list layer "I-SURROUNDING"))	
)
)
(princ)
)
(vl-load-com) (princ)

 

can you help me out?

Edited by SLW210
Fixed Code Tags
Link to comment
Share on other sites

Try this :-

(defun c:layname ()
 (vlax-for layer (vla-get-layers
	    (vla-get-activedocument (vlax-get-acad-object))
	  )
   (cond ((wcmatch (strcase (vla-get-name layer)) "*BAS*")
   (vl-catch-all-apply 'vla-put-name (list layer "I-BASEWALL"))
  )
  ((wcmatch (strcase (vla-get-name layer)) "*SUR*")
   (vl-catch-all-apply
     'vla-put-name
     (list layer "I-SURROUNDING")
   )
  )
   )
 )
 (princ)
)

Link to comment
Share on other sites

@Paul:

Strange, I do not see code in this topic that will create a layer. All code samples rename existing layers. And, as Lee has already explained, with a static new name you can only rename a single layer.

Maybe you want to do something else:

Merge layers?

Change the name of several layers using a fixed pattern?

Link to comment
Share on other sites

Yes Roy, thats right.

I got big list of different layers containing "*BAS*" i need to translate them to 1 layer I-BASEWALL - COLOR RED

I got big list of different layers containing "*SUR*" i need to translate them to 1 layer I-SURROUNDING - COLOR GREEN

I got big list of different layers containing "*EQP*" i need to translate them to 1 layer I-EQUIPMENT - COLOR 139

etc. etc. etc.

 

as i need to do a lot of drawings the manual laytrans is not an option. I tried to program some myself like ssget etc.... but its not working plz all help is welcome :oops:

Link to comment
Share on other sites

Just a suggestion to the code posted, if your going to do multiple layers it would be easier to make a defun that does the change layer bit and just past it oldname newname (chglay oldname newname)(chglay oldname newname) or use a list (old new old new old new) in conjunction with the defun. Paulvth has asked for 3 layers.

Link to comment
Share on other sites

  • 4 years later...

Hello, I have the same question about wildcards but related to blocks. There are the same blocks with names 3040555, 3040556, 3040557 and 20405, 20406 etc. and I need to input them all into the "old" section of this LISP to get replaced (blocks starting with 3040... with one block, blocks starting with 2040... with another block and so forth). Does anyone please know how to do that?

(defun c:Test  (/ lst ss i sn en f)
 ;;--------------------------------------------;;
 ;; Author : Tharwat . Date: 09.June.2015	;;
 ;; A function to replace a set of block names ;;
 ;; with a nother as per listed in the list	;;
 ;;--------------------------------------------;;
 (setq lst '(("old" . "new")
             ("sad" . "happy")
             )
       )
 (if (setq ss (ssget "_X" (list '(0 . "INSERT")
                                (cons 2 (apply 'strcat (mapcar '(lambda (x) (strcat (car x) ",")) lst))))))
   (repeat (setq i (sslength ss))
     (setq sn (ssname ss (setq i (1- i)))
           en (entget sn))
     (if (and (setq f (assoc (cdr (assoc 2 en)) lst))
              (tblsearch "BLOCK" (cdr f))
              (entmake (append (list '(0 . "INSERT") (cons 2 (cdr f)))
                (vl-remove-if-not '(lambda (v) (member (car v) '(6 8 10 41 42 43 50 62))) en)))
            )
        (entdel sn)
        )
     )
   )
 (princ)
 )

 

Edited by Janouren
Link to comment
Share on other sites

You could look at the block table and get all the names and compare if they match the 3040 pattern, so making a list of the block names, you have not said what is new name if that is what you want.

 

(setq bnames '())
(setq blocks (vla-get-blocks  (vla-get-activedocument (vlax-get-acad-object))))
(vlax-for blk blocks
(princ (setq bname (vla-get-name blk)))
(if (wcmatch bname "3040*,2040*")
(setq bnames (cons bname bnames))
)
)

You can then make your new old list. 

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