Usage

For the following examples, we will use Autocad and APoint.

from pyacad import Autocad

Initializing the AutoCAD API:

acad = Autocad()

Retrieving active document name

# Only the document name.
acad.doc.Name

# For the document path.
acad.doc.FullName

Adding objects to the active document

Points

p0 = APoint(2, 1)
point = acad.model.AddPoint(p0)

Lines

p0, p1 = APoint(1, 1), APoint(2, 1)
line = acad.model.AddLine(p0, p1)

Polylines

For drawing polylines we need to use aDouble.

from pyacad import aDouble
points = aDouble([0, 0, 1, 0, 1, 1, 0, 1, 0, 0])
polyline = acad.model.AddLightweightPolyline(points)

Circles

p0 = APoint(3, 1)
radius = .5
circle = acad.model.AddCircle(p0, radius)

Text

p0 = APoint(0, 3)
height = 1
textstring = "Hello World!"
text = acad.model.AddText(textstring, p0, height)

MultiLineText

p0 = APoint(0, 4)
width = 1
textstring = "This is a MText."
mtext = acad.model.AddMText(p0, width, textstring)

Hatch

For drawing hatchs we need to use aDispatch.

# Defining boundary.
outer_boundary = []
outer_boundary.append(acad.model.AddCircle(APoint(0, 0), 1))
outer_boundary_dispatch = aDispatch(outer_boundary)

# Creating hatch and adding boundary.
hatch = acad.model.AddHatch(0, "ANSI31", True)
hatch.AppendOuterLoop(outer_boundary_dispatch)
hatch.Evaluate()

Aligned Dimension

p0, p1, p2 = APoint(0, 4), APoint(4, 4), APoint(2, 4.5)
acad.model.AddDimAligned(p0, p1, p2)

Further information about objects

For more information on creating objects, I recommend visiting the official Autodesk site on ActiveX Automation for your corresponding version of AutoCAD.

Retrieving over documents, layouts, layer, objects and more.

Retrieving documents from the AutoCAD API

acad.iter_documents()

Retrieving blocks from the document

blocks = acad.iter_blocks()

Alternatively, you can pass a specific document using the document parameter, which should be of the type returned by the app.iter_documents() function.

docs = [*acad.iter_documents()]
doc_selected = docs[0]  # 0 if you want select first document of list.
blocks = acad.iter_blocks(document=doc_selected)

Retrieving dimension styles from the document

dim_styles = acad.iter_dim_styles()

You can also do it in the same way as shown in iter_blocks().

Retrieving layers from the document

layers = acad.iter_layers()

You can also do it in the same way as shown in iter_blocks().

Retrieving layouts from the document

layouts = acad.iter_layouts()

You can also do it in the same way as shown in iter_blocks().

Retrieving objects from the document

You can iterate over the objects in a drawing.

objects = acad.iter_objects()

Also you can filter for a concrete obejct type.

text_obejects = acad.iter_objects("Text")

Retrieving text styles from the document

text_styles = acad.iter_text_styles()

You can also do it in the same way as shown in iter_blocks().