from pyautocad import Autocad, APoint
# Initialize AutoCAD
acad = Autocad(create_if_not_exists=True)
acad.prompt("Hello, AutoCAD from Python\n")
# Function to draw a line between two points
def draw_line(start: APoint, end: APoint):
return acad.model.AddLine(start, end)
# Parameters for the tree
base_point = APoint(0, 0, 0)
trunk_height = 100
branch_angle = 30 # degrees
branch_length_ratio = 0.7
number_of_levels = 3
# Function to draw a tree using recursion
def draw_tree(start_point, angle, length, level):
if level == 0:
return
# Calculate the end point of the branch using trigonometry
end_x = start_point.x + length * math.cos(math.radians(angle))
end_y = start_point.y + length * math.sin(math.radians(angle))
end_point = APoint(end_x, end_y)
# Draw the branch
draw_line(start_point, end_point)
# Draw two branches recursively
draw_tree(end_point, angle - branch_angle, length * branch_length_ratio, level - 1)
draw_tree(end_point, angle + branch_angle, length * branch_length_ratio, level - 1)
import math
# Draw the tree starting from the base point
draw_line(base_point, APoint(base_point.x, base_point.y + trunk_height))
draw_tree(APoint(base_point.x, base_point.y + trunk_height), 90, trunk_height * branch_length_ratio, number_of_levels)
print("Tree drawing completed.")
Ensure AutoCAD is open while running this code, and check that PyAutoCAD is correctly set up to interact with it. Adjust the parameters like trunk_height, branch_angle, branch_length_ratio, and number_of_levels to create different tree styles.