AutoCAD Document object in pyautocad

In my previous article I discussed the AutoCAD Application object class. The most important part of any AutoCAD application is the Document object. This is because users perform all their actions on this Document object. Consequently, I am writing this new blog post, covering the AutoCAD Document object class.

The pyautcad module and its alternatives

Firstly, let me mention the modules I am going to use and what other options are available.

As you might have guessed from the title of this article I am using the pyautocad module for this tutorial. Moreover, in order to be able to access the document itself I am using acad.doc. You will see this in the code below:

from pyautocad import Autocad, APoint, aDouble

acad = Autocad(create_if_not_exists=True)

print(acad.doc.Name)

O/p:
Drawing1.dwg

However, I can use communication modules too. That is, other modules than pyautocad. In the example below I use pythoncom and win32com, so called communication modules, to access the AutoCAD Document object class.

from win32com.client import *
import pythoncom
import win32com

acad1 = win32com.client.Dispatch("AutoCAD.Application")
print(acad1.ActiveDocument.Name)

O/p:
Drawing1.dwg

Properties of the AutoCAD Document object class

To begin with this topic, I am introducing some important properties of the AutoCAD Document object. I find these properties important because they can be used for various automatization purposes.

For example, AutoCAD works with one document at a time. A AutoCAD Document object contains many objects, such as Layers, Layouts, Dimension Styles, Viewport, UCS, and other object types. Amongst these objects, only one can be active at any given point in time.

For instance, a Document object can contain n number of layers. However, only one of these layers can be active at a time.

To check whether the document present is the actually the currently active document I can use the Active method. This method returns a boolean value. You can see this in the example below:

# determine if the document is the active document
print(acad.doc.Active)

O/p:
True

Similarly, to check other Active sub-objects I can use the methods with method names in the format of Active<ObjectName>. Some examples could be ActiveLayout, ActiveLayer, ActiveViewport, and so on.

Object names that can be used are listed below:

  • DimStyle
  • Layer
  • Layout
  • Linetype
  • Material
  • PViewport
  • SelectionSet
  • Space
  • TextStyle
  • UCS
  • Viewport

I will show how some of these objects can be used.

print(acad.doc.ActiveDimStyle.Name)
print(acad.doc.ActiveLayer.Name)
print(acad.doc.ActiveLayout.Name)

O/p:
ISO-25
0
Model
AutoCAD Document object class documentation, figure 1.1: ActiveDimStyle
ActiveDimStyle
AutoCAD Document object class documentation, figure 1.2: ActiveLayer
ActiveLayer
AutoCAD Document object class documentation, figure 1.3: ActiveLayout
ActiveLayout

In the same way, I can use other properties too. These properties return the respective objects which can further be dealt with. I will post more detailed articles covering this in the future.

Accessing the AutoCAD Document object Blocks property

Moving ahead, to get the Blocks objects that are contained by the Document object I can use the Blocks property. This returns a collection of the various AutoCAD Block objects that are present in the AutoCAD Document object.

print(acad.doc.Blocks)
for i in (acad.doc.Blocks):
    print(i.Name)

O/p:
<comtypes.client.lazybind.Dispatch object at 0x00000209E60C1D60>
*Model_Space
*Paper_Space
*Paper_Space

If I want to return the database to which the object belongs I can use the Database object. Simply put this returns the database object. It does not have any name attribute.

acad.doc.Database

O/p:

<comtypes.client.lazybind.Dispatch object at 0x0000024FA64175B0>

Sub-objects contained by AutoCAD Document object

Earlier I discussed that a AutoCAD Document object contains multiple sub-objects. These objects are present in the form of their own specific type of objects e.g. Layers, Materials, LineTypes, etc.

To point out the collections I can simply use the methods with their names themselves. And If I want to access each of the objects present inside the collection, then I can iterate over the same. In a similar fashion as I did in the case of Blocks.

The list of object collections is given below:

  • Dictionaries
  • DimStyles
  • Layers
  • Layouts
  • Linetypes
  • Materials
  • ModelSpace
  • PaperSpace
  • PlotConfigurations
  • RegisteredApplications
  • SelectionSets
  • TextStyles
  • UserCoordinateSystems
  • Viewports
  • Views

Now that I have listed all the object collections, let me present an example of how to use these methods.

print(acad.doc.DimStyles)
for i in (acad.doc.DimStyles):
    print(i.Name)

O/p:
<comtypes.client.lazybind.Dispatch object at 0x00000209F4F11D00>
Standard
Annotative
ISO-25

In the same way, other important properties can also be fetched as given in the code below.

# height of document window
print(acad.doc.Height)

# width of document window
print(acad.doc.Width)

# lower Left to Upper Right Limits
print(acad.doc.Limits)

# return a boolean value for ObjectSnapMode to check if it is on/off
print(acad.doc.ObjectSnapMode)

# path of Document
print(acad.doc.Path)

# return if Document is ReadOnly/Read-Write using boolean value
print(acad.doc.ReadOnly)

# check if document contains any unsaved changes using boolean value
print(acad.doc.Saved)

# returns SummaryInfo objects which contains document metadata (Title, subject, author, keywords)
print(acad.doc.SummaryInfo)

# returns if window is Minimized, Maximized or in a Normal state
print(acad.doc.WindowState)

# returns the document title
print(acad.doc.WindowTitle)

O/p:

818
1517
(0.0, 0.0, 420.0, 297.0)
False
C:\Users\91998\OneDrive\Documents
False
False
<comtypes.client.lazybind.Dispatch object at 0x000001C227C460D0>
3
Drawing1.dwg

Methods of the Document object class in AutoCAD

Now that I have covered the various properties of the AutoCAD Document object I will move forward to some of the important methods of the AutoCAD Document class.

In fact, the very first method I am talking about is Activate() method. To activate any document I can use this method.

acad.doc.Activate

Thereafter, to evaluate the integrity of any drawing I can use the AuditInfo() method. Here, I can pass true or false as parameters for whether or not I want AutoCAD to fix the problems it encounters.

acad.doc.AuditInfo(True)

Certainly, there are also a few methods for regenerating, saving, and closing.

# regenerate drawing
acad.doc.Regen

# save drawing
acad.doc.Save

# while closing pass boolean to save changes or not followed by drawing name
acad.doc.Close(False, "Drawing2.dwg")3

Finally, to remove unused named references like blocks or layers from the Document I can use the PurgeAll command.

acad.doc.PurgeAll

Summary and concluding remarks

Finally, I conclude the following: Using the AutoCAD Document class methods documented in this article I can automatize different Document object-related tasks. This can be applied to automatize and optimize AutoCAD-related workflows.

Lastly, in case of any doubts, please feel free to comment below in the comment section. I am also open to technical consultation for which you can contact me by using our contact form. Please check out my other blogs covering various aspects of the pyautocad and pywin32 Python modules for AutoCAD automatization.

References to related AutoCAD automatization documentation

And now, a list of links to various AutoCAD automatization posts published by me on this blog:

Leave a Reply

Leave a Reply

Your email address will not be published.

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

Close

Meta