Author Topic: Lisp to count text in drawing  (Read 3660 times)

0 Members and 1 Guest are viewing this topic.

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Lisp to count text in drawing
« Reply #15 on: December 13, 2021, 06:29:13 AM »
Here is a quick example to count strings based on layer names they reside on and generate a table with : String , Layer , Quantity and sort rows based on layer names.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:Test (/ s i e l g p b c r)
  2.   ;; Tharwat Al Choufi - Date : 13.Dec.2021     ;;
  3.   (and (setq s (ssget "_X" '((0 . "TEXT,MTEXT"))))
  4.        (repeat (setq i (sslength s))
  5.          (setq i (1- i)
  6.                e (entget (ssname s i))
  7.                l (list (cdr (assoc 1 e)) (cdr (assoc 8 e)))
  8.          )
  9.          (or (and (vl-some (function (lambda (q)
  10.                                        (and (= (car q) (car l))
  11.                                             (= (cadr q) (cadr l))
  12.                                             (setq r q)
  13.                                        )
  14.                                      )
  15.                            )
  16.                            g
  17.                   )
  18.                   (setq g (vl-remove r g)
  19.                         g (cons (append l (list (1+ (caddr r)))) g)
  20.                   )
  21.              )
  22.              (setq g (cons (append l (list 1)) g))
  23.          )
  24.        )
  25.        (setq p (getpoint "\nTable insertion point :"))
  26.        (setq r 1
  27.              b (vla-addtable
  28.                  (vla-get-block
  29.                    (vla-get-activelayout
  30.                      (vla-get-activedocument (vlax-get-acad-object))
  31.                    )
  32.                  )
  33.                  (vlax-3d-point p)
  34.                  (+ 2 (length g))
  35.                  3
  36.                  635
  37.                  3100
  38.                )
  39.        )
  40.        (progn
  41.          (vla-put-RegenerateTableSuppressed b :vlax-true)
  42.          (vla-setcolumnwidth b 2 1300)
  43.          (mapcar '(lambda (r_ c_ s_)
  44.                     (set:text_ b r_ c_ 350 s_)
  45.                   )
  46.                  '(0 1 1 1)
  47.                  '(0 0 1 2)
  48.                  '("STRING COUNT" "String" "Layer" "QTY")
  49.          )
  50.          (foreach s (vl-sort g '(lambda (j k) (< (cadr j) (cadr k))))
  51.            (setq r (1+ r))
  52.            (mapcar '(lambda (c s) (set:text_ b r c 300 s))
  53.                    '(0 1 2)
  54.                    s
  55.            )
  56.          )
  57.          (vla-put-RegenerateTableSuppressed b :vlax-false)
  58.        )
  59.   )
  60.   (princ)
  61. )
  62. ;;                              ;;
  63. (defun set:text_ (obj row col hgt str)
  64.   (vla-settext obj row col str)
  65.   (vla-setcelltextheight obj row col hgt)
  66.   (vla-setcellalignment obj row col acmiddlecenter)
  67. )
  68.  

ancrayzy

  • Mosquito
  • Posts: 12
Re: Lisp to count text in drawing
« Reply #16 on: December 13, 2021, 08:15:55 PM »
Thank you Mr Tharwat,
It work well, but the lisp must ask user to select the area before processed.
If it automatically runs, it will count all the texts in the drawing and it is not necessary for user.
Regards

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: Lisp to count text in drawing
« Reply #17 on: December 14, 2021, 02:15:44 AM »
Thank you Mr Tharwat,
It work well, but the lisp must ask user to select the area before processed.
If it automatically runs, it will count all the texts in the drawing and it is not necessary for user.
Regards

remove the "_X" from ssget

ancrayzy

  • Mosquito
  • Posts: 12
Re: Lisp to count text in drawing
« Reply #18 on: December 14, 2021, 05:09:12 AM »
Thank you Mr It's Alive!,
It work awesome  :yay!:
It 'll help me (and other users) save a lot of time