lisp to replace the *layer rename functionality of autocad
So in Autocad there's a massively helpful tool that allows you to batch rename layers. You can use * wildcards to isolate multiple layers and rename/prefix/suffix them at will, unless I'm not looking properly this function isn't available in BCAD.
I have the following LISP for batch prefixing all my layers :
;;; Applies a prefix to all Layers in a Drawing except the 0 layer and Defpoints Layer ;;;
(defun C:LP ( / acadDocument layertable layName prefix)
(vl-load-com)
(setq prefix(getstring "\nEnter Layer Prefix to Apply to All Layers: "))
(setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
(setq layertable (vla-get-layers acadDocument))
(vlax-map-collection layertable 'layer-mod)
(princ)
)
(defun layer-mod (layertable)
(setq layName (vlax-get-property layertable 'Name))
(if (not (member layName '("0" "Defpoints")))
(vla-put-Name layertable (strcat prefix layName))
) ;endif
)
My ask is, if I wanted to only prefix layers with a specific suffix, so any layer ending with txt, does anyone have either an edit of the above code or some other LISP that will do the job? It's massively handy for me to isolate all my annotation text layers "txt" and prefix them so I can select/freez/vport freeze them all easier than ctrl-clicking my way through 250+ layers in the layers palette.
Comments
-
Please try the _-Rename command:
: -RENAME Rename [Block/Dimension style/LAyer/LineType/text Style/Table style/Ucs/VIew/ViewPort]:la Layer to be renamed: *_MySuffix NewLayer2_MySuffix NewLayer1_MySuffix NewLayer3_MySuffix New name: MyPrefix_* Layer 'NewLayer2_MySuffix' was renamed to 'MyPrefix_NewLayer2_MySuffix'. Layer 'NewLayer1_MySuffix' was renamed to 'MyPrefix_NewLayer1_MySuffix'. Layer 'NewLayer3_MySuffix' was renamed to 'MyPrefix_NewLayer3_MySuffix'.
0 -
Hi Colin,
if you want to prefix ALL Layers then the suggestion of Roy above is the best way to go...
But if you want to prefix only selected Layers , as you asked, then you need a filter string to select Layers !
So i changed the routine above to do just this .
You have to type a filter ( you can include blanks in the input string ) to select Layers to be prefixed
You can Type for example Z,DEF,G to select only layers containing in their name the specified strings,
in the example all layers containing in their names the above strings...
The routine uses the lisp Function wcmatch so i attach a PDF explaining the way you form the filter string!I attach the routine
;; Applies a prefix to selected Layers in a Drawing except the 0 layer and Defpoints Layer ;;; (defun C:LP ( / acadDocument layertable layName prefix filter) (vl-load-com) (setq prefix (getstring T "\nEnter Layer Prefix to Apply to selected Layers: ")) (setq filter (getstring T "\nEnter Layer Filter to select Layers to be prefixed or press Enter for all Layers: ")) (setq acadDocument (vla-get-activedocument (vlax-get-acad-object))) (setq layertable (vla-get-layers acadDocument)) (vlax-map-collection layertable 'layer-mod) (princ) ) (defun layer-mod (layertableRec) (setq layName (vlax-get-property layertableRec 'Name)) (cond ((= filter "") (if (NOT(wcmatch layName "Defpoints,DEFPOINTS,0" )) (vla-put-Name layertableRec (strcat prefix layName)) ) ;endif ) (T (if (AND (NOT(wcmatch layName "Defpoints,DEFPOINTS,0" )) (wcmatch layName filter) ) (vla-put-Name layertableRec (strcat prefix layName)) ) ) ) )
Example
Prefix = 399x_
Filter =***Z*,*DEF*,*G***
Result of prefixed Layers0 -
-
Thx guys, I had done a bit of searching around and setting drawing filters up then referencing them seemed like the easiest way to do it
but this solves the issue without having to standardise the filters across many workstations AND keep them updated.
Works awesomely, I just changed the user instructions on the command prompt to better clarify that it's case sensitive.
Thx
0 -
The attached LISP will allow you to rename layers based on search string and replacement string
Rename can be either:
- An exact match of the search string with the layer name
A partial match of the search string with the layer name
e.g.
EXACT MATCH
(CCL-LAYRENAME "C-PVMT-CONC-EX" "C-PAVE-CONC-EX" T) Renaming C-PVMT-CONC-EX -> C-PAVE-CONC-EX OK 1
PARTIAL MATCH
(CCL-LAYRENAME "C-PVMT-CONC-EX" "C-PAVE-CONC-EX" nil) Renaming C-PVMT-CONC-EX -> C-PAVE-CONC-EX OK Renaming C-PVMT-CONC-EX-HATCH -> C-PAVE-CONC-EX-HATCH OK Renaming C-PVMT-CONC-EX-NPLT -> C-PAVE-CONC-EX-NPLT OK Renaming C-PVMT-CONC-EX-TEXT -> C-PAVE-CONC-EX-TEXT OK 4
Regards,
Jason Bourhill
BricsCAD V20 Ultimate
CAD Concepts1