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
47struct Pad {
48 int footprint = -1;
49 std::string number;
52 Point pos; // absolute centre
53 double angle = 0; // absolute orientation, degrees
55 Coord drill_x = 0, drill_y = 0; // 0 = no hole
56 bool drill_oval = false;
57 Point drill_offset; // copper shape offset from the hole (pad position), in the pad's own frame
58 LayerMask copper = 0; // copper layers the pad has copper on
59 std::vector<std::string> layers; // all layer names as written (incl. mask/paste, wildcards expanded)
60 double roundrect_ratio = 0;
61 double chamfer_ratio = 0;
62 std::uint8_t chamfer_corners = 0; // bit 0 TL, 1 TR, 2 BL, 3 BR
63 Coord trapezoid_dx = 0, trapezoid_dy = 0; // rect_delta
64 // Custom-pad primitives in the pad frame, each already grown by half its stroke width (KiCad draws them with a round pen and fills
65 // them): a polygon or rectangle, a polyline (line, arc) or a disc (circle; pts[0] is the centre).
66 struct CustomPrim {
68 std::vector<Point> pts;
70 };
71 std::vector<CustomPrim> custom_prims;
72 Coord clearance = -1; // local override, -1 = none
73 Coord mask_margin = INT64_MIN; // local solder-mask expansion (unset = INT64_MIN)
75 Coord thermal_gap = -1, thermal_bridge_width = -1; // local overrides, -1 = none
76 double thermal_bridge_angle = -1; // degrees; -1 = KiCad's default (45 for round pads, 90 otherwise)
77 // Schematic pin name and electrical type (KiCad 6+ boards; empty in older files). Read-only, used to
78 // recognise component roles (doc 15 §3.1).
79 std::string pinfunction, pintype;
82};
83
84struct Graphic {
85 enum class Kind : std::uint8_t { Line, Arc, Circle, Rect, Poly, Curve } kind = Kind::Line;
86 std::string layer;
87 Point a, b, c; // line: a→b; arc: start a, mid c, end b; circle: centre a, point b; rect: a,b corners
88 std::vector<Point> pts; // poly / curve control points
90 bool filled = false;
91 NetId net = 0; // KiCad 8+ copper graphics can belong to a net
92 int footprint = -1; // owning footprint, -1 for board graphics
94};
95
96struct Text { // copper text is an obstacle; only position, layer and rough extent are kept
97 std::string layer;
98 std::string text;
100 double angle = 0;
102 int justify_h = 0; // -1 left, 0 centre, 1 right
103 int justify_v = 0; // -1 top, 0 centre, 1 bottom
104 bool mirror = false;
105 int footprint = -1;
106 bool hidden = false;
107};
108
109struct Footprint {
110 std::string lib_id;
111 std::string reference, value;
112 std::string description, keywords; // footprint (descr ...)/(tags ...) or Description property; read-only
113 std::vector<std::pair<std::string, std::string>> properties; // every (property "key" "value") in file order (MPN, Manufacturer, Datasheet ...); read-only
115 double angle = 0;
116 bool back = false;
117 bool locked = false;
118 bool attr_smd = false, attr_through_hole = false, board_only = false, exclude_from_pos = false,
120 std::vector<int> pads;
121 std::vector<int> graphics;
122 Coord clearance = -1; // footprint-level clearance override, -1 = none
123 Coord mask_margin = INT64_MIN; // footprint-level solder-mask expansion (unset = INT64_MIN)
124 ZoneConnect zone_connect = ZoneConnect::Inherited; // (zone_connect n): the pads' default
125 std::vector<std::vector<std::string>> net_tie_groups; // pad numbers joined by the footprint's own copper
126 std::string uuid;
128};
129
130struct Track {
133 int layer = -1; // copper index
135 bool locked = false;
137};
138
147
148enum class ViaType : std::uint8_t { Through, Blind, Micro };
149
150struct Via {
152 Coord size = 0, drill = 0;
153 int layer_top = 0, layer_bottom = 0; // copper indices, top <= bottom
156 bool locked = false;
158 bool free = false; // (free yes): a stitching via whose net KiCad's connectivity never changes
159};
160
161struct Zone {
164 std::vector<std::string> layers;
165 std::string name;
166 std::string uuid; // breaks priority ties between zones of different nets (ZONE::HigherPriority)
167 int priority = 0;
168 Coord clearance = -1; // (connect_pads (clearance x)): the zone's local clearance override, -1 = none
169 // Fill settings (doc 05 §36); defaults are KiCad's.
170 ZoneConnect connect = ZoneConnect::Thermal; // (connect_pads [yes|no|thru_hole_only])
173 int island_removal = 0; // 0 always remove islands, 1 never, 2 below island_area_min
174 double island_area_min = 0; // nm²
175 bool hatched = false; // (fill (mode hatch))
176 bool rule_area = false;
177 bool keepout_tracks = false, keepout_vias = false, keepout_pads = false, keepout_pour = false,
179 bool teardrop = false; // (attr (teardrop ...)): copper KiCad adds where a track meets a pad or via, not a plane
180 std::vector<std::vector<Point>> outline; // main outline + holes (arcs flattened)
181 std::vector<std::pair<int, std::vector<Point>>> fills; // (copper index, filled polygon)
182 int footprint = -1;
184};
185
186struct Board {
187 // 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
188 // 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).
189 bool infer_graphic_nets = false;
190 std::int64_t version = 0;
191 std::string generator;
192 std::vector<LayerDef> layers;
193 std::vector<int> copper; // indices into `layers`, in stack order
194 std::vector<Net> nets; // nets[0] is the unnamed net
195 bool named_nets = false; // true for KiCad 10 "(net "name")" syntax
196 std::vector<Footprint> footprints;
197 std::vector<Pad> pads;
198 std::vector<Graphic> graphics;
199 std::vector<Text> texts;
200 std::vector<Track> tracks;
201 std::vector<ArcTrack> arcs;
202 std::vector<Via> vias;
203 std::vector<Zone> zones;
204 Coord thickness = 1'600'000;
205 Coord pad_to_mask_clearance = 0; // board solder-mask expansion for pads (setup)
206 bool vias_tented = false; // (setup (tenting front back)); old boards: untented
207 Stackup stackup; // (setup (stackup ...)); present == false when the file has none
208 std::vector<std::string> warnings;
209
210 int copper_count() const { return static_cast<int>(copper.size()); }
211 const std::string& copper_name(int idx) const { return layers[static_cast<std::size_t>(copper[static_cast<std::size_t>(idx)])].name; }
212 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; }
213 int copper_index(std::string_view name) const; // -1 if not copper (canonical or file name)
214 NetId net_by_name(std::string_view name) const; // 0 if unknown
215 // Bounding box of everything on Edge.Cuts (graphics stroke widths excluded).
216 Box edge_bbox() const;
217
218 std::unordered_map<std::string, NetId> net_index;
219};
220
221} // 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
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....
sexpr::NodeId node
Definition board.hpp:145
std::string generator
Definition board.hpp:191
std::unordered_map< std::string, NetId > net_index
Definition board.hpp:218
const std::string & copper_file_name(int idx) const
Definition board.hpp:212
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:189
int copper_count() const
Definition board.hpp:210
std::vector< Pad > pads
Definition board.hpp:197
std::vector< Graphic > graphics
Definition board.hpp:198
std::vector< int > copper
indices into layers, in stack order
Definition board.hpp:193
bool named_nets
true for KiCad 10 "(net "name")" syntax
Definition board.hpp:195
std::vector< Track > tracks
Definition board.hpp:200
std::vector< ArcTrack > arcs
Definition board.hpp:201
Stackup stackup
(setup (stackup ...)); present == false when the file has none
Definition board.hpp:207
bool vias_tented
(setup (tenting front back)); old boards: untented
Definition board.hpp:206
Box edge_bbox() const
Bounding box of everything on Edge.Cuts (graphics stroke widths excluded).
std::vector< std::string > warnings
Definition board.hpp:208
std::vector< Zone > zones
Definition board.hpp:203
NetId net_by_name(std::string_view name) const
0 if unknown
const std::string & copper_name(int idx) const
Definition board.hpp:211
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:194
std::vector< LayerDef > layers
Definition board.hpp:192
std::vector< Text > texts
Definition board.hpp:199
std::int64_t version
Definition board.hpp:190
std::vector< Footprint > footprints
Definition board.hpp:196
Coord pad_to_mask_clearance
board solder-mask expansion for pads (setup)
Definition board.hpp:205
std::vector< Via > vias
Definition board.hpp:202
Coord clearance
footprint-level clearance override, -1 = none
Definition board.hpp:122
std::vector< int > pads
Definition board.hpp:120
Coord mask_margin
footprint-level solder-mask expansion (unset = INT64_MIN)
Definition board.hpp:123
ZoneConnect zone_connect
(zone_connect n): the pads' default
Definition board.hpp:124
std::string uuid
Definition board.hpp:126
std::vector< std::pair< std::string, std::string > > properties
every (property "key" "value") in file order (MPN, Manufacturer, Datasheet ...); read-only
Definition board.hpp:113
std::string reference
Definition board.hpp:111
std::vector< std::vector< std::string > > net_tie_groups
pad numbers joined by the footprint's own copper
Definition board.hpp:125
std::vector< int > graphics
Definition board.hpp:121
std::string description
Definition board.hpp:112
std::string lib_id
Definition board.hpp:110
std::string value
Definition board.hpp:111
sexpr::NodeId node
Definition board.hpp:127
std::string keywords
footprint (descr ...)/(tags ...) or Description property; read-only
Definition board.hpp:112
NetId net
KiCad 8+ copper graphics can belong to a net.
Definition board.hpp:91
int footprint
owning footprint, -1 for board graphics
Definition board.hpp:92
std::vector< Point > pts
poly / curve control points
Definition board.hpp:88
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:87
std::string layer
Definition board.hpp:86
sexpr::NodeId node
Definition board.hpp:93
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:66
std::vector< Point > pts
Definition board.hpp:68
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:79
std::vector< std::string > layers
all layer names as written (incl. mask/paste, wildcards expanded)
Definition board.hpp:59
Coord thermal_bridge_width
local overrides, -1 = none
Definition board.hpp:75
double thermal_bridge_angle
degrees; -1 = KiCad's default (45 for round pads, 90 otherwise)
Definition board.hpp:76
bool drill_oval
Definition board.hpp:56
std::string number
Definition board.hpp:49
LayerMask copper
copper layers the pad has copper on
Definition board.hpp:58
Coord thermal_gap
Definition board.hpp:75
Coord drill_x
Definition board.hpp:55
Coord drill_y
0 = no hole
Definition board.hpp:55
double chamfer_ratio
Definition board.hpp:61
PadType type
Definition board.hpp:50
Coord clearance
local override, -1 = none
Definition board.hpp:72
Point drill_offset
copper shape offset from the hole (pad position), in the pad's own frame
Definition board.hpp:57
PadShape shape
Definition board.hpp:51
std::string pintype
Definition board.hpp:79
Coord trapezoid_dy
rect_delta
Definition board.hpp:63
std::vector< CustomPrim > custom_prims
Definition board.hpp:71
Point pos
absolute centre
Definition board.hpp:52
std::uint8_t chamfer_corners
bit 0 TL, 1 TR, 2 BL, 3 BR
Definition board.hpp:62
Coord trapezoid_dx
Definition board.hpp:63
ZoneConnect zone_connect
(zone_connect n)
Definition board.hpp:74
double roundrect_ratio
Definition board.hpp:60
sexpr::NodeId node
Definition board.hpp:81
Coord mask_margin
local solder-mask expansion (unset = INT64_MIN)
Definition board.hpp:73
double angle
absolute orientation, degrees
Definition board.hpp:53
std::string text
Definition board.hpp:98
int justify_v
-1 top, 0 centre, 1 bottom
Definition board.hpp:103
std::string layer
< copper text is an obstacle; only position, layer and rough extent are kept
Definition board.hpp:97
int justify_h
-1 left, 0 centre, 1 right
Definition board.hpp:102
sexpr::NodeId node
Definition board.hpp:136
int layer
copper index
Definition board.hpp:133
bool free
(free yes): a stitching via whose net KiCad's connectivity never changes
Definition board.hpp:158
ViaType type
Definition board.hpp:154
sexpr::NodeId node
Definition board.hpp:157
int layer_bottom
copper indices, top <= bottom
Definition board.hpp:153
std::vector< std::vector< Point > > outline
main outline + holes (arcs flattened)
Definition board.hpp:180
std::string uuid
breaks priority ties between zones of different nets (ZONE::HigherPriority)
Definition board.hpp:166
bool keepout_tracks
Definition board.hpp:177
LayerMask copper
Definition board.hpp:163
ZoneConnect connect
Fill settings (doc 05 §36); defaults are KiCad's.
Definition board.hpp:170
sexpr::NodeId node
Definition board.hpp:183
bool keepout_footprints
Definition board.hpp:178
Coord thermal_bridge_width
Definition board.hpp:172
Coord thermal_gap
Definition board.hpp:172
double island_area_min
nm²
Definition board.hpp:174
std::string name
Definition board.hpp:165
bool teardrop
(attr (teardrop ...)): copper KiCad adds where a track meets a pad or via, not a plane
Definition board.hpp:179
Coord min_thickness
Definition board.hpp:171
Coord clearance
(connect_pads (clearance x)): the zone's local clearance override, -1 = none
Definition board.hpp:168
bool hatched
(fill (mode hatch))
Definition board.hpp:175
std::vector< std::string > layers
Definition board.hpp:164
int island_removal
0 always remove islands, 1 never, 2 below island_area_min
Definition board.hpp:173
std::vector< std::pair< int, std::vector< Point > > > fills
(copper index, filled polygon)
Definition board.hpp:181