Author Topic: Lisp help. Need to +/- to number in text string  (Read 4694 times)

0 Members and 1 Guest are viewing this topic.

econnerly

  • Newt
  • Posts: 42
Lisp help. Need to +/- to number in text string
« 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.


CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
Re: Lisp help. Need to +/- to number in text string
« Reply #1 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.
« Last Edit: October 18, 2011, 06:52:03 PM by CAB »
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.

econnerly

  • Newt
  • Posts: 42
Re: Lisp help. Need to +/- to number in text string
« Reply #2 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?

ronjonp

  • Needs a day job
  • Posts: 7282
Re: Lisp help. Need to +/- to number in text string
« Reply #3 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)
)

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
Re: Lisp help. Need to +/- to number in text string
« Reply #4 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.)
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.

econnerly

  • Newt
  • Posts: 42
Re: Lisp help. Need to +/- to number in text string
« Reply #5 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!

pBe

  • Bull Frog
  • Posts: 377
Re: Lisp help. Need to +/- to number in text string
« Reply #6 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
« Last Edit: October 19, 2011, 09:22:25 AM by pBe »

econnerly

  • Newt
  • Posts: 42
Re: Lisp help. Need to +/- to number in text string
« Reply #7 on: October 19, 2011, 12:13:48 PM »
Wow... even better! Thank you!

ronjonp

  • Needs a day job
  • Posts: 7282
Re: Lisp help. Need to +/- to number in text string
« Reply #8 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-)

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

econnerly

  • Newt
  • Posts: 42
Re: Lisp help. Need to +/- to number in text string
« Reply #9 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.

pBe

  • Bull Frog
  • Posts: 377
Re: Lisp help. Need to +/- to number in text string
« Reply #10 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

« Last Edit: October 20, 2011, 01:13:09 AM by pBe »