from pyautocad import Autocad, APoint
# Initialize AutoCAD
acad = Autocad(create_if_not_exists=True)
print(f"Active drawing: {acad.doc.Name}")
# Function to draw a circle (leaf)
def draw_leaf(center_x, center_y, radius):
center = APoint(center_x, center_y)
acad.model.AddCircle(center, radius)
# Function to draw a trunk (polyline)
def draw_trunk(start_x, start_y, width, height):
start_point = APoint(start_x, start_y)
# Define points of the rectangle representing the trunk
points = [
start_point,
APoint(start_x + width, start_y),
APoint(start_x + width, start_y + height),
APoint(start_x, start_y + height),
start_point # Close the polyline
]
acad.model.AddPolyline(points)
# Parameters
trunk_width = 5
trunk_height = 20
leaf_radius = 10
leaf_offset_x = 15
leaf_offset_y = 40
# Draw trunk
trunk_base_x = 50
trunk_base_y = 0
draw_trunk(trunk_base_x, trunk_base_y, trunk_width, trunk_height)
# Draw leaves
draw_leaf(trunk_base_x + trunk_width / 2, trunk_base_y + trunk_height + leaf_offset_y, leaf_radius)
draw_leaf(trunk_base_x + trunk_width / 2 - leaf_offset_x, trunk_base_y + trunk_height + leaf_offset_y - 5, leaf_radius)
draw_leaf(trunk_base_x + trunk_width / 2 + leaf_offset_x, trunk_base_y + trunk_height + leaf_offset_y - 5, leaf_radius)
print("Tree drawn in AutoCAD!")