from pyautocad import Autocad, APoint
import math
def draw_branch(ax, start_point, angle, length, depth):
if depth == 0:
return
# Calculate end point of the branch
end_point = APoint(
start_point.x + length * math.cos(math.radians(angle)),
start_point.y + length * math.sin(math.radians(angle))
)
# Draw the line for the branch
ax.model.AddLine(start_point, end_point)
# Recursively draw smaller branches
draw_branch(ax, end_point, angle - 20, length * 0.7, depth - 1)
draw_branch(ax, end_point, angle + 20, length * 0.7, depth - 1)
def draw_tree():
acad = Autocad(create_if_not_exists=True)
acad.prompt("Drawing a tree...\n")
start_point = APoint(0, 0)
trunk_length = 50
depth = 4 # Depth of recursion, feel free to adjust
# Draw the trunk and branches
draw_branch(acad, start_point, 90, trunk_length, depth)
if __name__ == "__main__":
draw_tree()
To run this script successfully, make sure the AutoCAD application is open, and this script connects to it. Adjust the angles and recursion depth for different styles of trees. Keep in mind that heavy recursion (deep trees) might slow down the performance, depending on the complexity and the power of your machine.