Hi.
I have used this routine for sometime from Stig Madsen, jan. 2002, but limited usage because the way it add multiple numbers in a attribute string.
What this lisp does is add/subtract INTEGER number to existing attribute numbers.
ie. add 5
bla123bla ==>
bla128bla
ie. add 2
bla-123bla.456/bla678 ==>
bla-125bla.458/bla680
But more often then not, I only need to add integer to the first set, or second set or third set of numbers in the string.
What i would like to accomplish is with additional options
(prompt "add to first number, add to second number, add to last number?")
ie. add 5 to first number
bla123bla ==>
bla128bla
ie. add 2 to first number
bla-123bla.456/bla678 ==>
bla-125bla.456/bla678
ie. add 10 to second number
bla-123bla.456bla ==>
bla-123bla.466bla
ie. add 10 to last number
bla-123bla.456/bla678 ==>
bla-123bla.456/bla778
Please show me the way.
Thankyou in advance
;;; ****************************************************
;;; ATTCALC - calculate numbers in attributes *
;;; Stig Madsen, jan. 2002 *
;;; ****************************************************
;; pflag is used if more consecutive numbers are
;; separated with periods. First period turns it
;; on and second numbers turns it off.
(defun makelist (str / alist a astr ch num test pflag pch)
(setq a 1
astr ""
pflag nil
)
(if (<= 48 (ascii (substr str 1 1)) 57)
(setq num T)
(setq num nil)
)
(repeat (strlen str)
(setq ch (substr str a 1))
(setq test num)
(cond
((= ch ".")
(if (>= a 2)
(if (and (<= 48 (ascii (substr str (1- a) 1)) 57)
(<= 48 (ascii (substr str (1+ a) 1)) 57)
)
(progn
(if pflag
(setq num (not test))
(progn (setq num T) (setq pflag T))
)
)
(setq num nil)
)
(setq num nil)
)
)
((<= 48 (ascii ch) 57) (setq num T))
(T (setq num nil))
)
(if (= test num)
(setq astr (strcat astr ch))
(progn
(setq alist (cons astr alist))
(setq astr ch)
)
)
(setq a (1+ a))
)
(setq alist (cons astr alist))
(reverse alist)
)
(defun strcalc (str func arg / alist)
(setq alist (makelist str))
(setq alist
(mapcar
'(lambda (n)
(cond
((<= 48 (ascii (substr n 1 1)) 57)
(if (setq pos (vl-string-position (ascii ".") n))
(setq dec (- (strlen n) (1+ pos)))
(setq dec 0)
)
(cond
((null arg)
(setq n (rtos (apply func (list (read n))) 2 dec))
)
((listp arg)
(setq n (rtos (apply func (append (list (read n)) arg)) 2 dec))
)
(T
(setq n (rtos (apply func (list (read n) arg)) 2 dec))
)
)
)
(T n)
)
)
alist
)
)
(apply 'strcat alist)
)
;;; Simply lists any attribute tags in ent
(defun listattribs (ent / i enttype entl name)
(setq i 0
enttype nil
)
(setq entl (entget ent))
(while (/= enttype "SEQEND")
(setq ent (entnext ent)
entl (entget ent)
)
(setq enttype (cdr (assoc 0 entl)))
(if (setq name (cdr (assoc 2 entl)))
(princ (strcat "\n" name))
)
(setq i (1+ i))
)
)
;;; Searches the block in ent and calculates the tags
(defun calcattribs (ent tag incr / i i enttype entl enttag val)
(setq i 0
j 0
enttype nil
)
(setq entl (entget ent))
(while (/= enttype "SEQEND")
(setq ent (entnext ent)
entl (entget ent)
)
(setq enttype (cdr (assoc 0 entl))
enttag (cdr (assoc 2 entl))
)
(if (setq val (cdr (assoc 1 entl)))
(progn
(setq j (1+ j))
(cond
((= tag "*")
(setq val (strcalc val '+ incr)
i (1+ i)
)
(entmod (subst (cons 1 val) (assoc 1 entl) entl))
)
((= tag enttag)
(setq val (strcalc val '+ incr)
i (1+ i)
)
(entmod (subst (cons 1 val) (assoc 1 entl) entl))
)
(T nil)
)
)
)
(entupd ent)
)
(cons i j)
)
;;; Main function
;;; Filters out wanted blocks and attributes and calls
;;; function calcattribs to calculate the tag values
(defun C:ATTCALC (/ a att tag sset count cmd tmp tmpval)
(setq cmd (getvar "CMDECHO"))
(setvar "CMDECHO" 0)
(command "UNDO" "Begin")
(initget 128 "Pick")
(setq att (getkword "\nEnter block name or Pick <*>: "))
(cond ((or (null att) (= att "") (= att "*"))
(setq sset (ssget "X" (list (cons 0 "INSERT") (cons 2 "*"))))
)
((= att "Pick")
(setq sset (ssget '((0 . "INSERT"))))
)
((= (type att) 'STR)
(setq sset (ssget "X" (list (cons 0 "INSERT") (cons 2 att))))
)
(T nil)
)
(while (null tag)
(initget 128 "?")
(setq tag (getkword "\nEnter tag name ?/<*>: "))
(cond ((or (null tag) (= tag "") (= tag "*"))
(setq tag "*")
)
((= tag "?")
(if sset
(listattribs (ssname sset 0))
(princ "\nNo attributes found")
)
(setq tag nil)
)
)
)
(if (numberp nval)
(cond
((= (type nval) 'REAL)
(setq tmpval
(getreal (strcat "\nEnter increment value <" (rtos nval) ">: "))
)
(if tmpval (setq nval tmpval))
)
((= (type nval) 'INT)
(setq
tmpval (getreal (strcat "\nEnter increment value <" (itoa nval) ">: "))
)
(if tmpval (setq nval tmpval))
)
)
(setq nval (getreal "\nEnter increment value <0>: "))
)
(if (null nval)
(setq nval 0)
)
(if (= (- nval (fix nval)) 0)(setq nval (fix nval)))
(setq a 0
ccount 0
scount 0
)
(if sset
(repeat (sslength sset)
(if (> (cdr (assoc 66 (entget (ssname sset a)))) 0)
(progn
(setq tmp (calcattribs (ssname sset a) (strcase tag) nval))
(setq ccount (+ ccount (car tmp)))
(setq scount (+ scount (cdr tmp)))
)
)
(setq a (1+ a))
)
(princ "\No blocks found")
)
(princ (strcat "\nSearched "
(itoa scount)
" attributes ("
(itoa ccount)
" changed)"
)
)
(command "UNDO" "End")
(setvar "CMDECHO" cmd)
(princ)
)