Jump to content

Sum of selected text values


sivapathasunderam

Recommended Posts

;Sum of numbers

(setq ents (ssget)) ;; Get all the text entities
(setq sum nil
      nums nil
)

(setq count1 0)
(while (< count1 (sslength ents))
  (setq nums (cons (atof (cdr (assoc 1 (entget (ssname ents count1))))) nums)) ;; Insert the value of the text into a list
  (setq count1 (1+ count1))
)
(setq sum (apply '+ nums)) ;; Add them all

(entmake (list (cons 0 "TEXT") (cons 1 (rtos sum)) (cons 10 (getpoint)) (cons 40 (getvar "TEXTSIZE")))) ;; Make a single line that will be placed at the cursor

Here is my solution. If anyone that sees this has a better solution, feel free to correct me. Hope that helps!

  • Like 2
Link to comment
Share on other sites

11 minutes ago, Aditya Bagaskara said:

I don't know how to do that, sorry ...

Like this !

(setq ss (ssget "_:L" '((0 . "TEXT,MTEXT"))))

 

  • Like 3
Link to comment
Share on other sites


Don't need to limit things not no locked layers since your just reading the values, and (0 . "TEXT,MTEXT") can be shorted to just (0 . "*TEXT")

 

This will allow you to select any text. it will only add up text that is only numbers. since distof will only work if the string is only numbers.

;;----------------------------------------------------------------------------;;
;;Sum of selected text that is only numbers
(defun C:foo (/ sum ss)
  (setq sum 0)
  (setq SS (ssget '((0 . "*TEXT") (1 . "*#*"))))
    (foreach text (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
      (if (setq x (distof (cdr (assoc 1 (entget text))))
        (setq sum (+ x sum))
      )
    )
  )
  (entmake (list (cons 0 "TEXT") (cons 1 (rtos sum 2 3)) (cons 10 (getpoint)) (cons 40 (getvar "TEXTSIZE")))) 
  (princ)
)

 

 

Edited by mhupp
  • Like 3
Link to comment
Share on other sites

mmm I guess would limit text that have numbers in it. only numbers IDK.

 

(setq SS (ssget '((0 . "*TEXT") (1 . "*#*"))))

 

-Edit

This says any text that doesn't have a letters in it.

(setq SS (ssget '((0 . "*TEXT") (1 . "~*@*"))))

 

-Edit2

I guess you could then check them against each other and keep the ones that are in both. if you really wanted to be sure.

 

Wildcards

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

From MHUPPs idea, try this: 

Added in Dimensions for my own use (I had something that worked, and then saw a better method and now want to spend time making mine better again)

 

  (setq SS (ssget
    '(
      (-4 . "<OR")
        (-4 . "<AND") (0 . "*TEXT") (-4 . "<AND") (1 . "*#*") (1 . "~*@*") (-4 . "AND>") (-4 . "AND>") ; maybe too many ANDs here
        (-4 . "<AND") (0 . "DIMENSION") (-4 . "AND>")
      (-4 . "OR>")
    )
  )) ; end ssget, setq

 

Edited by Steven P
  • Like 1
Link to comment
Share on other sites

This. but it doesn't solve the problem just makes one big selection set.

(setq SS (ssget '((0 . "*TEXT,DIMENSION") (1 . "*#*,~*@*"))))

 

Need to find the entities that are only in both selection sets. texts that have numbers but don't have any letters. and unless your going to process a lot of text. I think it might be a wash.  Since distof weeds out the text you can't use. but you can look at this and pull a few functions so you can process text that have letters and numbers.

 

(if (setq SS (ssget '((0 . "*TEXT,DIMENSION") (1 . "*#*"))))
  (progn
    (if (setq SS1 (ssget "_P" '((0 . "*TEXT,DIMENSION") (1 . "~*@*"))))
      (foreach text (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS1)))
        (if (ssmemb text ss)
          (ssadd text ss2)
        )
      )
    )
  )
)  

 

  • Like 3
Link to comment
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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...