from pyautocad import Autocad, APoint
# Initialize AutoCAD
acad = Autocad(create_if_not_exists=True)
acad.prompt("Hello! Automation begins...\n")
# Function to draw a line given start and end coordinates
def draw_line(start, end):
acad.model.AddLine(APoint(*start), APoint(*end))
# Function to draw a simple tree structure
def draw_tree(trunk_bottom, trunk_top, branch_length):
# Draw trunk
draw_line(trunk_bottom, trunk_top)
# Calculate branch positions on top of the trunk
center_x, trunk_top_y = trunk_top
left_branch_top = (center_x - branch_length, trunk_top_y + branch_length)
right_branch_top = (center_x + branch_length, trunk_top_y + branch_length)
# Draw left branch
draw_line(trunk_top, left_branch_top)
# Draw right branch
draw_line(trunk_top, right_branch_top)
# Define tree parameters
trunk_bottom = (0, 0) # starting point at origin
trunk_top = (0, 50) # height of the trunk
branch_length = 30 # length of each branch
# Draw the tree
draw_tree(trunk_bottom, trunk_top, branch_length)
acad.prompt("Drawing of a tree is completed.\n")
In this script, we define a simple tree with a trunk and two branches. The draw_tree function calculates the positions for the branches based on the given trunk height and branch length. The draw_line function uses pyautocad to add lines to the model space, effectively drawing the tree.
Before you run this script, ensure that AutoCAD is open and you have a drawing ready to be automated, as the pyautocad library connects to a running instance of AutoCAD. This script puts a basic tree shape directly into your AutoCAD workspace.