Cadalyst CAD Management

More Fun with Python and pyautocad

Written by Andrew G. Roe | Aug 18, 2022 10:29:00 AM

 

In a previous article, we introduced Python as an open-source programming platform. We also saw how to use Python in conjunction with Dynamo to create AutoCAD objects.

[Editor’s note: While pyautocad is AutoCAD-centric, the concepts apply to other CAD environments also. For those who use different CAD programs, we'll be digging into other programming tools in future articles.]

Python can be used in many other ways besides with Dynamo. Because Python is a high-level, interpreted language, you can readily develop applications that automate repetitive tasks, either in a standalone environment or within a CAD environment. In the AutoCAD world, a handy library called pyautocad can help you write ActiveX Automation scripts for AutoCAD using Python. It is particularly useful for working with coordinates, object iteration and searching, and data import and export.

In this article, we’ll review how to get started with pyautocad, and write some simple code to automate AutoCAD tasks. We’ll also explore some ideas on how to use pyautocad to develop more complex applications down the road.

Getting Started

If you have Python installed on your computer (see previous article), you can add pyautocad in a manner similar to that used for Python. (Note: The following steps assume you are using Windows. You may need to dust off some file management skills for these steps, and you may also find easier ways to complete these steps, but try these steps to get started.)

1. Identify where Python is installed on your computer. It’s typically found in a location similar to that shown below.

Click image to enlarge.

 

2. Navigate to the Scripts folder and confirm you have an application called pip, which is used to install other applications.

Click image to enlarge.

 

The pip installer should get installed with Python, but if not, you can find more information on installation here: Installing Packages — Python Packaging User Guide.

3. Once you have pip installed, you can install pyautocad by opening a command prompt window, navigating to the appropriate folder, and typing in the following:

pip install pyautocad

After entering that simple statement, pyautocad is then installed on your computer, and you should see something similar to the following:

Click image to enlarge.

 

Communicating with AutoCAD

With pyautocad installed, you can now move on to more interesting stuff, such as automating AutoCAD tasks. To do this, you need to first connect pyautocad with AutoCAD. You can find guidelines on this and various other pyautocad commands in the pyautocad documentation. Additional Python documentation is also included with the Python installation.

1. Start AutoCAD and open a new drawing.

2. From the Windows Start menu, select Python to open the Python interpreter.

 

3. In Python, enter the following lines of code to import AutoCAD and create an instance of AutoCAD.

from pyautocad import Autocad

acad = Autocad()

 

4. To confirm pyautocad is communicating with AutoCAD, enter the following line to display the drawing name:

print (acad.doc.Name)

Click image to enlarge.

 

The three symbols >>> serve as the command prompt in the Python interpreter. Output — in this case, the drawing name — is displayed without the command prompt.

 

Creating AutoCAD Objects

Now that pyautocad is communicating with AutoCAD, you can create some objects in AutoCAD. Let’s start by defining two points and drawing a line connecting those two points.

1. In the Python interpreter, enter the following lines to import the AutoCAD Point object and create two points.

from pyautocad import APoint

p1 = APoint(0, 0)

p2 = APoint(5, 5)

2. Create a line connecting these two points by entering the following line.

acad.model.AddLine(p1, p2)

3. Switch to AutoCAD to display the line drawn from Python.

 

Automating Redundant Tasks

To really harness the capabilities of Python, or any programming language, you can automate redundant tasks and save time. You can also group multiple lines of code together and write unified programs, rather than typing in individual lines in Python. Let’s see how this works with a slightly more advanced example. Assume you want to draw multiple lines and make each line a unique color.

1. In AutoCAD, erase the line just drawn with Python.

2. In the Python interpreter, enter the following lines.

from pyautocad import Autocad, APoint

acad = Autocad()

p1 = APoint(0, 0)

p2 = APoint(5, 5)

for i in range(5):

   myline = acad.model.AddLine(p1, p2)

    myline.color = i

    p2.y += 5

3. Press Enter to complete the routine.

4. Switch to AutoCAD to display the lines drawn by your Python code.

 

Let’s review the code that created these lines. As with the previous example, we first imported AutoCAD and created an instance of AutoCAD in the Python interpreter. We also imported the AutoCAD Point object in the same line that imported AutoCAD. We proceeded to create two points as before, but this time created a series of lines instead of one line. We did this with a for loop, which is a block of code that can be repeated a fixed number of times. Within the loop, the y value of each end point (p2) was incremented by 5.

Also within the loop, the color of each line was changed by accessing the color property. AutoCAD colors can be referenced by either a color number (1 through 255) or color name (e.g., red). In this case, the color property of an object called myline was changed by incrementing the value of the color property by 1.

 

Building on the Basics

We’ve still just scratched the surface with Python and pyautocad. In addition to creating lines, you can use Python to create virtually any other AutoCAD object, such as circles, polygons, polylines, text, and so on. You can also change other object properties besides the color, such as layer, line weight, and thickness, in a similar manner to that shown in the example.

You can also obtain drawing information, such as the number of objects of a certain type or the layer names in a drawing. If you want to exchange data between AutoCAD and external data sources, you can import and export data from Excel spreadsheets, text files, and other sources. The pyautocad library also has some useful utilities, such as a timer and various text formatting tools. Advanced Python programmers combine these and other features to manage and modify drawings, query drawings for information, and manage multiple drawings in various ways.

For more complex scripts, you may want to save your code in a file for reuse. Python code can be written with any text editor, though you may benefit by using more enhanced text editors like Visual Studio Code or Notepad++. From the Python shell interface, you can save your code for reuse by clicking File | Save and giving your file a name. You can then execute the script from a command line. More information on executing scripts can be found in the Python documentation.

 

Click image to enlarge.

 

Next Steps

With this brief introduction, you can see how Python and pyautocad can be used together to automate various AutoCAD tasks. With a basic understanding of AutoCAD's object model, you can access virtually every object, method, and property that you can access manually in AutoCAD. The level of sophistication in your programs is only limited by your creativity.

We’ll continue to explore how to use Python and other tools in future articles. If you have specific ideas for programming topics, feel free to drop us an email.