AutoLISP to Automate Your Tasks: Part 2 – Intermediate

Learning
AutoLISP to Automate Your Tasks Intermediate

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’.



Frank Mayfield

Frank has worked in various design and CAD Management roles with AutoCAD and Autodesk software since 1986 (v2.62). He is currently a Design Technology Consultant in Tulsa, OK. He also serves as Vice President on the AUGI Board of Directors and is a member of numerous Autodesk user panels. As a top-rated mentor for trial users of AutoCAD 2018 and AutoCAD LT 2018, he has provided live, real-time guidance and support for over 2,500 new users in more than 50 countries worldwide. He currently serves on the AUGI Board of Directors and is a member of numerous Autodesk user panels.

Try AutoCAD free for 15 days
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. Subscribe to keep up with AutoCAD

______
icon-svg-close-thick

Cookie preferences

Your privacy is important to us and so is an optimal experience. To help us customize information and build applications, we collect data about your use of this site.

May we collect and use your data?

Learn more about the Third Party Services we use and our Privacy Statement.

Strictly necessary – required for our site to work and to provide services to you

These cookies allow us to record your preferences or login information, respond to your requests or fulfill items in your shopping cart.

Improve your experience – allows us to show you what is relevant to you

These cookies enable us to provide enhanced functionality and personalization. They may be set by us or by third party providers whose services we use to deliver information and experiences tailored to you. If you do not allow these cookies, some or all of these services may not be available for you.

Customize your advertising – permits us to offer targeted advertising to you

These cookies collect data about you based on your activities and interests in order to show you relevant ads and to track effectiveness. By collecting this data, the ads you see will be more tailored to your interests. If you do not allow these cookies, you will experience less targeted advertising.

icon-svg-close-thick

THIRD PARTY SERVICES

Learn more about the Third-Party Services we use in each category, and how we use the data we collect from you online.

icon-svg-hide-thick

icon-svg-show-thick

Strictly necessary – required for our site to work and to provide services to you

Qualtrics
W
Akamai mPulse
W
Digital River
W
Dynatrace
W
Khoros
W
Launch Darkly
W
New Relic
W
Salesforce Live Agent
W
Wistia
W
Tealium
W
Upsellit
W
CJ Affiliates
W
Commission Factory
W
Google Analytics (Strictly Necessary)
W
Typepad Stats
W
Geo Targetly
W
SpeedCurve
W
Qualified
#

icon-svg-hide-thick

icon-svg-show-thick

Improve your experience – allows us to show you what is relevant to you

Google Optimize
W
ClickTale
W
OneSignal
W
Optimizely
W
Amplitude
W
Snowplow
W
UserVoice
W
Clearbit
#
YouTube
#

icon-svg-hide-thick

icon-svg-show-thick

Customize your advertising – permits us to offer targeted advertising to you

Adobe Analytics
W
Google Analytics (Web Analytics)
W
AdWords
W
Marketo
W
Doubleclick
W
HubSpot
W
Twitter
W
Facebook
W
LinkedIn
W
Yahoo! Japan
W
Naver
W
Quantcast
W
Call Tracking
W
Wunderkind
W
ADC Media
W
AgrantSEM
W
Bidtellect
W
Bing
W
G2Crowd
W
NMPI Display
W
VK
W
Adobe Target
W
Google Analytics (Advertising)
W
Trendkite
W
Hotjar
W
6 Sense
W
Terminus
W
StackAdapt
W
The Trade Desk
W
RollWorks
W

Are you sure you want a less customized experience?

We can access your data only if you select "yes" for the categories on the previous screen. This lets us tailor our marketing so that it's more relevant for you. You can change your settings at any time by visiting our privacy statement

Your experience. Your choice.

We care about your privacy. The data we collect helps us understand how you use our products, what information you might be interested in, and what we can improve to make your engagement with Autodesk more rewarding.

May we collect and use your data to tailor your experience?

Explore the benefits of a customized experience by managing your privacy settings for this site or visit our Privacy Statement to learn more about your options.