TraceMaker
C++ API of the engine
Loading...
Searching...
No Matches
board.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#pragma once
3// The board model (design doc 02 §3): every object TraceMaker reads from a .kicad_pcb, in absolute
4// integer-nanometre coordinates, with a link back to its s-expression node for lossless writing.
5#include <climits>
6#include <cstdint>
7#include <string>
8#include <unordered_map>
9#include <utility>
10#include <vector>
11
12#include "core/units.hpp"
13#include "geom/point.hpp"
14#include "model/stackup.hpp"
15#include "sexpr/sexpr.hpp"
16
17namespace tmk::model {
18
19using geom::Box;
20using geom::Point;
21using NetId = std::int32_t; // 0 = no net
22using LayerMask = std::uint64_t; // bit i = copper layer i in stack order (0 = F.Cu, last = B.Cu)
23
24inline constexpr LayerMask layer_bit(int copper_index) { return LayerMask{1} << copper_index; }
25
26struct LayerDef {
27 int ordinal = 0; // KiCad layer id as written in the file
28 std::string name; // canonical name, e.g. "F.Cu", "In1.Cu", "Edge.Cuts"
29 std::string file_name; // name as written in this file's layer table (old boards may use "Front")
30 std::string type; // signal, power, mixed, jumper, user
31 std::string user_name; // optional user-visible name
32 int copper_index = -1; // stack index for copper layers, -1 otherwise
33};
34
35struct Net {
36 NetId id = 0;
37 std::string name;
38 int file_number = -1; // number in a KiCad <= 9 net table, -1 for KiCad 10 files
39};
40
41enum class PadType : std::uint8_t { Smd, ThruHole, NpThruHole, Connect };
42enum class PadShape : std::uint8_t { Circle, Rect, Oval, Trapezoid, RoundRect, ChamferedRect, Custom };
43
44// How a pad joins a zone of its net (KiCad's ZONE_CONNECTION; the file stores these numbers).
45enum class ZoneConnect : std::int8_t { Inherited = -1, None = 0, Thermal = 1, Full = 2, ThtThermal = 3 };
46// A padstack's copper on layers nothing connects to (KiCad's UNCONNECTED_LAYER_MODE; doc 05 §60): kept on every layer, removed
47// from every layer, removed except on the drill's start and end layers, or only on the start and end layers. Vias and plated
48// through-hole pads; read from (remove_unused_layers), (keep_end_layers) and (start_end_only) in file order, as KiCad reads them.
50
51struct Pad {
52 int footprint = -1;
53 std::string number;
56 Point pos; // absolute centre
57 double angle = 0; // absolute orientation, degrees
59 Coord drill_x = 0, drill_y = 0; // 0 = no hole
60 bool drill_oval = false;
61 Point drill_offset; // copper shape offset from the hole (pad position), in the pad's own frame
62 LayerMask copper = 0; // copper layers the pad has copper on
63 std::vector<std::string> layers; // all layer names as written (incl. mask/paste, wildcards expanded)
64 double roundrect_ratio = 0;
65 double chamfer_ratio = 0;
66 std::uint8_t chamfer_corners = 0; // bit 0 TL, 1 TR, 2 BL, 3 BR
67 Coord trapezoid_dx = 0, trapezoid_dy = 0; // rect_delta
68 // Custom-pad primitives in the pad frame, each already grown by half its stroke width (KiCad draws them with a round pen and fills
69 // them): a polygon or rectangle, a polyline (line, arc) or a disc (circle; pts[0] is the centre).
70 struct CustomPrim {
72 std::vector<Point> pts;
74 };
75 std::vector<CustomPrim> custom_prims;
76 Coord clearance = -1; // local override, -1 = none
77 // Pad-to-die length and delay (KiCad's (die_length) and (die_delay); the delay in attoseconds, KiCad's time unit), added to the
78 // net's length and delay by length rules (drc/length.hpp).
80 std::int64_t die_delay = 0;
81 Coord mask_margin = INT64_MIN; // local solder-mask expansion (unset = INT64_MIN)
83 Coord thermal_gap = -1, thermal_bridge_width = -1; // local overrides, -1 = none
84 double thermal_bridge_angle = -1; // degrees; -1 = KiCad's default (45 for round pads, 90 otherwise)
85 // Schematic pin name and electrical type (KiCad 6+ boards; empty in older files). Read-only, used to
86 // recognise component roles (doc 15 §3.1).
87 std::string pinfunction, pintype;
89 UnconnectedLayers unconnected = UnconnectedLayers::KeepAll; // plated through-hole pads only
90 LayerMask zone_layers = 0; // (zone_layer_connections): layers where a zone fill joins the pad, so it keeps its copper
92};
93
94struct Graphic {
95 enum class Kind : std::uint8_t { Line, Arc, Circle, Rect, Poly, Curve } kind = Kind::Line;
96 std::string layer;
97 Point a, b, c; // line: a→b; arc: start a, mid c, end b; circle: centre a, point b; rect: a,b corners
98 std::vector<Point> pts; // poly / curve control points
100 bool filled = false;
101 NetId net = 0; // KiCad 8+ copper graphics can belong to a net
102 int footprint = -1; // owning footprint, -1 for board graphics
104};
105
106struct Text { // copper text is an obstacle; only position, layer and rough extent are kept
107 std::string layer;
108 std::string text;
110 double angle = 0;
112 int justify_h = 0; // -1 left, 0 centre, 1 right
113 int justify_v = 0; // -1 top, 0 centre, 1 bottom
114 bool mirror = false;
115 int footprint = -1;
116 bool hidden = false;
117};
118
119struct Footprint {
120 std::string lib_id;
121 std::string reference, value;
122 std::string description, keywords; // footprint (descr ...)/(tags ...) or Description property; read-only
123 std::vector<std::pair<std::string, std::string>> properties; // every (property "key" "value") in file order (MPN, Manufacturer, Datasheet ...); read-only
125 double angle = 0;
126 bool back = false;
127 bool locked = false;
128 bool attr_smd = false, attr_through_hole = false, board_only = false, exclude_from_pos = false,
130 std::vector<int> pads;
131 std::vector<int> graphics;
132 Coord clearance = -1; // footprint-level clearance override, -1 = none
133 Coord mask_margin = INT64_MIN; // footprint-level solder-mask expansion (unset = INT64_MIN)
134 ZoneConnect zone_connect = ZoneConnect::Inherited; // (zone_connect n): the pads' default
135 std::vector<std::vector<std::string>> net_tie_groups; // pad numbers joined by the footprint's own copper
136 std::string uuid;
138};
139
140struct Track {
143 int layer = -1; // copper index
145 bool locked = false;
147 bool tuning_pattern = false; // a member of a (generated (type tuning_pattern)) group: KiCad's pair coupling leaves it out
148};
149
150struct ArcTrack {
153 int layer = -1;
155 bool locked = false;
157 bool tuning_pattern = false; // a member of a (generated (type tuning_pattern)) group: KiCad's pair coupling leaves it out
158};
159
160enum class ViaType : std::uint8_t { Through, Blind, Micro };
161
162struct Via {
164 Coord size = 0, drill = 0;
165 int layer_top = 0, layer_bottom = 0; // copper indices, top <= bottom
168 bool locked = false;
170 bool free = false; // (free yes): a stitching via whose net KiCad's connectivity never changes
172 LayerMask zone_layers = 0; // (zone_layer_connections): layers where a zone fill joins the via, so it keeps its copper
173};
174
175struct Zone {
178 std::vector<std::string> layers;
179 std::string name;
180 std::string uuid; // breaks priority ties between zones of different nets (ZONE::HigherPriority)
181 int priority = 0;
182 Coord clearance = -1; // (connect_pads (clearance x)): the zone's local clearance override, -1 = none
183 // Fill settings (doc 05 §36); defaults are KiCad's.
184 ZoneConnect connect = ZoneConnect::Thermal; // (connect_pads [yes|no|thru_hole_only])
187 int island_removal = 0; // 0 always remove islands, 1 never, 2 below island_area_min
188 double island_area_min = 0; // nm²
189 bool hatched = false; // (fill (mode hatch))
190 bool rule_area = false;
191 bool keepout_tracks = false, keepout_vias = false, keepout_pads = false, keepout_pour = false,
193 bool teardrop = false; // (attr (teardrop ...)): copper KiCad adds where a track meets a pad or via, not a plane
194 std::vector<std::vector<Point>> outline; // main outline + holes (arcs flattened)
195 std::vector<std::pair<int, std::vector<Point>>> fills; // (copper index, filled polygon)
196 int footprint = -1;
198};
199
200struct Board {
201 // Board-level copper graphics (polygons on a copper layer) take the net of the one net whose pads, tracks or vias they touch (a designer's manual pour); ones touching
202 // several nets stay net-less. Off: every board copper graphic is net-less copper that no track may touch (route --soft-copper-graphics turns it on).
203 bool infer_graphic_nets = false;
204 std::int64_t version = 0;
205 std::string generator;
206 std::vector<LayerDef> layers;
207 std::vector<int> copper; // indices into `layers`, in stack order
208 std::vector<Net> nets; // nets[0] is the unnamed net
209 bool named_nets = false; // true for KiCad 10 "(net "name")" syntax
210 std::vector<Footprint> footprints;
211 std::vector<Pad> pads;
212 std::vector<Graphic> graphics;
213 std::vector<Text> texts;
214 std::vector<Track> tracks;
215 std::vector<ArcTrack> arcs;
216 std::vector<Via> vias;
217 std::vector<Zone> zones;
218 Coord thickness = 1'600'000;
219 Coord pad_to_mask_clearance = 0; // board solder-mask expansion for pads (setup)
220 bool vias_tented = false; // (setup (tenting front back)); old boards: untented
221 Stackup stackup; // (setup (stackup ...)); present == false when the file has none
222 std::vector<std::string> warnings;
223
224 int copper_count() const { return static_cast<int>(copper.size()); }
225 const std::string& copper_name(int idx) const { return layers[static_cast<std::size_t>(copper[static_cast<std::size_t>(idx)])].name; }
226 const std::string& copper_file_name(int idx) const { return layers[static_cast<std::size_t>(copper[static_cast<std::size_t>(idx)])].file_name; }
227 int copper_index(std::string_view name) const; // -1 if not copper (canonical or file name)
228 NetId net_by_name(std::string_view name) const; // 0 if unknown
229 // Bounding box of everything on Edge.Cuts (graphics stroke widths excluded).
230 Box edge_bbox() const;
231
232 std::unordered_map<std::string, NetId> net_index;
233};
234
235} // namespace tmk::model
ZoneConnect
How a pad joins a zone of its net (KiCad's ZONE_CONNECTION; the file stores these numbers).
Definition board.hpp:45
constexpr LayerMask layer_bit(int copper_index)
Definition board.hpp:24
std::int32_t NetId
0 = no net
Definition board.hpp:21
std::uint64_t LayerMask
bit i = copper layer i in stack order (0 = F.Cu, last = B.Cu)
Definition board.hpp:22
UnconnectedLayers
A padstack's copper on layers nothing connects to (KiCad's UNCONNECTED_LAYER_MODE; doc 05 §60): kept ...
Definition board.hpp:49
std::uint32_t NodeId
Definition sexpr.hpp:19
constexpr NodeId kNoNode
Definition sexpr.hpp:20
std::int64_t Coord
Engine coordinate: signed 64-bit nanometres.
Definition units.hpp:12
Integer-nanometre points and KiCad-compatible rotation.
Lossless s-expression documents for KiCad files (design doc 08 §3).
The board's physical stackup as KiCad stores it in (setup (stackup ...)) (design doc 15 §5....
bool tuning_pattern
a member of a (generated (type tuning_pattern)) group: KiCad's pair coupling leaves it out
Definition board.hpp:157
sexpr::NodeId node
Definition board.hpp:156
std::string generator
Definition board.hpp:205
std::unordered_map< std::string, NetId > net_index
Definition board.hpp:232
const std::string & copper_file_name(int idx) const
Definition board.hpp:226
bool infer_graphic_nets
Board-level copper graphics (polygons on a copper layer) take the net of the one net whose pads,...
Definition board.hpp:203
int copper_count() const
Definition board.hpp:224
std::vector< Pad > pads
Definition board.hpp:211
std::vector< Graphic > graphics
Definition board.hpp:212
std::vector< int > copper
indices into layers, in stack order
Definition board.hpp:207
bool named_nets
true for KiCad 10 "(net "name")" syntax
Definition board.hpp:209
std::vector< Track > tracks
Definition board.hpp:214
std::vector< ArcTrack > arcs
Definition board.hpp:215
Stackup stackup
(setup (stackup ...)); present == false when the file has none
Definition board.hpp:221
bool vias_tented
(setup (tenting front back)); old boards: untented
Definition board.hpp:220
Box edge_bbox() const
Bounding box of everything on Edge.Cuts (graphics stroke widths excluded).
std::vector< std::string > warnings
Definition board.hpp:222
std::vector< Zone > zones
Definition board.hpp:217
NetId net_by_name(std::string_view name) const
0 if unknown
const std::string & copper_name(int idx) const
Definition board.hpp:225
int copper_index(std::string_view name) const
-1 if not copper (canonical or file name)
std::vector< Net > nets
nets[0] is the unnamed net
Definition board.hpp:208
std::vector< LayerDef > layers
Definition board.hpp:206
std::vector< Text > texts
Definition board.hpp:213
std::int64_t version
Definition board.hpp:204
std::vector< Footprint > footprints
Definition board.hpp:210
Coord pad_to_mask_clearance
board solder-mask expansion for pads (setup)
Definition board.hpp:219
std::vector< Via > vias
Definition board.hpp:216
Coord clearance
footprint-level clearance override, -1 = none
Definition board.hpp:132
std::vector< int > pads
Definition board.hpp:130
Coord mask_margin
footprint-level solder-mask expansion (unset = INT64_MIN)
Definition board.hpp:133
ZoneConnect zone_connect
(zone_connect n): the pads' default
Definition board.hpp:134
std::string uuid
Definition board.hpp:136
std::vector< std::pair< std::string, std::string > > properties
every (property "key" "value") in file order (MPN, Manufacturer, Datasheet ...); read-only
Definition board.hpp:123
std::string reference
Definition board.hpp:121
std::vector< std::vector< std::string > > net_tie_groups
pad numbers joined by the footprint's own copper
Definition board.hpp:135
std::vector< int > graphics
Definition board.hpp:131
std::string description
Definition board.hpp:122
std::string lib_id
Definition board.hpp:120
std::string value
Definition board.hpp:121
sexpr::NodeId node
Definition board.hpp:137
std::string keywords
footprint (descr ...)/(tags ...) or Description property; read-only
Definition board.hpp:122
NetId net
KiCad 8+ copper graphics can belong to a net.
Definition board.hpp:101
int footprint
owning footprint, -1 for board graphics
Definition board.hpp:102
std::vector< Point > pts
poly / curve control points
Definition board.hpp:98
enum tmk::model::Graphic::Kind kind
Point c
line: a→b; arc: start a, mid c, end b; circle: centre a, point b; rect: a,b corners
Definition board.hpp:97
std::string layer
Definition board.hpp:96
sexpr::NodeId node
Definition board.hpp:103
int ordinal
KiCad layer id as written in the file.
Definition board.hpp:27
std::string user_name
optional user-visible name
Definition board.hpp:31
int copper_index
stack index for copper layers, -1 otherwise
Definition board.hpp:32
std::string file_name
name as written in this file's layer table (old boards may use "Front")
Definition board.hpp:29
std::string type
signal, power, mixed, jumper, user
Definition board.hpp:30
std::string name
canonical name, e.g. "F.Cu", "In1.Cu", "Edge.Cuts"
Definition board.hpp:28
std::string name
Definition board.hpp:37
int file_number
number in a KiCad <= 9 net table, -1 for KiCad 10 files
Definition board.hpp:38
Custom-pad primitives in the pad frame, each already grown by half its stroke width (KiCad draws them...
Definition board.hpp:70
std::vector< Point > pts
Definition board.hpp:72
enum tmk::model::Pad::CustomPrim::Kind kind
std::string pinfunction
Schematic pin name and electrical type (KiCad 6+ boards; empty in older files).
Definition board.hpp:87
std::vector< std::string > layers
all layer names as written (incl. mask/paste, wildcards expanded)
Definition board.hpp:63
Coord thermal_bridge_width
local overrides, -1 = none
Definition board.hpp:83
double thermal_bridge_angle
degrees; -1 = KiCad's default (45 for round pads, 90 otherwise)
Definition board.hpp:84
bool drill_oval
Definition board.hpp:60
std::string number
Definition board.hpp:53
LayerMask copper
copper layers the pad has copper on
Definition board.hpp:62
Coord thermal_gap
Definition board.hpp:83
Coord drill_x
Definition board.hpp:59
UnconnectedLayers unconnected
plated through-hole pads only
Definition board.hpp:89
Coord drill_y
0 = no hole
Definition board.hpp:59
LayerMask zone_layers
(zone_layer_connections): layers where a zone fill joins the pad, so it keeps its copper
Definition board.hpp:90
double chamfer_ratio
Definition board.hpp:65
std::int64_t die_delay
Definition board.hpp:80
Coord die_length
Pad-to-die length and delay (KiCad's (die_length) and (die_delay); the delay in attoseconds,...
Definition board.hpp:79
PadType type
Definition board.hpp:54
Coord clearance
local override, -1 = none
Definition board.hpp:76
Point drill_offset
copper shape offset from the hole (pad position), in the pad's own frame
Definition board.hpp:61
PadShape shape
Definition board.hpp:55
std::string pintype
Definition board.hpp:87
Coord trapezoid_dy
rect_delta
Definition board.hpp:67
std::vector< CustomPrim > custom_prims
Definition board.hpp:75
Point pos
absolute centre
Definition board.hpp:56
std::uint8_t chamfer_corners
bit 0 TL, 1 TR, 2 BL, 3 BR
Definition board.hpp:66
Coord trapezoid_dx
Definition board.hpp:67
ZoneConnect zone_connect
(zone_connect n)
Definition board.hpp:82
double roundrect_ratio
Definition board.hpp:64
sexpr::NodeId node
Definition board.hpp:91
Coord mask_margin
local solder-mask expansion (unset = INT64_MIN)
Definition board.hpp:81
double angle
absolute orientation, degrees
Definition board.hpp:57
std::string text
Definition board.hpp:108
int justify_v
-1 top, 0 centre, 1 bottom
Definition board.hpp:113
std::string layer
< copper text is an obstacle; only position, layer and rough extent are kept
Definition board.hpp:107
int justify_h
-1 left, 0 centre, 1 right
Definition board.hpp:112
bool tuning_pattern
a member of a (generated (type tuning_pattern)) group: KiCad's pair coupling leaves it out
Definition board.hpp:147
sexpr::NodeId node
Definition board.hpp:146
int layer
copper index
Definition board.hpp:143
UnconnectedLayers unconnected
Definition board.hpp:171
bool free
(free yes): a stitching via whose net KiCad's connectivity never changes
Definition board.hpp:170
ViaType type
Definition board.hpp:166
sexpr::NodeId node
Definition board.hpp:169
int layer_bottom
copper indices, top <= bottom
Definition board.hpp:165
LayerMask zone_layers
(zone_layer_connections): layers where a zone fill joins the via, so it keeps its copper
Definition board.hpp:172
std::vector< std::vector< Point > > outline
main outline + holes (arcs flattened)
Definition board.hpp:194
std::string uuid
breaks priority ties between zones of different nets (ZONE::HigherPriority)
Definition board.hpp:180
bool keepout_tracks
Definition board.hpp:191
LayerMask copper
Definition board.hpp:177
ZoneConnect connect
Fill settings (doc 05 §36); defaults are KiCad's.
Definition board.hpp:184
sexpr::NodeId node
Definition board.hpp:197
bool keepout_footprints
Definition board.hpp:192
Coord thermal_bridge_width
Definition board.hpp:186
Coord thermal_gap
Definition board.hpp:186
double island_area_min
nm²
Definition board.hpp:188
std::string name
Definition board.hpp:179
bool teardrop
(attr (teardrop ...)): copper KiCad adds where a track meets a pad or via, not a plane
Definition board.hpp:193
Coord min_thickness
Definition board.hpp:185
Coord clearance
(connect_pads (clearance x)): the zone's local clearance override, -1 = none
Definition board.hpp:182
bool hatched
(fill (mode hatch))
Definition board.hpp:189
std::vector< std::string > layers
Definition board.hpp:178
int island_removal
0 always remove islands, 1 never, 2 below island_area_min
Definition board.hpp:187
std::vector< std::pair< int, std::vector< Point > > > fills
(copper index, filled polygon)
Definition board.hpp:195