Author Topic: ? about this code  (Read 791 times)

0 Members and 1 Guest are viewing this topic.

jlogan02

  • Bull Frog
  • Posts: 327
? about this code
« 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!?!?!?
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

mhupp

  • Bull Frog
  • Posts: 250
Re: ? about this code
« Reply #1 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. )


jlogan02

  • Bull Frog
  • Posts: 327
Re: ? about this code
« Reply #2 on: September 02, 2022, 01:11:35 PM »
Roger that! Thanks mhupp
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10