Sheets
The SheetOperations module handles hierarchical sheet management.
sch = kicad.get_schematic()
sheets = sch.sheets
Creating Sheets
add_sheet(filename, position, size)
Creates a new hierarchical sheet.
from kipy.geometry import Vector2
pos = Vector2.from_xy_mm(100, 50)
size = Vector2.from_xy_mm(40, 30)
sheet_id = sch.sheets.add_sheet(
filename="power_supply.kicad_sch",
position=pos,
size=size,
name="Power Supply"
)
create_sheet_from_selection()
Creates a new sheet from selected items.
# First select items in the editor
sch.sheets.create_sheet_from_selection(
filename="new_sheet.kicad_sch",
name="Sub Circuit"
)
Navigating Sheets
get_sheets()
Gets all sheets in the current schematic.
sheets = sch.sheets.get_sheets()
for sheet in sheets:
print(f"{sheet.name}: {sheet.filename}")
enter_sheet(sheet_id)
Enters a hierarchical sheet.
sch.sheets.enter_sheet(sheet_id)
leave_sheet()
Returns to the parent sheet.
sch.sheets.leave_sheet()
get_current_sheet()
Gets the current sheet path.
path = sch.sheets.get_current_sheet()
print(f"Current sheet: {path}")
Sheet Properties
get_sheet_info(sheet_id)
Gets information about a sheet.
info = sch.sheets.get_sheet_info(sheet_id)
print(f"Name: {info.name}")
print(f"Filename: {info.filename}")
print(f"Size: {info.width} x {info.height}")
print(f"Position: {info.position}")
set_sheet_name(sheet_id, name)
Changes the sheet name.
sch.sheets.set_sheet_name(sheet_id, "Analog Input")
resize_sheet(sheet_id, size)
Resizes a sheet symbol.
new_size = Vector2.from_xy_mm(60, 40)
sch.sheets.resize_sheet(sheet_id, new_size)
Sheet Pins
add_sheet_pin(sheet_id, name, position, direction)
Adds a hierarchical pin to a sheet.
pin_pos = Vector2.from_xy_mm(100, 60) # Relative to sheet position
sch.sheets.add_sheet_pin(
sheet_id,
name="VCC",
position=pin_pos,
direction="input"
)
get_sheet_pins(sheet_id)
Gets all pins on a sheet.
pins = sch.sheets.get_sheet_pins(sheet_id)
for pin in pins:
print(f"Pin {pin.name}: {pin.direction}")
delete_sheet_pin(sheet_id, pin_name)
Removes a sheet pin.
sch.sheets.delete_sheet_pin(sheet_id, "VCC")
Example: Create Hierarchical Design
from kipy import KiCad
from kipy.geometry import Vector2
kicad = KiCad()
sch = kicad.get_schematic()
# Create sheet symbols on the main page
power_sheet = sch.sheets.add_sheet(
filename="power.kicad_sch",
position=Vector2.from_xy_mm(20, 20),
size=Vector2.from_xy_mm(40, 30),
name="Power Supply"
)
logic_sheet = sch.sheets.add_sheet(
filename="logic.kicad_sch",
position=Vector2.from_xy_mm(80, 20),
size=Vector2.from_xy_mm(40, 30),
name="Digital Logic"
)
# Add pins to power sheet
sch.sheets.add_sheet_pin(power_sheet, "VCC", Vector2.from_xy_mm(40, 10), "output")
sch.sheets.add_sheet_pin(power_sheet, "GND", Vector2.from_xy_mm(40, 20), "output")
# Add pins to logic sheet
sch.sheets.add_sheet_pin(logic_sheet, "VCC", Vector2.from_xy_mm(0, 10), "input")
sch.sheets.add_sheet_pin(logic_sheet, "GND", Vector2.from_xy_mm(0, 20), "input")
# Wire power to logic
power_vcc = Vector2.from_xy_mm(60, 30)
logic_vcc = Vector2.from_xy_mm(80, 30)
sch.wiring.add_wire([power_vcc, logic_vcc])
Example: Iterate Through All Sheets
def process_all_sheets(sch, callback):
"""Process all sheets recursively."""
sheets = sch.sheets.get_sheets()
for sheet in sheets:
# Process current sheet
callback(sch)
# Enter child sheet
sch.sheets.enter_sheet(sheet.id)
# Recursively process children
process_all_sheets(sch, callback)
# Return to parent
sch.sheets.leave_sheet()
def count_symbols(sch):
symbols = sch.symbols.get_all()
print(f"Sheet {sch.sheets.get_current_sheet()}: {len(symbols)} symbols")
# Count symbols in all sheets
process_all_sheets(sch, count_symbols)