TraceMaker
C++ API of the engine
Loading...
Searching...
No Matches
rules.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#pragma once
3// Design rules as KiCad defines them (design doc 08 §3–4): board minimums and net classes from the
4// .kicad_pro project file, plus custom rules from the .kicad_dru file.
5#include <cstdint>
6#include <map>
7#include <optional>
8#include <string>
9#include <vector>
10
11#include "core/units.hpp"
12
13namespace tmk::model {
14
15struct NetClass {
16 std::string name;
17 Coord clearance = 200'000;
18 Coord track_width = 250'000;
20 Coord via_drill = 300'000;
22 Coord uvia_drill = 100'000;
26 bool has_diff_pair_gap = false; // set explicitly in the project (not inherited)
27 int priority = 0; // lower value = higher priority (KiCad 9+); Default has INT_MAX
28 std::string tuning_profile; // KiCad 10: the name of the class's tuning profile ("" = none)
29 bool profile_from_default = false; // tuning_profile is Default's: the class names none (KiCad's addMissingDefaults)
30 // Which of the ten parameters KiCad 10.0.6 fills from Default when unset (NET_SETTINGS::addMissingDefaults) the project sets for
31 // this class, one bit each in kNetClassFields order. A net whose classes leave one unset has Default among its effective class's
32 // constituents, which hasNetclass('Default') sees.
33 std::uint16_t fields = 0;
34 static constexpr std::uint16_t kAllFields = 0x3ff;
35 // Set by the route job for a differential tuning profile (doc 05 §58): the pair gap its pairs are routed at. Like KiCad's profile rule
36 // as KiCad 10.0.6 applies it, it relaxes the clearance between the coupled halves' tracks only, not between a track and the partner's
37 // pads (the class's diff_pair_gap still does that). 0 = none.
39};
40
41// A KiCad 10 tuning profile (`.kicad_pro` `tuning_profiles`, doc 05 §58): per signal layer the track width and pair gap of the
42// class's nets there (0 = not set) and the propagation delay per millimetre (KiCad's time units; used only with time-domain tuning).
44 std::string layer; // signal layer name, e.g. "F.Cu"
45 Coord width = 0, gap = 0;
46 std::int64_t delay = 0;
47};
48// A via's delay for one signal-layer and via-layer span, instead of the profile's delay per mm (layer names as the
49// project gives them; KiCad matches them exactly, upper layer first).
52 std::int64_t delay = 0; // attoseconds
53};
55 std::string name;
56 bool differential = false; // KiCad's PROFILE_TYPE DIFFERENTIAL (1); SINGLE (0) otherwise
57 bool time_domain = false;
58 std::vector<TuningLayer> layers;
59 std::int64_t via_delay = 0; // attoseconds per mm of via height
60 std::vector<TuningViaOverride> via_overrides;
61 const TuningViaOverride* via_override(const std::string& sf, const std::string& st, const std::string& vf, const std::string& vt) const {
62 for (const auto& o : via_overrides)
63 if (o.signal_from == sf && o.signal_to == st && o.via_from == vf && o.via_to == vt) return &o;
64 return nullptr;
65 }
66 // The width set for `layer`, 0 if none.
67 Coord width_on(const std::string& layer) const {
68 for (const auto& l : layers)
69 if (l.layer == layer) return l.width;
70 return 0;
71 }
72};
73
80 Coord through_hole_diameter = 0; // minimum drill
87 Coord solder_mask_min_width = 0; // minimum mask web between openings (KiCad solder_mask_bridge test)
88 int min_resolved_spokes = 2; // thermal spokes a pad needs into its zone (KiCad starved_thermal; project rules.min_resolved_spokes)
90 bool allow_microvias = false;
91};
92
93// One constraint inside a custom rule, e.g. (constraint clearance (min 0.2mm)).
94struct Constraint {
95 std::string type; // clearance, track_width, disallow, …
96 std::optional<Coord> min, opt, max; // lengths in nm (angles/counts are stored ×1e6 as well)
97 std::vector<std::string> items; // disallow item types, or other bare words
98 bool time = false; // length or skew given in ps or fs: min/opt/max are attoseconds (KiCad's time domain)
99};
100
102 std::string name;
103 std::string condition; // raw expression text, compiled by the rule engine
104 std::string layer; // "", "outer", "inner", or a layer name
105 std::string severity;
106 std::vector<Constraint> constraints;
107 bool implicit = false; // one of the rules KiCad compiles itself (tuning profiles, doc 05 §58), not from the .kicad_dru
108};
109
112 std::vector<NetClass> classes; // classes[0] is "Default"
113 std::vector<std::pair<std::string, std::string>> patterns; // (net-name pattern, class name), in file order
114 std::map<std::string, std::vector<std::string>> assignments; // explicit net → class names
115 std::vector<CustomRule> custom;
116 std::map<std::string, std::string> severities; // KiCad violation type -> error | warning | ignore
117 std::vector<TuningProfile> tuning_profiles; // KiCad 10 tuning profiles, as the project lists them
118 // KiCad's "Include stackup height in track length calculations" (on unless the project turns it off): vias add
119 // the stackup height between the layers they join to a net's length (drc/length.hpp, via_heights).
121 std::vector<std::string> warnings; // anything that could not be interpreted
122
123 const NetClass& default_class() const { return classes.front(); }
124 // Effective net class for a net name (explicit assignment, then the first matching pattern, else Default).
125 const NetClass& class_for(const std::string& net_name) const;
126 const NetClass* find_class(const std::string& name) const;
127 // Names of every class that applies to a net (explicit assignments, then matching patterns), and "Default", which
128 // KiCad 10.0.6 counts in every net's effective class: the constituents hasNetclass() searches.
129 std::vector<std::string> classes_of(const std::string& net_name) const;
130 // The tuning profile a class names, if the project has one of that name.
131 const TuningProfile* profile_of(const NetClass& c) const {
132 for (const auto& p : tuning_profiles)
133 if (!c.tuning_profile.empty() && p.name == c.tuning_profile) return &p;
134 return nullptr;
135 }
136};
137
138// KiCad net-class pattern match: shell-style wildcards (* and ?), case-sensitive.
139bool wildcard_match(std::string_view pattern, std::string_view text);
140// Net-class pattern: wildcard match or full regular-expression match (KiCad accepts both).
141bool pattern_match(const std::string& pattern, const std::string& text);
142
143} // namespace tmk::model
bool pattern_match(const std::string &pattern, const std::string &text)
Net-class pattern: wildcard match or full regular-expression match (KiCad accepts both).
bool wildcard_match(std::string_view pattern, std::string_view text)
KiCad net-class pattern match: shell-style wildcards (* and ?), case-sensitive.
std::int64_t Coord
Engine coordinate: signed 64-bit nanometres.
Definition units.hpp:12
int min_resolved_spokes
thermal spokes a pad needs into its zone (KiCad starved_thermal; project rules.min_resolved_spokes)
Definition rules.hpp:88
Coord through_hole_diameter
minimum drill
Definition rules.hpp:80
Coord solder_mask_to_copper_clearance
Definition rules.hpp:86
Coord solder_mask_min_width
minimum mask web between openings (KiCad solder_mask_bridge test)
Definition rules.hpp:87
One constraint inside a custom rule, e.g. (constraint clearance (min 0.2mm)).
Definition rules.hpp:94
std::optional< Coord > min
Definition rules.hpp:96
std::vector< std::string > items
disallow item types, or other bare words
Definition rules.hpp:97
std::optional< Coord > opt
Definition rules.hpp:96
bool time
length or skew given in ps or fs: min/opt/max are attoseconds (KiCad's time domain)
Definition rules.hpp:98
std::string type
clearance, track_width, disallow, …
Definition rules.hpp:95
std::optional< Coord > max
lengths in nm (angles/counts are stored ×1e6 as well)
Definition rules.hpp:96
std::string layer
"", "outer", "inner", or a layer name
Definition rules.hpp:104
std::string condition
raw expression text, compiled by the rule engine
Definition rules.hpp:103
std::vector< Constraint > constraints
Definition rules.hpp:106
bool implicit
one of the rules KiCad compiles itself (tuning profiles, doc 05 §58), not from the ....
Definition rules.hpp:107
std::string severity
Definition rules.hpp:105
bool use_height_for_length
KiCad's "Include stackup height in track length calculations" (on unless the project turns it off): v...
Definition rules.hpp:120
const NetClass * find_class(const std::string &name) const
std::vector< CustomRule > custom
Definition rules.hpp:115
std::map< std::string, std::string > severities
KiCad violation type -> error | warning | ignore.
Definition rules.hpp:116
const NetClass & default_class() const
Definition rules.hpp:123
std::vector< NetClass > classes
classes[0] is "Default"
Definition rules.hpp:112
std::vector< std::string > warnings
anything that could not be interpreted
Definition rules.hpp:121
std::vector< TuningProfile > tuning_profiles
KiCad 10 tuning profiles, as the project lists them.
Definition rules.hpp:117
const NetClass & class_for(const std::string &net_name) const
Effective net class for a net name (explicit assignment, then the first matching pattern,...
std::vector< std::string > classes_of(const std::string &net_name) const
Names of every class that applies to a net (explicit assignments, then matching patterns),...
std::vector< std::pair< std::string, std::string > > patterns
(net-name pattern, class name), in file order
Definition rules.hpp:113
BoardMinimums minimums
Definition rules.hpp:111
std::map< std::string, std::vector< std::string > > assignments
explicit net → class names
Definition rules.hpp:114
const TuningProfile * profile_of(const NetClass &c) const
The tuning profile a class names, if the project has one of that name.
Definition rules.hpp:131
Coord diff_pair_via_gap
Definition rules.hpp:25
bool has_diff_pair_gap
set explicitly in the project (not inherited)
Definition rules.hpp:26
Coord tuning_pair_gap
Set by the route job for a differential tuning profile (doc 05 §58): the pair gap its pairs are route...
Definition rules.hpp:38
std::uint16_t fields
Which of the ten parameters KiCad 10.0.6 fills from Default when unset (NET_SETTINGS::addMissingDefau...
Definition rules.hpp:33
bool profile_from_default
tuning_profile is Default's: the class names none (KiCad's addMissingDefaults)
Definition rules.hpp:29
std::string tuning_profile
KiCad 10: the name of the class's tuning profile ("" = none).
Definition rules.hpp:28
std::string name
Definition rules.hpp:16
int priority
lower value = higher priority (KiCad 9+); Default has INT_MAX
Definition rules.hpp:27
static constexpr std::uint16_t kAllFields
Definition rules.hpp:34
A KiCad 10 tuning profile (.kicad_pro tuning_profiles, doc 05 §58): per signal layer the track width ...
Definition rules.hpp:43
std::string layer
signal layer name, e.g. "F.Cu"
Definition rules.hpp:44
std::int64_t delay
Definition rules.hpp:46
Coord width_on(const std::string &layer) const
The width set for layer, 0 if none.
Definition rules.hpp:67
std::vector< TuningViaOverride > via_overrides
Definition rules.hpp:60
bool differential
KiCad's PROFILE_TYPE DIFFERENTIAL (1); SINGLE (0) otherwise.
Definition rules.hpp:56
const TuningViaOverride * via_override(const std::string &sf, const std::string &st, const std::string &vf, const std::string &vt) const
Definition rules.hpp:61
std::int64_t via_delay
attoseconds per mm of via height
Definition rules.hpp:59
std::vector< TuningLayer > layers
Definition rules.hpp:58
A via's delay for one signal-layer and via-layer span, instead of the profile's delay per mm (layer n...
Definition rules.hpp:50
std::int64_t delay
attoseconds
Definition rules.hpp:52