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 <map>
6#include <optional>
7#include <string>
8#include <vector>
9
10#include "core/units.hpp"
11
12namespace tmk::model {
13
14struct NetClass {
15 std::string name;
16 Coord clearance = 200'000;
17 Coord track_width = 250'000;
19 Coord via_drill = 300'000;
21 Coord uvia_drill = 100'000;
25 bool has_diff_pair_gap = false; // set explicitly in the project (not inherited)
26 int priority = 0; // lower value = higher priority (KiCad 9+); Default has INT_MAX
27};
28
46
47// One constraint inside a custom rule, e.g. (constraint clearance (min 0.2mm)).
48struct Constraint {
49 std::string type; // clearance, track_width, disallow, …
50 std::optional<Coord> min, opt, max; // lengths in nm (angles/counts are stored ×1e6 as well)
51 std::vector<std::string> items; // disallow item types, or other bare words
52};
53
54struct CustomRule {
55 std::string name;
56 std::string condition; // raw expression text, compiled by the rule engine
57 std::string layer; // "", "outer", "inner", or a layer name
58 std::string severity;
59 std::vector<Constraint> constraints;
60};
61
64 std::vector<NetClass> classes; // classes[0] is "Default"
65 std::vector<std::pair<std::string, std::string>> patterns; // (net-name pattern, class name), in file order
66 std::map<std::string, std::vector<std::string>> assignments; // explicit net → class names
67 std::vector<CustomRule> custom;
68 std::map<std::string, std::string> severities; // KiCad violation type -> error | warning | ignore
69 // KiCad's "Include stackup height in track length calculations" (on unless the project turns it off): vias add
70 // the stackup height between the layers they join to a net's length (route/bus.hpp, via_heights).
72 std::vector<std::string> warnings; // anything that could not be interpreted
73
74 const NetClass& default_class() const { return classes.front(); }
75 // Effective net class for a net name (explicit assignment, then the first matching pattern, else Default).
76 const NetClass& class_for(const std::string& net_name) const;
77 const NetClass* find_class(const std::string& name) const;
78};
79
80// KiCad net-class pattern match: shell-style wildcards (* and ?), case-sensitive.
81bool wildcard_match(std::string_view pattern, std::string_view text);
82// Net-class pattern: wildcard match or full regular-expression match (KiCad accepts both).
83bool pattern_match(const std::string& pattern, const std::string& text);
84
85} // 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
Coord through_hole_diameter
minimum drill
Definition rules.hpp:35
Coord solder_mask_to_copper_clearance
Definition rules.hpp:41
Coord solder_mask_min_width
minimum mask web between openings (KiCad solder_mask_bridge test)
Definition rules.hpp:42
One constraint inside a custom rule, e.g. (constraint clearance (min 0.2mm)).
Definition rules.hpp:48
std::optional< Coord > min
Definition rules.hpp:50
std::vector< std::string > items
disallow item types, or other bare words
Definition rules.hpp:51
std::optional< Coord > opt
Definition rules.hpp:50
std::string type
clearance, track_width, disallow, …
Definition rules.hpp:49
std::optional< Coord > max
lengths in nm (angles/counts are stored ×1e6 as well)
Definition rules.hpp:50
std::string layer
"", "outer", "inner", or a layer name
Definition rules.hpp:57
std::string condition
raw expression text, compiled by the rule engine
Definition rules.hpp:56
std::vector< Constraint > constraints
Definition rules.hpp:59
std::string name
Definition rules.hpp:55
std::string severity
Definition rules.hpp:58
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:71
const NetClass * find_class(const std::string &name) const
std::vector< CustomRule > custom
Definition rules.hpp:67
std::map< std::string, std::string > severities
KiCad violation type -> error | warning | ignore.
Definition rules.hpp:68
const NetClass & default_class() const
Definition rules.hpp:74
std::vector< NetClass > classes
classes[0] is "Default"
Definition rules.hpp:64
std::vector< std::string > warnings
anything that could not be interpreted
Definition rules.hpp:72
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::pair< std::string, std::string > > patterns
(net-name pattern, class name), in file order
Definition rules.hpp:65
BoardMinimums minimums
Definition rules.hpp:63
std::map< std::string, std::vector< std::string > > assignments
explicit net → class names
Definition rules.hpp:66
Coord diff_pair_via_gap
Definition rules.hpp:24
bool has_diff_pair_gap
set explicitly in the project (not inherited)
Definition rules.hpp:25
std::string name
Definition rules.hpp:15
int priority
lower value = higher priority (KiCad 9+); Default has INT_MAX
Definition rules.hpp:26