TraceMaker
C++ API of the engine
Loading...
Searching...
No Matches
board_editor.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#pragma once
3// Edits a loaded .kicad_pcb document in place, KiCad style (design doc 08 §3). Only the nodes TraceMaker
4// owns change: new segment/via nodes, removed tracks, and footprint placement. Everything else is kept
5// byte for byte. Re-read the document after editing to get an updated board model.
6#include <cstdint>
7#include <string>
8#include <string_view>
9
11
12namespace tmk::io {
13
15 public:
16 // `seed` makes generated UUIDs deterministic.
17 BoardEditor(LoadedBoard& board, std::uint64_t seed = 1) : lb_(board), seed_(seed) {}
18
19 void add_track(const model::Track& t);
20 void add_via(const model::Via& v);
21 void remove_track(std::size_t index);
22 void remove_via(std::size_t index);
23 // Moves footprint `index` to `pos` with orientation `angle` (degrees). Pad and text orientations, which
24 // KiCad stores as absolute angles, are rotated by the same delta. Flipping sides is not supported here.
25 void move_footprint(std::size_t index, model::Point pos, double angle);
26 // Moves footprint `index` to the other side of the board the way KiCad does (FOOTPRINT::Flip, top/bottom, about
27 // its own origin: local y and pad orientations negated, text angles 180° − a, F.* <-> B.* layers, texts
28 // mirrored), then puts it at `pos` with the final orientation `angle` in degrees (flipped orientation −angle0
29 // plus any rotation). Only for footprints where flip_supported() holds.
30 void flip_footprint(std::size_t index, model::Point pos, double angle);
31
32 std::string write() const { return lb_.doc.write(); }
33 void save(const std::string& path) const { lb_.doc.save(path); }
34
35 // Deterministic RFC-4122-style v4 UUID string.
36 std::string next_uuid();
37
38 private:
39 std::string net_expr(model::NetId net) const;
40
41 LoadedBoard& lb_;
42 std::uint64_t seed_;
43 std::uint64_t uuid_counter_ = 0;
44};
45
46// True if BoardEditor::flip_footprint mirrors every item of footprint `index` exactly: no copper on inner
47// layers, no padstacks, no text on copper, and nothing it does not know (zones, text boxes, dimensions, private
48// layers, ...). `why` receives the first reason it cannot.
49bool flip_supported(const LoadedBoard& board, std::size_t index, std::string* why = nullptr);
50// KiCad's FlipLayer for outer layers: the first and last copper layer of `board` (by their names in the file) and
51// F.X <-> B.X for the other side-specific layers; any other name unchanged.
52std::string flip_layer_name(const model::Board& board, std::string_view name);
53
54// Formats an angle the way KiCad writes it: shortest decimal, no trailing zeros ("90", "45.5").
55std::string format_angle(double deg);
56
57} // namespace tmk::io
Reads .kicad_pcb files (KiCad 9 and 10 formats) into the board model, keeping the s-expression docume...
void add_via(const model::Via &v)
void flip_footprint(std::size_t index, model::Point pos, double angle)
Moves footprint index to the other side of the board the way KiCad does (FOOTPRINT::Flip,...
void save(const std::string &path) const
std::string next_uuid()
Deterministic RFC-4122-style v4 UUID string.
std::string write() const
void move_footprint(std::size_t index, model::Point pos, double angle)
Moves footprint index to pos with orientation angle (degrees).
void remove_track(std::size_t index)
void remove_via(std::size_t index)
BoardEditor(LoadedBoard &board, std::uint64_t seed=1)
seed makes generated UUIDs deterministic.
void add_track(const model::Track &t)
std::string format_angle(double deg)
Formats an angle the way KiCad writes it: shortest decimal, no trailing zeros ("90",...
std::string flip_layer_name(const model::Board &board, std::string_view name)
KiCad's FlipLayer for outer layers: the first and last copper layer of board (by their names in the f...
bool flip_supported(const LoadedBoard &board, std::size_t index, std::string *why=nullptr)
True if BoardEditor::flip_footprint mirrors every item of footprint index exactly: no copper on inner...
std::int32_t NetId
0 = no net
Definition board.hpp:21