BIM

Sequential numbering in AutoCAD: a lisp and a test of order

Sequential numbering in AutoCAD, a whole batch of blocks in one pass. Plus the built-in TCOUNT command, and a measurement of why the order comes out wrong.

  • TS. Đỗ Quốc Hoàng
  • 10 min read
The same eight blocks in one pass: above, the order a widely used lisp produced; below, the Institute's file Photo: Screenshot taken on AutoCAD 2027 at the Institute

Numbering columns, piles, sheets or rooms is repetitive enough that nobody wants to type it, which is why a numbering lisp is the most downloaded kind of lisp there is. Sequential numbering in AutoCAD looks simple until you run one of those files and watch the numbers land in a scrambled order instead of running left to right.

This article finds the cause by measurement on AutoCAD 2027 and offers the Institute's own file, which fixes exactly that. For the whole drafting and documentation workflow, see the AutoCAD for Construction course.

AutoCAD already has a numbering command, called TCOUNT

Before downloading anything, it is worth knowing that the full AutoCAD release ships with Express Tools, and TCOUNT lives there. Autodesk's TCOUNT page puts it in one sentence: "Adds sequential numbering to text and mtext objects as a prefix, suffix, or replacement text."

On our machine the ACADPREFIX variable contains the AutoCAD 2027\Express folder, so the toolset sits inside a standard install. The first call prints Initializing... and then runs; nothing else needs installing.

We typed the command and copied every prompt verbatim.

Sort selected objects by [X/Y/Select-order] <Select-order>:

Specify starting number and increment (Start,increment) <1,1>:

Placement of numbers in text [Overwrite/Prefix/Suffix/Find&replace..] < Prefix>:

4 objects modified.

One small thing worth recording: the help page still shows the older prompt wording, Sort selection set by X, Y, or Select-order, while the 2027 release asks with the square-bracket form. When the two disagree, trust the command line in front of you.

The three sort options carry the whole power of the command. X runs left to right, Y runs top to bottom, and Select-order follows the order you picked. The four placements do the rest: Overwrite replaces the text, Prefix adds at the front, Suffix adds at the end, and Find&replace.. swaps one string for the number.

Our test: four A strings 1500 apart, sorted by X, starting 1,1, placed as Prefix. The results read 1 A, 2 A, 3 A and 4 A.

The TCOUNT command Photo: The three sort options and four placements of TCOUNT, copied from the real prompts on the 2027 release

So does TCOUNT do everything? Not quite. We windowed a group of eight blocks carrying attributes and ran the command. AutoCAD answered with one line.

0 found

TCOUNT works on text objects only. A number stored in a block attribute is out of its reach, and that gap is what a lisp fills.

The dialog that blocks an unfamiliar lisp file

This is where many people assume the file is broken and give up. On the 2027 release the SECURELOAD variable defaults to 1. Autodesk describes that level as "Loads executable files only if their location is in the trusted locations specified in the TRUSTEDPATHS system variable." Load a .lsp from anywhere else and AutoCAD stops to ask.

The security dialog Photo: AutoCAD 2027 blocking a lisp file outside a trusted folder, with its three buttons: Always Load, Load Once and Do Not Load

The question reads "The publisher of this executable file could not be verified and the file is not located in a trusted folder. What do you want to do?", followed by "Make sure that this executable file comes from a trusted source and is located in a trusted folder."

The three buttons mean quite different things.

ButtonWhat it does
Always Loadremembers this file and stops asking
Load Onceloads it this time only; it asks again next time
Do Not Loaddoes not load, so the commands never appear

The tidy long-term fix: make one folder, say C:\Lisp, keep every lisp file there, and declare that folder trusted. Type OPTIONS, open the Files tab and add the path under Trusted Locations. That list is the TRUSTEDPATHS variable, so SETVAR reaches it too.

Where numbering lisps go wrong: the sort

Nearly every file in circulation sorts the same way: descending Y, and when two objects have Y values that are equal, fall back to ascending X. It sounds right.

The trouble is the word "equal". In a real drawing, two blocks that look like one row almost never share a Y value to the last digit. A few thousandths of a unit is enough for the comparison to stop seeing them as one row, and the order falls back to Y alone — which is to say, to chance.

We built a test to measure it. Eight blocks in two rows of four, with Y values inside each row differing by at most 0.003 units — invisible on screen, and far below anything that would print. Then we ran two lisp files over the same blocks, in the same single pass.

Two lisps compared Photo: The same eight blocks in one pass. Above, the order a widely used lisp produced; below, the Institute's file

Read left to right, the measured results:

RowThe common lispThe Institute's file
top row03, 01, 04, 0201, 02, 03, 04
bottom row05, 08, 06, 0705, 06, 07, 08
Why the order comes out wrong Photo: Two ways to sort, and why a difference smaller than a thousandth of a unit is enough to scramble the order

The cure is not mysterious: replace "equal to the last digit" with a row tolerance. Two blocks closer in Y than the tolerance count as one row, and only inside a row does X decide. Set the tolerance to roughly half the row spacing and it holds for any hand-drawn sheet.

The Institute's STT-IIC.lsp

We downloaded the circulating file, read all 44 lines of it, then wrote our own tool for sequential numbering in AutoCAD to suit real documentation work.

Download STT-IIC.lsp — 7 KB, three commands.

CommandWhat it does
STTnumbers a block attribute, sorted by row and then left to right
STTTnumbers existing text objects in the order you pick them
STTDplaces a fresh run of numbers, one per click

All three ask the same four settings: prefix, start number, step and digit width. Width 2 gives 01, 02; width 3 gives 001, 002. The file remembers them for the session, so carrying a sequence into the next drawing takes only Enter. A single full stop at the prefix prompt clears it.

The heart of the sort, which is the part that differs from other files:

(defun IIC:xep-hang (ds dsai / con hang y0 ra)
  (setq con (vl-sort ds '(lambda (a b) (> (cadr (cadr a)) (cadr (cadr b))))))
  (while con
    (setq y0 (cadr (cadr (car con))) hang nil)
    (while (and con (<= (abs (- (cadr (cadr (car con))) y0)) dsai))
      (setq hang (cons (car con) hang) con (cdr con)))
    (setq hang (vl-sort hang '(lambda (a b) (< (car (cadr a)) (car (cadr b))))))
    (setq ra (append ra hang)))
  ra)

Every command runs inside an undo group. One U restores everything, and the command line says GROUP STT so you can see what it just undid.

Using it, step by step

Step 1 — Load the file

Copy it into your lisp folder, type APPLOAD, point at the file, click Load then Close. If the security dialog appears, see the section above. To avoid loading it on every start, add it to the Startup Suite in that same dialog.

Step 2 — Point at one sample attribute

Type STT and press Enter. The prompt reads:

Chon MOT thuoc tinh mau trong block can danh so:

Click on the strokes of any attribute inside the block you want numbered. A click in the gap between two digits misses: we probed a grid across the text box and only points lying on a stroke registered. AutoCAD then reports the block and tag it recognised, for example Block: IIC-STT | Thuoc tinh: STT.

Step 3 — Give the four settings

Tien to (Enter de giu, go dau cham de xoa):

So bat dau <1>:

Buoc nhay <1>:

Be rong chu so (2 = 01, 3 = 001) <2>:

Enter keeps whatever is in the angle brackets.

Step 4 — Set the row tolerance, then select

Dung sai hang (hai block lech Y nho hon so nay la cung hang) <1.000>:

The number is in drawing units. For blocks scattered widely, leave the default. For a schedule whose rows sit 300 apart, use about 100.

Chon cac block can danh so:

Window the whole group and press Enter. AutoCAD reports Da danh so 8 thuoc tinh tren 8 block.

Numbering a block attribute: what to prepare

An attribute is the right home for a mark number, because the number travels with the geometry. Copy the block and the number goes with it; delete the block and the number goes too.

Three steps:

  1. Draw the shape around the number, usually a circle or a small box.
  2. Type ATTDEF. In the dialog, put a plain name in Tag, for example STT, and set the justification to middle centre so every number sits square in the shape.
  3. Type BLOCK, select both the shape and the attribute, name it and put the insertion point at the centre.

From then on, insert that block wherever a number is needed and run STT once over the whole sheet. Creating and editing blocks is covered in our article on blocks in AutoCAD, and text height and styles in our article on text in AutoCAD.

When two lisp files claim the same command name

This happens more often than people expect, and we met it during testing: the circulating file also names its command STT. Load both in one session and the one loaded second wins, with no warning of any kind.

So the Institute's file carries three spare names as well: STTIIC, STTTIIC and STTDIIC. These call the function body directly, so no other file can take them.

If you type STT and get unfamiliar questions, that is the sign another file holds the name. Type STTIIC instead.

Three things often repeated that are not true

Often repeatedWhat the 2027 release does
Bulk numbering needs a lispTCOUNT in Express Tools does it, with all three sort orders
You must install Express Tools to get TCOUNTThe full release ships with it; the first call only prints Initializing...
Sorting by row is enoughOnly with a tolerance; comparing Y for exact equality breaks

Frequently asked questions

Does a lisp replace TCOUNT?

It complements it. TCOUNT is faster when the number belongs inside a text string. A lisp is the only thing that reaches a block attribute.

Can I do sequential numbering in AutoCAD without a lisp at all?

Yes, when the number lives in text. Type TCOUNT, select the group, choose a sort order and a placement.

Why do the numbers land out of order?

Because the lisp compares Y values for exact equality. Two blocks a few thousandths of a unit apart already count as separate rows. The Institute's file uses a row tolerance to avoid exactly this.

Where can I download a numbering lisp safely?

From somewhere you know, and open the .lsp in a text editor before loading it — it is plain text. Do not switch SECURELOAD off for convenience; it is the last line of defence.

AutoCAD says I picked the wrong thing when I click the attribute.

You clicked the gap between two digits. Click on a stroke instead, such as the edge of a digit.

How do I undo a numbering pass?

Type U once. The whole pass is a single step, and the command line reports GROUP STT.

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