AutoLISP Tutorial: Summation of Entity Lengths

Welcome to this AutoLISP tutorial, where we’ll delve into the fascinating realm of AutoCAD customization and automation. Whether you’re an architect, engineer, or CAD enthusiast, mastering AutoLISP can significantly streamline your drawing tasks. In this tutorial, we will break down an essential script: Summation of Entity Lengths. By the end of this guide, you’ll not only understand this script line-by-line but also develop a strong foundation to build your own AutoLISP routines.

Let’s embark on this enlightening journey!

Key Takeaways

  • AutoLISP: A powerful tool for automating and customizing AutoCAD tasks, tailoring the software to your specific needs.
  • Entity Selection: Understand the nuances of how to select specific entities (like lines and polylines) from the drawing, ensuring accurate data extraction.
  • Data Processing: Grasp how to interpret and manipulate the data of selected entities, allowing for precise calculations and operations.
  • Loops and Conditionals: Recognize the importance of iterative processes and conditional checks in managing and processing multiple entities efficiently.
  • User Interaction: Appreciate the significance of user feedback, ensuring that your scripts are user-friendly and communicative.

AutoLISP program: Summation of Entity Lengths

Here’s a simple AutoLISP program to sum the lengths of selected lines or polylines:

(defun c:LengthSum ()
(setq ss (ssget ‘((0 . “LINE,POLYLINE”))))
(setq totalLength 0.0)

(if ss
(setq count (sslength ss))
(repeat count
(setq ent (ssname ss 0))
(setq data (entget ent))
(setq type (cdr (assoc 0 data)))

((= type “LINE”)
(setq startPt (cdr (assoc 10 data)))
(setq endPt (cdr (assoc 11 data)))
(setq totalLength (+ totalLength (distance startPt endPt)))
((= type “POLYLINE”)
(setq n (cdr (assoc 90 data)))
(setq prevPt (cdr (assoc 10 (entget (entnext ent)))))
(repeat (1- n)
(setq ent (entnext ent))
(setq pt (cdr (assoc 10 (entget ent))))
(setq totalLength (+ totalLength (distance prevPt pt)))
(setq prevPt pt)
(setq ss (ssdel ent ss))
(prompt (strcat “\nTotal Length: ” (rtos totalLength)))
(prompt “\nNo valid entities selected.”)

This code defines a command LengthSum that, when run, prompts the user to select lines and/or polylines. The program then sums up their lengths and displays the total.

To use it:

  1. Copy the provided code.
  2. Paste it into the Visual LISP Editor in AutoCAD.
  3. Load the program using (load "YourFileName") or simply save and drag & drop the LISP file into the AutoCAD window.
  4. Type LengthSum in the command line to run the program.

AutoLISP Tutorial: Summation of Entity Lengths

Objective: To understand and write an AutoLISP program that calculates the total length of selected lines and polylines in AutoCAD.

1. Introduction to AutoLISP Functions:

AutoLISP provides various built-in functions to work with entities in AutoCAD. For our objective, we’ll be using some of the essential functions like ssget, sslength, entget, and so on.

2. Program Overview:

The program prompts the user to select lines and/or polylines. After selection, it calculates the total length and displays the result.

3. Step-by-step Code Breakdown:

3.1. Starting the Program:

(defun c:LengthSum ()

defun defines a new function. The prefix c: allows this function to be called as a command from the AutoCAD command line.

3.2. Getting the Selection Set:

(setq ss (ssget ‘((0 . “LINE,POLYLINE”))))
  • ssget prompts the user to select entities.
  • '((0 . "LINE,POLYLINE")) is a filter list which ensures only lines and polylines are selected.
  • setq sets the value of ss to the result.

3.3. Initializing the Total Length Variable:

(setq totalLength 0.0)

This initializes the totalLength variable to zero.

3.4. Checking the Selection Set:

(if ss

This checks if any entities were selected.

3.5. Processing Each Entity in the Selection Set:

(progn (setq count (sslength ss))
  • progn groups multiple expressions into a single block.
  • sslength returns the number of entities in the selection set.

3.6. Looping through Each Entity:

(repeat count

repeat runs a set of commands multiple times (here, count times).

3.7. Getting Entity Data:

(setq ent (ssname ss 0)) (setq data (entget ent)) (setq type (cdr (assoc 0 data)))
  • ssname gets the entity name.
  • entget retrieves the data of an entity.
  • assoc and cdr fetch the entity type (like LINE or POLYLINE).

3.8. Checking Entity Type and Calculating Length:

(cond ((= type “LINE”)

cond is a conditional function.

If the entity is a LINE:

(setq startPt (cdr (assoc 10 data))) (setq endPt (cdr (assoc 11 data))) (setq totalLength (+ totalLength (distance startPt endPt)))
  • assoc gets the start and end point of the line.
  • distance calculates the distance between two points.


((= type “POLYLINE”) (setq n (cdr (assoc 90 data)))

assoc 90 gets the number of vertices in the polyline.

Then, we loop through each vertex:

(setq prevPt (cdr (assoc 10 (entget (entnext ent))))) (repeat (1- n) (setq ent (entnext ent)) (setq pt (cdr (assoc 10 (entget ent)))) (setq totalLength (+ totalLength (distance prevPt pt))) (setq prevPt pt) )
  • entnext gets the next entity (vertex) in the polyline.
  • We use distance again to calculate the lengths between vertices.

3.9. Updating the Selection Set:

(setq ss (ssdel ent ss))

ssdel removes an entity from the selection set, ensuring the loop progresses.

3.10. Displaying the Result:

(prompt (strcat “\nTotal Length: ” (rtos totalLength)))
  • prompt displays a message in the command line.
  • strcat concatenates strings.
  • rtos converts a real number to a string.

3.11. Handling No Valid Entities Selected:

(prompt “\nNo valid entities selected.”) ) )

If the user does not select any lines or polylines, this message will be displayed.


Congratulations on completing this AutoLISP tutorial! You’ve taken a significant step towards mastering the art of AutoCAD customization. With the knowledge you’ve gained today, you’re well-equipped to tackle complex drawing tasks with efficiency and precision. As with all programming languages, practice is key.

Continue to experiment, adapt, and innovate with AutoLISP. The power to transform your AutoCAD experience is now at your fingertips. Happy coding!