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
print(acad.doc.Name)
Drawing1.dwg

Next, I can e.g. create a circle in 2D plane. In other posts we also demonstrate how to add pyautocad polylines, pyautocad arcs, a pyautocad ellipse, pyautocad splines, pyautocad helices, pyautocad texts and pyautocad solid objects. We also wrote posts demonstrating e.g. polar arrays in pyautocad and rectangular arrays in pyautocad.

# 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():
    print(obj)
<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.

In other posts we also demonstrate how to implement and execute operations in pyautocad, as well as we e.g. demonstrate how to mirror objects in pyautocad.

Leave a Reply

31 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!

    1. Hello Mr. Vidal,

      Nice question. We can use the delete command over every object we receive by using the iter_objects function. e.g.

      from pyautocad import Autocad:
      acad = Autocad(create_if_not_exists=True)
      for object in acad.iter_objects():
      object.Delete()

      Very sorry for the delay in reply to your comment.

      Have a nice day, thank you.

  3. Hi Mr. Felkl,

    Can you please tell me where can I get a full-fledged course to learn the complete usage of pyautocad ?

  4. Hello,
    when I try to run the first command to print the name of the document I get a traceback error:
    OSError: [WinError -2147221005] Invalid class string

    any idea why this is happening?

    1. Hello Mr Papageorgiou,

      Thank you for the question.
      It is because the default permissions of the document are not allowing pyautocad to access information from the document. Please try changing the permissions of the documents or try running python IDE as administrator.

      Have a great day!

  5. Mr. Falkl,

    Most of you code works great for me; however, I can’t get the Copy() and Move() methods to work. Might I ask about your programming environment? What version of Python are you using? What version of pyautocad? What version of AutoCAD?

    My error is in comtypes\client\lazybind.py and it is an AttributeError on Copy, and/or Move.

    1. Dear Mr. Phillips,

      Thank you for your question. This was with Python 3.5, AutoCAD 2020. The pyautocad version must have been up to date in October 2020. I do not remember what pyautocad version it was back then.

      I have seen that you contacted Mr. Sawant. He is building the pyautocad documentation on this blog for now, and he will get back to you with a response.

      Thank you and have a good day.

      1. Linnart;

        Thanks for the reply. I’ve tried with Python 2.7 and Python 3.8. I’ve loaded the pyautocad module using pip in each instance of Python that I’m running. I too am using AutoCAD 2020.

        As I mentioned earlier, I can access most properties of the circle; however, I can’t access the “Move” method. The error I get is as follows:

        Traceback (most recent call last):
        File “”, line 1, in
        c2 = c1.Move(APoint(100,100),APoint(150,200))
        File “C:\Python27\StandAlone\lib\site-packages\comtypes\client\lazybind.py”, line 163, in __getattr__
        raise AttributeError(name)
        AttributeError: Move

        Of course it is different under Python 3.8 as there it references the “comtypes” package under 3.8. But other than that… same error.

        1. Hi Mr. Phillips,

          Thanks for your question. There must be a python version-related issue. As you said, higher versions of python referes to the “comtypes” package, we can try importing com packages in the lower versions where the move command is not working. Please try the below mentioned script:

          import win32com.client
          import pythoncom

          acad = win32com.client.Dispatch(“AutoCAD.Application”)
          acadModel = acad.ActiveDocument.ModelSpace

          def APoint(x, y, z = 0):
          return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z))

          p1 = APoint(100, 100)
          c1 = acadModel.AddCircle(p1, 100)

          c2 = c1.Move(p1, APoint(200,200))

          Have a great day!

          1. Mr. Sawant,

            Under Python 3.8.1 I get a “ModuleNotFoundError”:
            Traceback (most recent call last):
            File “”, line 1, in
            import win32com.client
            ModuleNotFoundError: No module named ‘win32com’

            And under Python 2.7.14 I get a similar error:
            Traceback (most recent call last):
            File “”, line 1, in
            import win32com.client
            ImportError: No module named win32com.client

            Each “pyautocad” install has included a “comtypes” module along with “pyautocad.” Does the “comtypes” module make calls to “win32com.client?” If this is a dependency, why does it not get installed when using “pip?”

            BTW the installed version of “comtypes” that came with the “pyautocad” pip install is “1.1.10”

          2. Hi Mr. Phillips,

            Since there is no package named win32com, it is showing the error as:
            “ImportError: No module named win32com.client”

            Please try installing win32com.client using the below-mentioned command and package name as pywin32:
            pip install pywin32

            Thank you

        2. Patrick,

          I forwarded your question to Tanmay Sawant and I can see he answered you. Tanmay is building a documentation for our blog here, and he is going to be expanding this documentation over the upcoming months.

          1. Thank You Tanmay,

            I don’t fully understand, but I expect pyautocad was not fully hooking into the AutoCAD application and/or its API. So with the “pywin32” module, or wrapper, we forced a better connection to the AutoCAD application object. Is that a fair assessment?

            Respectfully,

            Patrick

            P.S. Linnart, Thanks for maintaining interest in the subject and forwarding my early query to Mr. Sawant

          2. Most welcome Mr Phillips.

            Yes actually.
            As I said earlier, the problem must be with internal communication of python with AutoCAD. While communicating with any other application in a windows system with python, we use win32com.client, whether it is AutoCAD, excel or any other application. In previous versions of python, it must be like we have to specify the communication API by importing com libraries. But in the latest versions pyautocad module itself takes care of communication between python and AutoCAD.

            Hope the given script worked for you, to move the objects.

            Thank you

  6. Mr. Sawant,

    Yes, the most recent script worked using win32com.client. And I’m thankful for that. What I don’t understand is your statement that “in the latest versions pyautocad module itself takes care of communication between python and AutoCAD.” And I don’t understand it because I thought I was using the latest version of pyautocad (0.2.0?) and though I was able to use the properties (Layer, Color, Thickness, etc.) I was not able to use the Move() method. If I have the latest version of pyautocad, why did it only half way work? If I don’t have the latest version, how do I acquire the latest version? Shouldn’t the latest version have been installed when I asked pip to install it?

    Please know that I am not complaining. I am making an effort to understand pyautocad, its installation and use. I also hope that others learn from my questions and your polite answers.

    Thanks again for your time and effort.

    Respectfully,

    Patrick

    1. Hello Mr. Phillips,

      Yes, the pyautocad version that you installed has to be the latest version i.e. 0.2.0. What I wanted to say is, it might be because the python version on which you have installed the pyautocad module is an older version of python. I do understand your question that why only the Move() method is not working and others are. But as per the experience I have till now, even I have faced such issues while working with certain commands, and I am also finding solutions to those issues and I look forward to posting the solutions for those as soon as I find them.

      Also, I am very thankful to you for asking questions, as this will build a strong foundation for those who really want to learn pyautocad. Your questions or opinions may also help others understand this in a much better way. 🙂

      Have a nice day!

  7. Hello Mr. Sawant,

    I appreciate all your efforts so far and look forward to reading more of your articles in the near future.

    Enjoy your work and leave time for play!

      1. Yes and no. No, I can’t get “pyautocad” to work properly. I have always used the latest version, so that is not it. As for Python itself, I can’t get it to work properly with python 2.7 or python 3.8. Neither case works as it should. I can access many, but not all, properties but few methods. The issue seems to be with “COM” objects, but to what extent I don’t know.

        Mr. Sawant did demonstrate that If I use win32com objects through the “pywin32” module, I can access the “move” method of an AutoCAD Circle object. But I don’t know the full win32com object model and was hoping to use “pyautocad” as it has a shallower learning curve.

        1. Hello Mr. Phillips,

          Thanks for your reply. I think we can solve your problem if we get access to your system once, to check whether there is a module path-related error or is that a template referencing error that is fetching such issues. As that would be a paid session with a nominal fee, if you are interested, you can schedule an appointment with us by sending an email via the contact us page on our website.

          Have a great day!

  8. Hello Mr. Cris,

    Thanks for your question. Maybe it is possible with comtypes (pywin32) module in python, if the object model of Fabrication is available online.
    Since pyautocad is AutoCAD specific, I cant be sure whether it would work with Fabrication, but I can say that the way we integrate python using pywin32 with AutoCAD should be similar to that with Fabrication.

    Kind regards,
    Tanmay

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.

Close

Meta