To draw a simple tree using the PyAutoCAD library, you'll need to interact with AutoCAD through Python. In this example, we'll create a basic tree composed of a trunk (rectangle) and foliage (circles). First, make sure you have the pyautocad library installed. You can install it via pip if you haven't already:
from pyautocad import Autocad, APoint
from math import pi, cos, sin
def draw_rectangle(autocad, center, width, height):
half_width = width / 2
half_height = height / 2
p1 = APoint(center.x - half_width, center.y - half_height)
p2 = APoint(center.x + half_width, center.y + half_height)
autocad.model.AddRectangle(p1, width, height)
def draw_circle(autocad, center, radius):
autocad.model.AddCircle(center, radius)
def draw_tree(autocad, base_point, trunk_width, trunk_height, foliage_radius, foliage_levels=3):
# Draw the trunk
trunk_center = APoint(base_point.x, base_point.y + trunk_height / 2)
draw_rectangle(autocad, trunk_center, trunk_width, trunk_height)
# Draw the foliage
for level in range(foliage_levels):
y_offset = trunk_height + (foliage_radius * 2 * level)
foliage_center = APoint(base_point.x, base_point.y + y_offset)
draw_circle(autocad, foliage_center, foliage_radius)
if __name__ == "__main__":
# Connect to AutoCAD
autocad = Autocad(create_if_not_exists=True)
# Define parameters
base_point = APoint(0, 0)
trunk_width = 10
trunk_height = 30
foliage_radius = 15
# Draw the tree
draw_tree(autocad, base_point, trunk_width, trunk_height, foliage_radius)