AutoCAD API Tip of the Week = (layoutlist)
This article was inspired by Sam Lucido, an inspiring friend's post on how to delete those unnecessary paper space layouts you inherit when using Autodesk's out-of-the-box templates. My aim here is to show you the accessible power AutoLISP and Dynamo afford you. As time permits, I will revisit this article to include and refine the resulting scripts, source code and graphs.
The idea here will be to automatically delete all except the current paper space layouts. The beauty of programming is that you can generalize any problem so it will solve your original wish and a lot more. Once you know the buttons you can push, methods you can access, the sky is the limit is allowing you make AutoCAD, or almost any other program you deal with, act and behave exactly the way you wish.
For starters, we will use the AutoLISP (layoutlist) functions. It returns a list of all paper space layouts in the current drawing. However, what I am actually looking for is how to access said collection and manipulate it programmatically.
When you first tackle a programming challenge, it usually helps to begin by recreating the manual steps you perform to achieve your desired goal. However, this is rarely the best approach. For educational purposes, it is the path we will follow. But please know it is rarely the best approach. It is simply the most intuitive because it is based on skills you already have. Hopefully.
To remove undesired layouts you can right-click on the tab and select "Delete." Unfortunately, this a Graphical User Interface (GUI) based operation, and those do not readily lend themselves to automation through programs or scripts. We usually need a command that we can control through typing. Fortunately, AutoCAD provides "layout" command which includes a "Delete" option.
In my current drawing (layoutlist) returns: ("Layout1" "Layout2"). Therefore, typing the following string at the command prompt would delete Layout2:
Layout Delete Layout2
Or even better, as a one line LISP program:
(defun c:LOD () (command "._Layout" "Delete" "Layout2")(princ))
Please note LOD should not be confused with either, "Level Of Detail" or "Level of Disaster." Instead, please think of LOD as "LayOut Delete." Other smilar names might confuse it with LayDel, for Layer Delete. Part of the art of writing nice programs is to come up with easy, catchy, short and memorable names.
Let's assume that is all you want to do for now. I will return and refine this as time permits. You can save the little one-line LISP program above to a file you can reuse over and over. You can also modify the name of the layout to delete. You will see it will get a lot more fun when we generalize this program to become much more versatile and smart.
Then later on I will work on a Dynamo Graph to do perform a similar task. You will see the process is very manageable, no matter what method you use. There is no excuse to be performing tasks manually the old fashion way, like it was still 1999, or 1986. We are on 2020. Time to start using software tools more intelligently. That means taking advantage of Application Programming Interfaces (APIs).
In the meantime, enjoy!
2020.01.27 11:23 AM PST Update
As I have written elsewhere, https://www.dhirubhai.net/post/edit/6580847940305330176/, I love my LinkedIn contacts, friend and other people I follow. One such great example is Michael Puckett, who kindly whipped up a great solution to the problem, mere minutes after my initial post, thereby accelerating the progress I am making here: https://bit.ly/2Rx0Mqr
The example above illustrates the way a real programmer thinks. The thinking is completely different than the way you would perform the task manually. A real programmer thinks of the problem as manipulating a database, instead of replicating manual entry click and type procedures.
If we all contribute our talents collectively, at the rate we are going, we will endup with a program or graph that *should* be included in AutoCAD, out-of-the-box. In the meantime, this should be inspiring food for thought.
I am looking at AutoCAD Civil3D's nodes but can't find anything to readily look at the collection of paper space layouts. Please share any ideas or suggestions in the comments. Warm regards from Hollywood, CA.