Summation of numbers with prefix alphabets not working.

Summation of numbers with prefix alphabets not working.

Anonymous
Not applicable
1,114 Views
3 Replies
Message 1 of 4

Summation of numbers with prefix alphabets not working.

Anonymous
Not applicable

Dear All,

I got a lisp code that is well working for summation of number with suffix alphabets, but not working for numbers with prefix alphabets. Please have a look on attachments and below lisp code.

 

(defun C:ADCC (/ cpent elist en ip newtxt pt ss sum sumtxt txt)

(princ "\n\t\t>>>  Select text to get summ >>>")

(if

;;select texts/mtexts on screen :

(setq ss (ssget '((0 . "*TEXT"))))

;; if selected then :

(progn

  ;; store the first text entity for using 'em further :

(setq cpent (ssname ss 0))

  ;; set initial sum to zero :

  (setq sum 0.)

  ;; loop trough selected texts/mtexts :

  (while

    ;; get the first text in selection :

    (setq en (ssname ss 0))

    ;; get entity list of them :

    (setq elist (entget en))

    ;; get the textstring by key 1 from entity list :

    (setq txt (cdr (assoc 1 elist)))

    ;; create output string :

    (setq sumtxt

                   ;; concatenate strings :

                   (strcat

                     ;; convert digits to string :

                     (rtos

                       ;; add to summ the digital value of text :

                       (setq sum (+ (atof txt) sum))

                       ;; 2 is for metric units (3 for engineering) :

                       2

                       ;; set precision by current :

                       (getvar "dimdec")))

                  )

    ;; delete entity from selection set :

    (ssdel en ss)

    )

  ;; display message in the command line:

  (princ (strcat "\nSumm=" sumtxt))

  (setq pt (getpoint "\nSpecify the new text location: "))

  ;; get the insertion point of stored entity :

  (setq ip (cdr (assoc 10 (entget cpent))))

  ;; copy text entity to the new destination point :

  (command "_copy" cpent "" ip pt)

  ;; get the last created entity :

  (setq newtxt (entlast))

  ;; get entity list of them :

  (setq elist (entget newtxt))

  ;; modify entity list with new text string :

  (entmod (subst (cons 1 sumtxt)(assoc 1 elist) elist))

  ;; update changes :

  (entupd newtxt)

  )

)

(princ)

  )

(princ "\nStart command with STX...")

(princ)

0 Likes
Accepted solutions (3)
1,115 Views
3 Replies
Replies (3)
Message 2 of 4

Moshe-A
Mentor
Mentor
Accepted solution

@Anonymous hi,

 

it works cause the (atof) function ignore the suffix alphabet characters.

(setq sum (+ (atof txt) sum))

 

to ignore the prefix characters you need to define another function (as far as i know it  is not part of autolisp)

so i do it like the following but you need to take in consideration that if your prefix\suffix string contains numbers? they will also be return.

 

 

enjoy

moshe

 

 

(defun get-all-numeric (str / i num ch)
 (setq i 0 num "") 
 (repeat (strlen str)
  (setq i (1+ i))
  (setq ch (substr str i 1))

  (if (or
	(= (ascii ch) 46)     ; decimal point
	(and
	  (>= (ascii ch) 48)  ; form zero
	  (<= (ascii ch) 57)  ; until nine
        )
      )
   (setq num (strcat num ch))
  )
 ); repeat

 num 
)

 

Message 3 of 4

dbhunia
Advisor
Advisor
Accepted solution

Hi,

 

Also you can try this.. it works on "100 KVA" "KVA 100" "100 KVA 100" etc.

 

(defun C:ADCC (/ cpent elist en ip newtxt pt ss sum sumtxt txt)
(princ "\n\t\t>>> Select text to get summ >>>")
(if
;;select texts/mtexts on screen :
(setq ss (ssget '((0 . "*TEXT"))))
;; if selected then :
(progn
;; store the first text entity for using 'em further :
(setq cpent (ssname ss 0))
;; set initial sum to zero :
(setq sum 0.0)
;; loop trough selected texts/mtexts :
(while
;; get the first text in selection :
(setq en (ssname ss 0))
;; get entity list of them :
(setq elist (entget en))
;; get the textstring by key 1 from entity list :
(setq txt (cdr (assoc 1 elist)))
;; create output string :

(setq Read_Text (read (strcat "(" txt ")")))
(setq No 0)
(setq count 0)
(while (< count (length Read_Text))
(setq L1_T (nth No Read_Text))
(if (OR (= (isint_I L1_T) T) (= (isint_R L1_T) T))
(progn
(setq sumtxt (strcat (rtos (setq sum (+ L1_T sum)) 2 (getvar "dimdec"))))
)
(princ)
)
(setq No (+ No 1))
(setq count (+ count 1))
)

;; delete entity from selection set :
(ssdel en ss)
)
;; display message in the command line:
(princ (strcat "\nSumm=" sumtxt))
(setq pt (getpoint "\nSpecify the new text location: "))
;; get the insertion point of stored entity :
(setq ip (cdr (assoc 10 (entget cpent))))
;; copy text entity to the new destination point :
(command "_copy" cpent "" ip pt)
;; get the last created entity :
(setq newtxt (entlast))
;; get entity list of them :
(setq elist (entget newtxt))
;; modify entity list with new text string :
(entmod (subst (cons 1 sumtxt)(assoc 1 elist) elist))
;; update changes :
(entupd newtxt)
)
)
(princ)
)
(defun isint_R (a)
(if (= (type a) 'REAL)
T
nil
)
)
(defun isint_I (a)
(if (= (type a) 'int)
T
nil
)
)


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
Message 4 of 4

serag.hassouna
Advocate
Advocate
Accepted solution

Check this out, I guess it would simplify the matter a little bit by separating the string-to-real extraction part from the summation part.