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:
- Link: Extending the objects in AutoCAD using pyautocad in Python
- Link: Using Python lists and dictionaries to work with AutoCAD objects with pyautocad
- Link: Hatching objects on AutoCAD template using pywin32 in Python
- Link: Raster image object in AutoCAD with pyautocad in Python
- Link: Working with 3D mesh object in AutoCAD using pyautocad in Python
- Link: Creating adouble constructor using pywin32 in Python
- Link: Creating apoint method using pywin32 in Python
- Link: Python integration with AutoCAD using pywin32 and win32com
- Link: Deleting objects in a AutoCAD template with pyautocad and pywin32 in Python
- Link: Mirror object on a 2D plane with pyautocad in Python
- Link: Working with texts in Autocad using pyautocad in Python
- Link: Polar arrays in AutoCAD using pyautocad in Python
- Link: Rectangular arrays in AutoCAD using pyautocad in Python
- Link: Operations with AutoCAD objects using pyautocad in Python
- Link: Solid objects in AutoCAD using pyautocad in Python
- Link: Working with helices in AutoCAD using pyautocad in Python
- Link: Drawing splines in AutoCAD with pyautocad in Python
- Link: Polylines in pyautocad for drawing AutoCAD polygons in Python
- Link: Drawing ellipse arcs in AutoCAD using pyautocad in Python
- Link: Drawing arcs in AutoCAD using pyautocad in Python
- Link: Near simultaneous factory design and process optimization with Promodel AutoCAD edition
- Link: Python for AutoCAD pyautocad module
- Link: Region object in AutoCAD with Python
- Link: AutoCAD Application object class in Python
- Link: AutoCAD Document object in Python
- Link: AutoCAD Block object in Python
- Link: AutoCAD Attribute object in Python
- Link: DimAligned object in AutoCAD using Python
- Link: SelectionSet object in AutoCAD with Python
Data scientist focusing on simulation, optimization and modeling in R, SQL, VBA and Python
5 comments
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?
Hi
acad.model.AddPolyline(points)
is not working.. is it any AutoCAD version specific ?
Did you get any solution to this?
hi, can you please let me know where can I find the list of autocad commands that I can use with pyautocad? thanks in advance.
the most detailed documentation for this are the blog posts on this blog