AutoCAD – Drawings

Add up text in AutoCAD: total a column, align it and resize it in bulk

Add up text in AutoCAD to total a column of figures written on a drawing, plus commands to align and resize. A free lisp, and the comma-decimal trap.

  • TS. Đỗ Quốc Hoàng
  • 14 min read
Seven lines of text and four tests: adding up, editing figures, aligning and resizing, with three deliberate traps Photo: Diagram by the Institute of Information Technology in Civil Engineering

A quantity table on a drawing is almost always loose text rather than a real table. Totalling a column of forty figures means retyping them into a calculator, and retyping goes wrong. Straightening the column means dragging every line.

This article publishes the Institute's lisp file for four such jobs, with the source printed in full so you can read it before running it. The command to add up text in AutoCAD is the one that gets used most, but the trap that comes with it is the important part of this article. For the whole approach to text and tables on drawings see our AutoCAD for Construction course.

Four text jobs AutoCAD does not do

JobWhat existsWhere it falls short
Totalling a column of figures on a drawingnothingRetype into a calculator
Adding or subtracting across many linesnothingEdit each line by hand
Straightening a columnJUSTIFYTEXTChanges justification, does not move the text
Resizing many linesThe Properties paletteWorks, but only when the lines are all one type

The third row is the most misunderstood. JUSTIFYTEXT in Express Tools changes a line's anchor point while leaving it exactly where it is, so nothing appears to happen on screen. Straightening a column means moving the lines, which is a different operation entirely.

Download the Institute's file

Download TXT-IIC.lspfour commands. The source stays in the article, because running unknown code on your own machine deserves care.
CommandWhat it does
TXCadd up the numbers held in the selected text
TXNadd or subtract an amount from every selected number
TXLalign the selected text to one edge
TXHchange the text height in bulk

The file linked above prompts in English. A Vietnamese version is also published, with the same four command names.

;;; ==================================================================
;;;  TXT-IIC-EN.LSP  -  working on TEXT in bulk
;;;  Institute of Information Technology in Civil Engineering (IIC)
;;;  Hanoi University of Civil Engineering
;;;  https://iic.huce.edu.vn  -  Tel: 0989 427 809
;;; ------------------------------------------------------------------
;;;  Four commands:
;;;    TXC     add up the numbers held in the selected text
;;;    TXN     add or subtract an amount from EVERY selected number
;;;    TXL     align the selected text to one edge (left / right / centre)
;;;    TXH     change the text height in bulk
;;;
;;;  Why this file is needed: a quantity table on a drawing is usually
;;;  loose text rather than a real table. Totalling a column of 40
;;;  figures means retyping them into a calculator, and retyping goes
;;;  wrong. As for alignment, the JUSTIFYTEXT command in Express Tools
;;;  changes the JUSTIFICATION without MOVING the text, so the column
;;;  still looks ragged.
;;;
;;;  FOUR THINGS TO KNOW BEFORE RUNNING:
;;;    1. TXC only adds lines that ARE numbers. A line holding letters is
;;;       skipped, and the command PRINTS how many were skipped so nobody
;;;       assumes everything was counted. "12.5 m2" is skipped because of
;;;       the m2.
;;;    2. The decimal separator must be a FULL STOP. The line "12,5" is
;;;       SKIPPED ENTIRELY - measured on the 2027 release: it becomes
;;;       neither 12.5 nor 12, it drops out of the sum altogether. This
;;;       is the worst trap in any comma-decimal locale, because the
;;;       total still looks perfectly plausible.
;;;    3. TXL MOVES the text rather than changing its justification.
;;;       Afterwards each line keeps its own insertion point, with only
;;;       the X coordinate pulled to a common value.
;;;    4. Multiline text (MTEXT) carries formatting codes in its content,
;;;       for example "\A1;12.5". TXC strips leading \A...; codes, but
;;;       other codes still cause the line to be skipped - and it is
;;;       counted among the skipped lines.
;;;  Every command PRINTS how many lines it touched, so the result can
;;;  be checked.
;;;
;;;  Command names were checked before being chosen: the acad.pgp alias
;;;  file of the 2027 release has no TXC, TXN, TXL or TXH.
;;;
;;;  To load: type APPLOAD, pick this file, click Load.
;;;  Saved as UTF-8. If you edit it in Notepad, save it back as UTF-8.
;;; ==================================================================

(vl-load-com)

;;; ------------------------------------------------------------------
;;;  Strip a leading MTEXT formatting code: "\A1;12.5" -> "12.5"
;;; ------------------------------------------------------------------
(defun iic-clean (s / i)
  (if (and s (> (strlen s) 2) (= (substr s 1 1) "\\"))
    (progn
      (setq i (vl-string-search ";" s))
      (if i (substr s (+ i 2)) s)
    )
    s
  )
)

;;; ------------------------------------------------------------------
;;;  Turn a line of text into a number, or nil when it is not one.
;;;  distof returns nil on anything it cannot parse, so it doubles as
;;;  the test.
;;; ------------------------------------------------------------------
(defun iic-num (s / c)
  (setq c (iic-clean s))
  (if c
    (progn
      (setq c (vl-string-trim " \t" c))
      (if (= c "") nil (distof c 2))
    )
  )
)

;;; read / write the content of a text object
(defun iic-get (e) (cdr (assoc 1 (entget e))))
(defun iic-set (e s)
  (entmod (subst (cons 1 s) (assoc 1 (entget e)) (entget e)))
)

;;; ------------------------------------------------------------------
;;;  TXC - add up the numbers
;;; ------------------------------------------------------------------
(defun c:TXC (/ ss i e v total n skip)
  (princ "\nSelect the text to add up: ")
  (setq ss (ssget '((0 . "TEXT,MTEXT"))))
  (if ss
    (progn
      (setq total 0.0 n 0 skip 0 i 0)
      (while (< i (sslength ss))
        (setq e (ssname ss i)
              v (iic-num (iic-get e)))
        (if v
          (setq total (+ total v) n (1+ n))
          (setq skip (1+ skip))
        )
        (setq i (1+ i))
      )
      (princ "\n")
      (princ (strcat "\n  Lines added   : " (itoa n)))
      (princ (strcat "\n  Lines skipped : " (itoa skip)))
      (princ (strcat "\n  TOTAL         : " (rtos total 2 3)))
      (if (> skip 0)
        (princ "\n  (a skipped line holds letters, or uses a comma as its decimal mark)")
      )
      (princ "\n")
      (setq *IIC-TOTAL* total)
    )
    (princ "\nNo text selected.")
  )
  (princ)
)

;;; ------------------------------------------------------------------
;;;  TXN - add or subtract an amount from every number
;;; ------------------------------------------------------------------
(defun c:TXN (/ d ss i e v n skip)
  (setq d (getreal "\nAmount to add (a negative number subtracts): "))
  (if (null d) (progn (princ "\nNo amount given.") (exit)))
  (princ "\nSelect the text to change: ")
  (setq ss (ssget '((0 . "TEXT,MTEXT"))))
  (if ss
    (progn
      (setq n 0 skip 0 i 0)
      (while (< i (sslength ss))
        (setq e (ssname ss i)
              v (iic-num (iic-get e)))
        (if v
          (progn (iic-set e (rtos (+ v d) 2 2)) (setq n (1+ n)))
          (setq skip (1+ skip))
        )
        (setq i (1+ i))
      )
      (princ (strcat "\nChanged " (itoa n) " line(s), skipped " (itoa skip) "."))
    )
    (princ "\nNo text selected.")
  )
  (princ)
)

;;; ------------------------------------------------------------------
;;;  TXL - align the text to a common X coordinate
;;; ------------------------------------------------------------------
(defun c:TXL (/ k ss i e p x mark lst n o)
  (initget "Left Right Centre")
  (setq k (getkword "\nAlign to which edge [Left/Right/Centre] <Left>: "))
  (if (null k) (setq k "Left"))
  (princ "\nSelect the text to align: ")
  (setq ss (ssget '((0 . "TEXT,MTEXT"))))
  (if ss
    (progn
      (setq lst nil i 0)
      (while (< i (sslength ss))
        (setq e (ssname ss i)
              p (cdr (assoc 10 (entget e))))
        (setq lst (cons (cons e (car p)) lst))
        (setq i (1+ i))
      )
      (setq mark (cdar lst))
      (foreach o lst
        (cond
          ((= k "Left") (if (< (cdr o) mark) (setq mark (cdr o))))
          ((= k "Right") (if (> (cdr o) mark) (setq mark (cdr o))))
        )
      )
      (if (= k "Centre")
        (progn
          (setq x 0.0)
          (foreach o lst (setq x (+ x (cdr o))))
          (setq mark (/ x (float (length lst))))
        )
      )
      (setq n 0)
      (foreach o lst
        (if (/= (cdr o) mark)
          (progn
            (command "_.MOVE" (car o) "" (list (cdr o) 0.0) (list mark 0.0))
            (setq n (1+ n))
          )
        )
      )
      (princ (strcat "\nAligned " (itoa (length lst)) " line(s) to the "
                     k " edge, moving " (itoa n) " of them."))
      (princ (strcat "\nX coordinate used: " (rtos mark 2 3)))
    )
    (princ "\nNo text selected.")
  )
  (princ)
)

;;; ------------------------------------------------------------------
;;;  TXH - change text height in bulk
;;; ------------------------------------------------------------------
(defun c:TXH (/ h ss i e n)
  (setq h (getreal "\nNew text height: "))
  (if (or (null h) (<= h 0)) (progn (princ "\nThe height must be above 0.") (exit)))
  (princ "\nSelect the text to resize: ")
  (setq ss (ssget '((0 . "TEXT,MTEXT"))))
  (if ss
    (progn
      (setq n 0 i 0)
      (while (< i (sslength ss))
        (setq e (ssname ss i))
        (entmod (subst (cons 40 h) (assoc 40 (entget e)) (entget e)))
        (setq n (1+ n) i (1+ i))
      )
      (princ (strcat "\nSet " (itoa n) " line(s) to height " (rtos h 2 2) "."))
      (princ "\nNote: annotative text should have its scale changed, not its height.")
    )
    (princ "\nNo text selected.")
  )
  (princ)
)

(princ "\nTXT-IIC-EN loaded. Commands: TXC (add up), TXN (add/subtract), TXL (align), TXH (height).")
(princ)

The total against hand arithmetic

The test drawing holds seven lines of text. Four are plain numbers: 12.5, 30, 7.25, 100. The other three carry deliberate traps: 12,5 written with a comma, 12.5 m2 carrying letters, and Cot C1, which is not a number at all.

Adding the four by hand: 149.75.

Selecting all seven and running TXC prints:

7 found
  Lines added   : 4
  Lines skipped : 3
  TOTAL         : 149.750
  (a skipped line holds letters, or uses a comma as its decimal mark)

A match with the arithmetic. And just as important: the command says that three lines were skipped rather than quietly producing a figure and leaving you to trust it.

Adding up and editing text Photo: Seven lines of text, four tests, and three deliberate traps

The decimal mark: the worst trap of all

The line 12,5 was skipped entirely. It was not read as 12.5, and it was not read as 12 either — it dropped straight out of the sum.

This is the most dangerous thing in the article, for two reasons.

First, half the world writes decimals with a comma, while AutoCAD and every lisp file read a full stop. A quantity table pasted across from a spreadsheet very often brings commas with it.

Second, and worse: when a line is skipped, the total still looks perfectly plausible. Nothing on screen says anything is missing. If the lisp file does not print the number of skipped lines, nobody ever finds out.

The right habit is to read the Lines skipped figure. If it is not zero and you believed the whole column was numeric, some line is formatted wrongly. Change the commas to full stops and add up again. Editing text across a drawing is covered in our article on editing text in bulk in AutoCAD.

The line 12.5 m2 was skipped too, because of the letters. That is usually the right outcome — a unit belongs in the column heading, not beside every figure.

Adding or subtracting across the whole column

TXN asks how much to add and then rewrites the text. A negative figure subtracts.

The same seven lines, adding 10:

BeforeAfter
12.522.50
3040.00
7.2517.25
100110.00
12,5unchanged
12.5 m2unchanged
Cot C1unchanged

The four numeric lines changed correctly and the other three were left untouched. This is how a whole column of levels gets adjusted when the datum moves, or how a finish thickness gets added across a schedule of floor levels.

Note the rewritten figures carry two decimal places regardless of how they were written before: 30 becomes 40.00, not 40. Keeping the original style means editing by hand.

Aligning text by moving it, not by changing justification

TXL asks for the left, right or centre edge and then pulls the lines to one X coordinate.

The test: seven lines set 37 units apart horizontally.

X coordinate
Before1000, 1037, 1074, 1111, 1148, 1185, 1222
After aligning leftall seven at exactly 1000

The left edge takes the leftmost line as its mark, the right edge the rightmost, and the centre takes the mean. The command prints the coordinate it used, so you know where it pulled everything to.

The difference from JUSTIFYTEXT is exactly this: this command moves the text, the other changes the anchor and leaves the position alone. Only moving makes a column read straight on the drawing.

Resizing in bulk, and annotative text

TXH asks for a height and applies it to everything selected. The test: seven lines at 100, changed to 250, and all seven came out at 250.

The job comes up whenever a drawing arrives from another office at a text height that does not suit the plot scale. Text heights and styles are covered in our article on text in AutoCAD.

Worth deciding the target height deliberately rather than by eye. Printed text below about 1.8 mm on the sheet stops being comfortably legible, and a drawing that prints at 1:100 therefore wants model text of at least 180 units. Setting it once across a whole drawing takes one run of the command; discovering the problem after the set has gone out for tender does not.

One caution: annotative text should not have its height set this way, because its displayed height is governed by the annotation scale rather than by the stored number. For that kind of text, change the annotation scale instead.

Counting text objects

The adding command doubles as the quickest way of counting. The Lines added figure plus the Lines skipped figure is the number of text objects in the selection, and the 7 found line just above says the same thing.

In practice: window an area, run the command, ignore the total, and read those two figures to learn how many lines of text the area holds and how many of them are not numbers.

That turns out to be a useful audit on a drawing received from elsewhere. A schedule that should be forty numeric lines and reports thirty-six numbers plus four skips has four cells somebody typed with a unit, a note or a stray character. Finding them by eye on a dense sheet takes twenty minutes; running the command to add up text in AutoCAD and reading one figure takes ten seconds, and it points straight at whether there is a problem at all.

The filter only picks up two kinds of object, TEXT and MTEXT. Text inside blocks, text belonging to dimensions, and block attributes are all excluded. Counting those needs a different approach, and the two figures will not agree — so any report should say which kind was counted.

Technically, the test for "is this a number" rests on the AutoLISP distof function. Autodesk's reference describes it as "Converts a string that represents a real (floating-point) value into a real value", returning "A real number, if successful; otherwise nil." Anything it cannot parse comes back empty, and that is the signal that puts a line in the skipped pile.

Five things people say that are not true

What people sayWhat was measured
AutoCAD can total a column of figuresNo command does it
A figure written with a comma still adds up12,5 was skipped entirely
A sensible-looking total means nothing was missedThe total stays plausible with lines missing; read the skipped count
JUSTIFYTEXT straightens a columnIt changes justification and leaves the position alone
Adding an amount keeps the original formatting30 becomes 40.00, always two decimal places

Frequently asked questions

Which command totals a column on a drawing?

TXC. Select the text and press Enter; it prints the total with the number of lines added and skipped.

Why is the total missing some lines?

Those lines could not be read as numbers: they hold letters, or use a comma as the decimal mark. The Lines skipped figure says how many.

How do I fix figures written with commas?

Change the commas to full stops, using find and replace across the drawing, then add up again.

Can I add an amount to a whole column?

Yes, with TXN. Give the amount to add, or a negative figure to subtract.

Which command straightens a column of text?

TXL, choosing the left, right or centre edge. It moves the lines rather than just changing their justification.

Can it resize many lines at once?

Yes, with TXH and a new height. Annotative text should have its scale changed instead.

About the author

TS. Đỗ Quốc Hoàng — Vice Director of the Institute of Information Technology in Civil Engineering, Hanoi University of Civil Engineering, and a specialist in software development for construction