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.
- 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.
- AutoCAD installed and running on Windows (this library talks to AutoCAD through its COM Automation API, which is Windows-only).
- Python 3.x.
pywin32package installed (installable via pip).
-
Clone this repository:
git clone https://github.com/manufino/AutoCAD.git
-
Install the dependencies:
pip install pywin32
Below are some examples of how to use the library to automate operations in AutoCAD.
# 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(attach_to_running=False)# Define start and end points using APoint
start_point = APoint(0, 0, 0)
end_point = APoint(100, 100, 0)
# Draw the line
acad.add_line(start_point, end_point)# Define the center point and radius
center = APoint(50, 50, 0)
radius = 25
# Draw the circle
acad.add_circle(center, radius)# 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.add_ellipse(center, major_axis, ratio)# Define lower left and upper right corners
lower_left = APoint(10, 10, 0)
upper_right = APoint(60, 40, 0)
# Draw the rectangle
acad.add_rectangle(lower_left, upper_right)# 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(text_content, insertion_point, text_height)
acad.add_text(text)# 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),
dimension_type=DimensionType.LINEAR, rotation_angle=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),
dimension_type=DimensionType.ANGULAR, vertex_point=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),
dimension_type=DimensionType.RADIAL, leader_length=20
))# 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.repeat_block_horizontally("blockname", total_length, block_length, insertion_point)# Set the visibility of a layer
acad.set_layer_visibility("Linea di mezzeria", visible=False)# Lock a layer
acad.lock_layer("Quote", lock=True)# Delete a layer
acad.delete_layer("Simboli")# Change the color of a layer
acad.change_layer_color("Contorni", Color.YELLOW)# Set the linetype of a layer
acad.set_layer_linetype("Assi", "DASHED")# Iterate over model space objects, optionally filtering by entity type,
# and further filter by any object property (e.g. layer)
selected_objects = [obj for obj in acad.iter_objects(object_type="AcLine") if obj.Layer == "Contorni"]
print(f"Selected objects: {len(selected_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
acad.align_objects(selected_objects, alignment=Alignment.LEFT)
acad.distribute_objects(selected_objects, spacing=5)# Insert a block from a file
acad.insert_block_from_file("path_to_file.dwg", APoint(0, 0, 0))# Export a block to a file
acad.export_block_to_file("piatto", "path_to_export.dwg")# Modify block attributes
block_references = acad.get_block_references("piatto")
if block_references:
block_ref = block_references[0] # Get the first found block
acad.modify_block_attribute(block_ref, "Tag", "NewValue")# Delete block attributes
acad.delete_block_attribute(block_ref, "Tag")# Request user input
point = acad.get_user_input_point("Select a point")
text = acad.get_user_input_string("Enter a string")
integer = acad.get_user_input_integer("Enter an integer")# Display a message to the user
acad.show_message("Operation completed")# Create a group of objects
group = acad.create_group("MyGroup", selected_objects)# Add objects to a group
acad.add_to_group("MyGroup", selected_objects)# Remove objects from a group
acad.remove_from_group("MyGroup", selected_objects)# Select a group of objects
group_items = acad.select_group("MyGroup")
print(f"Objects in group 'MyGroup': {len(group_items)}")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.