traceforge-pcb 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- traceforge/__init__.py +3 -0
- traceforge/__main__.py +3 -0
- traceforge/acp_server.py +570 -0
- traceforge/agent.py +680 -0
- traceforge/altium/__init__.py +6 -0
- traceforge/altium/cfb.py +70 -0
- traceforge/altium/cli.py +60 -0
- traceforge/altium/model.py +163 -0
- traceforge/altium/outputs.py +24 -0
- traceforge/altium/pcb.py +301 -0
- traceforge/altium/project.py +328 -0
- traceforge/altium/schematic.py +400 -0
- traceforge/altium/tools.py +166 -0
- traceforge/approval.py +219 -0
- traceforge/attachments.py +346 -0
- traceforge/background.py +253 -0
- traceforge/bench/__init__.py +5 -0
- traceforge/bench/adapters.py +337 -0
- traceforge/bench/checks.py +410 -0
- traceforge/bench/cli.py +121 -0
- traceforge/bench/design.py +439 -0
- traceforge/bench/kicad.py +99 -0
- traceforge/bench/partscheck.py +159 -0
- traceforge/bench/review.py +100 -0
- traceforge/bench/runner.py +226 -0
- traceforge/bench/score.py +204 -0
- traceforge/bench/tasks.py +102 -0
- traceforge/chat.py +1185 -0
- traceforge/checks.py +328 -0
- traceforge/checks_sides.py +118 -0
- traceforge/cli.py +2224 -0
- traceforge/clipboard.py +107 -0
- traceforge/config.py +299 -0
- traceforge/context.py +276 -0
- traceforge/design/__init__.py +1 -0
- traceforge/design/constraints.py +397 -0
- traceforge/design/generate.py +1789 -0
- traceforge/design/outline.py +905 -0
- traceforge/design/schematic_layout.py +932 -0
- traceforge/design/schematic_lint.py +329 -0
- traceforge/design/schematic_route.py +207 -0
- traceforge/design/sheets.py +854 -0
- traceforge/design/sides.py +328 -0
- traceforge/design/spec.py +462 -0
- traceforge/design/specsheet.py +2351 -0
- traceforge/design/specsheet_render.py +183 -0
- traceforge/desktop/__init__.py +109 -0
- traceforge/desktop/keys.py +90 -0
- traceforge/desktop/project.py +443 -0
- traceforge/desktop/review.py +20 -0
- traceforge/desktop/server.py +1277 -0
- traceforge/desktop/static/app.css +223 -0
- traceforge/desktop/static/app.js +683 -0
- traceforge/desktop/static/highlight.js +77 -0
- traceforge/desktop/static/index.html +89 -0
- traceforge/desktop/static/md.js +90 -0
- traceforge/desktop/static/viewer.js +213 -0
- traceforge/desktop/symbols.py +212 -0
- traceforge/desktop/update.py +65 -0
- traceforge/diagnostics.py +67 -0
- traceforge/errata.py +219 -0
- traceforge/fab.py +108 -0
- traceforge/fab_layers.py +44 -0
- traceforge/features/__init__.py +6 -0
- traceforge/features/candidates.py +117 -0
- traceforge/features/cli.py +200 -0
- traceforge/features/package.py +433 -0
- traceforge/features/revdiff.py +224 -0
- traceforge/features/sarif.py +145 -0
- traceforge/features/viewer.py +265 -0
- traceforge/firmware/__init__.py +3 -0
- traceforge/firmware/cli.py +174 -0
- traceforge/firmware/drivers.py +372 -0
- traceforge/firmware/drivers_data.py +256 -0
- traceforge/firmware/mcus.py +177 -0
- traceforge/firmware/netlist.py +55 -0
- traceforge/firmware/pinmap.py +296 -0
- traceforge/firmware/scaffold.py +466 -0
- traceforge/firmware/toolchain.py +262 -0
- traceforge/hooks.py +237 -0
- traceforge/kicad/__init__.py +0 -0
- traceforge/kicad/cli.py +519 -0
- traceforge/kicad/diffpair.py +174 -0
- traceforge/kicad/edgegeom.py +191 -0
- traceforge/kicad/edit.py +405 -0
- traceforge/kicad/escape.py +1410 -0
- traceforge/kicad/formats.py +1073 -0
- traceforge/kicad/impedance.py +320 -0
- traceforge/kicad/install.py +75 -0
- traceforge/kicad/libraries.py +490 -0
- traceforge/kicad/models3d.py +942 -0
- traceforge/kicad/outputs.py +81 -0
- traceforge/kicad/pcb.py +787 -0
- traceforge/kicad/planes.py +1242 -0
- traceforge/kicad/project.py +136 -0
- traceforge/kicad/route.py +2698 -0
- traceforge/kicad/routegeom.py +164 -0
- traceforge/kicad/schematic.py +268 -0
- traceforge/kicad/sides.py +34 -0
- traceforge/kicad/stackup.py +424 -0
- traceforge/kicad_plugin/__init__.py +12 -0
- traceforge/kicad_plugin/actions.py +92 -0
- traceforge/kicad_plugin/cli.py +66 -0
- traceforge/kicad_plugin/client.py +86 -0
- traceforge/kicad_plugin/context.py +257 -0
- traceforge/kicad_plugin/discovery.py +81 -0
- traceforge/kicad_plugin/install.py +203 -0
- traceforge/kicad_plugin/overlay.py +60 -0
- traceforge/kicad_plugin/payload/action_plugin.py +65 -0
- traceforge/kicad_plugin/payload/ipc_main.py +42 -0
- traceforge/kicad_plugin/payload/plugin.json +90 -0
- traceforge/kicad_plugin/payload/requirements.txt +1 -0
- traceforge/kicad_plugin/views.py +202 -0
- traceforge/langchain.py +57 -0
- traceforge/local.py +580 -0
- traceforge/mcp_server.py +304 -0
- traceforge/models.py +310 -0
- traceforge/next_steps.py +110 -0
- traceforge/parts/__init__.py +52 -0
- traceforge/parts/alternates.py +488 -0
- traceforge/parts/bom.py +423 -0
- traceforge/parts/brief.py +705 -0
- traceforge/parts/catalog.py +297 -0
- traceforge/parts/cli_sourcing.py +156 -0
- traceforge/parts/data/duty_rates.yaml +37 -0
- traceforge/parts/data/hs_codes.yaml +103 -0
- traceforge/parts/datasheet.py +278 -0
- traceforge/parts/fx.py +100 -0
- traceforge/parts/model.py +185 -0
- traceforge/parts/net.py +187 -0
- traceforge/parts/offers.py +46 -0
- traceforge/parts/packages.py +397 -0
- traceforge/parts/pinout.py +714 -0
- traceforge/parts/providers/__init__.py +17 -0
- traceforge/parts/providers/base.py +100 -0
- traceforge/parts/providers/jlc.py +364 -0
- traceforge/parts/providers/keyed.py +661 -0
- traceforge/parts/report.py +508 -0
- traceforge/parts/supply.py +330 -0
- traceforge/parts/verify.py +455 -0
- traceforge/parts/web.py +575 -0
- traceforge/plans.py +70 -0
- traceforge/preferences.py +127 -0
- traceforge/prices.py +130 -0
- traceforge/prices.yaml +28 -0
- traceforge/prompts/__init__.py +54 -0
- traceforge/prompts/system.md +103 -0
- traceforge/providers.py +764 -0
- traceforge/remote/__init__.py +33 -0
- traceforge/remote/client.py +422 -0
- traceforge/remote/oidc.py +240 -0
- traceforge/remote/orgs.py +510 -0
- traceforge/remote/server.py +690 -0
- traceforge/remote/store.py +100 -0
- traceforge/review_changes.py +187 -0
- traceforge/routing.py +99 -0
- traceforge/runtime.py +435 -0
- traceforge/sessions.py +324 -0
- traceforge/sexpr.py +279 -0
- traceforge/sim/__init__.py +17 -0
- traceforge/sim/acmath.py +234 -0
- traceforge/sim/circuit.py +499 -0
- traceforge/sim/cli.py +162 -0
- traceforge/sim/engine.py +172 -0
- traceforge/sim/ltspice.py +175 -0
- traceforge/sim/measure.py +123 -0
- traceforge/sim/models.py +76 -0
- traceforge/sim/project.py +218 -0
- traceforge/sim/rawfile.py +123 -0
- traceforge/sim/results.py +298 -0
- traceforge/sim/tune.py +172 -0
- traceforge/skill_commands.py +165 -0
- traceforge/skill_library/circuit-simulation/SKILL.md +30 -0
- traceforge/skill_library/compliance-review/SKILL.md +51 -0
- traceforge/skill_library/design-review/SKILL.md +47 -0
- traceforge/skill_library/errata-hunt/SKILL.md +30 -0
- traceforge/skill_library/firmware/SKILL.md +26 -0
- traceforge/skill_library/power-budget/SKILL.md +27 -0
- traceforge/skill_library/release-package/SKILL.md +29 -0
- traceforge/skill_library/sourcing-brief/SKILL.md +24 -0
- traceforge/skill_library/thermal-review/SKILL.md +15 -0
- traceforge/skills.py +131 -0
- traceforge/thermal.py +557 -0
- traceforge/tools/__init__.py +48 -0
- traceforge/tools/agent_tools.py +213 -0
- traceforge/tools/base.py +303 -0
- traceforge/tools/batch.py +155 -0
- traceforge/tools/connections.py +245 -0
- traceforge/tools/files.py +777 -0
- traceforge/tools/firmware_tools.py +218 -0
- traceforge/tools/kicad_tools.py +1072 -0
- traceforge/tools/models3d_tools.py +141 -0
- traceforge/tools/parts_tools.py +473 -0
- traceforge/tools/processes.py +246 -0
- traceforge/tools/review_tools.py +178 -0
- traceforge/tools/sim_tools.py +230 -0
- traceforge/tui.py +620 -0
- traceforge/updates.py +124 -0
- traceforge/validate/__init__.py +77 -0
- traceforge/validate/builtins.py +1091 -0
- traceforge/validate/circuits.py +1240 -0
- traceforge/validate/config.py +410 -0
- traceforge/validate/default.yaml +113 -0
- traceforge/validate/design.py +191 -0
- traceforge/validate/explain.py +112 -0
- traceforge/validate/grounding.py +169 -0
- traceforge/validate/ipc2221.py +350 -0
- traceforge/validate/multilayer.py +805 -0
- traceforge/validate/netmodel.py +261 -0
- traceforge/validate/partcheck.py +472 -0
- traceforge/validate/report.py +280 -0
- traceforge/validate/review.py +322 -0
- traceforge/validate/runner.py +662 -0
- traceforge/workspace.py +171 -0
- traceforge_pcb-1.0.0.dist-info/METADATA +463 -0
- traceforge_pcb-1.0.0.dist-info/RECORD +219 -0
- traceforge_pcb-1.0.0.dist-info/WHEEL +4 -0
- traceforge_pcb-1.0.0.dist-info/entry_points.txt +2 -0
- traceforge_pcb-1.0.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
"""Two-sided boards: which side each part is on, and the footprint that goes on the bottom.
|
|
2
|
+
|
|
3
|
+
KiCad stores a bottom-side footprint the way its own Flip command writes it: the layer is
|
|
4
|
+
``B.Cu``, every layer name inside swaps front for back, and the footprint-local geometry is
|
|
5
|
+
mirrored in Y (pads, graphics, courtyard, text). The footprint's orientation is then
|
|
6
|
+
``orientation = 180 - orientation_before_flip`` for a left-right flip, so a part keeps the
|
|
7
|
+
orientation field it was given and only its local frame is mirrored. :func:`flip_footprint` does
|
|
8
|
+
that mirroring on a library footprint; nothing else in the node changes. The 3D model reference is
|
|
9
|
+
left alone: KiCad's 3D viewer turns the model over for every footprint on ``B.Cu`` itself.
|
|
10
|
+
|
|
11
|
+
The placer keeps one list of occupied rectangles per side (:class:`SidedRects`); a through-hole part
|
|
12
|
+
occupies both, because its leads and solder cones come out on the other side.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from traceforge import sexpr
|
|
18
|
+
from traceforge.sexpr import Node, Sym, find, fmt
|
|
19
|
+
|
|
20
|
+
TOP, BOTTOM = "top", "bottom"
|
|
21
|
+
_SWAP = {"F": "B", "B": "F"}
|
|
22
|
+
_CHAMFER = {
|
|
23
|
+
"top_left": "bottom_left",
|
|
24
|
+
"bottom_left": "top_left",
|
|
25
|
+
"top_right": "bottom_right",
|
|
26
|
+
"bottom_right": "top_right",
|
|
27
|
+
}
|
|
28
|
+
# Nodes whose first two numbers are a point in the footprint's frame.
|
|
29
|
+
_POINTS = {"at", "start", "end", "center", "mid", "xy", "offset", "pos", "anchor"}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def opposite(side: str) -> str:
|
|
33
|
+
return BOTTOM if side == TOP else TOP
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _swap_layer(name: str) -> str:
|
|
37
|
+
if len(name) > 1 and name[1] == "." and name[0] in _SWAP:
|
|
38
|
+
return _SWAP[name[0]] + name[1:]
|
|
39
|
+
return name
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _keep(old: str, new: str):
|
|
43
|
+
return Sym(new) if isinstance(old, Sym) else new
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def is_through_hole(footprint: Node) -> bool:
|
|
47
|
+
"""True when the footprint has a plated or unplated hole through the board."""
|
|
48
|
+
for pad in sexpr.find_all(footprint, "pad"):
|
|
49
|
+
if len(pad) > 2 and str(pad[2]) in {"thru_hole", "np_thru_hole"}:
|
|
50
|
+
return True
|
|
51
|
+
return False
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def flip_footprint(node: Node) -> None:
|
|
55
|
+
"""Turn a library footprint over, in place: swap its layers and mirror its frame in Y."""
|
|
56
|
+
_flip(node, parent=None)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _flip(node: Node, parent: str | None) -> None:
|
|
60
|
+
head = str(node[0]) if node and isinstance(node[0], str) else ""
|
|
61
|
+
for i, child in enumerate(node):
|
|
62
|
+
if isinstance(child, str) and i > 0 and head in {"layer", "layers"}:
|
|
63
|
+
node[i] = _keep(child, _swap_layer(str(child)))
|
|
64
|
+
elif isinstance(child, str) and i > 0 and head == "chamfer" and str(child) in _CHAMFER:
|
|
65
|
+
node[i] = _keep(child, _CHAMFER[str(child)])
|
|
66
|
+
if head in _POINTS and len(node) >= 3:
|
|
67
|
+
node[2] = fmt(-sexpr.num(node[2]))
|
|
68
|
+
if head == "at" and len(node) > 3:
|
|
69
|
+
# A pad turns by -angle; texts keep theirs (KiCad shows them upright).
|
|
70
|
+
if parent not in {"fp_text", "property"}:
|
|
71
|
+
node[3] = fmt(-sexpr.num(node[3]))
|
|
72
|
+
elif head == "angle" and parent == "fp_arc" and len(node) > 1:
|
|
73
|
+
node[1] = fmt(-sexpr.num(node[1]))
|
|
74
|
+
elif head == "effects":
|
|
75
|
+
justify = find(node, "justify")
|
|
76
|
+
if justify is None:
|
|
77
|
+
node.append([Sym("justify"), Sym("mirror")])
|
|
78
|
+
elif "mirror" not in [str(a) for a in justify[1:]]:
|
|
79
|
+
justify.append(Sym("mirror"))
|
|
80
|
+
for child in node:
|
|
81
|
+
if isinstance(child, list) and child:
|
|
82
|
+
_flip(child, head)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def mirror_extent(extent: tuple[float, float, float, float]) -> tuple[float, float, float, float]:
|
|
86
|
+
"""A footprint-local box after the flip (Y mirrored)."""
|
|
87
|
+
minx, miny, maxx, maxy = extent
|
|
88
|
+
return (minx, -maxy, maxx, -miny)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def mirror_pads(pads: list[tuple[str, float, float]]) -> list[tuple[str, float, float]]:
|
|
92
|
+
return [(n, x, -y) for n, x, y in pads]
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class SidedRects:
|
|
96
|
+
"""Occupied rectangles per board side; iterating gives those of ``side``.
|
|
97
|
+
|
|
98
|
+
``append`` records a rectangle on the current ``side`` and, when ``through`` is set, on the
|
|
99
|
+
other side too. ``+=`` records keep-outs that hold on both sides (mounting-hole corners).
|
|
100
|
+
"""
|
|
101
|
+
|
|
102
|
+
def __init__(self) -> None:
|
|
103
|
+
self.side = TOP
|
|
104
|
+
self.through = False
|
|
105
|
+
self._rects: dict[str, list[tuple[float, float, float, float]]] = {TOP: [], BOTTOM: []}
|
|
106
|
+
|
|
107
|
+
def __iter__(self):
|
|
108
|
+
if self.through: # leads come out on the far side: both sides must be free
|
|
109
|
+
return iter([*self._rects[TOP], *self._rects[BOTTOM]])
|
|
110
|
+
return iter(self._rects[self.side])
|
|
111
|
+
|
|
112
|
+
def use(self, side: str, through: bool = False) -> None:
|
|
113
|
+
self.side, self.through = side, through
|
|
114
|
+
|
|
115
|
+
def append(self, rect, *, both: bool = False) -> None:
|
|
116
|
+
sides = (TOP, BOTTOM) if both or self.through else (self.side,)
|
|
117
|
+
for side in sides:
|
|
118
|
+
self._rects[side].append(rect)
|
|
119
|
+
|
|
120
|
+
def __iadd__(self, rects):
|
|
121
|
+
for rect in rects:
|
|
122
|
+
self.append(rect, both=True)
|
|
123
|
+
return self
|
|
124
|
+
|
|
125
|
+
def add(self, rect, side: str, through: bool) -> None:
|
|
126
|
+
for s in (TOP, BOTTOM) if through else (side,):
|
|
127
|
+
self._rects[s].append(rect)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def requested_sides(parts) -> dict[str, str]:
|
|
131
|
+
"""Side given in the spec for every part (mounting holes stay on top)."""
|
|
132
|
+
return {
|
|
133
|
+
p.spec.ref: (TOP if p.spec.group == "__holes__" else p.spec.side)
|
|
134
|
+
for p in parts
|
|
135
|
+
if p.footprint is not None
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def derive_decoupling_sides(parts, satellites: dict, sides: dict[str, str]) -> dict[str, str]:
|
|
140
|
+
"""Move capacitors to the opposite side of a host that asks for it (``decoupling:
|
|
141
|
+
opposite``), unless the capacitor's own spec sets a side."""
|
|
142
|
+
by_ref = {p.spec.ref: p for p in parts}
|
|
143
|
+
result = dict(sides)
|
|
144
|
+
for host, subs in satellites.items():
|
|
145
|
+
host_part = by_ref.get(host)
|
|
146
|
+
if host_part is None or host_part.spec.decoupling != "opposite":
|
|
147
|
+
continue
|
|
148
|
+
for sub, _links in subs:
|
|
149
|
+
part = by_ref.get(sub)
|
|
150
|
+
if part is None or "side" in part.spec.model_fields_set:
|
|
151
|
+
continue
|
|
152
|
+
if part.spec.ref.rstrip("0123456789").upper() != "C":
|
|
153
|
+
continue
|
|
154
|
+
result[sub] = opposite(result.get(host, TOP))
|
|
155
|
+
return result
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
# ---------------------------------------------------------------- reference texts
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _text_items(footprint: Node):
|
|
162
|
+
"""Visible silkscreen reference texts of a footprint node: (item, text, height, width)."""
|
|
163
|
+
for item in footprint:
|
|
164
|
+
if not isinstance(item, list) or not item:
|
|
165
|
+
continue
|
|
166
|
+
head = str(item[0])
|
|
167
|
+
if head == "fp_text" and len(item) > 2 and str(item[1]) == "reference":
|
|
168
|
+
text = str(item[2])
|
|
169
|
+
elif head == "property" and len(item) > 2 and str(item[1]) == "Reference":
|
|
170
|
+
text = str(item[2])
|
|
171
|
+
else:
|
|
172
|
+
continue
|
|
173
|
+
layer = str(sexpr.value(item, "layer", ""))
|
|
174
|
+
if not layer.endswith("SilkS") or any(str(a) == "hide" for a in item):
|
|
175
|
+
continue
|
|
176
|
+
font = find(find(item, "effects") or [], "font")
|
|
177
|
+
size = find(font or [], "size")
|
|
178
|
+
h = sexpr.num(size[2]) if size is not None and len(size) > 2 else 1.0
|
|
179
|
+
w = sexpr.num(size[1]) if size is not None and len(size) > 1 else 1.0
|
|
180
|
+
yield item, text, h, w
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _abs(fp_at: tuple[float, float, float], x: float, y: float) -> tuple[float, float]:
|
|
184
|
+
from traceforge.kicad.pcb import _rotate
|
|
185
|
+
|
|
186
|
+
dx, dy = _rotate(x, y, fp_at[2])
|
|
187
|
+
return fp_at[0] + dx, fp_at[1] + dy
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _obstacles(fp: Node, owner: int) -> list[tuple]:
|
|
191
|
+
"""Silkscreen and copper a reference text must stay off: (owner, side, kind, data).
|
|
192
|
+
|
|
193
|
+
Segments are (x1, y1, x2, y2, half width); boxes are (x0, y0, x1, y1). side is F, B or FB."""
|
|
194
|
+
at = find(fp, "at")
|
|
195
|
+
if at is None:
|
|
196
|
+
return []
|
|
197
|
+
pos = (sexpr.num(at[1]), sexpr.num(at[2]), sexpr.num(at[3]) if len(at) > 3 else 0.0)
|
|
198
|
+
own = "B" if str(sexpr.value(fp, "layer", "F.Cu")).startswith("B") else "F"
|
|
199
|
+
out: list[tuple] = []
|
|
200
|
+
for item in fp:
|
|
201
|
+
if not isinstance(item, list) or not item:
|
|
202
|
+
continue
|
|
203
|
+
head = str(item[0])
|
|
204
|
+
if head == "pad":
|
|
205
|
+
a, size = find(item, "at"), find(item, "size")
|
|
206
|
+
if a is None or size is None:
|
|
207
|
+
continue
|
|
208
|
+
x, y = _abs(pos, sexpr.num(a[1]), sexpr.num(a[2]))
|
|
209
|
+
r = max(sexpr.num(size[1]), sexpr.num(size[2])) / 2 + 0.15
|
|
210
|
+
plated = len(item) > 2 and str(item[2]) in {"thru_hole", "np_thru_hole"}
|
|
211
|
+
out.append((owner, "FB" if plated else own, "pad", (x - r, y - r, x + r, y + r)))
|
|
212
|
+
elif head.startswith("fp_") and str(sexpr.value(item, "layer", "")).endswith("SilkS"):
|
|
213
|
+
layer = str(sexpr.value(item, "layer", ""))
|
|
214
|
+
side = layer[0]
|
|
215
|
+
stroke = find(item, "stroke")
|
|
216
|
+
width = (
|
|
217
|
+
sexpr.num(find(stroke, "width")[1]) if stroke and find(stroke, "width") else 0.12
|
|
218
|
+
)
|
|
219
|
+
if head == "fp_line":
|
|
220
|
+
s, e = find(item, "start"), find(item, "end")
|
|
221
|
+
if s is not None and e is not None:
|
|
222
|
+
x1, y1 = _abs(pos, sexpr.num(s[1]), sexpr.num(s[2]))
|
|
223
|
+
x2, y2 = _abs(pos, sexpr.num(e[1]), sexpr.num(e[2]))
|
|
224
|
+
out.append((owner, side, "seg", (x1, y1, x2, y2, width / 2)))
|
|
225
|
+
elif head in {"fp_circle", "fp_arc", "fp_rect", "fp_poly"}:
|
|
226
|
+
pts = []
|
|
227
|
+
for key in ("start", "mid", "end", "center"):
|
|
228
|
+
p = find(item, key)
|
|
229
|
+
if p is not None:
|
|
230
|
+
pts.append(_abs(pos, sexpr.num(p[1]), sexpr.num(p[2])))
|
|
231
|
+
if head == "fp_circle" and len(pts) == 2:
|
|
232
|
+
(cx, cy), (ex, ey) = pts[1], pts[0]
|
|
233
|
+
r = ((cx - ex) ** 2 + (cy - ey) ** 2) ** 0.5
|
|
234
|
+
pts = [(cx - r, cy - r), (cx + r, cy + r)]
|
|
235
|
+
if pts:
|
|
236
|
+
xs, ys = [q[0] for q in pts], [q[1] for q in pts]
|
|
237
|
+
out.append((owner, side, "box", (min(xs), min(ys), max(xs), max(ys))))
|
|
238
|
+
return out
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _hits(box, obstacle) -> bool:
|
|
242
|
+
kind, data = obstacle
|
|
243
|
+
if kind in {"box", "pad"}:
|
|
244
|
+
return _overlap(box, data)
|
|
245
|
+
x1, y1, x2, y2, hw = data
|
|
246
|
+
# segment against the box grown by the stroke half width (slab method)
|
|
247
|
+
lo, hi = 0.0, 1.0
|
|
248
|
+
for a, d, b0, b1 in (
|
|
249
|
+
(x1, x2 - x1, box[0] - hw, box[2] + hw),
|
|
250
|
+
(y1, y2 - y1, box[1] - hw, box[3] + hw),
|
|
251
|
+
):
|
|
252
|
+
if abs(d) < 1e-12:
|
|
253
|
+
if a < b0 or a > b1:
|
|
254
|
+
return False
|
|
255
|
+
continue
|
|
256
|
+
t0, t1 = sorted(((b0 - a) / d, (b1 - a) / d))
|
|
257
|
+
lo, hi = max(lo, t0), min(hi, t1)
|
|
258
|
+
if lo > hi:
|
|
259
|
+
return False
|
|
260
|
+
return True
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def declutter_reference_texts(footprints: list[Node]) -> int:
|
|
264
|
+
"""Move reference designators that would print over silkscreen or pads of another part.
|
|
265
|
+
|
|
266
|
+
Library reference texts sit just outside the part; on a dense board, small parts side by side
|
|
267
|
+
put them on top of each other. A text that overlaps another part's silkscreen, pad or an
|
|
268
|
+
earlier text is moved in steps of its own size until its box is free. Returns how many moved.
|
|
269
|
+
"""
|
|
270
|
+
from traceforge.kicad.pcb import _rotate
|
|
271
|
+
|
|
272
|
+
obstacles = [o for i, fp in enumerate(footprints) for o in _obstacles(fp, i)]
|
|
273
|
+
kept: dict[str, list[tuple[float, float, float, float]]] = {}
|
|
274
|
+
moved = 0
|
|
275
|
+
for index, fp in enumerate(footprints):
|
|
276
|
+
at = find(fp, "at")
|
|
277
|
+
if at is None:
|
|
278
|
+
continue
|
|
279
|
+
fx, fy = sexpr.num(at[1]), sexpr.num(at[2])
|
|
280
|
+
fa = sexpr.num(at[3]) if len(at) > 3 else 0.0
|
|
281
|
+
for item, text, h, w in _text_items(fp):
|
|
282
|
+
tat = find(item, "at")
|
|
283
|
+
if tat is None:
|
|
284
|
+
continue
|
|
285
|
+
layer = str(sexpr.value(item, "layer", ""))
|
|
286
|
+
lx, ly = sexpr.num(tat[1]), sexpr.num(tat[2])
|
|
287
|
+
dx, dy = _rotate(lx, ly, fa)
|
|
288
|
+
cx, cy = fx + dx, fy + dy
|
|
289
|
+
angle = sexpr.num(tat[3]) if len(tat) > 3 else fa
|
|
290
|
+
half_w, half_h = (len(text) * w * 0.75 + 0.3) / 2, (h + 0.2) / 2
|
|
291
|
+
if round(angle / 90) % 2:
|
|
292
|
+
half_w, half_h = half_h, half_w
|
|
293
|
+
side = layer[0]
|
|
294
|
+
blocking = [
|
|
295
|
+
(kind, data)
|
|
296
|
+
for owner, s, kind, data in obstacles
|
|
297
|
+
if (owner != index or kind == "pad") and side in s
|
|
298
|
+
]
|
|
299
|
+
boxes = kept.setdefault(layer, [])
|
|
300
|
+
|
|
301
|
+
def free(x: float, y: float, hw=half_w, hh=half_h, kept_boxes=boxes, hard=blocking):
|
|
302
|
+
box = (x - hw, y - hh, x + hw, y + hh)
|
|
303
|
+
return not any(_overlap(box, other) for other in kept_boxes) and not any(
|
|
304
|
+
_hits(box, o) for o in hard
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
if free(cx, cy):
|
|
308
|
+
boxes.append((cx - half_w, cy - half_h, cx + half_w, cy + half_h))
|
|
309
|
+
continue
|
|
310
|
+
spot = (cx, cy)
|
|
311
|
+
steps = []
|
|
312
|
+
for k in range(1, 7):
|
|
313
|
+
for ox, oy in ((0, k), (0, -k), (k, 0), (-k, 0)):
|
|
314
|
+
steps.append((ox * 2 * half_w, oy * 2 * half_h))
|
|
315
|
+
for ox, oy in steps:
|
|
316
|
+
if free(cx + ox, cy + oy):
|
|
317
|
+
spot = (cx + ox, cy + oy)
|
|
318
|
+
break
|
|
319
|
+
if spot != (cx, cy):
|
|
320
|
+
nx, ny = _rotate(spot[0] - fx, spot[1] - fy, -fa)
|
|
321
|
+
tat[1], tat[2] = fmt(nx), fmt(ny)
|
|
322
|
+
moved += 1
|
|
323
|
+
boxes.append((spot[0] - half_w, spot[1] - half_h, spot[0] + half_w, spot[1] + half_h))
|
|
324
|
+
return moved
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def _overlap(a, b) -> bool:
|
|
328
|
+
return a[0] < b[2] and b[0] < a[2] and a[1] < b[3] and b[1] < a[3]
|