TraceMaker
C++ API of the engine
Loading...
Searching...
No Matches
srj.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#pragma once
3// Simple Route JSON (SRJ), tscircuit's autorouting problem format (design doc 08 §9, D90): read a problem, write
4// it as a self-contained KiCad board plus project, and write a routed board's copper back as SRJ `traces`.
5//
6// Field reference: lib/types/srj-types.ts in github.com/tscircuit/tscircuit-autorouter (MIT) and the page
7// "advanced/simple-route-json" on docs.tscircuit.com. SRJ is millimetres, y up, with layers named "top",
8// "inner1" .. "innerN", "bottom"; KiCad is y down, so y is negated on the way in and back on the way out.
9#include <optional>
10#include <string>
11#include <vector>
12
13#include "core/units.hpp"
14#include "model/board.hpp"
15
16namespace tmk::io {
17
18struct SrjPoint {
19 double x = 0, y = 0; // millimetres as written (kept so that export repeats the terminal exactly)
20 std::vector<int> layers; // stack indices, ascending; one entry for `layer`, several for `layers`
21 std::string point_id, pcb_port_id;
22};
23
25 std::string id, component;
26 double x = 0, y = 0, width = 0, height = 0; // centre and size, millimetres
27 double rotation = 0; // ccwRotationDegrees
28 bool circle = false; // shape "circle": diameter = width
29 bool non_plated_hole = false, copper_pour = false;
30 std::vector<int> layers; // stack indices, ascending
31 std::vector<std::string> connected_to; // with offBoardConnectsTo appended: both join the obstacle to a net
32 int net = -1; // index into SrjProblem::nets, -1 = no net
33};
34
36 std::string name;
37 std::optional<double> width; // nominalTraceWidth (or the deprecated `width`), millimetres
38 std::vector<SrjPoint> points;
39 int net = -1;
40};
41
42// One electrical net: the connections and obstacles that tscircuit's connectivity map
43// (lib/utils/getConnectivityMapFromSimpleRouteJson.ts) puts in one group.
44struct SrjNet {
45 std::string name; // KiCad net name: the name of the net's first connection in file order
46 std::vector<int> connections; // ascending
47 std::vector<int> obstacles; // ascending
48};
49
50struct SrjRules {
51 double min_trace_width = 0;
52 std::optional<double> nominal_trace_width;
53 std::optional<double> via_pad, via_hole; // as resolved by tscircuit's getViaDimensions; unset = not in the file
54 std::optional<double> obstacle_margin; // defaultObstacleMargin
56 bool blind_buried_vias = false, via_in_pad = false;
57};
58
59// A bus whose `termination` is a plane: every connection of the bus ends in a plane on `layer`. Imported as a
60// zone of the net on that layer covering the bounds (the balls then reach it with a via, as plane drops do).
61struct SrjPlane {
62 int net = -1;
63 int layer = 0;
64};
65
66struct SrjProblem {
67 int layer_count = 0;
69 double min_x = 0, max_x = 0, min_y = 0, max_y = 0; // bounds
70 std::vector<std::pair<double, double>> outline; // empty = the bounds rectangle is the board
71 std::vector<SrjObstacle> obstacles;
72 std::vector<SrjConnection> connections;
73 std::vector<SrjNet> nets;
74 std::vector<SrjPlane> planes;
75 // Extension (not tscircuit's): `tracemakerTrackLayers`, the layers tracks may use; the others get a rule area that
76 // forbids tracks and still lets vias through. Empty = every layer.
77 std::vector<int> track_layers;
78 std::vector<std::string> warnings; // fields that were read but cannot be honoured
79};
80
81// Parses SRJ text. Throws std::runtime_error on malformed input or a field whose meaning is unknown where it
82// matters (e.g. a layer name outside top/innerN/bottom).
83SrjProblem read_srj(const std::string& text);
84SrjProblem read_srj_file(const std::string& path);
85
86// "top", "inner1", ..., "bottom" for a stack index, and back (-1 if the name is not a layer of the stack).
87std::string srj_layer_name(int z, int layer_count);
88int srj_layer_index(const std::string& name, int layer_count);
89
90// tscircuit's fallbacks for what a problem leaves out: its router's default obstacle margin
91// (`defaultObstacleMargin ?? 0.15`) and getViaDimensions' pad diameter.
92inline constexpr double kSrjDefaultClearanceMm = 0.15;
93inline constexpr double kSrjDefaultViaPadMm = 0.3;
94
96 std::optional<Coord> clearance; // copper clearance to use instead of defaultObstacleMargin / the 0.15 mm fallback
97 // Drill edge to drill edge, instead of the problem's minViaHoleEdgeToViaHoleEdgeClearance /
98 // minPlatedHoleDrillEdgeToDrillEdgeClearance (no rule when it has neither).
99 std::optional<Coord> hole_to_hole;
100 // The board outline (and plane zones) grow by this on every side; pads keep their coordinates. For problems whose
101 // connections end exactly on the bounds (breakout points), where the board-edge rule would forbid the copper.
103 // Obstacles of a net that no connection point lies on are keep-outs, not pads of the net: a fan-out problem lists
104 // the far component's pads in the nets (connectedTo) but only asks for the connections at the package.
105 bool unpointed_keepout = false;
106};
107
109 std::string pcb, pro, dru; // file contents; `dru` is empty when no custom rule is needed
110 std::vector<std::string> warnings;
111 int nets = 0, pads = 0, terminal_pads = 0; // terminal_pads: pads made for points that lie on no obstacle
112 // Connections the problem asks for (points - 1 per connection) and the ratsnest size the board implies
113 // (pads - 1 per net). They differ when a net has a pad without a point or its connections do not form a tree.
114 int srj_pairs = 0, board_pairs = 0;
116};
117
118// The board is the problem mirrored in y at the same coordinates (x, -y), one footprint per componentId. The
119// output depends only on the problem and the options (UUIDs are counted, not drawn), so it is byte-identical
120// across runs.
121SrjBoardFiles srj_to_kicad(const SrjProblem& problem, const SrjImportOptions& opt = {});
122
123struct SrjExport {
124 std::string json; // the problem with `traces` set to the routed copper
125 int connections = 0, routed = 0;
126 std::vector<std::string> unrouted; // connection names without a copper path between their points
127 int traces = 0, extra_traces = 0; // extra: copper on no path between two points of a connection (stubs, loops)
128 int vias = 0;
129 std::vector<std::string> warnings;
130};
131
132// Writes the tracks and vias of `routed` (a board made by srj_to_kicad from `problem_text`, then routed) as SRJ
133// traces: one trace per pair of consecutive points of a connection, following the copper between their pads,
134// then the copper no such path used. Nets are matched by name, pads by position.
135SrjExport srj_export(const std::string& problem_text, const model::Board& routed);
136
137} // namespace tmk::io
The board model (design doc 02 §3): every object TraceMaker reads from a .kicad_pcb,...
int srj_layer_index(const std::string &name, int layer_count)
SrjExport srj_export(const std::string &problem_text, const model::Board &routed)
Writes the tracks and vias of routed (a board made by srj_to_kicad from problem_text,...
std::string srj_layer_name(int z, int layer_count)
"top", "inner1", ..., "bottom" for a stack index, and back (-1 if the name is not a layer of the stac...
SrjBoardFiles srj_to_kicad(const SrjProblem &problem, const SrjImportOptions &opt={})
The board is the problem mirrored in y at the same coordinates (x, -y), one footprint per componentId...
constexpr double kSrjDefaultClearanceMm
tscircuit's fallbacks for what a problem leaves out: its router's default obstacle margin (defaultObs...
Definition srj.hpp:92
constexpr double kSrjDefaultViaPadMm
Definition srj.hpp:93
SrjProblem read_srj(const std::string &text)
Parses SRJ text.
SrjProblem read_srj_file(const std::string &path)
std::int64_t Coord
Engine coordinate: signed 64-bit nanometres.
Definition units.hpp:12
std::string pcb
Definition srj.hpp:109
std::vector< std::string > warnings
Definition srj.hpp:110
int srj_pairs
Connections the problem asks for (points - 1 per connection) and the ratsnest size the board implies ...
Definition srj.hpp:114
std::string dru
file contents; dru is empty when no custom rule is needed
Definition srj.hpp:109
std::string pro
Definition srj.hpp:109
int terminal_pads
terminal_pads: pads made for points that lie on no obstacle
Definition srj.hpp:111
std::vector< SrjPoint > points
Definition srj.hpp:38
std::optional< double > width
nominalTraceWidth (or the deprecated width), millimetres
Definition srj.hpp:37
std::string name
Definition srj.hpp:36
int extra_traces
extra: copper on no path between two points of a connection (stubs, loops)
Definition srj.hpp:127
std::vector< std::string > warnings
Definition srj.hpp:129
std::vector< std::string > unrouted
connection names without a copper path between their points
Definition srj.hpp:126
std::string json
the problem with traces set to the routed copper
Definition srj.hpp:124
Coord outline_margin
The board outline (and plane zones) grow by this on every side; pads keep their coordinates.
Definition srj.hpp:102
bool unpointed_keepout
Obstacles of a net that no connection point lies on are keep-outs, not pads of the net: a fan-out pro...
Definition srj.hpp:105
std::optional< Coord > clearance
copper clearance to use instead of defaultObstacleMargin / the 0.15 mm fallback
Definition srj.hpp:96
std::optional< Coord > hole_to_hole
Drill edge to drill edge, instead of the problem's minViaHoleEdgeToViaHoleEdgeClearance / minPlatedHo...
Definition srj.hpp:99
One electrical net: the connections and obstacles that tscircuit's connectivity map (lib/utils/getCon...
Definition srj.hpp:44
std::vector< int > connections
ascending
Definition srj.hpp:46
std::vector< int > obstacles
ascending
Definition srj.hpp:47
std::string name
KiCad net name: the name of the net's first connection in file order.
Definition srj.hpp:45
std::vector< std::string > connected_to
with offBoardConnectsTo appended: both join the obstacle to a net
Definition srj.hpp:31
std::string id
Definition srj.hpp:25
std::vector< int > layers
stack indices, ascending
Definition srj.hpp:30
int net
index into SrjProblem::nets, -1 = no net
Definition srj.hpp:32
bool circle
shape "circle": diameter = width
Definition srj.hpp:28
std::string component
Definition srj.hpp:25
double rotation
ccwRotationDegrees
Definition srj.hpp:27
double height
centre and size, millimetres
Definition srj.hpp:26
A bus whose termination is a plane: every connection of the bus ends in a plane on layer.
Definition srj.hpp:61
std::vector< int > layers
stack indices, ascending; one entry for layer, several for layers
Definition srj.hpp:20
double y
millimetres as written (kept so that export repeats the terminal exactly)
Definition srj.hpp:19
std::string pcb_port_id
Definition srj.hpp:21
std::string point_id
Definition srj.hpp:21
std::vector< SrjPlane > planes
Definition srj.hpp:74
std::vector< int > track_layers
Extension (not tscircuit's): tracemakerTrackLayers, the layers tracks may use; the others get a rule ...
Definition srj.hpp:77
std::vector< std::string > warnings
fields that were read but cannot be honoured
Definition srj.hpp:78
SrjRules rules
Definition srj.hpp:68
std::vector< SrjConnection > connections
Definition srj.hpp:72
std::vector< SrjNet > nets
Definition srj.hpp:73
std::vector< std::pair< double, double > > outline
empty = the bounds rectangle is the board
Definition srj.hpp:70
double max_y
bounds
Definition srj.hpp:69
std::vector< SrjObstacle > obstacles
Definition srj.hpp:71
std::optional< double > via_hole_to_via_hole
Definition srj.hpp:55
std::optional< double > trace_to_hole
Definition srj.hpp:55
double min_trace_width
Definition srj.hpp:51
std::optional< double > drill_to_drill
Definition srj.hpp:55
std::optional< double > pad_to_pad
Definition srj.hpp:55
std::optional< double > via_hole
as resolved by tscircuit's getViaDimensions; unset = not in the file
Definition srj.hpp:53
std::optional< double > trace_to_pad
Definition srj.hpp:55
std::optional< double > board_edge
Definition srj.hpp:55
bool via_in_pad
Definition srj.hpp:56
std::optional< double > nominal_trace_width
Definition srj.hpp:52
bool blind_buried_vias
Definition srj.hpp:56
std::optional< double > obstacle_margin
defaultObstacleMargin
Definition srj.hpp:54
std::optional< double > via_to_pad
Definition srj.hpp:55
std::optional< double > via_pad
Definition srj.hpp:53