netgraph

package
v0.11.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package netgraph assembles a pin-level netlist from schematic geometry. Both the xschem and gEDA readers reduce their format to the same neutral inputs on an integer grid. The inputs are wire segments, each optionally with a net name, named net anchors such as power taps and port or label markers, and component pin placements. netgraph unions everything that shares a grid point, names each resulting net, and reports which component pins sit on it.

It is format-agnostic and coordinate-agnostic. The caller quantizes its native units to an integer grid so that endpoints meant to touch compare equal. Two connection points at the same grid point are the same node; a wire additionally unions its two endpoints.

Index

Constants

View Source
const (
	// AttrPowerDriven marks a net asserted as fed (a PWR_FLAG or equivalent directive).
	AttrPowerDriven = "power_driven"
	// AttrNoConnect marks a net where a pin carries a no-connect flag. The flag is the designer's
	// own statement that the open pin is deliberate (see Net.NoConnect).
	AttrNoConnect = "no_connect"
	// AttrExternal marks a net that continues into something the read did NOT cover, a by-name
	// connection (global label, power symbol) whose other ends may live in unread files. It is
	// about READ SCOPE, not sheet membership, so a net spanning ten sheets of a completely-read
	// design carries no marking.
	AttrExternal = "external"
	// AttrGlobal marks a named rail on a completely-read design (AttrExternal, resolved).
	AttrGlobal = "global"
	// AttrAliases carries every distinct label a net arrived with, when there is more than one,
	// as "rank:name" entries joined by the US separator (names contain commas and slashes, and
	// \x1f does not occur in EDA names). Rank is the label's Anchor.Rank, which for KiCad is 0
	// for design-wide (global label, power rail) and 1 for sheet-scoped. The naming pass
	// collapses aliases to one Net.Name, and the conflict rules read this to see what was
	// collapsed, such as duplicate labels and rival rail taps. Parse with ParseAliases.
	AttrAliases = "aliases"
	// AttrSheets lists the sheet instance ids a net touches, in sheet order, joined by the US
	// separator (a KiCad Sheetname may contain a comma). The solver stamps no other membership
	// fact (WS9-028). It stamps this one because a wireless single-pin net on a sub-sheet has no
	// wire geometry to join, so the finding-panel sheet badge has no other source. Only a
	// multi-sheet read populates it, from the hierarchy walk's sheet bands. Parse with
	// ParseSheets.
	AttrSheets = "sheets"
	// AttrSheetGroups splits a net's aliases by where they met (Net.SheetGroups). The RS byte joins
	// the groups. Each group is its sheet followed by its names, joined by the US byte. It is
	// stamped only on a net whose names fall into two groups or more. Its absence means every alias
	// met in one place, and a reader solving one sheet always produces that. Parse with
	// ParseSheetGroups.
	AttrSheetGroups = "sheet_groups"
)

Net fact attributes are ir.Net.attributes keys that this file emits and resolves. The check layer reads them as FACTS. They live in the open attributes map rather than in typed Net fields, because C9 admits a typed field only once a second format populates it (WS3-004 designs the typed fact base). The constants keep producers and consumers typo-safe without closing the schema.

Variables

This section is empty.

Functions

func Build

func Build(wires []Wire, anchors []Anchor, pins []Pin, terminals []Point, opts ...Options) ([]Net, []Dangle, map[string]NetRef)

Build assembles nets from schematic geometry in two stages. First it unions connection points by grid coincidence, and a wire also unions its two endpoints. That gives geometric nodes. Then it unions nodes by EVERY label they carry. Schematic net labels and power taps connect by name across the whole sheet, not only where wires physically touch, so every "0" ground tap is one net regardless of geometry. A node carrying two labels aliases them into one net. The KiCad hierarchy walk uses this to stitch a parent sheet pin to its child hierarchical label. The merged net's name is its lowest-ranked label (wire labels first, then Anchor.Rank, ties by input order). Unnamed geometric nodes are kept separate and get a synthetic "N$<n>" name numbered by first appearance; an unnamed, pinless node is dropped as drawing noise. Conns within a net are deduplicated and sorted.

It also returns the dangling endpoints (see Dangle), each a wire endpoint whose grid point holds nothing else (no pin, anchor, terminal, or second wire endpoint). A format marks a junction dot or a no-connect flag as a legitimate wire end and passes those points as terminals. Formats with neither pass nil. Dangles are sorted.

The third return maps each identified wire, keyed by Wire.Id, to the net of its endpoints. The geometry sidecar uses the map to draw a KiCad wire by net (WS1-022). Only wires with a native id populate it, and a wire whose node is drawing noise is omitted.

func BuildWithPoints

func BuildWithPoints(wires []Wire, anchors []Anchor, pins []Pin, terminals []Point, opts ...Options) ([]Net, []Dangle, map[string]NetRef, map[Point]NetRef)

BuildWithPoints is Build plus a point->net-name map covering every input point (both wire endpoints, each anchor, each pin). The KiCad hierarchy walk offsets each sheet instance into its own grid band (WS9-028) and uses this map to attribute each net to the sheets it touches. A point on drawing noise (unnamed, pinless) is omitted, as in the wire map.

func EncodeSheets

func EncodeSheets(ids []string) string

EncodeSheets joins sheet ids into an AttrSheets value, empty for none (the caller then stamps nothing). The hierarchy reader uses it so it writes the encoding ParseSheets reads.

func ExpandBusName

func ExpandBusName(name string) []string

ExpandBusName expands a RANGE bus name into its member signal names in WRITTEN order: `DATA[7:0]` -> [DATA7 DATA6 ... DATA0], `A[0:3]` -> [A0 A1 A2 A3], `AN[0..7]` -> [AN0 AN1 ... AN7]. The member name is the prefix concatenated with the index, matching KiCad's bus-member naming and the tap labels. It returns nil for a name that is not a range bus (a scalar net, or an alias-named bus whose members the caller supplies). Written order lets a diagram read bits as drawn.

func IRDangles

func IRDangles(ds []Dangle, src, idKind string) []*ir.DanglingEndpoint

IRDangles maps the solver's dangling endpoints onto the IR diagnostic. idKind names the wire id's format (e.g. "kicad-uuid"); it and the id are omitted for formats with no per-wire id.

func IRNets

func IRNets(nets []Net, src string) []*ir.Net

IRNets maps assembled nets onto the IR wire form, the one place a solver Net becomes an ir.Net. Driven, External and multiple Aliases surface as the power_driven, external and aliases attributes the check rules read, and a net with none of them carries no attributes. Nets with no connections are kept, since a labeled wire whose pins did not resolve still names a net. A reader whose source tool omits pinless nets filters before calling.

func IsBusName

func IsBusName(name string) bool

IsBusName reports whether name carries an index range in either dialect's spelling. The readers share it as their geometry-free bus detector.

func ParseSheets

func ParseSheets(v string) []string

ParseSheets decodes an AttrSheets value into its ordered sheet ids. Empty or absent decodes to nil, as for any single-sheet or non-hierarchical read.

func ResolveExternal

func ResolveExternal(d *ir.Design)

ResolveExternal downgrades external to global on every net of a completely-read design (WS1-017). Imagine a read that covered the whole design, such as a KiCad .kicad_pro whose root has no unread sub-sheets. The external marking is then stale, and it would keep rules such as decoupling-present off the rails they exist for. Global keeps "is a named rail" queryable instead. Each reader still makes the completeness judgment itself, since only the reader knows what its source references and what it opened. See docsite/content/architecture/net-solving.md#the-hierarchy-walk.

func StampNetIDs

func StampNetIDs(d *ir.Design)

StampNetIDs fills ir.Net.id (WS9) for every net that lacks one, hashing its connection set the same way the solver does (hashPairs). A reader that builds ir.Net directly, as EDIF does, gets the same per-instance identity a netgraph-based reader gets from IRNets. It is idempotent and leaves a set id alone. A pinless net keeps its empty id.

Types

type Alias

type Alias struct {
	Name string
	Rank int
}

Alias is one label a net carries, with the Anchor.Rank it arrived with (wire labels use the wire rank, below every anchor).

func ParseAliases

func ParseAliases(v string) []Alias

ParseAliases decodes an AttrAliases value back into the solver's alias list. Empty or absent values decode to nil (single-named nets carry no attribute).

type Anchor

type Anchor struct {
	At       Point
	Label    string
	Driver   bool
	External bool
	// Rank orders naming when a net carries several labels. The lowest-ranked label names the
	// net, ties keep input order, and a wire label beats every anchor. Single-sheet callers
	// leave it zero. The KiCad hierarchy walk ranks a bare global/power name (0) over an
	// instance-qualified local name (1) over a synthetic hier-join label (2), which exists only
	// to union a parent sheet pin with its child hierarchical label.
	Rank int
	// Sheet is the sheet instance the anchor sits on, as for Wire.Sheet.
	Sheet string
}

Anchor names the net passing through a grid point, such as a power/ground tap or a port/label marker. It contributes a name but no pin. Driver marks an anchor that asserts the net is driven without being a pin, such as a KiCad PWR_FLAG. It sets Net.Driven, so a rail fed only by a flag is not read as undriven. External marks an anchor that continues the net onto another sheet, such as a global or hierarchical label or a global power rail. It sets Net.External.

type Conn

type Conn struct{ Comp, Pin, Dir string }

Conn is a pin's membership in a net.

type Dangle

type Dangle struct {
	At     Point
	WireId string
}

Dangle is a wire endpoint that terminates on nothing (no pin, anchor/label, junction, or other wire endpoint shares its grid point). At is the endpoint and WireId the Wire.Id it belongs to, "" when unknown. A wire end can sit mid-span on another wire without a junction, as a T-tap missing its dot does. Dangle does not report that case, which is WS1-012 and needs point-on-segment geometry.

type Net

type Net struct {
	Name string
	// ID is a hash of the sorted connection set (see netID), independent of Name, so two
	// distinct nets sharing a Name get distinct IDs. It is EPHEMERAL, recomputed every solve
	// and stable only while connectivity is unchanged, so it is a within-load join key and never
	// a durable handle for saved work. Empty for a pinless net; consumers fall back to Name.
	ID       string
	Conns    []Conn
	Driven   bool
	External bool
	// NoConnect is true when a pin on the net carries the format's no-connect flag. The flag is
	// the designer saying the open pin is deliberate. A net's name never says so, because KiCad
	// names every unlabelled one-pin net "unconnected-(...)", flagged or not (agni issue 1019).
	NoConnect bool
	Aliases   []Alias
	// SheetGroups splits Aliases by where the names met. Each group holds the names that share one
	// sheet's wiring, with that sheet. A wire joins them there, or one name drawn twice on that
	// sheet does, before any other sheet joins them. SheetGroups is set only when the net's names
	// fall into two groups or more, and only a caller that sets Sheet can produce two. Two labels
	// on one wire form one group. A parent sheet can strap two hierarchical labels together, and
	// those form two groups (agni issue 1249). Nil means every alias met in one place. A caller
	// that sets no Sheet always gets nil.
	SheetGroups []SheetGroup
}

Net is one assembled net, a name and the pins on it, with Conns sorted. Driven is true when a driver anchor such as a PWR_FLAG sits on the net. External is true when the net continues onto another sheet. An absence-based rule, such as "nothing drives this" or "only inputs here", then knows its view is incomplete and can decline to fire. Aliases lists EVERY distinct label on the net, Name included, with its rank, in first-appearance order. The naming pass collapses them to one name, and naming-conflict rules read Aliases to see what it collapsed, such as taps spelled "+3V3" and "+3.3V" on one net. Aliases is empty for unnamed nets.

type NetRef

type NetRef struct {
	Name string
	ID   string
}

NetRef identifies a net by its display Name and its deterministic ID (see Net.ID). The wire and point maps give each wire or point the NetRef of the net it resolved to. The geometry sidecar can then draw a wire by name AND join a highlight to the specific net instance by id, even when two nets share a name.

type Options added in v0.2.2

type Options struct {
	// AlphaTies breaks a tie between labels of equal rank by name rather than input order, and
	// picks a pin-derived AutoName the same way, preferring a name that does not end in a pad
	// number. KiCad names nets this way, so a read that follows it agrees with KiCad's netlist.
	AlphaTies bool
}

Options adjusts how Build names nets. The zero value is the shared behaviour every format without its own naming rules uses.

type Pin

type Pin struct {
	At        Point
	Comp, Pin string
	NoConnect bool
	// Dir is the pin's electrical direction in the check vocabulary ("power_in",
	// "power_out", ...). Only a pin on a VIRTUAL component sets it, such as a KiCad power
	// symbol (WS1-014). A virtual component never enters Components, so no part-type pin
	// exists to resolve the direction from. Dir is empty for ordinary pins, whose direction
	// resolves via the part type.
	Dir string
	// AutoName is the format tool's own name for an unlabelled net named after this pin, such
	// as KiCad's "Net-(R1-Pad2)". NCName is the tool's name for the lone stub of a
	// no-connect-flagged pin, such as "unconnected-(J1A-MID0-PadK40)". A format that sets
	// neither gets the synthetic "N$<n>" and "unconnected-(REF-PadN)" names.
	AutoName, NCName string
}

Pin is one component pin placed at a grid point. Comp is the component ref_des and Pin is the pin designator on that component. NoConnect marks a pin whose connect point carries the format's no-connect flag (a KiCad no_connect marker placed ON the pin, WS1-019). The caller detects the coincidence. Build then names that pin's LONE stub net in the tool-marker vocabulary ("unconnected-(REF-Pad)") instead of "N$<n>". The existing no-connect consumers recognise that name with no new IR field. They are intentionallyUnconnected and the NC channel. A marked pin on a multi-pin net is named normally, because that net is an nc-pin-connected problem rather than an intentional stub.

type Point

type Point struct{ X, Y int64 }

Point is a connection location on the caller's integer grid. The caller is responsible for quantizing native coordinates (e.g. xschem half-integers scaled by 2) so that points meant to coincide are exactly equal.

type SheetGroup added in v0.5.0

type SheetGroup struct {
	Sheet string
	Names []string
}

SheetGroup is the names of one net that met on one sheet's wiring (Net.SheetGroups). A name can sit in a group on a sheet other than its own. A parent sheet's pin carries its child's hierarchical name, so when the parent wires two child ports together, their names meet on the parent.

func ParseSheetGroups added in v0.5.0

func ParseSheetGroups(v string) []SheetGroup

ParseSheetGroups decodes an AttrSheetGroups value into its groups. Empty or absent decodes to nil, meaning the net's aliases all met in one place.

type Wire

type Wire struct {
	A, B  Point
	Label string
	Id    string
	// Sheet is the sheet instance the wire is drawn on, set by a reader that solves several sheets
	// at once (the KiCad hierarchy walk) and empty otherwise. See Net.SheetGroups.
	Sheet string
}

Wire is a net segment between two grid points. Label is the net name the segment carries inline (xschem "lab="), or "" when the format leaves wires unnamed (gEDA). Id is the source's native id for the wire (e.g. a KiCad uuid), carried through so a dangling-endpoint diagnostic can point back at the offending wire; "" when the format has no per-wire id.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL