from pyautocad import Autocad, APoint
def draw_line(acad, start_point, end_point):
"""Draw a line between two points."""
acad.model.AddLine(APoint(*start_point), APoint(*end_point))
def draw_tree(acad, base_point, trunk_height, branch_length):
"""Draw a simple tree with a trunk and two branches."""
# Draw the trunk
trunk_top = (base_point[0], base_point[1] + trunk_height)
draw_line(acad, base_point, trunk_top)
# Draw branches
branch_left = (trunk_top[0] - branch_length / 2, trunk_top[1] + branch_length)
branch_right = (trunk_top[0] + branch_length / 2, trunk_top[1] + branch_length)
draw_line(acad, trunk_top, branch_left)
draw_line(acad, trunk_top, branch_right)
def main():
# Connect to AutoCAD
acad = Autocad(create_if_not_exists=True)
print(f"Connected to {acad}")
# Define parameters for the tree
base_point = (0, 0) # Starting point of the tree's trunk
trunk_height = 10 # Vertical height of the trunk
branch_length = 7 # Length of the branches
# Draw the tree
draw_tree(acad, base_point, trunk_height, branch_length)
print("Tree drawn successfully.")
if __name__ == "__main__":
main()
Before running this script, ensure you have AutoCAD open, as pyautocad connects to an existing AutoCAD application. This code demonstrates the basics and can be extended or modified to create more complex tree shapes or patterns.