TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: econnerly on October 18, 2011, 03:45:10 PM

Title: Lisp help. Need to +/- to number in text string
Post by: econnerly on October 18, 2011, 03:45:10 PM
I need a lisp routine that will add or subtract to modify a value in a text string. Its used for drainage fixture units on waste & vent plans.
Example:
We use basic dtext strings that look like this 4"(6)
I would like to be able to add or subtract to the number that is ONLY in parenthesis and would like to be able to pick multiple text strings at one time so it would result in something like this:
4"(6) + 9 will become 4"(15)
 Does anyone have anything that can do something like this? I have a code that I have tried to modify, but have had no success.

Title: Re: Lisp help. Need to +/- to number in text string
Post by: CAB on October 18, 2011, 05:43:50 PM
Welcome to the Swamp.

See attached a modified (for you use) version of my Text Increment routine.
Title: Re: Lisp help. Need to +/- to number in text string
Post by: econnerly on October 18, 2011, 05:50:38 PM
Nice! You my friend are the man! Only one thing. Would it be quick to modify that so I can build a selection set, run your lisp, and apply it by using "P" for previous?
Title: Re: Lisp help. Need to +/- to number in text string
Post by: ronjonp on October 18, 2011, 06:42:39 PM
Here's a quick one to work with your example:


Code: [Select]
(defun c:num (/ e el i n ss str)
  (vl-load-com)
  (defun _addbetween (string chr1 chr2 number / l1 l2 n p)
    (if (and (setq l1 (vl-string-position (ascii chr1) string))
     (setq l2 (vl-string-position (ascii chr2) string))
     (setq n (atoi (substr string (+ l1 2) (- l2 3))))
)
      (strcat (substr string 1 (1+ l1)) (itoa (+ number n)) (substr string (1+ l2)))
      string
    )
  )
  (setq i -1)
  (and (setq n (getint "\nEnter number to add: "))
       (setq ss (ssget ":l" '((0 . "text") (1 . "*(*)*"))))
       (while (setq e (ssname ss (setq i (1+ i))))
(setq el (entget e))
(setq str (cdr (assoc 1 el)))
(entmod (subst (cons 1 (_addbetween str "(" ")" n)) (assoc 1 el) el))
       )
  )
  (princ)
)
Title: Re: Lisp help. Need to +/- to number in text string
Post by: CAB on October 18, 2011, 06:52:58 PM
Nice! You my friend are the man! Only one thing. Would it be quick to modify that so I can build a selection set, run your lisp, and apply it by using "P" for previous?

Modified the 33 special so try it again. (Very little testing.)
Title: Re: Lisp help. Need to +/- to number in text string
Post by: econnerly on October 18, 2011, 07:01:28 PM
PERFECT! Thank you so very much. This is going to save me a huge amount of time!
Title: Re: Lisp help. Need to +/- to number in text string
Post by: pBe on October 19, 2011, 06:28:43 AM
(defun c:test ( / popit  ss str num str_)
(vl-load-com) 
(defun popit (lst  / a b c)
  (while (setq a (car lst))
       (setq b (cdr lst))
          (if (= a 40)
           (setq    c
           (vl-remove-if-not '(lambda (j)
                       (or (and (>= j 48)
                                (<= j 57))
                           (= j 41))) b)))
                (setq lst b))
     (atoi (vl-list->string c)))
     (if (not val) (setq val 1))
         (setq val (cond
                       ((getint (strcat "\nEnter Value < " (itoa val) " >: ")))
                       (val)))
(setq ss (ssget ":L" '((0 . "*TEXT") (1 . "*(*)*"))))
(repeat (sslength ss) 
(setq str (entget (ssname ss 0)))
(setq num (popit (vl-string->list
         (setq str_ (cdr (assoc 1 (entget (ssname ss 0)))))
       )
     )
)
   (vla-put-textstring (vlax-ename->vla-object (ssname ss 0))
        (vl-string-subst
        (strcat "(" (itoa (+  num val)) ")")
        (strcat "(" (itoa num) ")") str_))
  (setq ss (ssdel (ssname ss 0) ss))
  )
(princ)
  )

EDIT: modified for multiple selection
Title: Re: Lisp help. Need to +/- to number in text string
Post by: econnerly on October 19, 2011, 12:13:48 PM
Wow... even better! Thank you!
Title: Re: Lisp help. Need to +/- to number in text string
Post by: ronjonp on October 19, 2011, 12:55:02 PM
Wow... even better! Thank you!

IMO .. that's not better than CAB's solution as his will honor real numbers as well.  8-)
Title: Re: Lisp help. Need to +/- to number in text string
Post by: econnerly on October 19, 2011, 01:18:50 PM
Wow... even better! Thank you!

IMO .. that's not better than CAB's solution as his will honor real numbers as well.  8-)

Very good point. Im keeping them both. Im currently at a MEP firm and they tend to only use integers. I'm normally in a civil design positions where CAB's routine would reign superior.
Title: Re: Lisp help. Need to +/- to number in text string
Post by: pBe on October 19, 2011, 11:40:34 PM
Quote
IMO .. that's not better than CAB's solution as his will honor real numbers as well.  8-)

That is correct.  cant argue with that. :)
I would take CAB's or Ron's code over mine anytime.

Very good point. Im keeping them both. Im currently at a MEP firm and they tend to only use integers. I'm normally in a civil design positions where CAB's routine would reign superior.

Good for you