AutoLISP Tutorial: Sequential Auto-Numbering of Blocks and Text

Welcome to this comprehensive AutoLISP tutorial on sequential auto-numbering of blocks and text in AutoCAD. As beginners, delving into the world of programming might seem daunting. However, this tutorial promises to simplify the complexities and guide you through creating and understanding a practical AutoLISP program. By the end, you’ll be equipped with the knowledge to customize your AutoCAD workflows, improving efficiency and precision.

Key Takeaways:

  • Grasp of AutoLISP Syntax: Understand the foundational structure and syntax unique to AutoLISP.
  • Selection Sets Mastery: Learn how to prompt users for object selection and manipulate these selections.
  • Conditional Logic Implementation: Dive deep into decision-making in scripts, ensuring your program responds aptly to varied inputs.
  • Hands-on with Text and Block Attributes: Gain expertise in accessing and altering textual data within your drawings.
  • Real-world Application: Equip yourself with a tool that can be immediately applied to streamline your AutoCAD projects.

AutoLISP Program: Sequential Auto-Numbering of Blocks and Text

Here’s the AutoLISP code for the “AutoNumber” function:

(defun c:AutoNumber (/ ss n startNum obj att)

;; Prompt user to select blocks or text objects
(setq ss (ssget ‘((0 . “INSERT,TEXT,MTEXT”))))

(if ss
;; Prompt user to enter a starting number
(setq startNum (getint “\nEnter starting number: “))
(setq n 0)

(repeat (sslength ss)
(setq obj (ssname ss n))
(setq n (1+ n))

;; If it’s a block, update the first attribute (assumes numbering attribute is first)
((= (vla-get-ObjectName (vlax-ename->vla-object obj)) “AcDbBlockReference”)
(setq att (vlax-curve-getStartParam obj))
(vla-put-TextString att (itoa startNum))
;; If it’s a text or mtext object, update the text string
((or (= (vla-get-ObjectName (vlax-ename->vla-object obj)) “AcDbText”)
(= (vla-get-ObjectName (vlax-ename->vla-object obj)) “AcDbMText”))
(vla-put-TextString (vlax-ename->vla-object obj) (itoa startNum))

(setq startNum (1+ startNum))


  • The code assumes that if you’re numbering blocks, the first attribute of the block is the one that will get the number. If your block has multiple attributes and you want to number a different one, the code will need adjustments.
  • The program supports TEXT, MTEXT, and block references (INSERT). For blocks, it modifies the first attribute.
  • Always backup your drawing before running scripts or Lisp routines to avoid any unexpected results.

You can load this code into AutoCAD, run the AutoNumber command, and start numbering your objects!


AutoLISP Tutorial: Sequential Auto-Numbering of Blocks and Text

This AutoLISP program is designed to sequentially number selected blocks or text objects within an AutoCAD drawing. The user is prompted to select objects, followed by a prompt to input a starting number. The program then numbers each object, in the order they were selected, starting from the input number.

Step-by-Step Breakdown with Detailed Explanation:

Function Declaration:

(defun c:AutoNumber (/ ss n startNum obj att)
  • defun: This is a common LISP function that stands for “define function”. It’s used to create a new function.
  • c:AutoNumber: This is the name of our function. Prefixing it with c: makes the function callable directly from the AutoCAD command line.
  • /: Variables after this symbol are local to the function, meaning they can’t be accessed outside of this function.

Prompt User to Select Blocks or Text Objects:

(setq ss (ssget ‘((0 . “INSERT,TEXT,MTEXT”))))
  • ssget: This function prompts the user to select objects from the drawing. It can also accept filtering criteria.
  • '((0 . "INSERT,TEXT,MTEXT")): This is a selection filter. The 0 group code specifies the object type. Our filter limits the selection to block inserts (INSERT), single line texts (TEXT), and multiline texts (MTEXT).

Check if Objects are Selected and Continue:

(if ss (progn
  • if: A conditional statement that checks if the result from the ssget function (our selection) is non-nil (meaning objects were selected).
  • progn: A function used to group multiple expressions together.

Prompt User to Input Starting Number:

(setq startNum (getint “\nEnter starting number: “))
  • getint: This function prompts the user to enter an integer. We’re using it to get the starting number for our numbering sequence.

Initialization of a Counter:

(setq n 0)
  • We’re initializing a counter variable n to zero. This counter will help us loop through each of the selected objects.

Looping Through Selected Objects:

(repeat (sslength ss)
  • repeat: A function that repeats the enclosed expressions a specified number of times.
  • sslength: This function returns the number of objects in the selection set. We’re using it to determine how many times to run our loop.

Getting Each Object from the Selection:

(setq obj (ssname ss n)) (setq n (1+ n))
  • ssname: This function retrieves an object’s name (or entity name) from a selection set, based on its position/index.
  • (1+ n): This increments our counter n by one for each iteration of the loop.

Determine Object Type and Number Accordingly:

(cond ;; For block references ((= (vla-get-ObjectName (vlax-ename->vla-object obj)) “AcDbBlockReference”) (setq att (vlax-curve-getStartParam obj)) (vla-put-TextString att (itoa startNum)) ) ;; For text or mtext objects ((or (= (vla-get-ObjectName (vlax-ename->vla-object obj)) “AcDbText”) (= (vla-get-ObjectName (vlax-ename->vla-object obj)) “AcDbMText”)) (vla-put-TextString (vlax-ename->vla-object obj) (itoa startNum)) ) )
  • cond: A conditional function, like an “if-else” chain.
  • vlax-ename->vla-object: Converts an entity name to a VLA-object, which is a type of object we can manipulate using Visual LISP functions.
  • vla-get-ObjectName: Retrieves the object type of a VLA-object.
  • vlax-curve-getStartParam: For block references, we’re making an assumption that the first attribute is the one we want to change. This function retrieves that attribute.
  • vla-put-TextString: This sets the text string of a text or attribute object.
  • itoa: Converts an integer to a string.

Incrementing the Starting Number:

(setq startNum (1+ startNum))
  • After numbering an object, we increment the starting number for the next object.

End of the Program:

(princ) )
  • princ: This is used to return a nil value, which suppresses any unwanted output in the command line, ensuring a clean exit from the function.

Please follow these steps:

  1. Copy the provided AutoLISP code.
  2. Open AutoCAD.
  3. Load the AutoLISP code by either dragging the .lsp file into AutoCAD or by using the APPLOAD command.
  4. Once loaded, type AutoNumber into the command line and press Enter to execute the function.
  5. You’ll be prompted to select blocks or text objects.
  6. Then you’ll be prompted to enter a starting number.
  7. The selected objects will be numbered sequentially.


Diving into AutoLISP can truly elevate your AutoCAD game. By mastering sequential auto-numbering, you’re not only streamlining your tasks but making your designs more precise. If you’re keen on optimizing your CAD projects and seeking tutorials that break it down simply, this guide on AutoLISP for auto-numbering is your go-to resource. Dive in, optimize, and stand out in the CAD community.