10 exemplary AutoCAD commands in Python

I frequently expand on our the existing AutoCAD automation documentation in Python and VBA. In this article I show 10 exemplary AutoCAD commands in Python. For this I first setup a reference to AutoCAD in Python using pyautocad.

import pyautocad

acad = pyautocad.Autocad()

Add line AutoCAD command in Python with AddLine

AddLine() adds a line to the current space.

line = acad.model.AddLine(start_point, end_point)

I could also set and modify properties of the line using e.g. pyautocad in Python.

Add circle AutoCAD command in Python with AddCircle

AddCircle() adds a circle to the current space.

center_point = (0, 0)
radius = 10
circle = acad.model.AddCircle(center_point, radius)

Adding arc to AutoCAD drawing in Python

AddArc() adds an arc to the current space.

center_point = (0, 0)
radius = 10
start_angle = 0
end_angle = 90
arc = acad.model.AddArc(center_point, radius, start_angle, end_angle)

Adding polyline in AutoCAD with Python

AddPolyline() adds a polyline to the current space.

points = [(0, 0), (10, 0), (10, 10), (0, 10)]
polyline = acad.model.AddPolyline(points)

Adding text elements in AutoCAD with Python

AddText() adds a text object to the current space.

insert_point = (0, 0)
height = 2.5
text_string = "Hello, world!"
text = acad.model.AddText(text_string, insert_point, height)

AutoCAD command in Python for adding block

AddBlock() adds a block definition to the current drawing.

block_name = "my_block"
insert_point = (0, 0)
block = acad.model.AddBlock(insert_point, block_name)

Creating attribute definitions in AutoCAD with Python

AddAttributeDefinition: Adds an attribute definition to a block.

tag = "my_attribute"
prompt = "Enter a value for my_attribute:"
insert_point = (0, 0)
height = 2.5
attrib_def = block.AddAttributeDefinition(tag, insert_point, height, prompt)

Adding viewport to layout using Python

AddViewport() adds a viewport to the layout.

lower_left_corner = (0, 0)
upper_right_corner = (10, 10)
viewport = acad.model.AddViewport(lower_left_corner, upper_right_corner)

AutoCAD zoom extension in Python

ZoomExtents() zooms to the extents of the drawing.

acad.ZoomExtents()

Purging unused objects with Python command

Purge() purges unused objects from the current drawing.

acad.ActiveDocument.Purge(acad.GetConstant("AcDb::kAll"))

Final remarks and related content

These are some few simple examples of AutoCAD commands in Python. If you are interested in AutoCAD automatization in Python you can check our existing documentation on this blog. Some exemplary entries into our documentation are listed below:

You May Also Like

Leave a Reply

2 comments

Lam says:

Hi Linnart!
Please tell me: is there any way to solve the problem, put 4 shapes: circle, square, triangle, pentagon, into a rectangle of given size, with gaps between the The figure is not smaller than 1 cm. Let the size of the rectangle covering the above 4 shapes be the smallest?

Prit says:

Hi
acad.model.AddPolyline(points)
is not working.. is it any AutoCAD version specific ?

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.