Python for AutoCAD (pyautocad module)

In this post I want to introduce a module available in Python for automatizing the creation of drawings in AutoCAD. The module is called pyautocad. It is supported by pip install for easy installation. The pyautocad module will also allow you to adjust existing drawings, by e.g. manipulating objects in the drawing.

In a previous post I have already introduced AutoCAD basics and various AutoCAD commands. For example I have introduced AutoCAD LINE, AutoCAD MLINE, AutoCAD PLINE, AutoCAD XLINE, AutoCAD CIRCLE, AutoCAD RECTANG, AutoCAD OFFSET and many more. A full overview is provided under the AutoCAD tab of this blog, or as part of my AutoCAD tutorials.

pyautocad recognizes the AutoCAD drawing currently open and it also recognizes the currently active model space. Before running the Python code below I therefore open a new AutoCAD drawing.

I then import pyautocad. I create an AutoCAD instance using pyautocad.

# importing pyautocad
import pyautocad
# creating AutoCAD instance
acad = pyautocad.Autocad()

Let us check that the module really recognizes the currently active AutoCAD drawing:

# print name of document currently recognized as being active in AutoCAD

Next, I can e.g. create a circle in 2D plane.

# specify x and y coordinates of circle center point
from pyautocad import APoint
point1 = APoint(100.0,100.0) # x and y coordinates of points
# add circle to drawing
circle1 = acad.model.AddCircle(point1,100)

The following circle has been added to my AutoCAD drawing:

The change in color of the circle is possible with pyautocad too. I do so in the code below:

# change color of circle to red
circle1.Color = 10 # 10 is a red color

The result in my drawing when executing the Python code looks like this:

The color index numbers can be found in AutoCAD. See screenshot below.

I can also check other current property values for the circle I just added to the drawing. Below are some examples:

# check layer assignment
print("current layer: "  + str(circle1.Layer))
# check current linetype
print("current linetype: " + str(circle1.Linetype))
# check linetype scale
print("current linetype scale: " + str(circle1.LinetypeScale))
# check current line weight
print("current line weight: " + str(circle1.Lineweight))
# check current thickness
print("current thickness: " + str(circle1.Thickness))
# check current material
print("current material:" + str(circle1.Material))
current layer: 0
current linetype: ByLayer
current linetype scale: 1.0
current line weight: -1
current thickness: 0.0
current material:ByLayer

I can use the AutoCAD PROPERTIES command in AutoCAD to get an understanding of the property attrbitutes that an object can have:

I can use the property attributes of e.g. the circle object to make adjustments. For example I have the layer “circles” which I want to assign the circle to. I can do this using pyautocad:

circle1.Layer = "circles"

I could now also adjust the color setting for the circle object, such that the color is specified by the layer – the index for this is the color index number 256:

circle1.Color = 256

Below you see the result in AutoCAD:

I now create two additional circles inside my drawing, using pyautocad. Afterwards I use pyautocad iter_objects method to loop through all objects in the drawing, demonstrating the option of doing so if needed:

# adding two circles to drawing
circle2 = acad.model.AddCircle(APoint(200.0,200.0),100)
circle3 = acad.model.AddCircle(APoint(300.0,300.0),100)
# looping through all objects
for obj in acad.iter_objects():
<POINTER(IAcadCircle) ptr=0x223f8ed65f8 at 223faa5c848>
<POINTER(IAcadCircle) ptr=0x223f8ed6358 at 223faa5ca48>
<POINTER(IAcadCircle) ptr=0x223f89ede28 at 223faa5c148>

This is what the result looks like in my AutoCAD drawing:

This completes my introduction to working with AutoCAD in Python, using pyautocad. To get a better understanding of AutoCAD I refer to my other AutoCAD trainings and recommend checking the pyautocad documentation.

You can find some more examples on using pyautocad on Youtube. E.g. I found a more detailed example here:

Leave a Reply

3 thoughts on “Python for AutoCAD (pyautocad module)

  1. Hi Mr. Felkl

    Thank you for this, very useful content. I wanted to ask if there is a way to read .dwg file in python completely without having autoCAD in my machine to start with?

    For example if its possible to read entire content of a .dwg file using python and print it on the console?

    Thank you!

  2. Hi Mr. Felk!

    What a great post!
    I have a doubt… how can I delete everything in the model with pyautocad, could you help me with that?

    Thx in advance!

Leave a Reply

Your email address will not be published. Required fields are marked *

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