AutoCAD Application object class in Python

In this blog post I will discuss the AutoCAD Application object and some of its important methods and properties. I will show how to implement the AutoCAD Application object in pyautocad.

Starting this coding example I import relevant classes and methods from pyautocad. I create a new model in the code below.

from pyautocad import Autocad, APoint, aDouble
acad = Autocad(create_if_not_exists=True)

Fetching the AutoCAD application object in Python

In order for me to be able to perform any actions on the AutoCAD Application object I need to fetch that object. In pyautocad I use “acad.app” for this.

If I want to use communication modules instead of pyautocad I can proceed as coded below. In that case I use win32com and pythoncom Python modules.

from win32com.client import *
import pythoncom

acad1 = win32com.client.Dispatch("AutoCAD.Application")

In this case, acad1 represents the Application object.

Properties of the Application AutoCAD object

Many application object properties improve the automation of AutoCAD drawings. I will discuss some of the important properties, going through them one by one.

To get different kinds of information related to the AutoCAD Application object, such as e.g. the name of the application, the full name of the application, or e.g. the document name, I can use the code.

# application details
print(acad.app.Application.Name)
print(acad.app.Caption)
print(acad.app.ActiveDocument.Name)

print(acad.app.Path)
print(acad.app.FullName)

O/p:
AutoCAD
Autodesk AutoCAD 2018 - [Drawing2.dwg]
Drawing2.dwg
C:\Program Files\Autodesk\AutoCAD 2018
C:\Program Files\Autodesk\AutoCAD 2018\acad.exe

Let’s say I want to check all the documents opened currently in the AutoCAD application. For this, I can e.g. use the Documents property and iterate over that property.

I will demonstrate this with a simple example in AutoCAD. In his example I have two drawings open, Drawing1 and Drawing2. The screenshot below shows this.

AutoCAD Application object tutorial

The drawing “Drawing2.dwg” is currently selected. From my previous output and the image above it is clear that this drawing is fetched as ActiveDocuments. But let me check if it returns Drawing1.dwg also. This should be the case, since that file is also open.

# get currently opened docuements
print(acad.app.Documents)
for i in acad.app.Documents:
    print(i.Name)

O/p:
Drawing1.dwg
Drawing2.dwg

MenuGroups and MenuBar attributes

Now I get the menu bar and menu group objects currently in use. I do so in the code below:

# get menu bar object from the session
print(acad.app.MenuBar)

# a collection of PopupMenu objects representing the current AutoCAD menu bar.
print(acad.app.MenuGroups)
for i in acad.app.MenuGroups:
    print(i.Name)

O/p:
<comtypes.client.lazybind.Dispatch object at 0x0000021BBCA23EB0>
ACAD
CUSTOM
MODELDOC
EXPRESS
AUTODESKSEEK

Here, I can see that the first code of MenuBar returned a dispatch object which is an active menu bar from the AutoCAD drawing. I will discuss the MenuBar object in another article.

AutoCAD Application object WindowState property

To adjust or view the state of the AutoCAD application or AutoCAD document window I can use the WindowState attribute. WindowState is encoded with integer values. In brief, the integers represent window states in e.g. the following way:

  • acMin(1): The window is minimized.
  • acMax(2): The window is maximized.
  • acNorm(3): The window is normal (not minimized nor maximized).
# specify if window is minimized, maximized or normal
print(acad.app.WindowState)

O/p:
1

AutoCAD Application object methods

Now, I will discuss a few important pyautocad methods for handling AutoCAD applications. Actually, everyone should know while performing operations with the AutoCAD application object.

Whenever I write a code I must check whether the application is in a Quiescent(Inactive) state or not. To check that I use the IsQuiescent method. Here is an example:

# get current state of application
print(acad.app.GetAcadState().IsQuiescent)

O/p:
False

As I was using the application after running this code it is showing the application as active( i.e. False).

Also, If I want to close the current document, I can use the Quit method. Of course, this only closes the currently active document and not the entire application returning a null value.

# close drawing file and exist application
acad.app.Quit()

Finally, to update the application after applying the code I can use the Update method which returns null as well.

# update the application
acad.app.Update()

Concluding remarks and related content

For other informative articles covering AutoCAD automatization do check my other articles related to pyautocad and pywin32. Please comment down the questions you have in the comment section. Feel free to contact me for any technical consultation. You can use the contact form for the same.

Here are some related articles covering AutoCAD automatization and AutoCAD scripting in Python:

Leave a Reply

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close

Meta