Glen1980 Posted April 2, 2012 Share Posted April 2, 2012 I have to go around 20 odd drawings and knock out an incorrect 5 digit layer prefix and replace it with the correct 2 digit layer prefix. I can do this long hand but would like to do it a bit quicker if there is a LISP or a way of block changing layer names that I didn't have in LT (only had full vanilla for 2 months.) Thanks in advance. Quote Link to comment Share on other sites More sharing options...
pBe Posted April 2, 2012 Share Posted April 2, 2012 For multiple layers? Quote Link to comment Share on other sites More sharing options...
Glen1980 Posted April 2, 2012 Author Share Posted April 2, 2012 Ah yes I missed that vital bit of info out. About 20-30 leyers on each drawing, hence my reluctance to do it long hand without checking to see if there is an easier/quicker way. Quote Link to comment Share on other sites More sharing options...
pBe Posted April 2, 2012 Share Posted April 2, 2012 (defun FixPrefix (OldPrefix NewPrefix ln) (vl-load-com) (vlax-for layer (vla-get-layers (vla-get-ActiveDocument (vlax-get-acad-object))) (if (wcmatch (setq ln (vla-get-name layer)) (strcat OldPrefix "*")) (vla-put-name layer (vl-string-subst NewPrefix OldPrefix ln)))) ) (FIXPREFIX "12345" "ABC") Or hard-coded prefix (defun c:FixPrefix ( / OldPrefix NewPrefix ln) (vl-load-com) (setq OldPrefix "12345" NewPrefix "ABC") (vlax-for layer (vla-get-layers (vla-get-ActiveDocument (vlax-get-acad-object))) (if (wcmatch (setq ln (vla-get-name layer)) (strcat OldPrefix "*")) (vla-put-name layer (vl-string-subst NewPrefix OldPrefix ln)))) ) command: FIXPREFIX You can use either script or ODBX or even on a opened drawign file , which one do you prefer? Quote Link to comment Share on other sites More sharing options...
Glen1980 Posted April 2, 2012 Author Share Posted April 2, 2012 I've used scripts before and never heard of ODBX before. Of course this instantly intruiges me and makes me want to use ODBX Which is simpler as I have to get this done? Quote Link to comment Share on other sites More sharing options...
pBe Posted April 2, 2012 Share Posted April 2, 2012 Which is simpler as I have to get this done? ODBX:------> Faster, a lot faster Since you only need to do this on 20 or so drawings and you already know how to write a script then i suggest you stick with it. To satisfy your curiosity: ODBX_Sample Sample using Lee Macs' ObjextDBX Wrapper [color=blue](defun c:sample ( / _FIXPREFIX )[/color] (defun _FIXPREFIX (OldPrefix NewPrefix doc ) (vlax-for layer (vla-get-layers doc) (if (wcmatch (setq ln (vla-get-name layer)) (strcat OldPrefix "*")) (vla-put-name layer (vl-string-subst NewPrefix OldPrefix ln))) ) ) [color=blue](LM:ODBX '_FIXPREFIX "12345" "ABC")[/color] (princ) ) Quote Link to comment Share on other sites More sharing options...
Glen1980 Posted April 2, 2012 Author Share Posted April 2, 2012 Brilliant! I'm having some problems as the wrong prefix has spaces and a hyphen in it but I'm sure I can sort that out. I've looked at Lee's ODBX but the problem I have the prefix has the drawing series number in it so the different drawings have different prefixes. I will have to open and change the LISP with each drawing but still quicker than manually change it. Also someone has doubled up on prefixes e.g. xx55 - Electrics xx55-Electrics Thanks for the help pBe Quote Link to comment Share on other sites More sharing options...
marko_ribar Posted April 2, 2012 Share Posted April 2, 2012 (edited) Here is my try in vanilla : (defun layerlist ( / layn lst ) (while (not (null (setq layn (cdr (assoc 2 (tblnext "LAYER" (null layn))))))) (setq lst (cons layn lst)) ) (setq lst (reverse lst)) ) (defun c:rlp nil (c:replacelayerpref)) (defun c:replacelayerpref ( / oldpref newpref layn laynl layrest laynew ) (setq oldpref (getstring T "\nInput old preffix of layers you want to replace (CASE SENSITIVE) : ")) (setq newpref (getstring T "\nInput new preffix of layers you want to replace old with (CASE SENSITIVE) : ")) (foreach lay (layerlist) (if (wcmatch lay (strcat oldpref "*")) (progn (setq layn (tblobjname "LAYER" lay) laynl (entget layn) layrest (substr lay (+ (strlen oldpref) 1) (- (strlen lay) (strlen oldpref))) laynew (strcat newpref layrest)) (entupd (cdr (assoc -1 (entmod (subst (cons 2 laynew) (assoc 2 laynl) laynl))))) ) ) ) (princ) ) (princ) (princ "\nShortcut for c:replacelayerpref is c:rlp - Start command with : rlp") (princ) But you must apply it on each *.dwg (not sure if you can use LM:ODBX Wrapper)... M.R. Edited April 2, 2012 by marko_ribar (added parameter (getstring T "\n...")) to allow multiple words strings Quote Link to comment Share on other sites More sharing options...
pBe Posted April 2, 2012 Share Posted April 2, 2012 Brilliant!....... but the problem I have the prefix has the drawing series number in it so the different drawings have different prefixes. I will have to open and change the LISP with each drawing but still quicker than manually change it. Also someone has doubled up on prefixes e.g. You dont need to change every time you open a drawing. if you use the first code i posted via script _.open "C:\Folderpath\54512Project.dwg" (FIXPREFIX "54512" "512") _.save _close _.open "C:\Folderpath\78427Project.dwg" (FIXPREFIX "78427" "427") _.save _close Note that the prompts and their order may change from version to version. Holler if you cant figure out how to overcome this: xx55 - Electrics xx55-Electrics Brilliant!Thanks for the help pBe You are Welcome Quote Link to comment Share on other sites More sharing options...
pBe Posted April 2, 2012 Share Posted April 2, 2012 To account for "doubled up on prefixes" and "prefix has spaces and a hyphen" (defun FixPrefix (OldPrefix NewPrefix / ln) (vl-load-com) (vlax-for layer (vla-get-layers (vla-get-ActiveDocument (vlax-get-acad-object))) (if (wcmatch (setq ln (vla-get-name layer)) (strcat OldPrefix "*")) (vla-put-name layer (vl-string-subst NewPrefix OldPrefix [color=blue] (vl-list->string[/color] [color=blue] (vl-remove-if[/color] [color=blue] '(lambda (k) (= 32 k))[/color] [color=blue] (vl-string->list ln))[/color]))))) ) Quote Link to comment Share on other sites More sharing options...
Glen1980 Posted April 2, 2012 Author Share Posted April 2, 2012 You dont need to change every time you open a drawing. if you use the first code i posted via script _.open "C:\Folderpath\54512Project.dwg" (FIXPREFIX "54512" "512") _.save _close _.open "C:\Folderpath\78427Project.dwg" (FIXPREFIX "78427" "427") _.save _close I wondered why I couldn't make the first code work. I'll have a play after lunch. Quote Link to comment Share on other sites More sharing options...
pBe Posted April 2, 2012 Share Posted April 2, 2012 I wondered why I couldn't make the first code work. I'll have a play after lunch. Perhaps you can use the code at post #10 You can however still use LMs' ODX Wrapper by referencing the drawing filename for variable oldprefix e.g. Given the Filename prefix is the same as the Layer names prefixes. Drawing name: 85391-FloorPlan.dwg (setq fname (vla-get-name (vla-get-ActiveDocument (vlax-get-acad-object)))) 85391-FloorPlan.dwg (if (assoc (setq OldPrefix (substr fname 1 5)) '(("65477" . "477")([color=blue]"85391" [/color]. "391")("297671" . "671"))) (dothis) ) HTH Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted April 3, 2012 Share Posted April 3, 2012 Sample using Lee Macs' ObjextDBX Wrapper [color=blue](defun c:sample ( / _FIXPREFIX )[/color] (defun _FIXPREFIX (OldPrefix NewPrefix doc ) (vlax-for layer (vla-get-layers doc) (if (wcmatch (setq ln (vla-get-name layer)) (strcat OldPrefix "*")) (vla-put-name layer (vl-string-subst NewPrefix OldPrefix ln))) ) ) [color=blue](LM:ODBX '_FIXPREFIX "12345" "ABC")[/color] (princ) ) Many thanks for the recommendation pBe, much appreciated However, the example would need to be: (defun c:sample ( / _fixprefix ) (defun _fixprefix ( oldprefix newprefix doc ) (vlax-for layer (vla-get-layers doc) (if (wcmatch (vla-get-name layer) (strcat oldprefix "*")) (vla-put-name layer (vl-string-subst newprefix oldprefix (vla-get-name layer))) ) ) ) (LM:ODBX '(lambda ( doc ) (_fixprefix "12345" "ABC" doc)) nil t) (princ) ) Quote Link to comment Share on other sites More sharing options...
pBe Posted April 4, 2012 Share Posted April 4, 2012 (LM:ODBX '[color=blue](lambda ( doc ) ([/color]_fixprefix "12345" "ABC" [color=blue]doc)) nil t[/color]) (princ) ) Oh my We wouldnt want those arguments missing now do we... Apologies Lee Cheers Quote Link to comment Share on other sites More sharing options...
Glen1980 Posted April 4, 2012 Author Share Posted April 4, 2012 Cheers guys. Hopefully I won't have to use this LISP again but you never can tell when people will decide to different stuff. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted April 4, 2012 Share Posted April 4, 2012 Oh my We wouldnt want those arguments missing now do we... Apologies Lee No worries dude Quote Link to comment Share on other sites More sharing options...
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.