manufino
AutoCAD
Python

This Python library provides a set of classes and methods to interact with AutoCAD using the COM API. The library allows automating many common operations in AutoCAD, such as creating and managing layers, objects, blocks, attributes, and groups of objects.

Last updated Jul 4, 2026
59
Stars
12
Forks
0
Issues
0
Stars/day
Attention Score
56
Language breakdown
Python 100.0%
โ–ธ Files click to expand
README

AutoCAD Python Automation

This Python library provides a set of classes and methods to interact with AutoCAD using the COM API. The library allows automating many common operations in AutoCAD, such as creating and managing layers, objects, blocks, attributes, and groups of objects. Additionally, you can directly draw various primitives like lines, circles, ellipses, and rectangles in AutoCAD.

Main Features

  • Layer Management: Create, modify, set visibility, lock/unlock, change color, and manage layer linetype.
  • Object Management: Create, select, move, scale, rotate, align, and distribute objects.
  • Block Management: Insert, export, create, modify, and remove blocks.
  • Attribute Management: Add, modify, and delete block attributes.
  • User Input and Output: Request input from the user (points, strings, integers) and display messages.
  • Group Management: Create, modify, add/remove objects, and select groups.
  • Dimensioning: Add aligned, linear, angular, radial, and diametric dimensions.

Requirements

  • AutoCAD installed and running on Windows (this library talks to AutoCAD through its COM Automation API, which is Windows-only).
  • Python 3.x.
  • pywin32 package installed (installable via pip).

Installation

  • Clone this repository:
git clone https://github.com/manufino/AutoCAD.git
  • Install the dependencies:
pip install pywin32

Usage Examples

Below are some examples of how to use the library to automate operations in AutoCAD.

Create the AutoCAD object

# Attaches to an already-running AutoCAD instance if one exists,

otherwise launches a new one (default behavior)

acad = AutoCAD()

Force launching a brand new AutoCAD instance instead

acad = AutoCAD(attachtorunning=False)

Draw a Line

# Define start and end points using APoint
start_point = APoint(0, 0, 0)
end_point = APoint(100, 100, 0)

Draw the line

acad.addline(startpoint, end_point)

Draw a Circle

# Define the center point and radius
center = APoint(50, 50, 0)
radius = 25

Draw the circle

acad.add_circle(center, radius)

Draw an Ellipse

# Define the center point and major axis
center = APoint(75, 75, 0)
major_axis = APoint(50, 0, 0)
ratio = 0.5  # Minor axis ratio

Draw the ellipse

acad.addellipse(center, majoraxis, ratio)

Draw a Rectangle

# Define lower left and upper right corners
lower_left = APoint(10, 10, 0)
upper_right = APoint(60, 40, 0)

Draw the rectangle

acad.addrectangle(lowerleft, upper_right)

Add Text

# Define the text content, insertion point, and height
text_content = "Hello AutoCAD"
insertion_point = APoint(20, 20, 0)
text_height = 5

Create and add the text

text = Text(textcontent, insertionpoint, text_height) acad.add_text(text)

Add Dimensions

# Aligned dimension
acad.add_dimension(Dimension(
    APoint(0, 0), APoint(600, 0), APoint(0, -50),
    dimension_type=DimensionType.ALIGNED
))

Linear (rotated) dimension: 0 degrees measures the horizontal distance

acad.add_dimension(Dimension( APoint(0, 0), APoint(600, -600), APoint(-1000, -300), dimensiontype=DimensionType.LINEAR, rotationangle=0 ))

Angular dimension requires the angle vertex in addition to the two points

acad.add_dimension(Dimension( APoint(600, 0), APoint(0, 600), APoint(300, 300), dimensiontype=DimensionType.ANGULAR, vertexpoint=APoint(0, 0) ))

Radial and diametric dimensions take a leader length instead of a text point

acad.add_dimension(Dimension( APoint(0, 0), APoint(100, 0), dimensiontype=DimensionType.RADIAL, leaderlength=20 ))

Repeat block horizontally

# Repeat the "blockname" block horizontally
total_length = 100  # Total length X
block_length = 10  # Length of the block "blockname"
insertion_point = APoint(0, 0, 0)  # Initial insertion point

Execute the block repetition

acad.repeatblockhorizontally("blockname", totallength, blocklength, insertion_point)

Set the visibility of a layer

# Set the visibility of a layer
acad.setlayervisibility("Linea di mezzeria", visible=False)

Lock a layer

# Lock a layer
acad.lock_layer("Quote", lock=True)

Delete a layer

# Delete a layer
acad.delete_layer("Simboli")

Change the color of a layer

# Change the color of a layer
acad.changelayercolor("Contorni", Color.YELLOW)

Set the linetype of a layer

# Set the linetype of a layer
acad.setlayerlinetype("Assi", "DASHED")

Select objects

# Iterate over model space objects, optionally filtering by entity type,

and further filter by any object property (e.g. layer)

selectedobjects = [obj for obj in acad.iterobjects(object_type="AcLine") if obj.Layer == "Contorni"] print(f"Selected objects: {len(selected_objects)}")

Move, scale, and rotate objects

# Move, scale, and rotate objects
for obj in selected_objects:
    acad.move_object(obj, APoint(10, 10, 0))
    acad.scale_object(obj, APoint(0, 0, 0), 2)
    acad.rotate_object(obj, APoint(0, 0, 0), 45)

Align and distribute objects

# Align and distribute objects
acad.alignobjects(selectedobjects, alignment=Alignment.LEFT)
acad.distributeobjects(selectedobjects, spacing=5)

Insert a block from a file

# Insert a block from a file
acad.insertblockfromfile("pathto_file.dwg", APoint(0, 0, 0))

Export a block to a file

# Export a block to a file
acad.exportblocktofile("piatto", "pathto_export.dwg")

Modify block attributes

# Modify block attributes
blockreferences = acad.getblock_references("piatto")
if block_references:
    blockref = blockreferences[0]  # Get the first found block
    acad.modifyblockattribute(block_ref, "Tag", "NewValue")

Delete block attributes

# Delete block attributes
acad.deleteblockattribute(block_ref, "Tag")

Request user input

# Request user input
point = acad.getuserinput_point("Select a point")
text = acad.getuserinput_string("Enter a string")
integer = acad.getuserinput_integer("Enter an integer")

Display a message to the user

# Display a message to the user
acad.show_message("Operation completed")

Create a group of objects

# Create a group of objects
group = acad.creategroup("MyGroup", selectedobjects)

Add objects to a group

# Add objects to a group
acad.addtogroup("MyGroup", selected_objects)

Remove objects from a group

# Remove objects from a group
acad.removefromgroup("MyGroup", selected_objects)

Select a group of objects

# Select a group of objects
groupitems = acad.selectgroup("MyGroup")
print(f"Objects in group 'MyGroup': {len(group_items)}")

Error Handling

Every method wraps AutoCAD/COM failures in an AutoCADError, which also logs the message via the standard logging module. Catch it around calls you want to handle gracefully:

try:
    acad.delete_layer("NonExistentLayer")
except AutoCADError as e:
    print(f"Operation failed: {e}")

The original exception is preserved as e.cause, so traceback.print_exc() still shows the real COM error underneath.

๐Ÿ”— More in this category

ยฉ 2026 GitRepoTrend ยท manufino/AutoCAD ยท Updated daily from GitHub