API Reference¶
Core Drawing Module¶
Module for generating traces for KiCad PCB.
- class kicad_draw.PCBmodule.PCBdraw(stackup: Literal['default_4layer', 'default_6layer'], mode: Literal['print', 'file'] = 'print', visualizer: PCBVisualizer | None = None, enable_visualization: bool = True)¶
Module for generating traces for KiCad PCB.
- disable_visualization() None¶
Disable SVG visualization to save memory.
- draw_helix(params: HelixParams) None¶
Draw helix coil pattern.
- Parameters:
params – HelixParams object containing all parameters
- draw_helix_rectangle(params: HelixRectangleParams) None¶
Draw a rectangle with rounded corners for each layer in layer_index_list.
If port_gap > 0, creates ports (gaps) on the right side of the rectangle with tabs extending outward for layer connections.
- draw_polyline_arc(x0: float, y0: float, radius: float, port_angle: float, layer_index: int, net_number: int, line_width: float, angle_offset: float = 0, segment_number: int = 100) None¶
Draw arc-shaped conductive trace.
- draw_via(x: float, y: float, via_size: float, drill_size: float, net_number: int, layer_index_1: int, layer_index_2: int) None¶
Draw via.
- drawline(x1: float, y1: float, x2: float, y2: float, line_width: float, net_number: int, layer_index: int) None¶
Draw linear conductive trace.
- enable_visualization(width: float = 800, height: float = 600) None¶
Enable SVG visualization.
- Parameters:
width – SVG canvas width in pixels
height – SVG canvas height in pixels
- export() str¶
Export PCB elements as KiCad s-expressions string.
- Returns:
String containing all KiCad s-expressions
- get_available_layers() List[str]¶
Get list of all layers that have elements.
- Returns:
List of layer names, or empty list if visualization not enabled
- get_svg() str¶
Get SVG string of current visualization.
- Returns:
SVG string, or empty string if visualization not enabled
- get_visible_layers() List[str]¶
Get list of currently visible layers.
- Returns:
List of visible layer names, or empty list if visualization not enabled
- hide_all_layers() None¶
Hide all layers in visualization.
- hide_layer(layer: str) None¶
Hide a specific layer in visualization.
- Parameters:
layer – Layer name (e.g., “F.Cu”, “In1.Cu”, “B.Cu”)
- open_pcbfile(path)¶
Open pcb file (not used yet).
- save(output_path: str, template_path: str = 'asset.kicad_pcb') None¶
Save PCB elements to a KiCad PCB file using a template.
This method only works when in file mode.
- save_svg(filename: str) None¶
Save current visualization as SVG file.
- Parameters:
filename – Output SVG filename
- set_mode(mode: Literal['print', 'file']) None¶
Switch between print and file modes.
- Parameters:
mode – “print” for direct s-expression output, “file” for collecting elements
- set_via_visibility(visible: bool) None¶
Control via visibility in visualization.
- Parameters:
visible – True to show vias, False to hide them
- show_all_layers() None¶
Show all layers in visualization.
- show_layer(layer: str) None¶
Show a specific layer in visualization.
- Parameters:
layer – Layer name (e.g., “F.Cu”, “In1.Cu”, “B.Cu”)
- show_only_layer(layer: str) None¶
Show only the specified layer, hide all others.
- Parameters:
layer – Layer name to show exclusively
- show_svg() None¶
Display SVG in Jupyter notebook or print SVG string.
- toggle_layer(layer: str) bool¶
Toggle layer visibility in visualization.
- Parameters:
layer – Layer name (e.g., “F.Cu”, “In1.Cu”, “B.Cu”)
- Returns:
True if layer is now visible, False if hidden
- toggle_vias() bool¶
Toggle via visibility in visualization.
- Returns:
True if vias are now visible, False if hidden
- visualize(visible_layers: List[int] | None = None, show_vias: bool = True, width: float = 800, height: float = 600) str¶
Create SVG visualization of the PCB.
This method can visualize PCB elements whether they were captured during drawing or need to be reconstructed from exported s-expressions.
- Parameters:
visible_layers – List of layer indices to show (0=F.Cu, 1=In1.Cu, etc.). If None, show all.
show_vias – Whether to show vias
width – SVG canvas width in pixels
height – SVG canvas height in pixels
- Returns:
SVG string
Parameter Models¶
Data models for KiCad PCB drawing parameters.
This module contains Pydantic models that define the parameters for various PCB drawing operations, ensuring type safety and validation.
- class kicad_draw.models.HelixParams(*, x0: float, y0: float, radius: float, port_gap: float, tab_gap: float, angle_step: float, layer_index_list: List[int], track_width: float, connect_width: float, drill_size: float, via_size: float, net_number: int, tab_position: Literal['IN', 'OUT'] = 'OUT', base_angle_offset: float = 0, segment_number: int = 100)¶
Parameters for drawing circular helix coil patterns.
This model defines all the geometric and electrical parameters needed to generate a circular helix coil on a PCB, including center position, radius, layer configuration, and connection details.
- x0¶
X-coordinate of the helix center (mm)
- Type:
float
- y0¶
Y-coordinate of the helix center (mm)
- Type:
float
- radius¶
Radius of the circular helix (mm)
- Type:
float
- port_gap¶
Gap size for ports (mm)
- Type:
float
- tab_gap¶
Extension distance for connection tabs (mm)
- Type:
float
- angle_step¶
Angular step between layers (radians)
- Type:
float
- layer_index_list¶
List of layer indices for the helix (0=F.Cu, 1=In1.Cu, etc.)
- Type:
List[int]
- track_width¶
Width of the main coil traces (mm)
- Type:
float
- connect_width¶
Width of the connection traces between layers (mm)
- Type:
float
- drill_size¶
Diameter of via drill holes (mm)
- Type:
float
- via_size¶
Outer diameter of vias (mm)
- Type:
float
- net_number¶
KiCad net number for electrical connectivity
- Type:
int
- tab_position¶
Position of connection tabs (“IN” or “OUT”, default: “OUT”)
- Type:
Literal[‘IN’, ‘OUT’]
- base_angle_offset¶
Base angular offset for the entire helix (radians, default: 0)
- Type:
float
- segment_number¶
Number of segments for curved sections (default: 100)
- Type:
int
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class kicad_draw.models.HelixRectangleParams(*, x0: float, y0: float, width: float, height: float, corner_radius: float, layer_index_list: List[int], track_width: float, connect_width: float, drill_size: float, via_size: float, net_number: int, segment_number: int = 100, port_gap: float = 0.0, tab_gap: float = 0.0)¶
Parameters for drawing rectangular helix coil patterns.
This model defines all the geometric and electrical parameters needed to generate a rectangular helix coil on a PCB, including dimensions, layer configuration, and connection details.
- x0¶
X-coordinate of the rectangle center (mm)
- Type:
float
- y0¶
Y-coordinate of the rectangle center (mm)
- Type:
float
- width¶
Width of the rectangle (mm)
- Type:
float
- height¶
Height of the rectangle (mm)
- Type:
float
- corner_radius¶
Radius for rounded corners (mm)
- Type:
float
- layer_index_list¶
List of layer indices for the helix (0=F.Cu, 1=In1.Cu, etc.)
- Type:
List[int]
- track_width¶
Width of the main coil traces (mm)
- Type:
float
- connect_width¶
Width of the connection traces between layers (mm)
- Type:
float
- drill_size¶
Diameter of via drill holes (mm)
- Type:
float
- via_size¶
Outer diameter of vias (mm)
- Type:
float
- net_number¶
KiCad net number for electrical connectivity
- Type:
int
- segment_number¶
Number of segments for curved sections (default: 100)
- Type:
int
- port_gap¶
Gap size for ports in mm (0 means no ports, default: 0.0)
- Type:
float
- tab_gap¶
Extension distance for connection tabs in mm (default: 0.0)
- Type:
float
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
Visualization¶
SVG-based visualization for PCB patterns.
- class kicad_draw.visualizer.PCBVisualizer(width: float = 800, height: float = 600)¶
SVG-based visualizer for PCB patterns.
- add_arc(center_x: float, center_y: float, radius: float, start_angle: float, end_angle: float, width: float, layer: str, segments: int = 32) None¶
Add an arc by converting to line segments.
- add_line(x1: float, y1: float, x2: float, y2: float, width: float, layer: str) None¶
Add a line element.
- add_via(x: float, y: float, size: float) None¶
Add a via element.
- clear() None¶
Clear all elements.
- generate_svg(layer_order: List[str] | None = None) str¶
Generate SVG string.
- get_available_layers() List[str]¶
Get list of all layers that have elements.
- Returns:
List of layer names that have elements
- get_visible_layers() List[str]¶
Get list of currently visible layers.
- Returns:
List of visible layer names
- hide_all_layers() None¶
Hide all layers.
- hide_layer(layer: str) None¶
Hide a layer.
- Parameters:
layer – Layer name (e.g., “F.Cu”, “In1.Cu”, “B.Cu”)
- save_svg(filename: str) None¶
Save SVG to file.
- set_via_visibility(visible: bool) None¶
Control via visibility.
- Parameters:
visible – True to show vias, False to hide them
- show_all_layers() None¶
Show all layers that have elements.
- show_layer(layer: str) None¶
Make a layer visible.
- Parameters:
layer – Layer name (e.g., “F.Cu”, “In1.Cu”, “B.Cu”)
- show_only_layer(layer: str) None¶
Show only the specified layer, hide all others.
- Parameters:
layer – Layer name to show exclusively
- toggle_layer(layer: str) bool¶
Toggle layer visibility.
- Parameters:
layer – Layer name (e.g., “F.Cu”, “In1.Cu”, “B.Cu”)
- Returns:
True if layer is now visible, False if hidden
- toggle_vias() bool¶
Toggle via visibility.
- Returns:
True if vias are now visible, False if hidden
Layer Management¶
Layer management for PCB drawing.
- class kicad_draw.layers.LayerManager(stackup: Literal['default_4layer', 'default_6layer'])¶
Manages PCB layers and stackups.
- get_layer_name(index: int) str¶
Get layer name by index.
- get_layer_names(indices: List[int]) List[str]¶
Get layer names by indices.
- validate_layer(index: int) bool¶
Check if layer index is valid.
- validate_layers(indices: List[int]) bool¶
Check if all layer indices are valid.
Geometry Classes¶
Geometric primitives for PCB drawing.
- class kicad_draw.geometry.Arc(center: Point, radius: float, start_angle: float, end_angle: float, width: float)¶
An arc defined by center, radius, and angles.
- class kicad_draw.geometry.Line(start: Point, end: Point, width: float)¶
A line segment defined by two points.
- class kicad_draw.geometry.Point(x: float, y: float)¶
A point in 2D space.
Formatting¶
KiCad output formatting.
Configuration¶
Configuration settings for KiCad PCB drawing.
This module contains default layer stackup configurations for common PCB types used in the drawing operations.