If you're happy with this, Retable, by @exceed
Then you can do this, then RETABLE it.
(defun drawText (pt hgt str)
(entmakex (list (cons 0 "TEXT")
(cons 10 pt)
(cons 40 hgt)
(cons 1 str)))
)
;; command CMBL for Count Mtext By Layer
(defun c:cmbl ( / ss i layer val subvalue sum_by_layer xy x y pt hgt rowhgt str)
;; user selects Mtext objects
(setq ss (ssget (list (cons 0 "MTEXT"))))
;; associated list. (list (cons layer_name sum) ... )
(setq sum_by_layer (list))
(setq i 0)
(repeat (sslength ss)
;; read the layer name and value.
(setq layer (cdr (assoc 8 (entget (ssname ss i)))))
(setq val (cdr (assoc 1 (entget (ssname ss i)))))
;; check if that value is a number. If there's anything else than a number we'll skip it
(if (> (atof val) 0.0)
(progn
;; check if that layer already has an associated sub sum
(if (assoc layer sum_by_layer)
(progn
;;
(setq subsum (cdr (assoc layer sum_by_layer))) ;; read the already present value
(setq subsum (+ subsum (atof val))) ;; add new value
;; now substitute the assoc value ... (subst new old list)
(subst (cons layer subsum) (assoc layer sum_by_layer) sum_by_layer )
)
(progn
;; add that layer-value couple to the list
(setq sum_by_layer (append sum_by_layer (list (cons layer (atof val)))))
)
)
)
(progn
)
)
(setq i (+ i 1))
)
(princ sum_by_layer)
;;;; Table
(setq xy (getpoint "\nTable position: "))
(setq x (nth 0 xy))
(setq y (nth 1 xy))
(setq hgt 2.5) ;; text height
(setq rowhgt 8) ;; row height
;;
(drawText (list (+ x 24.0) y) hgt "COUNT")
(setq y (- y rowhgt))
(drawText (list x y) hgt "ITEM")
(drawText (list (+ x 24.0) y) hgt "LAYER")
(drawText (list (+ x 60.0) y) hgt "QTY")
(drawText (list (+ x 80.0) y) hgt "DESCRIPTION")
(foreach row sum_by_layer
(setq y (- y rowhgt))
(drawText (list x y) hgt "PIPE")
(drawText (list (+ x 24.0) y) hgt (car row))
(drawText (list (+ x 60.0) y) hgt (rtos (cdr row) 2 2))
(drawText (list (+ x 80.0) y) hgt ".")
)
(princ)
)