AutoLISP: Remove Binding Prefixes from XREFs

After you have bound an XREF to a drawing you know the nastiness that comes into your drawing – Layers, Blocks and styles now a have a prefix added to the front of their names.

This routine (I wish I knew who to give credit to…) removes these prefixes from a number of objects/entities after you have bound them to your drawing. These objects include:

  • Layers
  • Blocks
  • Styles (various, text dimensions…)
  • UCS
  • Saved Views

Here’s how:

After using the BIND command to bind an XREF

  • RBP <enter> to start Remove Bind Prefixes

That’s it…

~enjoy

(defun c:RBP(/ ActDoc Name NewName)

; RemoveBindPrefixes

; Renames layers, blocks, dimension styles, text styles, user coordinate systems, and views

; by taking out the bind as bind prefix

; Example Drawing1$0$Layer1 -> Layer1

(vl-load-com)

(defun RemoveBindPrefix (String / Pos LastPos)

(if (setq Pos (vl-string-search "$" String))

(progn

(setq LastPos Pos)

(while (setq Pos (vl-string-search "$" String (1+ Pos)))

(setq LastPos Pos)

)

(substr String (+ 2 LastPos))

)

String

)

)

;---------------------------------------------------------

(setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object)))

(vlax-for Obj (vla-get-Layers ActDoc)

(setq Name (vla-get-Name Obj))

(if (/= (setq NewName (RemoveBindPrefix Name)) Name)

(if (vl-catch-all-error-p (vl-catch-all-apply 'vla-put-Name (list Obj NewName)))

(prompt (strcat "\n Layer: " Name " was not renamed."))

)

)

)

(vlax-for Obj (vla-get-Blocks ActDoc)

(setq Name (vla-get-Name Obj))

(if (/= (setq NewName (RemoveBindPrefix Name)) Name)

(if (vl-catch-all-error-p (vl-catch-all-apply 'vla-put-Name (list Obj NewName)))

(prompt (strcat "\n Block: " Name " was not renamed."))

)

)

)

(vlax-for Obj (vla-get-TextStyles ActDoc)

(setq Name (vla-get-Name Obj))

(if (/= (setq NewName (RemoveBindPrefix Name)) Name)

(if (vl-catch-all-error-p (vl-catch-all-apply 'vla-put-Name (list Obj NewName)))

(prompt (strcat "\n Text style: " Name " was not renamed."))

)

)

)

(vlax-for Obj (vla-get-Views ActDoc)

(setq Name (vla-get-Name Obj))

(if (/= (setq NewName (RemoveBindPrefix Name)) Name)

(if (vl-catch-all-error-p (vl-catch-all-apply 'vla-put-Name (list Obj NewName)))

(prompt (strcat "\n View: " Name " was not renamed."))

)

)

)

(vlax-for Obj (vla-get-UserCoordinateSystems ActDoc)

(setq Name (vla-get-Name Obj))

(if (/= (setq NewName (RemoveBindPrefix Name)) Name)

(if (vl-catch-all-error-p (vl-catch-all-apply 'vla-put-Name (list Obj NewName)))

(prompt (strcat "\n UCS: " Name " was not renamed."))

)

)

)

(vlax-for Obj (vla-get-DimStyles ActDoc)

(setq Name (vla-get-Name Obj))

(if (/= (setq NewName (RemoveBindPrefix Name)) Name)

(if (vl-catch-all-error-p (vl-catch-all-apply 'vla-put-Name (list Obj NewName)))

(prompt (strcat "\n Dimension style: " Name " was not renamed."))

)

)

)

(princ)

)
Advertisement

About AutoCAD Tips

This blog serves as a knowledge base for myself (and anyone else) so that I can reference tips & tricks that I have learned and also refer others to it as well. I hope that this blog helps you learn at least one tip to make your drafting/design experience better.
This entry was posted in AutoLISP, Blocks, Layers, Modifying, Text, TIPS, XREFs. Bookmark the permalink.

4 Responses to AutoLISP: Remove Binding Prefixes from XREFs

  1. Aldrin Leung says:

    Hi,
    Thanks for your lisp for delete prefix from layers on simple drawing.
    However, it doesn’t work on complex drawing blinded with many drawings and each drawing contain the same layers.
    For Example:
    XE_36918_Fen_T1_Level7$0$A-Door
    XE_36918_Fen_T1_Level9-17$0$A-Door
    XE-36918-CORE-COM-L07$0$A-Door
    XE-36918-CORE-COMM-MECH-ELEC SHAFT$0$A-Door
    XE-36918-CORE-RES_L07$0$A-Door
    XE-36918-EXIT STAIR 4_L4-7$0$A-Door
    XE-36918-T1_Type C$0$A-Door

    Is it possible to merge or combine those layer as one layer?
    Can it also apply to suffix?

    A lots of thanks

    Aldrin

    • Alvaro says:

      you just add this line:
      (vl-cmdf “_.-laymrg” “_n” Name “” “_n” NewName “_y”)

      before the line :
      (prompt (strcat “\n Layer: ” Name ” was not renamed.”))

      and it should merge the layers when they exist already

  2. Billy says:

    Does this work for xreffed then bound Linestyles as well? I noticed a Hidden Linestyle in your demo, it did not get renamed. Thanks

  3. David says:

    It didn’t work at all for me. Any updates to this lip?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s