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.
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:
- Link: Python for AutoCAD pyautocad module
- Link: add() method in pyautocad
- Link: Solved call was rejected by callee in pythoncom
- Link: Tree data structure for AutoCAD objects using Python
- Link: Extending the objects in AutoCAD using pyautocad in Python
- Link: Using Python lists and dictionaries to work with AutoCAD objects with pyautocad
- Link: Hatching objects on AutoCAD template using pywin32 in Python
- Link: Raster image object in AutoCAD with pyautocad in Python
- Link: Working with 3D mesh object in AutoCAD using pyautocad in Python
- Link: Creating adouble constructor using pywin32 in Python
- Link: Creating apoint method using pywin32 in Python
- Link: Python integration with AutoCAD using pywin32 and win32com
- Link: Deleting objects in a AutoCAD template with pyautocad and pywin32 in Python
- Link: Mirror object on a 2D plane with pyautocad in Python
- Link: Working with texts in Autocad using pyautocad in Python
- Link: Polar arrays in AutoCAD using pyautocad in Python
- Link: Rectangular arrays in AutoCAD using pyautocad in Python
- Link: Operations with AutoCAD objects using pyautocad in Python
- Link: Solid objects in AutoCAD using pyautocad in Python
- Link: Working with helices in AutoCAD using pyautocad in Python
- Link: Drawing splines in AutoCAD with pyautocad in Python
- Link: Polylines in pyautocad for drawing AutoCAD polygons in Python
- Link: Drawing ellipse arcs in AutoCAD using pyautocad in Python
- Link: Drawing arcs in AutoCAD using pyautocad in Python
- Link: Near simultaneous factory design and process optimization with Promodel AutoCAD edition
- Link: Python for AutoCAD pyautocad module
- Link: Region object in AutoCAD with Python
Civil engineer interested in automation in core subjects such as civil, mechanical and electrical, using IT skills comprising cloud computing, devops, programming languages and databases along with the technical skills gained while working as a civil engineer since past 3 years.
Leave a Reply