rickh Posted May 16, 2013 Posted May 16, 2013 Greetings, I am trying to wcmatch a variable with another variable but I want to also include wildcards before/after. Shown below is the portion of code I am working on...trying to rename all layers that start with "V-LABEL-" to "V-ANNO-..." I just can't seem to add a wildcard to the end of "V-LABEL-". My wcmatch portion is near the bottom. Any help is greatly appreciated. Thanks. (setq laylist1 '("V-LABEL-" "V-ANNO-" "V-MARKER-" "V-MARK-" "V-SURFACE-" "V-SURF-" ) ) (vl-load-com) (setq acaddoc (vla-get-activedocument (vlax-get-acad-object)))) (setq layers_Name (vla-get-layers acaddoc)) (setq count 0) (vlax-for item layers_Name (setq Lay_Name (vlax-get-property item 'name)) (setq layold (nth count laylist1)) (setq laynew (nth (+ 1 count) laylist1)) (cond ( (and (not (= layold nil)) (not (= laynew nil)) (wcmatch (strcase lay_name) (strcat layold "*"));<<-DOESN'T WORK );and (setq layremainder (substr lay_name (+ 1 (strlen layold)))) (vlax-put-property item 'name (strcat laynew layremainder)) ) );cond (setq count (+ 2 count)) );vlax-for Quote
MSasu Posted May 16, 2013 Posted May 16, 2013 Your code will ensure that the layers were renamed only if the first 3 layers found in drawing were named as in your renaming rules list; this will never happen since each drawing will contain at least one layer ("0") in the front of those. Please consider instead: [code](setq laylist1 '(("V-LABEL-" . "V-ANNO-") ("V-MARKER-" . "V-MARK-") ("V-SURFACE-" . "V-SURF-"))) (vl-load-com) (setq acaddoc (vla-get-activedocument (vlax-get-acad-object)) layers_Name (vla-get-layers acaddoc)) (vlax-for item layers_Name (setq oldName (vlax-get-property item 'name)) (foreach nameRule laylist1 (if (wcmatch (strcase oldName) (strcat (car nameRule) "*")) (progn (setq newName (strcat (cdr nameRule) (substr oldName (1+ (strlen (car nameRule)))))) (vlax-put-property item 'name newName) (prompt (strcat "\nLayer " oldName " was rename as " newName ".")) ) ) ) ) [/code] Quote
rickh Posted May 16, 2013 Author Posted May 16, 2013 Thanks Mircea, I see what you mean with the way I wrote the list handling.... Thanks a bunch! And I will certainly look into your method in the morning. Quote
Tharwat Posted May 16, 2013 Posted May 16, 2013 (edited) Simply this should work . (setq lst '(("V-LABEL-" "V-ANNO-") ("V-MARKER-" "V-MARK-") ("V-SURFACE-" "V-SURF-"))) (if (setq l (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))) (vlax-for x l (foreach s lst (if (wcmatch (setq n (vla-get-name x)) (strcat (car s) "*")) (if (not (tblsearch "LAYER" (setq nm (strcat (cadr s) (substr n (1+ (strlen (car s)))))))) (vla-put-name x nm) ) ) ) ) ) Edited May 16, 2013 by Tharwat Modified to include the list of layer names Quote
MSasu Posted May 16, 2013 Posted May 16, 2013 Tharwat, please don't miss that your suggested solution will cover only the first case from OP's renaming rules list. Quote
Tharwat Posted May 16, 2013 Posted May 16, 2013 Tharwat, please don't miss that your suggested solution will cover only the first case from OP's renaming rules list. You're right Mircea Codes updated . Quote
Lee Mac Posted May 16, 2013 Posted May 16, 2013 I would recommend: (defun c:renamelayers ( / lst nme ) (setq lst '( ("V-LABEL-*" "V-ANNO-") ("V-MARKER-*" "V-MARK-") ("V-SURFACE-*" "V-SURF-") ) ) (vlax-for lay (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) (setq nme (vla-get-name lay)) (vl-some (function (lambda ( str / new ) (if (wcmatch nme (car str)) (or (tblsearch "layer" (setq new (strcat (cadr str) (substr nme (strlen (car str)))))) (vla-put-name lay new) ) ) ) ) lst ) ) (princ) ) (vl-load-com) (princ) @Tharwat, why the if statment to test for the existence of the Layers Collection? Since layer "0" will always be present, this collection will also always be present. Also, I believe your program will leave a trailing hyphen when concatenating the pattern with the new layer name. Quote
Tharwat Posted May 16, 2013 Posted May 16, 2013 @Tharwat, why the if statment to test for the existence of the Layers Collection? Since layer "0" will always be present, this collection will also always be present. Yes you are right on that Also, I believe your program will leave a trailing hyphen when concatenating the pattern with the new layer name. I have just added one to the strlen function to step over the hyphen mark . Thanks 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.