Starting developing for AutoCAD

Hans Martin A Eikerol
3 min readApr 7, 2016

--

The first time I realized that it’s possible to customize AutoCAD with ones own plugins and routines was about a year ago. I attended a BIM-seminar at work where a guru in Norconsult made me aware of the opportunities that lies with AutoCAD. How amazing! was my first thought.

Having some background being a hobby programmer with experience in PHP and HTML, I figured I’d try creating small fixes for some of the most annoying things I’d been experiencing in AutoCAD.

I started experimenting with LISP, and worked with it until last summer. LISP is a great language for simple routines in AutoCAD, but it has its limited uses for more complex and integrated routines. For those of you not familiar with the .NET-languages such as C#, LISP can get you startes more easily. As this is my first blog post, I’m thinking I’ll start by introducing the path of understanding how to program your own plugins and functions for AutoCAD. These first few posts will mention good practices regarding LISP, then I’ll move over to C# programming later on.

First off: where do you go looking for some example code? A true LISP magician is a man named Lee Mac. He’s a complete guru on LISP for AutoCAD, and has covered alot of interesting topics. Please visit his website for some free sample code. He has tons of working examples that allows you to do lots of interesting stuff.

Thank you Lee. He has some great tutorials on how to get started working with LISP, I suggest you start looking there.

Now, moving towards writing your first lines of code. I’ve created a template for all new LISP functions I’m making myself, using a few functions to store and reset system variables after all LISP runs. Know that correctly setting system variables for snapping or similar affecting settings is important before you go making changes through LISP. Modifying vertices of a line with wrong snap settings set will give you quite some unexpected results.

This is a very simple sample template for developing your own custom LISP routines for AutoCAD. Currently is does not do much, but it gives you a nice shell to start creating your own routines.

Note the bottom line of the template. It is something I copy into the AutoCAD command line while I develop new functions so that I easlily can hit QQ on the command line to reload the new changes I just made to the file.

To help get you started with LISP, I’ll explain the basic syntax here:

(setq variable "value") ;; Define a new variable and set its value(setq listVariable
(list "One" "Two" "Three") ;; Define a list
)

To extract elements from a list, the car, cadr and caddr will be useful when handling coordinates.

(car listVariable)   ;; Returns "One"
(cadr listVariable) ;; Returns "Two"
(caddr listVariable) ;; Returns "Three"

The if-statement is a bit special. The first paranthesis is the “if true” result, the second is similar as “else”. Here’a case:

(setq variable 1)(if (= variable 1)
(setq str "IsOne")
(setq str "IsNotOne")
)
(princ str)(terpri)
(if (/= variable 1)
(setq str "IsNotOne")
(setq str "IsOne")
)
(princ str)(terpri)
;; Output to screen:
IsOne
IsOne

(princ “string”) is a decent way to output string to the AutoCAD Editor command line, which is always nice in use of debugging. (terpri) is short for “terminate print”, and is simply a new line command.

Beware though! If you’re using (princ “str”) inside an IF-test, make sure to skip the (terpri) command, or you’ll suddenly finding yourself in having three (or four!) result blocks inside the IF-test. You’ll spend a long time trying to understand why.

;; This is okay
(if (= variable 1)
(princ "IsOne")
(princ "IsNotOne")
)
(terpri)
;; This will crash your program (4 result parantheses)
(if (= variable 1)
(princ "IsOne")(terpri)
(princ "IsNotOne")(terpri)
)
;; Instead wrap your if-results inside the (progn )-function
;; This will work (2 result parantheses)
(if (= variable 1)
(progn (princ "IsOne")(terpri))
(progn (princ "IsNotOne")(terpri))
)

As you probably have realized: LISP (Lost In Stupid Parantheses) is all about parentheses. Getting them right is always a tough challenge.

--

--

Hans Martin A Eikerol

Water and sanitation engineer at Norconsult. BIM-specialist and AutoCAD Developer