<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=157406051691882&amp;ev=PageView&amp;noscript=1">

Rediscovering AutoLISP

CAD Programming: Using AutoLISP to automate CAD tasks.

CADProgramming-450

Over the past couple of years, we’ve covered a variety of CAD programming topics. We’ve seen how to develop macros in AutoCAD and MicroStation, as well as how to program in various environments, including VBA, .NET, Dynamo, and Python.   

No discussion of CAD programming would be complete without a discussion of AutoLISP. Based on the LISP language developed for artificial intelligence applications in the 1950s, AutoLISP provided one of the first widely accessible means to program AutoCAD in the mid-1980s. With a somewhat cryptic syntax relying heavily on parentheses, AutoLISP can initially confuse novice programmers, but supporters claim it provides easier interaction with AutoCAD than VBA or .NET. It remains popular today, even after numerous alternatives have been offered to programmers in recent years. Click through to find Cadalyst’s archive of AutoLISP and CAD Tips.

As a quick introduction, you can type an AutoLISP statement in the AutoCAD Command line and see the results directly. Even if you’ve never used AutoLISP, try typing (+ 2 2) at the Command line and see how AutoLISP can serve as a calculator.

2023-01-13prog-fig1

To make AutoLISP easier to use, Autodesk introduced Visual LISP (VLISP) in the late 1990s, with an interactive development environment (IDE) similar to that of VBA, along with a compiler, debugger, and other tools. It has served AutoLISP programmers well, streamlining some of the arduous work to match up parentheses and identify syntax errors.

In AutoCAD 2021, Autodesk introduced the AutoLISP Extension for Visual Studio (VS) Code. This tool allows you to edit and debug AutoLISP files with VS Code on Windows or Mac OS. While it requires AutoLISP programmers to learn yet another environment, it offers potential efficiency gains with new features such as IntelliSense, which provides smart completions of statements as you type, and enhanced debugging features. VS Code, not to be confused with the Visual Studio programming environment, is a free code editor from Microsoft, and is a separate installation from AutoCAD.

 

Building an AutoLISP Application

If you’re new to AutoLISP, perhaps the best way to learn it is by trying an example. Autodesk provides extensive documentation of AutoLISP with a Developer’s Guide and Reference Guide that may be helpful as you try to learn the nuances of AutoLISP. For now, here are some key concepts to consider as we explore an example.

  • defun is an AutoLISP function that defines the beginning of a custom function (a.k.a., an AutoLISP routine or program that you develop).

  • The setq function sets the value of a symbol or symbols to associated expressions.

  • The command function executes an AutoCAD command.

  • Mathematical functions, such as those for addition and subtraction, begin with the mathematical operator, followed by the numbers to be operated upon (e.g., (+ 2 2)).

  • Parentheses essentially enclose everything in an AutoLISP program, including the program as a whole, as well as individual functions embedded in your program. Parentheses need to be paired up accordingly, which represents perhaps the biggest challenge to beginning programmers.

  • Comments begin with one or more semicolons (;) and are useful for documenting the program.

VLISP IDE Example

With that brief introduction, let’s get started. For this example, we’ll use the VLISP IDE in AutoCAD 2022. While it may be phased out in the future as the AutoLISP Extension for VS Code becomes more widely adopted, it still provides a handy entry point for new programmers.

1. From the Manage tab in AutoCAD, click Visual LISP Editor.

2023-01-13prog-fig2
Click image to enlarge.

Depending on the version of AutoCAD you are using, you may get a message that says the AutoLISP Extension components are not yet installed. You could follow the prompts to install those components and learn about the AutoLISP Extension, but for this example, we’ll use a workaround that allows you to continue using the VLISP IDE. We’ll save discussion of the AutoLISP Extension for a future article.

2. If you see a message regarding installation of the AutoLISP Extension, type LISPSYS at the Command line and press Enter.

3. When prompted to enter a value, type 0 and press Enter.

2023-01-13prog-fig3

When the LISPSYS system variable is set to 0, the legacy AutoLISP engine and VLISP IDE are used for editing and debugging AutoLISP files. If LISPSYS is set to 1 or 2, the AutoCAD AutoLISP Extension can be used. You may have to restart AutoCAD after setting the LISPSYS system variable.

4. When you open the VLISP Editor, you should see a window like this:

2023-01-10_CADPRG-fig4
Click image to enlarge.

5. In the code window (the upper blank window), enter the following code. (Hint: You can copy/paste this code to avoid typing!)

(defun drawlines (/ x1 y1 x2 y2 pt1 pt2)

  (setq x1 0.0

           y1 0.0

           x2 5.0

           y2 5.0

   )

   (setq pt1 (list x1 y1)

            pt2 (list x2 y2)

   )

   (setq i 1)

   (while (<= i 5)

     (setq y2 (+ y2 5))

     (setq pt2 (list x2 y2))

     ; check to see that the two points exist

     (if (and pt1 pt2)

        (command "_.line" pt1 pt2 "")        

         (princ "\nError in point data.")

     )

     (command "_.chprop" (entlast) "" "C" i "")

     (setq i (+ 1 i))

   )

  (princ "Number of lines drawn: ")

  (princ (- i 1))

  (princ)

)

6. In the VLISP IDE, Click File > Save to save your project.

7. Type a file name and click Save to save the LSP source file.

8. Click Tools > Load Text in Editor.

9. In the Visual LISP Console, type (drawlines). If you’ve entered everything correctly, you should see a series of lines drawn in AutoCAD.

2023-01-13prog-fig5

 

Reviewing the Code and Moving On

Let’s briefly review what we’ve done. We defined a function called “drawlines” using the AutoLISP defun function. In the same line, we also listed the variable names preceded by a forward slash (/).

We used the setq function to define x and y coordinates for two points: x1, y1 and x2, y2. We then used the setq function again to assign those coordinates to the p1 and p2 variables. This defined the end points for the first of several lines drawn with the command function and the AutoCAD Line command. The colors of the lines were changed using the command function and the AutoCAD Chprop command.

We used the while function to repeat the commands and draw five lines of different colors. At the end, we used the princ function to print the number of lines drawn.

For more details about AutoLISP code, refer to the previously mentioned AutoLISP documentation. You may also want to explore other ways of saving and executing your code. For example, you can create and edit AutoLISP source code files using a plain text editor.

You can also create custom commands that can be accessed from the Command prompt, just like standard AutoCAD commands. A custom command is defined with the defun function, but uses a special naming convention: they use the characters c: as a prefix.

You can also develop AutoLISP routines to work in conjunction with applications developed in other environments, such as Python or .NET. We saw an example of this in a previous article on Python.

AutoLISP continues to provide useful features for AutoCAD programmers, nearly 40 years after its introduction. Other tools may provide more user-friendly features, but AutoLISP remains a favorite for a sizable segment of programming community. If you’ve never used it, give it a try; you might be surprised at its capabilities. Find out more about AutoLISP in More Fun with AutoLISP.

We’ll explore other programming tools in future articles. If you have specific ideas for programming topics, feel free to drop us an email. 

CAD-PROGRAMMING_PILLAR_CTA

 

 

 

Andrew G. Roe

Cadalyst contributing editor Andrew G. Roe is a registered civil engineer and president of AGR Associates. He is author of Using Visual Basic with AutoCAD, published by Autodesk Press. He can be reached at editors@cadalyst.com.

View All Articles

MORE ON THIS TOPIC

Drive Workflows to Gain CAD Standards Compliance

I often see questions in my Facebook group asking, “How should I setup the standards for Civil 3D, Revit, or Solidworks,” amongst others. Sometimes...

Measuring the Benefits of Digital Transformation

In our previous article, we explored how to leverage different technologies to achieve tangible benefits through digital transformation. In this...

Pushing for What Your CAD Team Needs

It seems like CAD managers live in a state of perpetual struggle to get ever-increasing project work done with insufficient budget or control to do...