Making a list of amounts of each numbers

Making a list of amounts of each numbers

koseku3
Participant Participant
862 Views
4 Replies
Message 1 of 5

Making a list of amounts of each numbers

koseku3
Participant
Participant

Hello people,

 

For instance i have texts in drawing as: 1 2 2 3 3 3

So i want a lisp which can give me a list like this:

1(1)

2(2)

3(3)

 

So i can know how many are there from each value at drawing?

note: these numbers are in seperate texts

 

I hope i could tell my request, sorry for bad english....

0 Likes
Accepted solutions (1)
863 Views
4 Replies
Replies (4)
Message 2 of 5

DannyNL
Advisor
Advisor
Accepted solution

There's probably a better way but here is my quick code.

It just counts text, so it's not limited to only numbers. So if you have A A B C, you can count them as well.

 

(defun c:TxtCnt (/ TC_Selection TC_Count TC_Text TC_Found TC_CountList)
   (setq TC_Selection (ssget '((0 . "TEXT"))))
   (if
      TC_Selection
        (progn
           (setq TC_Count 0)
           (repeat (sslength TC_Selection)
              (setq TC_Text (vla-get-TextString (vlax-ename->vla-object (ssname TC_Selection TC_Count))))
              (if
                 (setq TC_Found (assoc TC_Text TC_CountList))
                   (setq TC_CountList (subst (list TC_Text (1+ (nth 1 TC_Found))) TC_Found TC_CountList))
                   (setq TC_CountList (append TC_CountList (list (list TC_Text 1))))
              )
              (setq TC_Count (1+ TC_Count))
           )
        )
   )
   (if
      TC_CountList
        (progn
           (textscr)
           (foreach TC_Item (vl-sort TC_CountList '(lambda (TC_A TC_B) (< (car TC_A) (car TC_B))))
              (princ (strcat "\n" (car TC_Item) " \(" (itoa (cadr TC_Item)) "\)"))
           )
        )
   )
   (princ)
)
Message 3 of 5

koseku3
Participant
Participant

Thank you so much for quick reply, this is exactly what i needed! Works perfect.

0 Likes
Message 4 of 5

DannyNL
Advisor
Advisor

Nice! Glad I could help Smiley Happy

0 Likes
Message 5 of 5

Kent1Cooper
Consultant
Consultant

@koseku3 wrote:

.... 

For instance i have texts in drawing as: 1 2 2 3 3 3

So i want a lisp which can give me a list like this:

1(1)

2(2)

3(3)

 

So i can know how many are there from each value at drawing?

....


Maybe not "better," but shorter, anyway:

 

(defun C:IQR ; = Integer Quantity Report
  (/ ss qua inlist)
  (setq
    ss (ssget '((0 . "TEXT") (1 . "#,##,###")))
    qua (sslength ss)
    intlist ; list of text contents converted to integers
      (mapcar 'atoi
        (mapcar '(lambda (x) (cdr (assoc 1 (entget (cadr x)))))
          (vl-remove-if '(lambda (y) (listp (cadr y))) (ssnamex ss))
        ); mapcar
      ); mapcar
  ); setq
  (foreach int (vl-sort intlist '<); ascending numerical order, once each
    (prompt (strcat "\n" (itoa int) " (" (itoa (- qua (length (vl-remove int intlist)))) ")"))
  ); foreach
  (princ)
); defun

 

That also restricts it to only integer-content Text of up to three digits.  If yours would be all only one  digit as in your example, change the "#,##,###" in the filter list to just "#"; for two digits, "#,##"; if they might sometimes go to four digits, use "#,##,###,####"; etc.

 

It converts the text content to integers  [which means it has to convert back for the prompts] because it can then more easily report them in order of numerical value.  If you have a list of text-string contents like this:

 

("3" "1" "2" "123")

 

sorting by text-string value results in this:

 

("1" "123" "2" "3")

 

whereas converting them to integers and sorting gives this:

 

(1 2 3 123)

 

EDIT:  As with @DannyNL's routine, it requires the User to select objects  [though in each routine you can grab a whole area with a window, lasso, etc., and it will "see" only the appropriate objects].  But if you want it to report all such objects in the entire drawing, without the User needing to select  them, change this line:

 

    ss (ssget '((0 . "TEXT") (1 . "#,##,###")))

 

to this:

 

    ss (ssget "_X" '((0 . "TEXT") (1 . "#,##,###")))

Kent Cooper, AIA