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

0 Members and 1 Guest are viewing this topic.

psuchewinner

  • Guest
ChangeText.lsp with comments
« on: January 18, 2012, 04:30:07 PM »
Does anybody have a copy of ChangeText.lsp that has been completely commented?  When I look at the copy that I have, the variables are really cryptic and I am having a hard time figuring out exactly how it works.  I know it may be really simple but sometimes the easiest things are hard for me to figure out.

MeasureUp

  • Bull Frog
  • Posts: 465
Re: ChangeText.lsp with comments
« Reply #1 on: January 18, 2012, 05:51:16 PM »
Every author could name his/her code with "ChangeText.lsp" which may come with different context/idea.
I think it would be easier if you attache your code here and some of the Lisp giants will help you out instantly. :-)
« Last Edit: January 18, 2012, 07:03:09 PM by MeasureUp »

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: ChangeText.lsp with comments
« Reply #2 on: January 18, 2012, 08:51:17 PM »
I didn't think ChangeText was still viable. Isn't it from the r10ish± days?
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

BlackCADDER

  • Mosquito
  • Posts: 17
Re: ChangeText.lsp with comments
« Reply #3 on: January 18, 2012, 09:19:32 PM »
1990 AutoDesk version, works only on TEXT. maybe it will help.  :-D
"Permission to ask a question, sir... "
"Permission granted, Baldrick, as long as isn't the one about where babies come from."

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: ChangeText.lsp with comments
« Reply #4 on: January 18, 2012, 10:06:32 PM »
1990 AutoDesk version, works only on TEXT. maybe it will help.  :-D
I saw a modified version Cadaver has posted that would also work on MText, but I saw it back in '07.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

psuchewinner

  • Guest
Re: ChangeText.lsp with comments
« Reply #5 on: January 19, 2012, 07:52:15 AM »
You know....?.....I didnt even think about attaching the code.....Here it is.  Its an oldie but goodie.  I tried to change some of the variable to make them more descriptive.  I may have som of them crossed up.  I haven't tried it yet.  If someone could please add commenting to the guts of it, particularly the "substr" and "subst" parts I would be very thankful.  Its an oldie but goodie.

Code - Auto/Visual Lisp: [Select]
  1. ; **********************************************************************
  2. ;               CHGTEXT.LSP Changed to CT.LSP for EZ Input
  3. ;                                    
  4. ;  This program will replace every occurrence of an "old string" with a    
  5. ;  "new string".  You will be prompted to select the text you wish
  6. ;  to change.  Then you will be asked to enter the "old string" and
  7. ;  the "new string".  After the text has been changed, the total number
  8. ;  of changed lines is displayed.
  9. ; **********************************************************************
  10.  
  11. (defun chgterr (s)
  12.    (if (/= s "Function cancelled")   ; If an error (such as CTRL-C) occurs
  13.       (princ (strcat "\nError: " s)) ; while this command is active...
  14.    )
  15.    (setq Selectionset nil)                      ; Free selection set
  16.    (setq *error* olderr)             ; Restore old *error* handler
  17.    (princ)
  18. )
  19.  
  20. (defun C:CT ()
  21.        ;(/ SelectionSet l n e os as ns st s nsl osl sl si chf chm olderr)
  22.    (setq olderr  *error*             ; Initialize variables
  23.          *error* chgterr
  24.          chm     0)
  25.    (setq SelectionSet
  26. (ssget))                  ; Select objects
  27.    (if SelectionSet (progn                      ; If any objects selected
  28.       (while (= 0 (setq OldStringLength (strlen (setq OldString (getstring t "\nOld string: ")))))
  29.             (princ "Null input invalid")
  30.       )
  31.       (setq NewStringLength (strlen (setq NewString (getstring t "\nNew string: "))))
  32.       (setq l 0 SetLength (sslength Selectionset))
  33.       (while (< l SetLength)                 ; For each selected object...
  34.          (if (= "TEXT"               ; Look for TEXT entity type (group 0)
  35.                 (cdr (assoc 0 (setq Entity (entget (ssname Selectionset l))))))
  36.            ;-------------------------------------------------------------progn
  37.                (setq chf nil si 1)
  38.                (setq s (cdr (setq as (assoc 1 Entity))))
  39.                (while (= OldStringLength (setq sl (strlen
  40.                              (setq st (substr s si OldStringLength)))))
  41.                   (if (= st os)
  42.                       (progn
  43.                         (setq s (strcat (substr s 1 (1- si)) NewString;creates a new string that contains the
  44.                                         (substr s (+ si OldStringLength))));string with the leftover text (old string stripped)
  45.                         (setq chf t) ; Found old string
  46.                         (setq si (+ si NewStringLength))
  47.                       )
  48.                       (setq si (1+ si))
  49.                   )
  50.                )
  51.                (if chf (progn        ; Substitute new string for old
  52.                   (setq Entity (subst (cons 1 s) as Entity))
  53.                   (entmod Entity)         ; Modify the TEXT entity
  54.                   (setq chm (1+ chm))
  55.                ))
  56.             )
  57.          )
  58.          (setq l (1+ l))
  59.       )
  60.    ))
  61.    (princ "Changed ")                ; Print total lines changed
  62.    (princ chm)
  63.    (princ " text lines.")
  64.    (terpri)
  65.    (setq *error* olderr)             ; Restore old *error* handler
  66.    (princ)
  67. )

<edit: code tags added>
« Last Edit: January 20, 2012, 12:10:52 PM by CAB »

Lee Mac

  • Seagull
  • Posts: 12927
  • London, England
Re: ChangeText.lsp with comments
« Reply #6 on: January 19, 2012, 08:39:59 AM »
Looks like a Find & Replace job to me, anyway, here is a variation:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:ct ( / *error* ch en in nl ns os ps ss st )
  2.  
  3.     (defun *error* ( msg )
  4.         (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
  5.             (princ (strcat "\nError: " msg))
  6.         )
  7.         (princ)
  8.     )
  9.  
  10.     (if
  11.         (and
  12.             (not (eq "" (setq os (getstring t "\nOld String: "))))
  13.             (setq ns (getstring t "\nNew String: ")
  14.                   ss (ssget "_:L" '((0 . "TEXT")))
  15.             )
  16.         )
  17.         (progn
  18.             (setq nl (strlen ns)
  19.                   ch 0
  20.             )
  21.             (repeat (setq in (sslength ss))
  22.                 (setq en (entget (ssname ss (setq in (1- in))))
  23.                       st (cdr (assoc 1 en))
  24.                       ps 0
  25.                 )
  26.                 (if (vl-string-search os st)
  27.                     (progn
  28.                         (while (setq ps (vl-string-search os st ps))
  29.                             (setq st (vl-string-subst ns os st ps)
  30.                                   ps (+ ps nl)
  31.                             )
  32.                         )
  33.                         (entmod (subst (cons 1 st) (assoc 1 en) en))
  34.                         (setq ch (1+ ch))
  35.                     )
  36.                 )
  37.             )
  38.             (princ (strcat "\nChanged " (itoa ch) " text objects."))
  39.         )
  40.     )
  41.     (princ)
  42. )

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: ChangeText.lsp with comments
« Reply #7 on: January 19, 2012, 08:44:46 AM »
Looks like a Find & Replace job to me, anyway, here is a variation:
Here's another:


:P
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12927
  • London, England
Re: ChangeText.lsp with comments
« Reply #8 on: January 19, 2012, 08:46:25 AM »
Looks like a Find & Replace job to me, anyway, here is a variation:
Here's another:

:P

exactly  :-P

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: ChangeText.lsp with comments
« Reply #9 on: January 19, 2012, 08:49:34 AM »
Looks like a Find & Replace job to me, anyway, here is a variation:
Here's another:

:P

exactly  :-P

I like how you still used cryptic variables.  :-D
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12927
  • London, England
Re: ChangeText.lsp with comments
« Reply #10 on: January 19, 2012, 08:50:14 AM »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: ChangeText.lsp with comments
« Reply #11 on: January 19, 2012, 09:38:18 AM »
Looks like I have yet another version in my files under the same file name.
Code: [Select]
;;; The old CHGTEXT command - rudimentary text editor
(defun C:CHGTEXT () (cht_Edit nil))

(defun cht_Edit (objs / last_o tot_o ent o_str n_str st s_temp
                       n_slen o_slen si chf chm cont ans class)
  ;; Select objects if running standalone
(command "undo" "BEGIN")
  (if (null objs)
    (setq objs (ssget))
  )
  (setq chm 0)
  (if objs
    (progn                   ;; If any objects selected
      (if (= (type objs) 'ENAME)
        (progn
          (setq ent (entget objs))
          (princ (strcat "\nExisting string: " (cdr (assoc 1 ent))))
        )
        (if (= (sslength objs) 1)
          (progn
            (setq ent (entget (ssname objs 0)))
            (princ (strcat "\nExisting string: " (cdr (assoc 1 ent))))
          )
        )
      )
      (setq o_str (getstring "\nMatch string   : " t))
      (setq o_slen (strlen o_str))
      (if (/= o_slen 0)
        (progn
          (setq n_str (getstring "\nNew string     : " t))
          (setq n_slen (strlen n_str))
          (setq last_o 0
                tot_o  (if (= (type objs) 'ENAME)
                         1
                         (sslength objs)
                       )
          )
          ;; For each selected object...
          (while (< last_o tot_o)
            (setq class (cdr (assoc 0 (setq ent (entget (ssname objs last_o))))))
            (if (cond (= "TEXT" class)
                      (= "mtext" class)
                      (= "DIMENSION" class) )
              (progn
                (setq chf nil si 1)
                (setq s_temp (cdr (assoc 1 ent)))
                (while (= o_slen (strlen (setq st (substr s_temp si o_slen))))
                  (if (= st o_str)
                    (progn
                      (setq s_temp (strcat
                                     (if (> si 1)
                                       (substr s_temp 1 (1- si))
                                       ""
                                     )
                                     n_str
                                     (substr s_temp (+ si o_slen))
                                   )
                      )
                      (setq chf t)    ;; Found old string
                      (setq si (+ si n_slen))
                    )
                    (setq si (1+ si))
                  )
                )
                (if chf
                  (progn              ;; Substitute new string for old
                    ;; Modify the TEXT entity
                    (entmod (subst (cons 1 s_temp) (assoc 1 ent) ent))
                    (setq chm (1+ chm))
                  )
                )
              )
            )
            (setq last_o (1+ last_o))
          )
        )
        ;; else go on to the next line...
      )
    )
  )
  (if (/= (type objs) 'ENAME)
    ;; Print total lines changed
    (if (/= (sslength objs) 1)
      (princ (strcat (rtos chm 2 0) " text lines changed."))
    )
  )
(command "undo" "END")
  (terpri)
)
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 #12 on: January 19, 2012, 10:16:01 AM »
I dont get much time to do any coding so its really hard to keep my skill up so variables are a big deal for me.  Maybe in a few years after i get my degree, I can be a full time coder!

MeasureUp

  • Bull Frog
  • Posts: 465
Re: ChangeText.lsp with comments
« Reply #13 on: January 19, 2012, 09:00:08 PM »
Sounds like you give up.  8-)

psuchewinner

  • Guest
Re: ChangeText.lsp with comments
« Reply #14 on: January 20, 2012, 10:53:30 AM »
Sounds like you give up.  8-)

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.  My 10 year old son has seen that in me and taken it to an extreme level and I have to back him off a little and tell him to wave the white flag when he needs to.
I do however ask for a little help every now and then.  Just a nudge usually.

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: 12927
  • 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