Considering the issues we face while working with the pyautocad module or while working with some of the functions provisioned by pyautocad I decided to write a blog post covering win32com, i.e. pywin32. This Python module provides an alternative approach for integrating Python with AutoCAD.
Introducing pywin32, a win32com module for Python
The pywin32 package provides access to many of the Windows APIs from within a Python script. We can interact with different windows applications such as Excel, Word, AutoCAD and many more by using VBA object models for the respective application.
This pywin32 package supports only Python 3 and not the older versions of Python.
To install the pywin32 package I can simply use the Python package manager and the pip install command:
pip install pywin32
Now, to use this package, I have to import the module “win32com.client”.
VBA object model for AutoCAD
Most of the applications we use in windows have their own object model. AutoCAD VBA (Visual Basic for Applications) has its own object model too.
VBA enables the creation of user-defined functions (UDFs). It furthermore allows for process automatization and provides access to the Windows API as well as other low-level functionality. This is facilitated through dynamic-link libraries (DLLs). Let us take a look at the VBA object model as provided by and described in the AutoCAD documentation.
Initiating a drawing template using pywin32
Those who have already used VBA for automation purposes in AutoCAD can relate the commands with the object model as shown in Figure 1. For example, Application is the root model at which the actual document is located.
We work on a single AutoCAD template or sheet, which is known as “model space”.
Now, to open a new drawing sheet using VBA, we use the following line of code in VBA:
# pyautocad import pyautocad NewDrawing1 = ThisDrawing.Application.Documents.Add("")
To perform the same action with the pywin32 package we can use :
# pywin32 import win32com.client acad = win32com.client.Dispatch("AutoCAD.Application") acad.Visible = True acadModel = acad.ActiveDocument.ModelSpace
Clearly, we can relate the above-mentioned codes as demonstrated below:
To initiate AutoCAD application
# pyautocad ThisDrawing.Application.
# pywin32 acad = win32com.client.Dispatch("AutoCAD.Application") acad.Visible = True
To create a new template
# pyautocad .Documents.Add("")
# pywin32 acad.ActiveDocument.ModelSpace
Im my previous blog posts related to pyautocad and AutoCAD scripting in Python I used the method-call Autocad(create_if_not_exists=True) for initiating the application as well as the model space.
In upcoming blog posts I will be discussing how to perform various different tasks and operations using pywin32. I wrote a similar post related to AutoCAD operations with pyautocad in Pyhton as well.
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.