PDA

View Full Version : Add .17 to text numbers globally


ss_66ss396
2008-12-06, 12:41 AM
Hello,
I'm looking for a lisp routine to add .17 to my text. It would be nice if it would do mtext as well as regular text. I also need to retain the letters within the text. For example, I have a callout that reads 512.15 TC and I want it to say 512.32 TC. I have about a miles worth of 6" curb that needs to be 8" curb-both sides and a median along a large portion. I found a lisp that will add whatever number I want, but it takes away the suffix, ie TC, FS, etc. Below is the text for that command. If anyone has a different one or can help me modify this one that would be great!

thanks
Josh



; ************************* DP2.LSP *****************************
; Program to select required text (on a layer called LEVELS only)
; and change the numerical part of each text item by adding or
; subtracting a user-entered value. 20 May 1998
; Version 3 Modified to convert negative number values
; Written by Colin Browning
; ================================================== =============
(Defun *ERROR* (MSG)
(Princ "Error: ")
(Princ MSG)
(Princ)
)
; *** SUB-ROUTINE TO UPDATE NUMBER VALUE:-
(Defun Processit ()
(setvar "DIMZIN" 0)
(Setq NUM1 nil)
(Setq TLa (Strlen T2a)) ;calc string length
(Setq KNT 1)
(Setq PRE "") ;initialise prefix store as an empty string
(Repeat TLa
(Setq C1 (Substr T2a KNT 1)) ;check each char
(Setq C2 (Ascii C1)) ;get ascii value
(If (Or (< C2 45)(> C2 57)(= C2 46)(= C2 47));if not a number
(Progn ;char or a minus sign then...
(Setq PRE (Strcat PRE C1)) ;build prefix string
(Setq KNT (1+ KNT))
) ;end of Progn
(Setq NUM1 (Substr T2a KNT)) ;else get remaining number
) ;end of IF
) ;end of Repeat
(If NUM1 ;check if number element exists
(Progn
(Setq TLb (Strlen NUM1)) ;calc no. of chars in number string
(Setq NUM2 (Atof NUM1)) ;convert string number to a real
(Setq TLc (Strlen (Itoa (Fix NUM2))));calc no. of chars in integer part
(If (= TLb TLc) ;calc no. dec places
(Setq TLd 0)
(Setq TLd (- TLb TLc 1))
) ;end of 1st IF
(Setq NUM3 (+ NUM2 V2)) ;update number value as a real
(Setq NUM4 (Rtos NUM3 2 2)) ;convert back to string
(Setq T2b (Strcat PRE NUM4)) ;add back prefix code if any
) ;end of Progn
(Setq T2b PRE) ;Else re-use prefix if no no. element
) ;end of outer IF
(Princ)
)
; *** MAIN FUNCTION:-
(Defun C:P2 ()
(Graphscr)
(Initget "Global Manual")
(Setq S1 (Getkword "\nSelect text Globally or Manually (G/M) <Globally>: "))
(If (Null S1)(Setq S1 "Global"))
(If (= S1 "Global")
(Setq S2 (Ssget "X" (List '(0 . "TEXT"))))
)
(If (= S1 "Manual")
(Setq S2 (Ssget (List '(0 . "TEXT"))))
)
(If S2 ;if valid selection...
(Progn ;then do this...
(Initget 1)
(Setq V2 (Getreal "\nEnter value to change levels by: "))
(Setq L2 (Sslength S2)) ;calcs how many items selected
(Setq CNT 0) ;initialise counter
(Repeat L2 ;loop thru each
(Setq N2 (Ssname S2 CNT)) ;gets entity name
(Setq A2 (Entget N2)) ;gets assoc data listing
(Setq T2 (Assoc 1 A2)) ;extracts text sub-list
(Setq T2a (Cdr T2)) ;extracts actual text item
(Processit) ;sub-routine to change text values
(Setq A3 (Subst (Cons 1 T2b) T2 A2)) ;subst modified text value
(Entmod A3) ;update drawing
(Setq CNT (1+ CNT)) ;increment counter
) ;end of Repeat loop
) ;end of Progn
(Prompt "\nNo valid selection. Try again!");Else option if no selection
) ;end of IF function
(Princ) ;exit cleanly
)

irneb
2008-12-06, 02:14 PM
Is there always a space between the number and the pre-/suffix? Also, have you used any fields in your text? If 1st is true, then it might be possible by tokenizing each text string, then adding the value to each number portion & recreating the string. If the 2nd is true, then running such a routine would destroy the fields. On 2nd thought, it may even be possible to increment the numbers even if no spaces ... so scratch that.

Do you want to select only sertain text entities, or should all of them (in the current DWG) be updated?

lpseifert
2008-12-06, 08:12 PM
Try this, compliments of Mr. Carl B.

(defun c:txm()
(terpri)
;; Perform math on text numbers in-place, by Carl B., coded March 2000

(setvar "cmdecho" 0)
;; select lines to annotate
(setq mfactor 1 afactor 0 input nil)
(initget "M A")
(setq input (getkword "\nMultiply<M> or Add<A>?: "))
(cond
((= input "M") (initget 1)
(setq mfactor (getreal "\nMult Factor: ")))
((= input "A") (initget 1)
(setq afactor (getreal "\nAdd Amount: ")))
(T nil)
) ; cond
(initget 1)
(setq precis (getint "\nPlaces after decimal: "))
(setq units (getstring T "\nEnter units to append: "))

(princ "\nSelect text to revise: ")
(setq textset nil)
(while (not textset) (setq textset (ssget '((0 . "*TEXT")))))
(setq numitems (sslength textset))
;; loop to isolate text & change text
(setq itemno 0)
(repeat numitems
(setq entname (ssname textset itemno))
;;(setq enttype (cdr (assoc 0 (entget entname))))
(setq oldtxt_pr (assoc 1 (entget entname)))
(setq oldtxt (cdr oldtxt_pr))
(setq oldtext# (atof oldtxt))
(setq newtext# (+ (* oldtext# mfactor) afactor))
(setq newtxt (strcat (rtos newtext# 2 precis) units))
(setq newtxt_pr (cons 1 newtxt))
(setq entstuff (entget entname))
(setq entstuff (subst newtxt_pr oldtxt_pr entstuff))
(entmod entstuff)
(setq itemno (1+ itemno))
) ; repeat
(princ)
) ; the end
(princ "Do math on text Start with \"TXM\"")
(princ)

ss_66ss396
2008-12-08, 06:28 PM
irneb - there always is a space between the numbers and letters and we dont use fields. Our typical text string that we will have to change is 55.69 TC. I want to be able to select certain text entities although it is a large portion of the dwg. I don't mind isolating the ones I want and puting them on a increment layer or similar.
Ipseifert - I tried that routine, but I couldn't get it to work right. What do I enter in for units?

lpseifert
2008-12-08, 08:46 PM
If you enter something at the Units prompt (e.g. ft) it will append the string 'ft' to the number (e.g. 123.45 ft). Skip the prompt if you don't want any units appended. If you want to retain TC, enter TC

ss_66ss396
2008-12-08, 09:07 PM
Ok I got it to work. That is better than what I had. I had a secod lisp (TextPS) that adds prefix/suffix that I was going to use.
Irneb-how would I go about tokenizing the text string and recreating the string? Sorry, I'm not Lisp educated.

Thanks for your help. This makes changing the curb to 8" much less daunting!
Josh

irneb
2008-12-09, 07:08 AM
Attached, command is AddFactor. You're asked for the number to add (could include decimals and / or be negative). Then the precision (how many digits after point). Then to select the entities to change. Works on Text & MText (not Attribs, but could be done). Only Add portion done as yet, for multiply - this can be done by mainly copying the c:AddFactor function & modifying slightly.

Note, it does this for each number portion in all strings selected, so even when you've got a string with "aa345.67dg45dd" and you add 100 at precision 2, the result would be "aa445.67dg145.00dd"

ss_66ss396
2008-12-09, 07:45 PM
Thats perfect! Just what I needed! Thanks!

irneb
2008-12-10, 06:24 AM
Pleasure, glad I could help. :p Update to my code:

Added MultFactor command to multiply numbers by a factor as well. Similar to lpseifert's code.
Allowed the same to be done on Attributes and MLeaders
Fixed a bug where: if the resulting number was a whole number, the decimals were dropped ignoring the precision set.

ss_66ss396
2008-12-11, 02:06 AM
Even better! We are going to dynamic blocks now for pretty much all of our callouts so this is a great tool. I'm sure there are lots of others who will find this useful. Thanks again.

Josh