Graphics

The GraphicsOperations module handles graphical elements like text, shapes, and annotations.

sch = kicad.get_schematic()
graphics = sch.graphics

Text

add_text(text, position, ...)

Adds a text annotation.

from kipy.geometry import Vector2

pos = Vector2.from_xy_mm(50, 50)
text_id = sch.graphics.add_text("Note: Check voltage levels", pos)

# With formatting
text_id = sch.graphics.add_text(
    "Important!",
    pos,
    size_mm=2.5,
    bold=True,
    italic=False,
    rotation=0
)

add_text_box(text, position, size)

Adds a text box with a border.

pos = Vector2.from_xy_mm(10, 10)
size = Vector2.from_xy_mm(40, 20)

sch.graphics.add_text_box(
    "Design Notes:\n- Rev 1.0\n- Initial release",
    pos,
    size
)

Shapes

add_line(start, end)

Adds a line.

start = Vector2.from_xy_mm(0, 0)
end = Vector2.from_xy_mm(50, 50)
line_id = sch.graphics.add_line(start, end)

add_rectangle(position, size)

Adds a rectangle.

pos = Vector2.from_xy_mm(10, 10)
size = Vector2.from_xy_mm(30, 20)
rect_id = sch.graphics.add_rectangle(pos, size)

add_circle(center, radius)

Adds a circle.

center = Vector2.from_xy_mm(50, 50)
radius_mm = 10
circle_id = sch.graphics.add_circle(center, radius_mm)

add_arc(center, radius, start_angle, end_angle)

Adds an arc.

sch.graphics.add_arc(
    center=Vector2.from_xy_mm(50, 50),
    radius_mm=15,
    start_angle=0,
    end_angle=90
)

add_polygon(points)

Adds a polygon.

points = [
    Vector2.from_xy_mm(0, 0),
    Vector2.from_xy_mm(10, 0),
    Vector2.from_xy_mm(5, 10),
]
polygon_id = sch.graphics.add_polygon(points)

Styling

set_stroke(item_id, width_mm, color)

Sets the stroke style.

sch.graphics.set_stroke(line_id, width_mm=0.2, color="#FF0000")

set_fill(item_id, fill_type, color)

Sets the fill style.

# No fill
sch.graphics.set_fill(rect_id, fill_type="none")

# Solid fill
sch.graphics.set_fill(rect_id, fill_type="solid", color="#00FF00")

# Background fill
sch.graphics.set_fill(rect_id, fill_type="background")

Bitmap Images

add_image(path, position, scale)

Adds a bitmap image.

sch.graphics.add_image(
    "/path/to/logo.png",
    position=Vector2.from_xy_mm(10, 10),
    scale=1.0
)

Getting Graphics

get_all()

Returns all graphic items.

items = sch.graphics.get_all()
for item in items:
    print(f"{item.type} at {item.position}")

Modifying Graphics

move(item_id, position)

Moves a graphic item.

sch.graphics.move(item_id, new_position)

delete(item_id)

Deletes a graphic item.

sch.graphics.delete(item_id)

Example: Add Documentation Box

from kipy import KiCad
from kipy.geometry import Vector2

kicad = KiCad()
sch = kicad.get_schematic()

# Add a documentation box
box_pos = Vector2.from_xy_mm(200, 10)
box_size = Vector2.from_xy_mm(50, 40)

# Draw rectangle
rect = sch.graphics.add_rectangle(box_pos, box_size)
sch.graphics.set_stroke(rect, width_mm=0.3, color="#000000")
sch.graphics.set_fill(rect, fill_type="background")

# Add title
title_pos = Vector2.from_xy_mm(205, 15)
sch.graphics.add_text("NOTES", title_pos, size_mm=3, bold=True)

# Add notes
notes_pos = Vector2.from_xy_mm(205, 25)
sch.graphics.add_text(
    "1. Check polarity\n2. 10V max input\n3. See datasheet",
    notes_pos,
    size_mm=1.5
)

Example: Draw Reference Lines

# Draw horizontal and vertical reference lines
sch.graphics.add_line(
    Vector2.from_xy_mm(0, 100),
    Vector2.from_xy_mm(200, 100)
)

sch.graphics.add_line(
    Vector2.from_xy_mm(100, 0),
    Vector2.from_xy_mm(100, 200)
)

# Add crosshair labels
sch.graphics.add_text("CENTER", Vector2.from_xy_mm(102, 98))