Skip to main content

Annotation

This assigns reference designators (refdes) to components on the schematic. These refdes are important for ensuring that the link between schematic and PCB is maintained.

For new schematics that have not started PCB layout, the refdes assignment is not critical. However, once the PCB layout is started, it is very important to maintain the link between schematic components and PCB footprints.

When new components are added into an existing circuit, it might cause the refdes of exisiting components to be changed. This is not a bug, but how Circuitscript works.

In this example below, the 1k resistor is initially annotated with a refdes of R1.

from "std" import *

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

# Original circuit
at v5
wire down 100
add res(1k)
wire down 100
to gnd
line 7:15VComponent R1: line 9:1R11kline 11:4GND

When a new circuit is added this circuit, due to the way Circuitscript runs the code, the 1k resistor is now annotated with a refdes of R2.

from "std" import *

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

# Added circuit
at v5
wire down 100
add res(2.1k)
wire down 100
to gnd

# Original circuit
at v5
wire down 100
add res(1k)
wire down 100
to gnd
line 7:15VComponent R1: line 9:1R12.1kline 11:4GNDline 14:15VComponent R2: line 16:1R21kline 18:4GND

Maintaining stable refdes​

Stable refdes refers to components maintaining their assigned refdes even though there are code changes. This ensures that the correct component links to the PCB are maintained and do not cause a mismatch.

Refdes comments​

Circuitscript manages refdes annotation using a special comment symbol: #=. In circuitscript, the # symbol denotes the start of a comment and #= provides a refdes hint to the circuitscript interpreter through comments. When the interpreter encounters this symbol, it will extract the refdes information from the comment and apply it to the active component.

Besides stating clearly the refdes of the component, another benefit of refdes comments are that they will "follow" the code if it is copied/moved to another location. This ensures that the refdes remains stable for a given circuit.

To assign another refdes, the refdes comment can be removed or specifically set to another value.

In the example below, the 1k resistor is assigned a refdes of R99 because of the #= R99 comment annotation.

from "std" import *

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

# Added circuit
at v5
wire down 100
add res(2.1k)
wire down 100
to gnd

# Original circuit
at v5
wire down 100
add res(1k) #= R99
wire down 100
to gnd
line 7:15VComponent R1: line 9:1R12.1kline 11:4GNDline 14:15VComponent R99: line 16:1R991kline 18:4GND

For circuits that have loops or are within functions, the comment annotation must have a underscore character at the end.

from "std" import *

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

for i in range(0, 5):
at v5
wire down 100
branch:
wire right 200 down 100
add cap(1n) #= C23_
wire down 100
to gnd

wire down 100
add res(1k) #= R65_
wire down 100
to gnd
line 7:55VComponent C23_1: line 11:9C23_11nline 13:12GNDComponent R65_1: line 16:5R65_11kline 18:8GNDline 7:55VComponent C23_2: line 11:9C23_21nline 13:12GNDComponent R65_2: line 16:5R65_21kline 18:8GNDline 7:55VComponent C23_3: line 11:9C23_31nline 13:12GNDComponent R65_3: line 16:5R65_31kline 18:8GNDline 7:55VComponent C23_4: line 11:9C23_41nline 13:12GNDComponent R65_4: line 16:5R65_41kline 18:8GNDline 7:55VComponent C23_5: line 11:9C23_51nline 13:12GNDComponent R65_5: line 16:5R65_51kline 18:8GND

A placeholder refdes (ending with _) on a component that is not created inside a loop or function is treated as a normal refdes, and a warning suggests the corrected comment:

Warning: Placeholder refdes 'R5_' should be a normal refdes. Change it to 'R5' at 2:1-2:10

When a component is created inline with create component: in an at, add or to command, the refdes comment goes after the colon:

at create component: #= J1
pins: 4
wire right 100

Refdes clashes​

Annotation stops with an error that includes the source locations when:

  • the same explicit refdes is used twice, for example Duplicate refdes 'R1': first used at line 2, again at line 4,
  • the same placeholder refdes is used in more than one function,
  • a placeholder refdes conflicts with an explicit refdes.

Alternative refdes assignment​

Besides the refdes comments, the refdes can be manually assigned using the refdes parameter. In this example below, the 1k resistor is defined with a fixed refdes of "R1".

from "std" import *

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

# Added circuit
at v5
wire down 100
add res(2.1k)
wire down 100
to gnd

# Original circuit
at v5
wire down 100
add res(1k)
..refdes = "R1"
wire down 100
to gnd
line 7:15VComponent R2: line 9:1R22.1kline 11:4GNDline 14:15VComponent R1: line 16:1R11kline 19:4GND
tip

Refdes comments are preferred, since they are more flexible and are not directly "hardcoded" into the circuit itself. When a circuit is reused in a new design, a different refdes might be assigned.

Refdes assignment modes​

Components created inside loops and functions are numbered according to the refdes assignment mode:

ModeDescriptionExample
indexedDefault. Each loop or function gets a base refdes and every instance gets an index. Nested loops add one index per level.R1_1_1, R1_1_2, R1_2_1, R1_2_2
indexed-flatEach loop or function site gets a base refdes and a single running index, even when loops are nested.R1_1, R1_2, R1_3, R1_4
flatNo indexes. Components get plain running numbers.R1, R2, R3, R4

The examples above are for one resistor created in two nested loops of two iterations each. For a single loop, indexed and indexed-flat give the same result.

The mode can be set in the script:

document.refdes_assignment = "indexed-flat"

or on the command line with --refdes-mode <mode>, which takes precedence over the script setting. --no-indexed-refdes is a shorthand for --refdes-mode flat. See the CLI reference.

warning

In flat mode, refdes comments are not written for components created in loops and functions, and their refdes may change between runs.

Component namespaces​

When the same function is called several times, a component namespace can be added to the refdes of the components created by each call. This is requested with an @ decorator before the call; see Namespaces for the full syntax.

@ amplifier()         # component namespace only
@/ch1@ amplifier() # net namespace "ch1" and a component namespace

The namespace becomes a prefix of the refdes. Without an explicit name, namespaces are generated as A1, A2, ... so the resistor in the first call becomes A1R1_1 and the one in the second call A2R1_2. The annotation comment on the call sets the namespace explicitly, using a leading underscore:

@ amplifier() #= _U5

Here the components created by this call get refdes such as U5R1_1. With -u or -j, generated namespaces are written back into the source as #= _A1 comments so they stay stable. An annotation comment on a call without a component namespace decorator (for example @/ch1 amplifier() #= _A1) is ignored with a warning.

Automatic refdes comments​

Using the CLI tool​

The refdes comments can be automatically added into the source files by using the -u parameter in the Circuitscript CLI tool.

Imported libraries​

By default, refdes comments are not added into imported libraries. To add these refdes comments, the #= annotate keyword must be added to the end of the import. This ensures that imported libraries are not modified unless clearly intended.

Example:

from "std" import *
from "my_library" import my_component #= annotate

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

tmp = my_component()

at tmp pin 1
wire left 100 up 100
to v5

at tmp pin 6
wire right 100 down 100
to gnd

Shared libraries​

For imported libraries that may be shared with other designs, refdes comments should not be added within the library code itself. In Circuitscript, these are also known as external libraries.

Please specify the #= annotate-external keyword at the end of the import statement and an external <projectName>.refdes.json file is created with the refdes annotations for all external libraries that are used in the project.

warning

Only use #= annotation-external for libraries that do not change! If the library code is changed, it may cause refdes assignment issues as the <projectName>.refdes.json file no longer accurately reflects the refdes information of the external library in the given codebase.