AutoLISP to Automate Your Tasks: Part 2 – Intermediate

Learning

By Frank Mayfield

In part one of our series, we talked about some of the basics you should know when beginning with AutoLISP and what kinds of things you can automate with that knowledge.

Today’s post assumes you’re no longer a beginner, but not an expert yet either. You know what you’re doing…but you’re still somewhere in between and in a fuzzy intermediate zone.

By now, you’ve graduated from creating your routines in Notepad to the VLIDE (Visual Lisp Integrated Development Environment). You’ve learned to load all or some of your code and can probably set Breakpoints and step through your code. No doubt you watch variables or maybe just use the Last Evaluation box.

What You Should Know by Now

At this point, your left hand can probably type SETQ as easily as it can ASDF. No doubt you’re pretty good with object selection, whether it’s a single entity using ENTSEL, or multiple items using SSGET (and of course you’re familiar with all the modes and filters it uses).

Since you’re operating on multiple items in a selection set, you’re comfortable using the WHILE looping function. Other iteration functions you know well should include REPEAT, FOREACH, and VLAX-FOR. I’ll even include the often mis-understood MAPCAR function (so naturally, you know how to make an anonymous function with LAMBDA).

You’re good at getting entities, so you’re probably good at manipulating them as well. You may do it in the native AutoLISP way, with ENTGET, ENTMAKE, SUBST etc., or you may have learned to convert it to a Visual LISP object with VLAX-ENAME>VLA-OBJECT, access its properties, and use its methods, such as GET and PUT.

Note: If you are using the various Visual Lisp functions, you also know that you must first load the ActiveX (COM) library with (VL-LOAD-COM). You only need to do it once, so I add it to the top of my ACADDOC.lsp file. Then I don’t have to worry about it not being loaded for any future function calls.

AutoLISP Visual Lisp Console

Since our AutoCAD drawings are made up of both entities and words, you’re probably good at manipulating strings as well. More than likely, you’re also using the VL- string functions since they offer more functionality and are easier to use than the AutoLISP functions.

I find that most intermediate level AutoLISP’ers understand and employ error handling in their code. They have the ability to read from and write to external text files. Also, they might actively utilize the power of ACAD.lsp and ACADDOC.lsp (also known as the CAD Manager’s best friend!).

Many of the AutoLISP routines we create require getting various kinds of input from the user. Perhaps you’ve moved on from using functions like GETSTRING, GETREAL, etc. to presenting the user with a dialog box created in DCL (Dialog Control Language).

AutoLISP dialog box

Obviously, this list isn’t all-inclusive, and you may or may not have mastered all these skills, but it should give you a good idea about where you are on your AutoLISP journey.

Ways an Intermediate User Can Automate Even More

Suffice it to say that you have a lot more tools in your tool belt than you did as a beginner. While there’s still the need to do some of the things we discussed in Part One, you’ve probably found that you can automate the process by adding them to the ACADDOC.lsp file. Most likely, you include your favorite short commands and control your environment by setting SYSVARs each time you open a file.

Perhaps you’re responsible for managing an entire department. You might help your users ensure CAD Standards in numerous ways.

For example, if your standard dictates that Polylines are always to be used instead of Lines, make sure they always do it right by undefining the LINE command, and replacing it with your own function that calls PLINE instead

AutoLISP PLINE

We’ve established that you’re probably proficient with most of the available string manipulation functions, as well as reading and writing external files, so why not combine those skills to further automate things?

It should be well within your skill level to manage your standard Layers from a text file. Decide on a format (typically all layer properties will be on a single line) and build or reset your layers one at a time.

AutoLISP external layers

While you’re automating your Layer and Style standards, you might want to decide to conditionalize what loads and when. In other words, evaluate the drawing being opened via the ACADDOC.lsp file, and do different things based on what you learn.

In this example, the client name is part of the path structure to the file. Simply evaluate with a COND, bind the layers file name to a string (which represents a path), and pass that to your function that opens and iterates through the file.

AutoLISP layer

I love this last idea. Find what’s unique about your drawings, evaluate it as it opens with ACADDOC.lsp, and act accordingly. Your key may be as simple as the drive letter, something in the path (as our previous example shows), or even something within the drawing, such as the title block name, or an entry in DWGPROPS. There’s so much you can automate this way.

Final Thoughts

I’ve just scratched the surface with some of the possibilities of applying your intermediate level of AutoLISP knowledge to automate things in AutoCAD. Think about and identify your own pain points. Look for repetitive tasks or just things that seem to be done “the long way.” You’ll soon find your own things to automate.

Of course, the example code snippets I present aren’t quite ready for prime time yet, but they should give you some idea of each concept. More than anything, I hope they are catalysts for the light bulb that should be going off above your head.

I hope you join me again next time. In part three of the series, we’ll talk about what you should know as an “Advanced” AutoLISP programmer and some of the things you can do. Until then, keep on programmin’.

Try AutoCAD free for 30 days

Related Articles

Add comment

2 comments

  1. Avatar
    ikocev

    Great ideas, thanks for sharing with us :)

    1. Frank Mayfield
      Frank Mayfield

      Thank you! Stay tuned for part 3.

The AutoCAD Blog is all about helping you get the most out of AutoCAD. It's also the best way for you to connect with the makers of AutoCAD.

Try AutoCAD free for 30 days