TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: jlogan02 on August 31, 2022, 04:01:40 PM

Title: ? about this code
Post by: jlogan02 on August 31, 2022, 04:01:40 PM
Pulled this off the interwebs on cadtutor https://www.cadtutor.net/forum/topic/37911-layer-prefixes/ written by pBe.

I've tested this on a group of layers where I want to change just the prefix. It appears to work but my concern is my existing layer names have the same 4 letters in the body of the layer name. Is this bullet proof enough to not change that body text?

TBLK_TBLK_####

Result should be

TTBK_TBLK_####

Code - Auto/Visual Lisp: [Select]
  1. (defun c:FixPrefix  ( / OldPrefix NewPrefix ln)
  2.      (vl-load-com)
  3.      (setq OldPrefix "TBLK"
  4.            NewPrefix "TTBK")
  5.      (vlax-for
  6.             layer
  7.             (vla-get-layers
  8.                   (vla-get-ActiveDocument (vlax-get-acad-object)))
  9.            (if (wcmatch
  10.                      (setq ln (vla-get-name layer))
  11.                      (strcat OldPrefix "*"))
  12.                  (vla-put-name
  13.                        layer
  14.                        (vl-string-subst NewPrefix OldPrefix ln))))
  15.      )

I knowwww...why did you name them that way in the first place!?!?!?
Title: Re: ? about this code
Post by: mhupp on September 02, 2022, 08:21:50 AM
http://docs.autodesk.com/ACD/2014/ENU/index.html?url=files/GUID-D8EE91DC-D4DB-43E0-9AFE-5FA166C0896D.htm,topicNumber=d30e640068
Quote
(vl-string-subst "Obi-wan" "Ben" "Ben Kenobi Ben")
"Obi-wan Kenobi Ben"
Note that there are two occurrences of “Ben” in the string that was searched, but vl-string-subst replaces only the first occurrence.

formatted the code to look nicer
Code - Auto/Visual Lisp: [Select]
  1. (defun c:FixPrefix (/ OldPrefix NewPrefix ln)
  2.   (setq OldPrefix "TBLK"
  3.         NewPrefix "TTBK"
  4.   )
  5.     (if (wcmatch (setq ln (vla-get-name layer)) (strcat OldPrefix "*"))
  6.       (vla-put-name layer (vl-string-subst NewPrefix OldPrefix ln))
  7.     )
  8.   )
  9. )

Title: Re: ? about this code
Post by: jlogan02 on September 02, 2022, 01:11:35 PM
Roger that! Thanks mhupp