Skip to main content

Creating components

Component creation​

A new component is created by using the create component command and specifying the pins parameter.

my_component = create component:
pins: 2

at my_component
Component J1: line 1:11122J1

The code above creates a component object with 2 pins. The pins are automatically generated with a pin ID of 1 and 2 respectively.

Pin names can also be defined in the symbol object:

my_component = create component:
pins:
1: "anode"
2: "cathode"
at my_component
Component J1: line 1:1anode1cathode2J1

Properties during component creation​

The following properties may be declared during component creation:

NameDescription
pinsIf a single number is specified, the number of pins will be automatically created. Otherwise, pins with the given ID and names will be generated
typeType of component, this may be used to determine the refdes group of the component. Supported values: net, label, res, cap, ind, diode, conn
subtypeOptional string that refines type. Validated values are "label" for type: "net" (a net label), and "text", "table" and "no_connect" for type: "graphic"; these control how the component is treated by ERC and KiCad export. For other types the value is stored and otherwise ignored. It replaces the older is_label property and the text_object, table_object and no_connect params, which are now rejected with an error.
displaySpecify the graphic to display as the symbol of the component. If unspecified, a default rectangular symbol is generated.
arrangeIf display is not specified, then the arrangement of pins must be specified.
copyIf this is set to true, whenever this component is referenced, a copy of the component will be created and used instead.
refdes_prefixCustom reference designator prefix for the component (e.g., "U", "IC", "X"). Overrides the default prefix determined by the component type.
follow_wire_orientationOptional boolean, default true. When true, the component is automatically rotated to follow the direction of the wire it is connected to. Set to false to keep the component at its default orientation. Script-facing option names use snake_case (for example font_size, port_type, flipx, flipy).
paramsA map that specifies parameters of the component. This may be changed after component creation. Please see this page for more information. Built-in parameters such as place and bom_exclude (omits the component from the BOM and sets KiCad in_bom to no, see BOM generation) can be set here or after creation.
erc_net_bridgeOptional boolean, default false. Only valid on components with exactly 2 pins. Treats the component's two pins as electrically joined for ERC purposes only, without affecting layout, rendering, or export. Like params, this may be changed after component creation (e.g. R1.erc_net_bridge = true). See Bridging nets for ERC for more information.

Component graphical symbol​

By default, the graphical symbol for the component is generated automatically. Pins are arranged in a zig-zag arrangement.

Default graphical symbol​

my_component = create component:
pins: 10

at my_component # need at least an `at` command to display the component in the schematic.
Component J1: line 1:11133557799224466881010J1

Specifying pin positions​

The position of each component pin can be specified using the arrange parameter. Specify which pins are to be placed on the left, right, top, and bottom sides of the component. Each entry can reference a pin by its ID or by its name, and IDs, names, and blank-slot markers can be mixed freely in the same list.

my_component = create component:
pins: 10
arrange:
left: 1, 2, 3, 4
right: 5, 6, 7, 8, 9, 10
at my_component
Component J1: line 1:11122334455667788991010J1

Pins can also be arranged by name instead of by ID:

u1 = create component:
pins:
1: "VCC"
2: "GND"
3: "IN"
4: "OUT"
width: 300
height: 300
arrange:
left: "IN"
right: "OUT"
top: "VCC"
bottom: "GND"
at u1
Component J1: line 1:1IN3OUT4VCC1GND2J1

If an entry does not match either a pin ID or a pin name, this is a hard error (Could not find pin ...) and the render fails. If the same pin is specified more than once in the arrange property — whether by repeating the same ID, the same name, or referencing the same pin by both its ID and its name — this produces a warning (Warning: Pin ... specified more than once in arrange property) rather than an error, and the render still succeeds.

Blank pin positions​

To add spaces between pins, use the blank placeholder [<number] to specify a blank pin position.

my_component = create component:
pins: 10
arrange:
left: [1], 1, [2], 2, 3
right: 4, 5, 6, 7, 8, 9, 10
at my_component
Component J1: line 1:11122334455667788991010J1

Component pin types​

The pin type of each component can be specified to provide more information about the pin. The following pin types are supported:

Pin TypeString LiteralDescription
Passive"passive"Default type; no drive capability, connects freely (resistors, capacitors)
Any"any"Matches any other pin type; no connectivity restrictions
Input"input"Signal input; consumes a driven signal
Output"output"Signal output; drives a signal onto the net
IO"io"Bidirectional signal pin (input or output)
HiZ"hiz"High-impedance; can be tristated, does not actively drive
OpenCollector"open_collector"Open-collector/open-drain; pulls low, needs external pull-up
OpenEmitter"open_emitter"Open-emitter; pulls high, needs external pull-down
Power"power"Power net declarations (i.e. supply symbols, VCC, VDD)
PowerReference"power_reference"Reference potential net (e.g. GND symbol, component GND pins)
PowerInput"power_input"Consumes from a voltage rail (component VCC/VDD pins)
PowerOutput"power_output"Sources/drives a voltage rail (regulator output)
NoConnect"no_connect"Explicitly unconnected; suppresses ERC unconnected-pin warnings

Example:

my_component = create component:
pins:
1: "IN", "input"
2: "OUT", "output"
3: "VCC", "power_input"
4: "GND", "power_reference"

at my_component
Component J1: line 1:1IN1VCC3OUT2GND4J1

The above component created has two power pins, one for pin 3 (VCC) and pin 4 (GND). Pin 1 is a signal input pin and pin 2 is a signal output pin.

Drawing custom component graphics​

Custom component graphics is drawn using the create graphic command. Specfic drawing commands can be used to draw the component body and also define the placement of component pins.

width = 100
height = 100

my_component = create component:
pins: 2
display: create graphic(params):
triangle: -width/2, 0, width/2, 0, height
vline: width/2, -height/2, height
hpin: 1, -width/2-100, 0, 100 # anode
hpin: 2, width/2 + 100, 0, -100 # cathode
label: params.refdes, 0, -80, font_size=50, anchor="center", vanchor="bottom"

at my_component
Component J1: line 4:112J1

The syntax for drawing commands are as follows: <command>: <arg1>, <arg2>, ...

pin command​

pin: <pin ID: number>, <pin start x: number>, <pin start y: number>, <pin end x: number>, <pin end y: number>

The pin consists of the pin start point and the pin end point. The pin start point is where wires and other components connect to.

tip

If the pin type is not defined, the pin will have a pin type of "passive".

Example:

my_component = create component:
pins: 2
display: create graphic:
rect: -100, -50, 200, 100
pin: 1, -200, 0, -100, 0
pin: 2, 200, 0, 100, 0
path: ("M", 125, -50, "L", 200, -50,
"M", 150, -75, "L", 125, -50, "L", 150, -25)
path: ("M", -125, -50, "L", -200, -50,
"M", -150, -75, "L", -125, -50, "L", -150, -25)

at my_component pin 1
wire left 100

at my_component pin 2
wire right 100
Component J1: line 1:112

In the example above, the arrows indicate the direction of the pins. When building circuits, wires and other components are always connected at the start of the pin. In the example above, the wires are connected at side of the pin without the arrow-head.

pin command with pin name​

pin: <pin ID: number>, <pin name: string>, <pin type: string>, <pin start x: number>, <pin start y: number>, <pin end x: number>, <pin end y: number>

This command displays the pin name beside the pin and the pin name is placed at the end point of the pin.

Example:

my_component = create component:
pins: 2
display: create graphic:
rect: -100, -50, 200, 100
pin: 1, "A", "passive", -200, 0, -100, 0
pin: 2, "B", "passive", 200, 0, 100, 0
path: "M", 125, -50, "L", 200, -50, \
"M", 150, -75, "L", 125, -50, "L", 150, -25
path: "M", -125, -50, "L", -200, -50, \
"M", -150, -75, "L", -125, -50, "L", -150, -25

at my_component pin 1
wire left 100

at my_component pin 2
wire right 200
Component J1: line 1:1A1B2

In the above example, the pin name is displayed beside the pins and the pins are defined with a type of "passive".

Additional properties:​

  • Set display_id=false to skip drawing of pinId beside the pin
  • Set display_name=false to skip drawing of name beside the pin

hpin command​

This command is a simplification of the pin command and is used to draw horizontal pins.

hpin: <pin id>, <pin start x: number>, <pin start y: number>, <pin x length>

With pin name and type:

hpin: <pin id>, <pin name: string>, <pin type: string>, <pin start x: number>, <pin start y: number>, <pin x length>

See this table for the possible pin types.

vpin command​

This command is a simplification of the pin command and is used to draw vertical pins.

vpin: <pin id>, <pin start x: number>, <pin start y: number>, <pin y length>`

With pin name and type:

vpin: <pin id>, <pin name: string>, <pin type: string>, <pin start x: number>, <pin start y: number>, <pin y length>`

See this table for the possible pin types.

rect command​

Draws a rectangle by specifying the top-left coordinate of the rectangle, followed by the width and height.

rect: <x: number>, <y: number>, <width: number>, <height: number>

crect command​

Draws a rectangle by specifying the center of the rectangle, followed by the width and height.

crect: <center x: number>, <center y: number>, <width: number>, <height: number>

label command​

label: <text: string>, <x: number>, <y: number>, <...named parameters>

Currently, only the following named parameters are accepted:

  • font_size: number
  • anchor: One of "left", "center", "right",
  • vanchor: One of "top", "center", "bottom"

hline command​

Draws a horizontal line of a given length starting from the point.

hline: <x: number>, <y: number>, <length: number>

vline command​

Draws a vertical line of a given length starting from the specified point.

vline: <x: number>, <y: number>, <length: number>

arc command​

Draws a section of a circle at a given location and radius. The section is drawn in a clockwise direction from the starting angle to the ending angle.

arc: <center x>, <center y>, <radius>, <starting angle>, <ending angle>

path command​

path: <... array of drawing instructions>

The path command is similar to path in the SVG spec.

Example:

path: "M", 0, 0, "L", 10, 10

This path command moves the drawing cursor to (0, 0) and draws a line until (10, 10)

path: "M", 100, 100, "L", 200, 200, "L", 200, 100

This path commands starts at (100, 100), draws a line until (200, 200) and a further line until (200, 100)

At the moment, only the "M" and "L" instructions are supported.

fill command​

Sets the current fill color used for drawing closed shapes (circles, rectangles, paths, etc.)

text_color command​

Sets the current text color used for drawing text

line_color command​

Sets the current line color used for drawing line

line_width command​

Sets the current line width used for drawing lines

table command​

Draws a grid with text in each cell. The table is drawn with hline, vline and text commands, so it uses the current fill, line_color and line_width settings.

table: <x: number>, <y: number>, col_widths=<list of numbers>, <...named parameters>
header: <cell>, <cell>, ...
row: <cell>, <cell>, ...

x and y are the top-left corner of the table, and rows run downward from there. The following named parameters are accepted:

NameDefaultDescription
col_widthsrequiredList of column widths
row_height100Height of every row
padding20Cell inset for left and right anchored text
font_size, bold, italic, anchor, vanchor, text_coloranchor="left", vanchor="center"Default text style for the cells

row: takes one value per cell and accepts the same text style keywords (bold, italic, font_size, anchor, vanchor, text_color) to override the table style for that row. header: is a row: that is bold by default. Only row:, header: and markdown: are allowed in the table body, and they can be wrapped in for and if. A row with fewer cells than columns leaves the remaining cells blank; more cells than columns is an error.

The rows can also be written as a markdown table with markdown:. The first row is the header, and the separator row sets the column alignment (:--- left, :---: center, ---: right). {name} in a cell is replaced by the graphic's parameter of that name, or by a variable in scope; unknown names are left unchanged.

notes = create graphic:
table: 0, 0, col_widths=[200, 300], row_height=60, font_size=30
header: "Ref", "Notes"
row: "R1", "Pull-up"
row: "C1", "Decoupling"

notes_md = create graphic:
table: 0, 0, col_widths=[200, 300], row_height=60, font_size=30
markdown: "
| Ref | Notes |
|:--- | ---:|
| R1 | Pull-up |
| C1 | Decoupling |
"

at notes
at notes_md
line 16:1RefNotesR1Pull-upC1Decouplingline 17:1RefNotesR1Pull-upC1Decoupling

Mark the component with subtype: "table" to export the table to KiCad as sheet-level lines and text instead of a symbol.

Conditionals and the pre: block in graphics​

Graphic blocks support if / else if / else, where the condition can use values from params. Only the graphic commands in the branch that is taken are drawn.

A pre: block can be placed at the start of a graphic block to compute values first. It accepts assignments, function calls and control flow, and the variables it defines are visible to the graphic commands that follow it in the same block.

my_component = create component:
pins: 2
params:
rounded: true
size: 100
display: create graphic(params):
pre:
half = params.size / 2
offset = half + 50

rect: -half, -half, params.size, params.size
hpin: 1, -offset - 100, 0, 150
hpin: 2, offset + 100, 0, -150

if params.rounded:
crect: 0, 0, 40, 40
else:
hline: -20, 0, 40

at my_component
Component J1: line 1:112

Undefined symbols used inside graphic bodies are reported when the script is validated.

Graphics as components​

A graphic created with create graphic can be used directly in at, add and to, without wrapping it in create component. It is converted into a single-pin graphic component with the pin at the origin (0, 0), unless the graphic already defines a pin. Using the same graphic variable again reuses the same component.

from "std" import *

probe = create graphic:
crect: 0, 0, 40, 40
label: "PROBE", 40, 0, font_size=30, vanchor="center"

v5 = supply("5V")
gnd = dgnd()

at v5
wire down 100
add res(10k)
wire down 100
branch:
wire right 100
add probe
wire down 100
add res(1k)
wire down 100
to gnd
line 10:15VComponent R1: line 12:1R110kline 16:5PROBEComponent R2: line 18:1R21kline 20:4GND

Two extra commands are accepted in a graphic block for this use:

  • subtype: "<value>" sets the component subtype ("text", "table" or "no_connect").
  • params: sets the component parameters. follow_wire_orientation: false keeps the graphic upright, and skip_default_pin: true stops the implicit pin from being added.
my_note = create graphic:
subtype: "text"
text:
content: "Fit for 5V variant only"
offset: 0, 0
font_size: 40
params:
follow_wire_orientation: false

The standard library text(), marker_point() and no_connect() functions are written this way.

Light/dark theming for fill, text_color, line_color​

Each of fill, text_color and line_color accepts a single color:

line_color: "#333333"

or a light/dark pair, given as a dark= keyword argument alongside the light color:

line_color: "#cccccc", dark="#333333"

This renders as an inline CSS light-dark(#cccccc, #333333) value, so the color automatically follows the viewer's light/dark theme when the SVG is opened in a browser.

An optional var=<name> argument additionally exposes the color as a named CSS custom property, instead of an inline value:

line_color: "#cccccc", dark="#333333", var="my-line-color"

This compiles the element's color to var(--cs-my-line-color), and adds a declaration to the document's :root block:

:root {
--cs-my-line-color: light-dark(#cccccc, #333333);
}

Anything consuming the exported SVG can then override the color for that document by setting --cs-my-line-color on the SVG (or an ancestor element) in its own stylesheet - without needing to touch the individual elements that use it:

svg {
--cs-my-line-color: #ff0000;
}

var= names share the --cs- prefix with Circuitscript's own built-in theme variables (--cs-line-color, --cs-text-color, --cs-wire-color, etc.), so a name that collides with one of those is rejected - pick a name that isn't one of the built-in color roles.

Note that PDF and KiCad export have no concept of light/dark themes: both always resolve a themed color down to its light value.