TraceMaker
C++ API of the engine
Loading...
Searching...
No Matches
route_job.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#pragma once
3// One `tracemaker route` run as a reusable function, shared by the CLI (src/app/main.cpp) and the Python bindings
4// (bindings/), so both take exactly the same code path (design doc 08 §5).
5#include <functional>
6#include <string>
7
8#include <nlohmann/json.hpp>
9
12#include "route/bus.hpp"
13#include "route/router.hpp"
14
15namespace tmk::app {
16
17struct RouteJob {
18 std::string in; // input .kicad_pcb (its project files supply the design rules)
19 std::string out; // output .kicad_pcb; empty = do not write a board
20 route::RouterOptions opt; // `sink` is set here when viewing or recording
21 int threads = 8; // worker threads for the portfolio variants
22 // Portfolio size (1 = single router). 0 = automatic: all variants (route::portfolio_size()) with a work budget,
23 // so the output does not depend on `threads` (D47); otherwise `threads` (wall-clock mode: one variant per thread).
24 int variants = 0;
25 std::string kb_path; // knowledge base file; empty = none
26 std::string items_out; // --emit-items file; empty = none
27 std::string json_out; // --json summary file; empty = none
28 // --report FILE (doc 18 D3, D136): a run report for designers, HTML (or Markdown for .md), rendered from the summary alone. It adds
29 // the rules read and not read, the stuck report and a DRC of the board before and after to the summary, and writes the summary
30 // to `json_out`, else beside the report as FILE.json, so every number in the report is a field of a file the reader has.
31 std::string report_out;
32 nlohmann::json options_given; // the options as given on the command line and in --config, [{"name", "value"}]; reported only
33 bool view = false; // stream to the browser viewer
34 // Reference-return audit of selected nets on the routed board (issue #7, doc 05 §42): off unless a selector is given. Read-only; the
35 // report goes to the log and the summary, and each net's return path to the viewer.
37 // Critical nets (issue #24, doc 05 §44, D117): route critical signals first, give them dearer vias, keep them over solid reference copper. Off
38 // unless asked; `si_rules` turns the three on with the settings below unless they were given.
39 std::vector<std::string> critical_nets; // KiCad wildcards: nets that are critical whatever their role (tier 1)
40 bool critical_auto = false; // also the nets the component rules' roles make critical, and differential pairs
41 bool critical_first = false; // route them before all other nets, tier by tier
42 double critical_via_mult = 1; // a via costs them this many times as much
43 bool critical_via_given = false; // set by the command line: with si_rules, a value that was given is kept (else x3)
44 int critical_via_max = 0; // warn when a critical net has more vias than this (0 = no limit)
45 double split_cost = 0; // pitches per lattice step over a void of the reference plane
46 bool split_cost_given = false; // as critical_via_given (else +4 with si_rules)
47 bool split_hard = false; // such cells are closed to critical nets
48 bool si_rules = false;
49 // With si_rules: layer changes of critical nets that the reference-return audit finds without a stitch get return-path stitching vias
50 // of their plane net (doc 05 §57); --no-si-stitch turns it off.
51 bool si_stitch = true;
52 // --footprint-refs (issue #5, doc 05 §43): every footprint is checked against the datasheet land patterns of a reference library before the first track is
53 // routed; a discrepancy is an error and stops the job unless footprint_warn_only.
54 std::string footprint_refs; // the library: a directory or a JSON file; empty = no check
55 std::string footprint_trusted; // trusted brand list; empty = trusted_brands.txt beside the references
56 bool footprint_trust_all = false;
57 bool footprint_warn_only = false;
59 bool view_freeze = true; // the viewer holds the scene on a routing stall (issue #8); --no-freeze says false in the board snapshot
60 std::string view_host; // "" = $TM_VIEW_HOST, else loopback: a viewer on any other address needs a token (issue #4, D116)
61 std::string view_token; // the token for a non-loopback viewer (empty = a random one)
62 bool view_insecure = false; // a non-loopback viewer without a token (--view-insecure)
63 std::string view_embed; // pages that may frame the viewer (--view-embed); empty = none
64 int view_port = 8766;
65 bool hold = false; // keep serving the viewer after routing; never returns (CLI only)
66 std::string record; // events file for replay (ignored while viewing)
67 // --component-rules (doc 15 §3.5): "off" (default), "report" or "soft" (detect; write the sidecar .kicad_dru next
68 // to the output), "on" (also route with the generated keep-outs as in-memory rule areas; never written into the
69 // output board).
70 std::string component_rules = "off";
71 // --reroute: remove every unlocked track and via first and route the whole board again (locked copper, arcs and
72 // zones stay; rule 6).
73 bool reroute = false;
74 // --copper-layers N (doc 05 §53): route the board with N copper layers, inner layers added (or empty ones removed) in memory and in the
75 // output; 0 = as the file has them. `route --try-layers` sets it for each count it tries.
77 // --pour (doc 05 §53, M20 C3): zones to add before routing, "auto" or "NET@LAYER" (passes::plan_pours). They are routed with soft
78 // zones (switched on with them) and written into the output with the engine's fill.
79 std::vector<std::string> pours;
80 // Zones without a fill (doc 05 §54): filled in memory before routing and routed with soft zones, so a plane net's pads drop a via into
81 // their plane instead of being joined by tracks; --no-fill-planes turns it off (the zones are then ignored while routing).
82 bool fill_planes = true;
83 std::string warm_pcb; // --warm-pcb: a routed board whose tracks and vias are the starting point (kept where legal under the input's rules; nets of one connection)
84 std::string rules_override; // --rules-override: user override file (JSON, doc 15 §6.3); empty = none
85 // Advice (doc 17, issue #26). The engine never talks to a model: it reads an advice file, and an advisor (any program
86 // that turns a stuck report into an advice file) is consulted only when the deterministic route leaves connections open.
87 std::string advice; // --advice FILE: typed hints (src/advice/advice.hpp); advised variants run next to the unadvised ones
88 int advice_variants = 0; // --advice-variants N: advised variants (0 = the file's "variants", else 4)
89 std::string advisor; // --advisor CMD: a shell command that reads the stuck report on stdin and writes advice on stdout
90 double advisor_timeout_s = 120; // --advisor-timeout S: the advisor's time limit (its wall time is not routing work)
91 int advisor_rounds = 1; // --advisor-rounds N: consult again with the result of the advised run while connections are left
92 bool advisor_redact = false; // --advisor-redact: the report names nets and parts by alias (N1, P3); the advice is mapped back
93 std::string advice_out; // --advice-out FILE: where the advice that was used is saved (default: <out>.advice.json)
94 std::string stuck_report_out; // --stuck-report FILE: write the stuck report (of the run the advisor saw, else the final one)
95 // With soft zones: refill the zones after routing and route what the refill left unconnected (doc 05 §36).
96 bool refill_repair = true;
97 // --no-tracks-on / --layer-cost (doc 05 §29): copper layer names, and "LAYER=FACTOR" entries. Resolved against
98 // the board into opt.no_track_layers / opt.layer_cost_pm; an unknown layer or a bad factor throws.
99 std::vector<std::string> no_tracks_on;
100 std::vector<std::string> layer_costs;
101 // Buses (doc 05 §38): "NAME=PATTERN" groups (--bus), automatic detection (--buses) and the skew limit of the
102 // command-line groups in mm (--bus-skew-mm; 0 = none). A custom skew rule of the board wins over the option.
103 std::vector<std::string> buses;
104 bool detect_buses = false;
105 double bus_skew_mm = 0;
106 // After-route passes (doc 05 §39), all off by default. Each changes the new copper only through the router's
107 // exact check and is kept only if no net has more unconnected items with it than without (rule 4).
108 // --widen-power: new tracks narrower than their net class width go back up to it where there is room; nets with
109 // a target (--widen-net "PATTERN=WIDTH_MM", or a width the component rules computed) are widened towards it.
110 bool widen_power = false;
111 std::vector<std::string> widen_nets;
112 // --stitch-vias: "planes" (vias of a plane's net on a grid where its fills overlap on two or more layers),
113 // "return" (a plane via near each via of the signals that need a reference plane: component rules of kind
114 // reference_plane, and --stitch-return "PATTERN=PLANE_NET"), or "all". Empty = off.
115 std::string stitch_vias;
116 double stitch_pitch_mm = 0; // 0 = the catalogue's stitch_via_within_mm
117 double stitch_distance_mm = 0; // 0 = the rule's stitch_via_mm, else the catalogue value
118 std::vector<std::string> stitch_returns;
119 long pass_work = 2'000'000; // work budget of each pass, in exact checks (rule 5)
120 // Progress lines (the CLI's stdout text, one line per call, no trailing newline). Empty = silent.
121 std::function<void(const std::string&)> log;
122};
123
126 nlohmann::json items; // new copper in the --emit-items layout (layer and net by name, nm)
127 nlohmann::json summary; // the --json layout
128 std::string viewer_url; // empty when not viewing
129 int unconnected_after_refill = -1; // with soft zones: what KiCad's refill would leave unconnected
130 // 0 when everything planned was routed (and, with soft zones, nothing is unconnected after the refill).
131 int exit_code() const { return unconnected_after_refill >= 0 ? (unconnected_after_refill == 0 ? 0 : 3) : result.routed == result.connections ? 0 : 3; }
132};
133
134// Reads the board and its rules, routes (a portfolio when more than one variant runs), writes the requested outputs.
135// Throws std::exception on unreadable input.
137
138// The layer-count advisor (`route --try-layers`, doc 05 §53, M20 C3): routes the board with each copper layer count in `counts`,
139// fewest first, and stops at the first that completes (exit code 0: everything routed and, with soft zones, nothing unconnected after
140// the refill). Its board is written to `job.out` and its summary returned, with a "layers" object listing every count tried. A count
141// the board cannot take (inner layers that are not empty) is reported and skipped. When none completes, the result of the count that
142// left the fewest connections open is returned (the first such count on a tie).
143RouteJobResult run_layer_advisor(RouteJob job, std::vector<int> counts);
144
145// Buses (doc 05 §38): the groups `specs` ("NAME=PATTERN") name on the board, then, with `detect`, the groups found in
146// the net names that share no net with an earlier group. Each group gets the skew limit of the board's custom
147// rules, else, for groups from `specs`, `skew_mm` (0 = none). `notes` receives warnings. Throws on a bad spec.
148std::vector<route::BusGroup> resolve_buses(const model::Board& b, const model::DesignRules& rules, const std::vector<std::string>& specs,
149 bool detect, double skew_mm, std::vector<std::string>* notes = nullptr);
150// A group measured on the copper of `b` (`joined`: route::nets_joined of the same board), as the JSON object of the
151// route summary's `buses` array and of `tracemaker buses --json`; `line`, if given, gets the one-line text form.
152// Lengths are KiCad's: tracks plus the stackup height of the vias (`rules` says whether the project counts it).
153nlohmann::json bus_report(const model::Board& b, const model::DesignRules& rules, const std::vector<std::uint8_t>& joined, const route::BusGroup& g,
154 std::string* line = nullptr);
155
156// The GPU device used for cost-to-go fields: the first listed device, or -1 (none, or `use_gpu` false).
157int default_gpu_device(bool use_gpu);
158
159} // namespace tmk::app
Buses (design doc 05 §38): groups of nets that belong together (a data bus, an address bus,...
Footprint check against datasheet land patterns (issue #5, docs/05-routing.md §43,...
RouteJobResult run_layer_advisor(RouteJob job, std::vector< int > counts)
The layer-count advisor (route --try-layers, doc 05 §53, M20 C3): routes the board with each copper l...
std::vector< route::BusGroup > resolve_buses(const model::Board &b, const model::DesignRules &rules, const std::vector< std::string > &specs, bool detect, double skew_mm, std::vector< std::string > *notes=nullptr)
Buses (doc 05 §38): the groups specs ("NAME=PATTERN") name on the board, then, with detect,...
RouteJobResult run_route_job(RouteJob job)
Reads the board and its rules, routes (a portfolio when more than one variant runs),...
int default_gpu_device(bool use_gpu)
The GPU device used for cost-to-go fields: the first listed device, or -1 (none, or use_gpu false).
nlohmann::json bus_report(const model::Board &b, const model::DesignRules &rules, const std::vector< std::uint8_t > &joined, const route::BusGroup &g, std::string *line=nullptr)
A group measured on the copper of b (joined: route::nets_joined of the same board),...
Reference-return audit of high-speed nets (issue #7, docs/05-routing.md §42, D114).
TraceMaker's detailed router, version 1 (roadmap M4; design doc 05 §2, §5).
std::string viewer_url
empty when not viewing
int exit_code() const
0 when everything planned was routed (and, with soft zones, nothing is unconnected after the refill).
int unconnected_after_refill
with soft zones: what KiCad's refill would leave unconnected
route::RouteResult result
nlohmann::json items
new copper in the –emit-items layout (layer and net by name, nm)
nlohmann::json summary
the –json layout
std::string rules_override
–rules-override: user override file (JSON, doc 15 §6.3); empty = none
Definition route_job.hpp:84
std::string warm_pcb
–warm-pcb: a routed board whose tracks and vias are the starting point (kept where legal under the in...
Definition route_job.hpp:83
std::vector< std::string > widen_nets
std::string footprint_trusted
trusted brand list; empty = trusted_brands.txt beside the references
Definition route_job.hpp:55
std::vector< std::string > buses
Buses (doc 05 §38): "NAME=PATTERN" groups (–bus), automatic detection (–buses) and the skew limit of ...
bool fill_planes
Zones without a fill (doc 05 §54): filled in memory before routing and routed with soft zones,...
Definition route_job.hpp:82
bool hold
keep serving the viewer after routing; never returns (CLI only)
Definition route_job.hpp:65
std::string stitch_vias
–stitch-vias: "planes" (vias of a plane's net on a grid where its fills overlap on two or more layers...
int copper_layers
–copper-layers N (doc 05 §53): route the board with N copper layers, inner layers added (or empty one...
Definition route_job.hpp:76
std::string report_out
–report FILE (doc 18 D3, D136): a run report for designers, HTML (or Markdown for ....
Definition route_job.hpp:31
std::string advisor
–advisor CMD: a shell command that reads the stuck report on stdin and writes advice on stdout
Definition route_job.hpp:89
double stitch_pitch_mm
0 = the catalogue's stitch_via_within_mm
std::vector< std::string > stitch_returns
bool reroute
–reroute: remove every unlocked track and via first and route the whole board again (locked copper,...
Definition route_job.hpp:73
std::function< void(const std::string &)> log
Progress lines (the CLI's stdout text, one line per call, no trailing newline). Empty = silent.
std::vector< std::string > layer_costs
std::string advice
Advice (doc 17, issue #26).
Definition route_job.hpp:87
std::string component_rules
–component-rules (doc 15 §3.5): "off" (default), "report" or "soft" (detect; write the sidecar ....
Definition route_job.hpp:70
std::vector< std::string > pours
–pour (doc 05 §53, M20 C3): zones to add before routing, "auto" or "NET\@LAYER" (passes::plan_pours).
Definition route_job.hpp:79
std::string record
events file for replay (ignored while viewing)
Definition route_job.hpp:66
int advice_variants
–advice-variants N: advised variants (0 = the file's "variants", else 4)
Definition route_job.hpp:88
int variants
Portfolio size (1 = single router).
Definition route_job.hpp:24
analysis::FootprintCheckOptions footprint_opt
Definition route_job.hpp:58
double split_cost
pitches per lattice step over a void of the reference plane
Definition route_job.hpp:45
std::string json_out
–json summary file; empty = none
Definition route_job.hpp:27
long pass_work
work budget of each pass, in exact checks (rule 5)
std::string kb_path
knowledge base file; empty = none
Definition route_job.hpp:25
int critical_via_max
warn when a critical net has more vias than this (0 = no limit)
Definition route_job.hpp:44
std::string view_host
"" = $TM_VIEW_HOST, else loopback: a viewer on any other address needs a token (issue #4,...
Definition route_job.hpp:60
bool si_stitch
With si_rules: layer changes of critical nets that the reference-return audit finds without a stitch ...
Definition route_job.hpp:51
double advisor_timeout_s
–advisor-timeout S: the advisor's time limit (its wall time is not routing work)
Definition route_job.hpp:90
bool advisor_redact
–advisor-redact: the report names nets and parts by alias (N1, P3); the advice is mapped back
Definition route_job.hpp:92
nlohmann::json options_given
the options as given on the command line and in –config, [{"name", "value"}]; reported only
Definition route_job.hpp:32
std::string items_out
–emit-items file; empty = none
Definition route_job.hpp:26
int threads
worker threads for the portfolio variants
Definition route_job.hpp:21
double stitch_distance_mm
0 = the rule's stitch_via_mm, else the catalogue value
std::string view_token
the token for a non-loopback viewer (empty = a random one)
Definition route_job.hpp:61
route::RouterOptions opt
sink is set here when viewing or recording
Definition route_job.hpp:20
std::string out
output .kicad_pcb; empty = do not write a board
Definition route_job.hpp:19
analysis::ReturnPathOptions return_paths
Reference-return audit of selected nets on the routed board (issue #7, doc 05 §42): off unless a sele...
Definition route_job.hpp:36
bool view_insecure
a non-loopback viewer without a token (–view-insecure)
Definition route_job.hpp:62
bool widen_power
After-route passes (doc 05 §39), all off by default.
std::string view_embed
pages that may frame the viewer (–view-embed); empty = none
Definition route_job.hpp:63
std::vector< std::string > no_tracks_on
–no-tracks-on / –layer-cost (doc 05 §29): copper layer names, and "LAYER=FACTOR" entries.
Definition route_job.hpp:99
int advisor_rounds
–advisor-rounds N: consult again with the result of the advised run while connections are left
Definition route_job.hpp:91
bool split_cost_given
as critical_via_given (else +4 with si_rules)
Definition route_job.hpp:46
std::vector< std::string > critical_nets
Critical nets (issue #24, doc 05 §44, D117): route critical signals first, give them dearer vias,...
Definition route_job.hpp:39
bool split_hard
such cells are closed to critical nets
Definition route_job.hpp:47
std::string footprint_refs
–footprint-refs (issue #5, doc 05 §43): every footprint is checked against the datasheet land pattern...
Definition route_job.hpp:54
bool refill_repair
With soft zones: refill the zones after routing and route what the refill left unconnected (doc 05 §3...
Definition route_job.hpp:96
std::string advice_out
–advice-out FILE: where the advice that was used is saved (default: <out>.advice.json)
Definition route_job.hpp:93
bool critical_auto
also the nets the component rules' roles make critical, and differential pairs
Definition route_job.hpp:40
std::string in
input .kicad_pcb (its project files supply the design rules)
Definition route_job.hpp:18
double critical_via_mult
a via costs them this many times as much
Definition route_job.hpp:42
bool view_freeze
the viewer holds the scene on a routing stall (issue #8); –no-freeze says false in the board snapshot
Definition route_job.hpp:59
bool critical_first
route them before all other nets, tier by tier
Definition route_job.hpp:41
bool critical_via_given
set by the command line: with si_rules, a value that was given is kept (else x3)
Definition route_job.hpp:43
bool view
stream to the browser viewer
Definition route_job.hpp:33
std::string stuck_report_out
–stuck-report FILE: write the stuck report (of the run the advisor saw, else the final one)
Definition route_job.hpp:94