from pyautocad import Autocad, APoint
# Initialize the AutoCAD application
acad = Autocad(create_if_not_exists=True)
acad.prompt("Hello, AutoCAD from Python\n")
print(acad.doc.Name)
# Function to draw a line
def draw_line(start, end):
return acad.model.AddLine(APoint(*start), APoint(*end))
# Function to draw a circle
def draw_circle(center, radius):
return acad.model.AddCircle(APoint(*center), radius)
# Draw the trunk of the tree (as a rectangle)
def draw_trunk(base_point, width, height):
x, y = base_point
# Draw the sides of the trunk
draw_line((x, y), (x, y + height))
draw_line((x + width, y), (x + width, y + height))
# Draw the top and bottom of the trunk
draw_line((x, y + height), (x + width, y + height))
draw_line((x, y), (x + width, y))
# Draw the canopy of the tree (as a circle)
def draw_canopy(center, radius):
draw_circle(center, radius)
# Parameters for the tree
base_point = (50, 0) # Base point of the trunk
trunk_width = 10
trunk_height = 30
canopy_center = (base_point[0] + trunk_width / 2, base_point[1] + trunk_height + 20) # Center point for the canopy
canopy_radius = 20
# Drawing the tree by combining the trunk and the canopy
draw_trunk(base_point, trunk_width, trunk_height)
draw_canopy(canopy_center, canopy_radius)
print("Tree drawn in AutoCAD.")
Before running the script, make sure AutoCAD is open on your machine and the document is ready to receive the drawing. Modify the coordinates and dimensions to fit your needs or AutoCAD workspace size.