from pyautocad import Autocad, APoint
def draw_line(acad, start_point, end_point):
"""Draw a line in AutoCAD from start_point to end_point."""
acad.model.AddLine(start_point, end_point)
def draw_tree(acad, origin, trunk_height=10, branch_length=7, levels=3):
"""Draw a simple tree with a trunk and a few branches."""
# Draw trunk
trunk_top = APoint(origin.x, origin.y + trunk_height)
draw_line(acad, origin, trunk_top)
# Recursively draw branches
def draw_branches(start_point, angle, length, level):
"""Recursive function to draw branches."""
if level == 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 branch
draw_line(acad, start_point, end_point)
# Draw the next set of branches
draw_branches(end_point, angle - 30, length * 0.7, level - 1)
draw_branches(end_point, angle + 30, length * 0.7, level - 1)
import math
# Start drawing branches from the top of the trunk
draw_branches(trunk_top, -90, branch_length, levels)
def main():
acad = Autocad(create_if_not_exists=True)
acad.prompt("Drawing Tree with PyAutoCAD...\n")
# Define the origin point for the tree
origin = APoint(0, 0)
# Draw the tree
draw_tree(acad, origin)
print("Tree drawing complete.")
if __name__ == "__main__":
main()