OMEGA-ThundeR Posted August 28, 2013 Posted August 28, 2013 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... Quote
Lee Mac Posted August 28, 2013 Posted August 28, 2013 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). 1 Quote
OMEGA-ThundeR Posted August 28, 2013 Author Posted August 28, 2013 Hot-damn! i don't understand any of the code, but it works like a charm. You made my life a lot easier! Thanx! Quote
Lee Mac Posted August 28, 2013 Posted August 28, 2013 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. Quote
Paulvth Posted January 19, 2017 Posted January 19, 2017 (edited) 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 January 19, 2017 by SLW210 Fixed Code Tags Quote
satishrajdev Posted January 19, 2017 Posted January 19, 2017 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) ) Quote
Paulvth Posted January 19, 2017 Posted January 19, 2017 I don't understand why, but it still doesn't do the job for me. seems like the "*" is not selecting the right layers...? Quote
Lee Mac Posted January 19, 2017 Posted January 19, 2017 Does your drawing already contain a layer with name "I-BASEWALL"? Quote
Paulvth Posted January 19, 2017 Posted January 19, 2017 No, for instance, it creates the layer I-BASEWALL running the lisp... but only converts 1 layer from the layer properties. Quote
Roy_043 Posted January 19, 2017 Posted January 19, 2017 @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? Quote
Paulvth Posted January 19, 2017 Posted January 19, 2017 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 Quote
OMEGA-ThundeR Posted January 19, 2017 Author Posted January 19, 2017 Edit: nah, just a brainfart Quote
BIGAL Posted January 20, 2017 Posted January 20, 2017 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. Quote
Janouren Posted April 12, 2021 Posted April 12, 2021 (edited) 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 April 12, 2021 by Janouren Quote
BIGAL Posted April 13, 2021 Posted April 13, 2021 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. Quote
Recommended Posts
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.