Author Topic: ChangeText.lsp with comments  (Read 6339 times)

0 Members and 1 Guest are viewing this topic.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: ChangeText.lsp with comments
« Reply #15 on: January 20, 2012, 12:45:01 PM »
Good for you Andy.

I took a few minutes and changed variable names & commented Lee's code. I hope he doesn't mind.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:ct() (c:ChangeText)) ; short cut
  2.  
  3. (defun c:ChangeText ( / *error* ; always localize your error handler
  4.                         ChangedCount EntityList IndexPointer NewStringLength NewString
  5.                         OldString StringPointer SelectionSet StringFound )
  6.    ;;  error handler
  7.    (defun *error* ( msg )
  8.        (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")) ; ignore common exit error messages
  9.            (princ (strcat "\nError: " msg))
  10.        )
  11.        (princ)
  12.    )
  13.  
  14.    (if
  15.        (and ; both must be true or not nil to proceed
  16.            (not (eq "" (setq OldString (getstring t "\nOld String: ")))) ; user input [Case sensitive]
  17.            (setq NewString    (getstring t "\nNew String: ") ; user input, but not tested with the AND
  18.                  SelectionSet (ssget "_:L" '((0 . "TEXT"))) ; user picked selection set
  19.            )
  20.        )
  21.        (progn
  22.            (setq NewStringLength (strlen NewString)
  23.                  ChangedCount    0  ; counter for message at end of routine
  24.            )
  25.            (repeat (setq IndexPointer (sslength SelectionSet)) ; step through the selection set
  26.                   ;; get the next entity in the selection set & get its entity list, increment the index pointer
  27.                (setq EntityList    (entget (ssname SelectionSet (setq IndexPointer (1- IndexPointer))))
  28.                      StringFound   (cdr (assoc 1 EntityList)) ; extract the string from the entity list
  29.                      StringPointer 0  ; initialize the pointer
  30.                )
  31.                (if (vl-string-search OldString StringFound) ; test for existence of the old string
  32.                    (progn ; use the WHILE because there may be more that one occurrence of the old string
  33.                        (while (setq StringPointer (vl-string-search OldString StringFound StringPointer))
  34.                            ;;  replace the old string with the new string and save the change back to found string
  35.                            (setq StringFound   (vl-string-subst NewString OldString StringFound StringPointer)
  36.                                  StringPointer (+ StringPointer NewStringLength)
  37.                            )
  38.                        )
  39.                        ;;  update the entity list with the change & update the entity in the drawing
  40.                        (entmod (subst (cons 1 StringFound) (assoc 1 EntityList) EntityList))
  41.                        (setq ChangedCount (1+ ChangedCount)) ; increment the counter
  42.                    )
  43.                )
  44.            )
  45.            (princ (strcat "\nChanged " (itoa ChangedCount) " text objects."))
  46.        )
  47.    )
  48.    (princ) ; makes sure a nil is not displayed at the command line
  49. )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

psuchewinner

  • Guest
Re: ChangeText.lsp with comments
« Reply #16 on: January 20, 2012, 01:24:05 PM »
Thanks CAB!

Lee Mac

  • Seagull
  • Posts: 12925
  • London, England
Re: ChangeText.lsp with comments
« Reply #17 on: January 20, 2012, 01:28:38 PM »
I took a few minutes and changed variable names & commented Lee's code. I hope he doesn't mind.

Not at all Alan, thanks for taking the time to comment it  :-)


MeasureUp

  • Bull Frog
  • Posts: 465
Re: ChangeText.lsp with comments
« Reply #18 on: January 22, 2012, 04:28:59 PM »
Give up????? NEVER!!!!!!!!!  Actually, I am the type of person that will not give up no matter how frustrated I get.  The harder it gets, the more determined I get.
...
Sounds good!
Just like what Andrea said, "keep smile" :-D