Jump to content
bacad

Sum of numbers

Recommended Posts

bacad

I have found some really great lisps that do a great job of adding a series of numbers in (dtext, mtext, mleaders, etc), but have not found a lisp that will add all if commas are being used. I would like to find something that will add all types of text numbers (dtext, mtext, mleaders, etc) with commas and allow me to insert the sum as a dtext or mtext. Any help would be appreciated.

Share this post


Link to post
Share on other sites
BIGAL

Search for parse.lsp there are a few versions around Lee-mac has one it will make a list of a string seperating comma strings x,y,z = (x y z)

Share this post


Link to post
Share on other sites
Lee Mac

Are the commas being used as thousands separators or as decimal points?

Share this post


Link to post
Share on other sites
bacad

Thank you bigal, I have actually used Lee Mac's versions and while I'm writing about Lee Mac I will answer his response with yes some of our employees do use commas as a thousand separator. I'm uploading a couple of small examples of what we’re trying to do.

Example.dwg

Share this post


Link to post
Share on other sites
BIGAL

2nd part your text is not actually in a table as it first appears, so just grab a column of text use Lee's parse lisp and add the numbers up.

 

1st part selecting leaders you are actaully comparing the text part and adding up the numbers for leaders with the same text.

 

May have some time tomorrow to look at it a bit more or Lee may jump in over night.

Share this post


Link to post
Share on other sites
bacad

You are correct that the 2nd part text is not a table (we have a long way to go to create tables with block count and the information we need, but that's for another post). We have used the following lisp (author unknown), but it does not place the text nor works with mtext and mleaders.

 

;;;add selected numbers.  highlight numbers that cannot convert.

(defun c:add (/ txtss badset total cntr ent elist str nstr)
 (setvar "cmdecho" 0)
 (setq total 0
cntr 0
nstr (list))  
 (princ "\nSelect numbers to add: ")
 (setq txtss (ssget '((0 . "TEXT")) ))
 (while (< cntr (sslength txtss))
   (setq ent (ssname txtss cntr)
  elist (entget ent))
   (setq str (cdr (assoc 1 elist)))
   (if (and (> (strlen str) 3) (wcmatch str "*`,*"))
     (repeat (/ (strlen str) 4)
(setq nstr (cons (substr str (- (strlen str) 2) 3) nstr))
(setq str (substr str 1 (- (strlen str) 4)))
);repeat
     );if
   (if nstr
     (foreach v nstr
(setq str (strcat str v)))
     );if
   (setq total (+ total (atof str)))
   (setq cntr (1+ cntr))
   (setq nstr (list))
   );while
 (princ (strcat "\nTotal: " (rtos total 2 2)))
 (setvar "cmdecho" 0)
 (princ)
 );eof

 

Here is another (author unknown) we have used in the past that does almost what we need (works with mtext), but doesn't work with commas or mleaders. I'm a novice when working with lisp routines just enough to be dangerous and have tried to somewhat combine the two with no luck and not sure I would be able to get them to work with mleaders. If I'm just out of luck trying to get a total for mleaders then I'll be happy getting these two to work together.

 

; ADDN04.LSP - ADDS NUMBER STRINGS AND CONVERTS TO SQUARE FOOT AND ACRES with 4 decimal places
;
(defun C:ADDNO4 ()
 (SETQ CMD (GETVAR "CMDECHO"))
 (SETVAR "CMDECHO" 0)
 (SETQ SS NIL)
 (PRINC "/nSELECT TEXT:")(PRINC)
 (SETQ SS (SSGET))
 (SETQ NO -1)
 (setq TX 0)
 (REPEAT (SSLENGTH SS)
   (SETQ NO (1+ NO))
   (SETQ ENT (ENTGET (SSNAME SS NO)))
   (SETQ OLDL (ASSOC 1 ENT))
   (SETQ TXX (CDR OLDL))
   (SETQ TXX (ATOF TXX))
   (SETQ TX (+ TX TXX))
 )
[color="red"]  (CLUF)
 (SETQ TX (* TX LUF))
 (SETQ MID (GETPOINT "\nSELECT MIDDLE POINT OF TEXT:"))
 (SETQ TXT (RTOS TX 2 4))
 (COMMAND "TEXT" "M" MID "" "0" TXT)
 (PRIN1)
(SETVAR "CMDECHO" CMD)
)[/color]
(DEFUN CLUF ()
 (SETQ LUN (GETVAR "LUNITS"))
 (IF (OR (= LUN 3) (= LUN 4))
     (SETQ LUF 0.00694444)
     (SETQ LUF 1)
 )
)

Edited by bacad

Share this post


Link to post
Share on other sites
SLW210

Please read the Code Posting Guidelines and edit your post to include Code Tags (not Quote Tags).

[noparse]

Your Code Here

[/noparse] =

Your Code Here

Share this post


Link to post
Share on other sites
bacad

Newbie error.......it is revised.

Share this post


Link to post
Share on other sites
bacad

I'm sooo close with Mr. fixo's lsp (http://www.cadtutor.net/forum/showthread.php?26151-Sum-of-numbers-in-text-strings/page2, just need it to add thousands...

(defun C:STX (/ 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)

Share this post


Link to post
Share on other sites
bacad
Are the commas being used as thousands separators or as decimal points?

 

Lee Mac does you text calculator http://lee-mac.com/textcalculator.html have the option for adding thousands and an option to select multiple numbers at once? thanks

Share this post


Link to post
Share on other sites
foster5476

The text calculator with the ability to select multiple numbers would be great!!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...