Jump to content

Layer Prefixes.


Glen1980

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

(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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by marko_ribar
(added parameter (getstring T "\n...")) to allow multiple words strings
Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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])))))
     )

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :thumbsup:

 

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

Link to comment
Share on other sites

(LM:ODBX '[color=blue](lambda ( doc ) ([/color]_fixprefix "12345" "ABC" [color=blue]doc)) nil t[/color])
(princ)
)

 

Oh my :o

 

We wouldnt want those arguments missing now do we... :lol:

Apologies Lee

 

Cheers

Link to comment
Share on other sites

Cheers guys.

 

Hopefully I won't have to use this LISP again but you never can tell when people will decide to different stuff. :x

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