dsh-stm32 0.1.8 → 0.1.9
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.
- package/README.md +567 -409
- package/README_EN.md +544 -410
- package/client/client.js +7033 -5021
- package/docs/reading-guide.md +921 -921
- package/lib/client.js +7033 -5021
- package/lib/commands/register.js +2 -0
- package/lib/data/firmware-packages.js +35 -0
- package/lib/data/firmware-pins.js +7 -0
- package/lib/data/install-guides.js +110 -0
- package/lib/data/ioc-presets.js +118 -0
- package/lib/engine/build.js +88 -2
- package/lib/engine/datasheet/auto.js +74 -0
- package/lib/engine/datasheet/digest.js +74 -0
- package/lib/engine/datasheet/lcsc.js +60 -0
- package/lib/engine/datasheet/pinout.js +119 -0
- package/lib/engine/datasheet/price.js +152 -0
- package/lib/engine/datasheet/search.js +147 -0
- package/lib/engine/datasheet/store.js +371 -0
- package/lib/engine/debug-session.js +2 -1
- package/lib/engine/ecad/autobuild.js +156 -0
- package/lib/engine/ecad/bom.js +238 -0
- package/lib/engine/ecad/brief.js +205 -0
- package/lib/engine/ecad/checks.js +239 -0
- package/lib/engine/ecad/design.js +177 -0
- package/lib/engine/ecad/dfm.js +314 -0
- package/lib/engine/ecad/easyeda.js +454 -0
- package/lib/engine/ecad/fabrication.js +104 -0
- package/lib/engine/ecad/gen/footprint.js +159 -0
- package/lib/engine/ecad/gen/symbol.js +103 -0
- package/lib/engine/ecad/import.js +372 -0
- package/lib/engine/ecad/jlcpcb.js +141 -0
- package/lib/engine/ecad/libs.js +316 -0
- package/lib/engine/ecad/models3d.js +89 -0
- package/lib/engine/ecad/netgate.js +286 -0
- package/lib/engine/ecad/normalize.js +182 -0
- package/lib/engine/ecad/package.js +126 -0
- package/lib/engine/ecad/padmerge.js +128 -0
- package/lib/engine/ecad/padprecheck.js +123 -0
- package/lib/engine/ecad/part-import.js +257 -0
- package/lib/engine/ecad/pcb/apply.js +53 -0
- package/lib/engine/ecad/pcb/emc-deep.js +396 -0
- package/lib/engine/ecad/pcb/emc.js +275 -0
- package/lib/engine/ecad/pcb/geometry.js +63 -0
- package/lib/engine/ecad/pcb/gerber.js +499 -0
- package/lib/engine/ecad/pcb/hand-sync.js +242 -0
- package/lib/engine/ecad/pcb/impedance.js +119 -0
- package/lib/engine/ecad/pcb/ipc-open.js +78 -0
- package/lib/engine/ecad/pcb/ipc-script.js +276 -0
- package/lib/engine/ecad/pcb/ipc.js +129 -0
- package/lib/engine/ecad/pcb/krt.js +182 -0
- package/lib/engine/ecad/pcb/links.js +393 -0
- package/lib/engine/ecad/pcb/mech.js +201 -0
- package/lib/engine/ecad/pcb/model-fix.js +178 -0
- package/lib/engine/ecad/pcb/netlist.js +146 -0
- package/lib/engine/ecad/pcb/pcb-svg.js +199 -0
- package/lib/engine/ecad/pcb/placement.js +140 -0
- package/lib/engine/ecad/pcb/power.js +203 -0
- package/lib/engine/ecad/pcb/render3d.js +208 -0
- package/lib/engine/ecad/pcb/route.js +167 -0
- package/lib/engine/ecad/pcb/router.js +469 -0
- package/lib/engine/ecad/pcb/rules.js +122 -0
- package/lib/engine/ecad/pcb/scene.js +513 -0
- package/lib/engine/ecad/pcb/stackup.js +154 -0
- package/lib/engine/ecad/pcb/swap.js +222 -0
- package/lib/engine/ecad/pcb/tracks.js +70 -0
- package/lib/engine/ecad/runtime.js +227 -0
- package/lib/engine/ecad/toolchain.js +146 -0
- package/lib/engine/ecad/tsci.js +362 -0
- package/lib/engine/ecad/zip.js +98 -0
- package/lib/engine/generate-script.js +4 -1
- package/lib/engine/generate.js +145 -5
- package/lib/engine/ioc-plan.js +12 -2
- package/lib/engine/mcu-info.js +134 -0
- package/lib/engine/pdf/curve.js +321 -0
- package/lib/engine/pdf/png.js +83 -0
- package/lib/engine/pdf/reader.js +336 -0
- package/lib/engine/pdf/schematic.js +93 -0
- package/lib/engine/pdf/tables.js +249 -0
- package/lib/engine/pdf/toolchain.js +50 -0
- package/lib/engine/probe.js +45 -0
- package/lib/engine/project-conf.js +93 -38
- package/lib/engine/project-layout.js +198 -0
- package/lib/engine/project-repair.js +205 -0
- package/lib/engine/schematic/checks.js +153 -0
- package/lib/engine/schematic/draw.js +390 -0
- package/lib/engine/schematic/edit.js +229 -0
- package/lib/engine/schematic/kicad-sch.js +225 -0
- package/lib/engine/schematic/model.js +442 -0
- package/lib/engine/schematic/nets.js +219 -0
- package/lib/engine/schematic/pcb.js +144 -0
- package/lib/engine/schematic/review.js +134 -0
- package/lib/engine/schematic/sexpr.js +134 -0
- package/lib/engine/schematic/to-ioc.js +197 -0
- package/lib/engine/schematic/transform.js +25 -0
- package/lib/engine/session-context.js +93 -6
- package/lib/engine/toolchain-gate.js +8 -0
- package/lib/engine/web-api.js +269 -32
- package/lib/index.js +56 -5
- package/lib/skill/stm32-workflow.js +1 -1
- package/lib/tools/build.js +10 -2
- package/lib/tools/code-patch.js +2 -2
- package/lib/tools/datasheet.js +447 -0
- package/lib/tools/debug-session.js +9 -4
- package/lib/tools/debug.js +2 -2
- package/lib/tools/doctor.js +2 -2
- package/lib/tools/ecad.js +903 -0
- package/lib/tools/flash.js +2 -2
- package/lib/tools/gen.js +166 -0
- package/lib/tools/generate.js +7 -5
- package/lib/tools/import-circuit.js +146 -0
- package/lib/tools/ioc-apply.js +2 -2
- package/lib/tools/ioc-inspect.js +2 -2
- package/lib/tools/ioc-plan.js +2 -2
- package/lib/tools/ioc-verify.js +2 -2
- package/lib/tools/layout.js +92 -0
- package/lib/tools/mcu-info.js +163 -0
- package/lib/tools/part-import.js +182 -0
- package/lib/tools/pcb.js +2316 -0
- package/lib/tools/pdf.js +423 -0
- package/lib/tools/project-dir.js +19 -1
- package/lib/tools/regs.js +2 -2
- package/lib/tools/route-plan.js +2 -2
- package/lib/tools/rtt.js +2 -2
- package/lib/tools/sch-draw.js +433 -0
- package/lib/tools/sch-edit.js +145 -0
- package/lib/tools/schematic.js +817 -0
- package/package.json +1 -1
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 图纸手绘层:直接改 `.kicad_sch` 文本,不依赖 KiCad 进程。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不写生成目录:tscircuit 每次 `ecad_build` 都重写
|
|
5
|
+
* `hardware/circuit/dist/<工程>/kicad/*.kicad_sch`,画在那里下次构建就没了。
|
|
6
|
+
* 所以手绘层固定在 `hardware/schematic/`,构建从不触碰;需要底图时用 `seed` 从最近一次
|
|
7
|
+
* 构建拷一份过来。
|
|
8
|
+
*
|
|
9
|
+
* 本模块只做四件事:连线、标名、克隆元件、挪元件。都按「平衡括号取块」改写文本,
|
|
10
|
+
* 改完的图纸可以用 `kicad-cli sch export netlist` 校验。
|
|
11
|
+
*/
|
|
12
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
13
|
+
import { randomUUID } from "node:crypto";
|
|
14
|
+
import { dirname, join } from "node:path";
|
|
15
|
+
import { parseSchText } from "./kicad-sch.js";
|
|
16
|
+
import { placePin } from "./transform.js";
|
|
17
|
+
export function drawSheetPath(projectDir, name = "schematic") {
|
|
18
|
+
return join(projectDir, "hardware", "schematic", name + ".kicad_sch");
|
|
19
|
+
}
|
|
20
|
+
function num(value) {
|
|
21
|
+
return String(Math.round(value * 10000) / 10000);
|
|
22
|
+
}
|
|
23
|
+
function samePoint(left, right) {
|
|
24
|
+
return Math.abs(left.x - right.x) < 0.001 && Math.abs(left.y - right.y) < 0.001;
|
|
25
|
+
}
|
|
26
|
+
/** 从 `index` 处的 `(` 起按括号平衡找到块尾(含)。找不到返回 -1。 */
|
|
27
|
+
function blockEnd(text, index) {
|
|
28
|
+
let depth = 0;
|
|
29
|
+
let inString = false;
|
|
30
|
+
for (let cursor = index; cursor < text.length; cursor += 1) {
|
|
31
|
+
const char = text[cursor];
|
|
32
|
+
if (inString) {
|
|
33
|
+
if (char === "\\")
|
|
34
|
+
cursor += 1;
|
|
35
|
+
else if (char === '"')
|
|
36
|
+
inString = false;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (char === '"')
|
|
40
|
+
inString = true;
|
|
41
|
+
else if (char === "(")
|
|
42
|
+
depth += 1;
|
|
43
|
+
else if (char === ")") {
|
|
44
|
+
depth -= 1;
|
|
45
|
+
if (depth === 0)
|
|
46
|
+
return cursor + 1;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return -1;
|
|
50
|
+
}
|
|
51
|
+
/** 图纸里 `(symbol (lib_id …) …)` 实例块的起止位置。 */
|
|
52
|
+
export function symbolBlocks(text) {
|
|
53
|
+
const blocks = [];
|
|
54
|
+
let cursor = 0;
|
|
55
|
+
while (cursor < text.length) {
|
|
56
|
+
const index = text.indexOf("(symbol", cursor);
|
|
57
|
+
if (index < 0)
|
|
58
|
+
break;
|
|
59
|
+
const end = blockEnd(text, index);
|
|
60
|
+
if (end < 0)
|
|
61
|
+
break;
|
|
62
|
+
const body = text.slice(index, end);
|
|
63
|
+
if (body.includes("(lib_id ") && !body.includes("(lib_name ")) {
|
|
64
|
+
const match = /\(property "Reference" "([^"]*)"/u.exec(body);
|
|
65
|
+
blocks.push({ ref: match?.[1] ?? "", start: index, end });
|
|
66
|
+
cursor = end;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
cursor = index + "(symbol".length;
|
|
70
|
+
}
|
|
71
|
+
return blocks;
|
|
72
|
+
}
|
|
73
|
+
function labelText(text, at) {
|
|
74
|
+
const escaped = text.replace(/\\/gu, "\\\\").replace(/"/gu, '\\"');
|
|
75
|
+
return (' (label "' +
|
|
76
|
+
escaped +
|
|
77
|
+
'" (at ' +
|
|
78
|
+
num(at.x) +
|
|
79
|
+
" " +
|
|
80
|
+
num(at.y) +
|
|
81
|
+
" 0)\n" +
|
|
82
|
+
" (effects (font (size 1.27 1.27)) (justify left bottom))\n" +
|
|
83
|
+
' (uuid "' +
|
|
84
|
+
randomUUID() +
|
|
85
|
+
'")\n' +
|
|
86
|
+
" )\n");
|
|
87
|
+
}
|
|
88
|
+
function wireText(points) {
|
|
89
|
+
const pts = points.map((point) => `(xy ${num(point.x)} ${num(point.y)})`).join(" ");
|
|
90
|
+
return (" (wire (pts " +
|
|
91
|
+
pts +
|
|
92
|
+
")\n" +
|
|
93
|
+
" (stroke (width 0) (type default))\n" +
|
|
94
|
+
' (uuid "' +
|
|
95
|
+
randomUUID() +
|
|
96
|
+
'")\n' +
|
|
97
|
+
" )\n");
|
|
98
|
+
}
|
|
99
|
+
function resolvePoint(sheet, point) {
|
|
100
|
+
if (typeof point.x === "number" && typeof point.y === "number")
|
|
101
|
+
return { x: point.x, y: point.y };
|
|
102
|
+
if (typeof point.ref === "string" && typeof point.pin === "string") {
|
|
103
|
+
const symbol = sheet.symbols.find((item) => item.ref === point.ref);
|
|
104
|
+
if (symbol === undefined)
|
|
105
|
+
return null;
|
|
106
|
+
const pins = sheet.libPins.get(symbol.libId) ?? [];
|
|
107
|
+
const unitPins = pins.filter((pin) => pin.unit === 0 || pin.unit === symbol.unit);
|
|
108
|
+
const pin = (unitPins.length > 0 ? unitPins : pins).find((item) => item.number === point.pin);
|
|
109
|
+
if (pin === undefined)
|
|
110
|
+
return null;
|
|
111
|
+
return placePin({ x: pin.x, y: pin.y }, symbol);
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
function insert(text, addition) {
|
|
116
|
+
const end = text.lastIndexOf(")");
|
|
117
|
+
if (end < 0)
|
|
118
|
+
return text;
|
|
119
|
+
const head = text.slice(0, end).replace(/\s*$/u, "\n");
|
|
120
|
+
return head + addition + text.slice(end);
|
|
121
|
+
}
|
|
122
|
+
function cloneBlock(block, ref, at, value, footprint) {
|
|
123
|
+
let next = block.replace(/\(uuid [^)]*\)/gu, '(uuid "' + randomUUID() + '")');
|
|
124
|
+
next = next.replace(/(\(property "Reference" ")[^"]*(")/u, "$1" + ref + "$2");
|
|
125
|
+
if (value !== undefined)
|
|
126
|
+
next = next.replace(/(\(property "Value" ")[^"]*(")/u, "$1" + value + "$2");
|
|
127
|
+
if (footprint !== undefined)
|
|
128
|
+
next = next.replace(/(\(property "Footprint" ")[^"]*(")/u, "$1" + footprint + "$2");
|
|
129
|
+
next = next.replace(/\(at [^)]*\)/u, "(at " + num(at.x) + " " + num(at.y) + " 0)");
|
|
130
|
+
next = next.replace(/(\(path "[^"]*"\s*\(reference ")[^"]*(")/u, "$1" + ref + "$2");
|
|
131
|
+
return next.startsWith(" ") ? next : " " + next;
|
|
132
|
+
}
|
|
133
|
+
function moveBlock(block, at) {
|
|
134
|
+
return block.replace(/\(at [^)]*\)/u, "(at " + num(at.x) + " " + num(at.y) + " 0)");
|
|
135
|
+
}
|
|
136
|
+
function refsOf(sheet) {
|
|
137
|
+
const map = new Map();
|
|
138
|
+
for (const symbol of sheet.symbols)
|
|
139
|
+
map.set(symbol.ref, symbol);
|
|
140
|
+
return map;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* 把一组动作画到图纸文本上。`file` 只用于解析报错出处,不改盘。
|
|
144
|
+
* 返回的 `text` 由调用方决定写不写。
|
|
145
|
+
*/
|
|
146
|
+
export function drawOnSheet(text, file, ops, library) {
|
|
147
|
+
const empty = { ok: true, error: "", text, applied: 0, skipped: 0, failed: [], notes: [] };
|
|
148
|
+
if (ops.length === 0)
|
|
149
|
+
return empty;
|
|
150
|
+
let sheet;
|
|
151
|
+
try {
|
|
152
|
+
sheet = parseSchText(text, file, file);
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
return { ...empty, ok: false, error: "读不懂这个 .kicad_sch:" + error.message };
|
|
156
|
+
}
|
|
157
|
+
let next = text;
|
|
158
|
+
const failed = [];
|
|
159
|
+
const notes = [];
|
|
160
|
+
let applied = 0;
|
|
161
|
+
let skipped = 0;
|
|
162
|
+
for (const op of ops) {
|
|
163
|
+
if (op.op === "wire") {
|
|
164
|
+
const from = resolvePoint(sheet, op.a);
|
|
165
|
+
const to = resolvePoint(sheet, op.b);
|
|
166
|
+
if (from === null) {
|
|
167
|
+
failed.push("连线起点找不到:" + describePoint(op.a));
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (to === null) {
|
|
171
|
+
failed.push("连线终点找不到:" + describePoint(op.b));
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
const points = Math.abs(from.x - to.x) < 0.001 || Math.abs(from.y - to.y) < 0.001
|
|
175
|
+
? [from, to]
|
|
176
|
+
: [from, { x: to.x, y: from.y }, to];
|
|
177
|
+
const first = points[0];
|
|
178
|
+
const last = points[points.length - 1];
|
|
179
|
+
const exists = sheet.wires.some((wire) => (samePoint(wire.a, first) && samePoint(wire.b, last)) ||
|
|
180
|
+
(samePoint(wire.a, last) && samePoint(wire.b, first)));
|
|
181
|
+
if (exists) {
|
|
182
|
+
skipped += 1;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
// 一段一个 wire 块:多点多段写法的末端在 KiCad 里是折点,很多解析器只认首两点。
|
|
186
|
+
next = insert(next, points.slice(1).map((point, index) => wireText([points[index], point])).join(""));
|
|
187
|
+
sheet = parseSchText(next, file, file);
|
|
188
|
+
applied += 1;
|
|
189
|
+
notes.push("连线 " + describePoint(op.a) + " ↔ " + describePoint(op.b));
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (op.op === "label") {
|
|
193
|
+
const at = resolvePoint(sheet, op.at);
|
|
194
|
+
if (at === null) {
|
|
195
|
+
failed.push("标名位置找不到:" + describePoint(op.at));
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (sheet.labels.some((label) => label.name === op.text && samePoint(label.at, at))) {
|
|
199
|
+
skipped += 1;
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
next = insert(next, labelText(op.text, at));
|
|
203
|
+
sheet = parseSchText(next, file, file);
|
|
204
|
+
applied += 1;
|
|
205
|
+
notes.push("标名 " + op.text + " @" + describePoint(op.at));
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const blocks = symbolBlocks(next);
|
|
209
|
+
if (op.op === "place") {
|
|
210
|
+
if (blocks.some((block) => block.ref === op.ref)) {
|
|
211
|
+
skipped += 1;
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
const symbolText = library === undefined ? null : library(op.libId);
|
|
215
|
+
if (symbolText === null) {
|
|
216
|
+
failed.push("找不到符号库:" + op.libId + "(放一个 .kicad_sym 到 libs/symbols,或确认 KiCad 库里有它)");
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
const extracted = extractSymbol(symbolText, op.libId);
|
|
220
|
+
if (!extracted.ok) {
|
|
221
|
+
failed.push(extracted.error);
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
const span = libSymbolsSpan(next);
|
|
225
|
+
if (span === null) {
|
|
226
|
+
failed.push("图纸里没有 (lib_symbols 段:这张图不是 KiCad 生成的");
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
const embedded = next.slice(span.start, span.end).replace(/\s*\)$/u, "\n" + indent(extracted.block, 4) + "\n )");
|
|
230
|
+
next = next.slice(0, span.start) + embedded + next.slice(span.end);
|
|
231
|
+
next = insert(next, instanceText(op.libId, op.ref, op.at, extracted.pins, {
|
|
232
|
+
value: op.value,
|
|
233
|
+
footprint: op.footprint,
|
|
234
|
+
rotation: op.rotation,
|
|
235
|
+
sheetUuid: rootUuid(text),
|
|
236
|
+
}));
|
|
237
|
+
sheet = parseSchText(next, file, file);
|
|
238
|
+
applied += 1;
|
|
239
|
+
notes.push("放置 " + op.libId + " → " + op.ref + "(" + String(extracted.pins.length) + " 脚)");
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (op.op === "clone") {
|
|
243
|
+
const source = blocks.find((block) => block.ref === op.from);
|
|
244
|
+
if (source === undefined) {
|
|
245
|
+
failed.push("图纸里没有 " + op.from + ",无法克隆");
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
if (blocks.some((block) => block.ref === op.ref)) {
|
|
249
|
+
skipped += 1;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
const origin = refsOf(sheet).get(op.from);
|
|
253
|
+
const at = op.at ?? {
|
|
254
|
+
x: (origin?.at.x ?? 0) + 25.4,
|
|
255
|
+
y: origin?.at.y ?? 0,
|
|
256
|
+
};
|
|
257
|
+
next = insert(next, cloneBlock(next.slice(source.start, source.end), op.ref, at, op.value, op.footprint));
|
|
258
|
+
sheet = parseSchText(next, file, file);
|
|
259
|
+
applied += 1;
|
|
260
|
+
notes.push("克隆 " + op.from + " → " + op.ref + " @" + num(at.x) + "," + num(at.y));
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
const target = blocks.find((block) => block.ref === op.ref);
|
|
264
|
+
if (target === undefined) {
|
|
265
|
+
failed.push("图纸里没有 " + op.ref);
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (op.op === "move") {
|
|
269
|
+
next = next.slice(0, target.start) + moveBlock(next.slice(target.start, target.end), op.at) + next.slice(target.end);
|
|
270
|
+
sheet = parseSchText(next, file, file);
|
|
271
|
+
applied += 1;
|
|
272
|
+
notes.push("挪动 " + op.ref + " → " + num(op.at.x) + "," + num(op.at.y));
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
next = next.slice(0, target.start) + next.slice(target.end);
|
|
276
|
+
sheet = parseSchText(next, file, file);
|
|
277
|
+
applied += 1;
|
|
278
|
+
notes.push("删除 " + op.ref);
|
|
279
|
+
}
|
|
280
|
+
return { ok: failed.length === 0, error: failed.join(";"), text: next, applied, skipped, failed, notes };
|
|
281
|
+
}
|
|
282
|
+
/** `.kicad_sym` 里取出一个符号,外层名字改成 `Lib:Name`,同时收集它的引脚号。 */
|
|
283
|
+
export function extractSymbol(symbolText, libId) {
|
|
284
|
+
const colon = libId.indexOf(":");
|
|
285
|
+
const name = colon >= 0 ? libId.slice(colon + 1) : libId;
|
|
286
|
+
const marker = '(symbol "' + name + '"';
|
|
287
|
+
const start = symbolText.indexOf(marker);
|
|
288
|
+
if (start < 0)
|
|
289
|
+
return { ok: false, error: "符号库里没有 " + name, block: "", pins: [] };
|
|
290
|
+
const end = blockEnd(symbolText, start);
|
|
291
|
+
if (end < 0)
|
|
292
|
+
return { ok: false, error: "符号 " + name + " 的块不完整", block: "", pins: [] };
|
|
293
|
+
let block = symbolText.slice(start, end);
|
|
294
|
+
block = block.replace(marker, '(symbol "' + libId + '"');
|
|
295
|
+
const pins = [];
|
|
296
|
+
// 引脚号后面通常跟 (effects …),所以只认引号结束,不要求紧跟 ")"。
|
|
297
|
+
const pinPattern = /\(pin\s+(?:input|output|bidirectional|tri_state|passive|free|unspecified|power_in|power_out|open_collector|open_emitter|no_connect)\s+[\s\S]*?\(number "([^"]+)"/gu;
|
|
298
|
+
for (const match of block.matchAll(pinPattern)) {
|
|
299
|
+
const number = match[1];
|
|
300
|
+
if (number !== undefined && !pins.includes(number))
|
|
301
|
+
pins.push(number);
|
|
302
|
+
}
|
|
303
|
+
return { ok: true, error: "", block, pins };
|
|
304
|
+
}
|
|
305
|
+
function libSymbolsSpan(text) {
|
|
306
|
+
const index = text.indexOf(" (lib_symbols");
|
|
307
|
+
if (index < 0)
|
|
308
|
+
return null;
|
|
309
|
+
const end = blockEnd(text, index + 2);
|
|
310
|
+
if (end < 0)
|
|
311
|
+
return null;
|
|
312
|
+
return { start: index, end };
|
|
313
|
+
}
|
|
314
|
+
function rootUuid(text) {
|
|
315
|
+
const match = /\n\s*\(uuid ([0-9a-fA-F-]{8,})\)/u.exec(text);
|
|
316
|
+
return match?.[1] ?? "";
|
|
317
|
+
}
|
|
318
|
+
function instanceText(libId, ref, at, pins, options) {
|
|
319
|
+
const rotation = options.rotation ?? 0;
|
|
320
|
+
const pinLines = pins
|
|
321
|
+
.map((pin) => ' (pin "' + pin + '"\n (uuid "' + randomUUID() + '")\n )\n')
|
|
322
|
+
.join("");
|
|
323
|
+
return (" (symbol\n" +
|
|
324
|
+
' (lib_id "' + libId + '")\n' +
|
|
325
|
+
" (at " + num(at.x) + " " + num(at.y) + " " + num(rotation) + ")\n" +
|
|
326
|
+
" (unit 1)\n" +
|
|
327
|
+
" (exclude_from_sim no)\n" +
|
|
328
|
+
" (in_bom yes)\n" +
|
|
329
|
+
" (on_board yes)\n" +
|
|
330
|
+
" (dnp no)\n" +
|
|
331
|
+
' (uuid "' + randomUUID() + '")\n' +
|
|
332
|
+
" (property \"Reference\" \"" + ref + "\"\n" +
|
|
333
|
+
" (at " + num(at.x) + " " + num(at.y - 6.35) + " 0)\n" +
|
|
334
|
+
" (effects (font (size 1.27 1.27)) (justify bottom))\n" +
|
|
335
|
+
" )\n" +
|
|
336
|
+
" (property \"Value\" \"" + (options.value ?? libId.slice(libId.indexOf(":") + 1)) + "\"\n" +
|
|
337
|
+
" (at " + num(at.x) + " " + num(at.y + 6.35) + " 0)\n" +
|
|
338
|
+
" (effects (font (size 1.27 1.27)) (justify top))\n" +
|
|
339
|
+
" )\n" +
|
|
340
|
+
" (property \"Footprint\" \"" + (options.footprint ?? "") + "\"\n" +
|
|
341
|
+
" (at " + num(at.x) + " " + num(at.y) + " 0)\n" +
|
|
342
|
+
" (effects (font (size 1.27 1.27)) (hide yes))\n" +
|
|
343
|
+
" )\n" +
|
|
344
|
+
" (property \"Datasheet\" \"~\"\n" +
|
|
345
|
+
" (at " + num(at.x) + " " + num(at.y) + " 0)\n" +
|
|
346
|
+
" (effects (font (size 1.27 1.27)) (hide yes))\n" +
|
|
347
|
+
" )\n" +
|
|
348
|
+
pinLines +
|
|
349
|
+
" (instances\n" +
|
|
350
|
+
' (project ""\n' +
|
|
351
|
+
' (path "/' + options.sheetUuid + '"\n' +
|
|
352
|
+
' (reference "' + ref + '")\n' +
|
|
353
|
+
" (unit 1)\n" +
|
|
354
|
+
" )\n" +
|
|
355
|
+
" )\n" +
|
|
356
|
+
" )\n" +
|
|
357
|
+
" )\n");
|
|
358
|
+
}
|
|
359
|
+
function indent(block, spaces) {
|
|
360
|
+
const pad = " ".repeat(spaces);
|
|
361
|
+
return block
|
|
362
|
+
.split("\n")
|
|
363
|
+
.map((line) => (line.length === 0 ? line : pad + line))
|
|
364
|
+
.join("\n");
|
|
365
|
+
}
|
|
366
|
+
function describePoint(point) {
|
|
367
|
+
if (typeof point.ref === "string" && typeof point.pin === "string")
|
|
368
|
+
return point.ref + "." + point.pin;
|
|
369
|
+
if (typeof point.x === "number" && typeof point.y === "number")
|
|
370
|
+
return "(" + num(point.x) + "," + num(point.y) + ")";
|
|
371
|
+
return "未指定位置";
|
|
372
|
+
}
|
|
373
|
+
/** 手绘层是否存在;不存在时返回空串。 */
|
|
374
|
+
export function readDrawSheet(file) {
|
|
375
|
+
return existsSync(file) ? readFileSync(file, "utf8") : "";
|
|
376
|
+
}
|
|
377
|
+
export function writeDrawSheet(file, text) {
|
|
378
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
379
|
+
writeFileSync(file, text, "utf8");
|
|
380
|
+
}
|
|
381
|
+
/** 以最近一次构建的图纸为底图;已存在的底图不覆盖。 */
|
|
382
|
+
export function seedDrawSheet(buildSheet, target) {
|
|
383
|
+
if (existsSync(target))
|
|
384
|
+
return { ok: true, error: "", seeded: false };
|
|
385
|
+
if (!existsSync(buildSheet))
|
|
386
|
+
return { ok: false, error: "还没有可用的构建图纸:" + buildSheet, seeded: false };
|
|
387
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
388
|
+
writeFileSync(target, readFileSync(buildSheet, "utf8"), "utf8");
|
|
389
|
+
return { ok: true, error: "", seeded: true };
|
|
390
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 原理图编辑:把「连两根引脚、给引脚起名」这类动作存成一份指令表,构建后重放。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不是直接改文件:tscircuit 是原理图与 PCB 的唯一写者,每次 `ecad_build` 都会重写
|
|
5
|
+
* `dist/<工程>/kicad/*.kicad_sch`。所以人工编辑只能落在「指令」这一层,由构建收尾时重放,
|
|
6
|
+
* 否则下一次构建就没了。
|
|
7
|
+
*
|
|
8
|
+
* 指令只描述电气意图(哪两个引脚连起来、哪个引脚叫什么),坐标由当时的符号位置算出来;
|
|
9
|
+
* 因此元件挪了位置、甚至换了封装,重放仍然对,只有引脚没了才会失败——失败要报出来,
|
|
10
|
+
* 不能静默跳过。
|
|
11
|
+
*/
|
|
12
|
+
import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
13
|
+
import { randomUUID } from "node:crypto";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
import { parseSchText } from "./kicad-sch.js";
|
|
16
|
+
import { placePin } from "./transform.js";
|
|
17
|
+
function describeError(error) {
|
|
18
|
+
return error instanceof Error ? error.message : String(error);
|
|
19
|
+
}
|
|
20
|
+
const EMPTY = {
|
|
21
|
+
ok: true,
|
|
22
|
+
error: "",
|
|
23
|
+
applied: 0,
|
|
24
|
+
skipped: 0,
|
|
25
|
+
failed: [],
|
|
26
|
+
notes: [],
|
|
27
|
+
text: "",
|
|
28
|
+
};
|
|
29
|
+
export function editStorePath(circuitDir) {
|
|
30
|
+
return join(circuitDir, "schematic-edits.json");
|
|
31
|
+
}
|
|
32
|
+
export function readSchEdits(file) {
|
|
33
|
+
if (!existsSync(file))
|
|
34
|
+
return [];
|
|
35
|
+
try {
|
|
36
|
+
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
37
|
+
const edits = Array.isArray(parsed.edits) ? parsed.edits : [];
|
|
38
|
+
return edits.filter((edit) => typeof edit === "object" && edit !== null && "op" in edit);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export function writeSchEdits(file, edits) {
|
|
45
|
+
const store = { version: 1, edits };
|
|
46
|
+
writeFileSync(file, JSON.stringify(store, null, 2) + "\n", "utf8");
|
|
47
|
+
}
|
|
48
|
+
function findSymbol(sheet, ref) {
|
|
49
|
+
for (const symbol of sheet.symbols) {
|
|
50
|
+
if (symbol.ref === ref)
|
|
51
|
+
return symbol;
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
function pinsOf(sheet, symbol) {
|
|
56
|
+
const pins = sheet.libPins.get(symbol.libId) ?? [];
|
|
57
|
+
const unitPins = pins.filter((pin) => pin.unit === 0 || pin.unit === symbol.unit);
|
|
58
|
+
return unitPins.length > 0 ? unitPins : pins;
|
|
59
|
+
}
|
|
60
|
+
/** 引脚端点在图纸上的绝对坐标;找不到返回 null。 */
|
|
61
|
+
export function pinPoint(sheet, ref, number) {
|
|
62
|
+
const symbol = findSymbol(sheet, ref);
|
|
63
|
+
if (symbol === null)
|
|
64
|
+
return null;
|
|
65
|
+
const pin = pinsOf(sheet, symbol).find((item) => item.number === number);
|
|
66
|
+
if (pin === undefined)
|
|
67
|
+
return null;
|
|
68
|
+
return placePin({ x: pin.x, y: pin.y }, symbol);
|
|
69
|
+
}
|
|
70
|
+
function samePoint(left, right) {
|
|
71
|
+
return Math.abs(left.x - right.x) < 0.001 && Math.abs(left.y - right.y) < 0.001;
|
|
72
|
+
}
|
|
73
|
+
function wireExists(sheet, points) {
|
|
74
|
+
const first = points[0];
|
|
75
|
+
const last = points[points.length - 1];
|
|
76
|
+
if (first === undefined || last === undefined)
|
|
77
|
+
return false;
|
|
78
|
+
for (const wire of sheet.wires) {
|
|
79
|
+
if (samePoint(wire.a, first) && samePoint(wire.b, last))
|
|
80
|
+
return true;
|
|
81
|
+
if (samePoint(wire.a, last) && samePoint(wire.b, first))
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
function labelExists(sheet, text, at) {
|
|
87
|
+
return sheet.labels.some((label) => label.name === text && samePoint(label.at, at));
|
|
88
|
+
}
|
|
89
|
+
function num(value) {
|
|
90
|
+
return String(Math.round(value * 10000) / 10000);
|
|
91
|
+
}
|
|
92
|
+
function wireText(points) {
|
|
93
|
+
const pts = points.map((point) => `(xy ${num(point.x)} ${num(point.y)})`).join(" ");
|
|
94
|
+
return (" (wire (pts " +
|
|
95
|
+
pts +
|
|
96
|
+
")\n" +
|
|
97
|
+
" (stroke (width 0) (type default))\n" +
|
|
98
|
+
' (uuid "' +
|
|
99
|
+
randomUUID() +
|
|
100
|
+
'")\n' +
|
|
101
|
+
" )\n");
|
|
102
|
+
}
|
|
103
|
+
function labelText(text, at) {
|
|
104
|
+
const escaped = text.replace(/\\/gu, "\\\\").replace(/"/gu, '\\"');
|
|
105
|
+
return (' (label "' +
|
|
106
|
+
escaped +
|
|
107
|
+
'" (at ' +
|
|
108
|
+
num(at.x) +
|
|
109
|
+
" " +
|
|
110
|
+
num(at.y) +
|
|
111
|
+
" 0)\n" +
|
|
112
|
+
" (effects (font (size 1.27 1.27)) (justify left bottom))\n" +
|
|
113
|
+
' (uuid "' +
|
|
114
|
+
randomUUID() +
|
|
115
|
+
'")\n' +
|
|
116
|
+
" )\n");
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* 把指令画到图纸文本上。`file` 只用于解析时给出报错出处与多图纸路径,不改盘。
|
|
120
|
+
* 返回的 `text` 由调用方决定写不写。
|
|
121
|
+
*/
|
|
122
|
+
export function applySchEdits(text, file, edits) {
|
|
123
|
+
if (edits.length === 0)
|
|
124
|
+
return { ...EMPTY, text };
|
|
125
|
+
let sheet;
|
|
126
|
+
try {
|
|
127
|
+
sheet = parseSchText(text, file, file);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
return { ...EMPTY, ok: false, error: "读不懂这个 .kicad_sch:" + error.message, text };
|
|
131
|
+
}
|
|
132
|
+
const additions = [];
|
|
133
|
+
const failed = [];
|
|
134
|
+
const notes = [];
|
|
135
|
+
let applied = 0;
|
|
136
|
+
let skipped = 0;
|
|
137
|
+
for (const edit of edits) {
|
|
138
|
+
if (edit.op === "connect") {
|
|
139
|
+
const from = pinPoint(sheet, edit.a.ref, edit.a.pin);
|
|
140
|
+
const to = pinPoint(sheet, edit.b.ref, edit.b.pin);
|
|
141
|
+
if (from === null) {
|
|
142
|
+
failed.push(`图纸里没有 ${edit.a.ref} 的 ${edit.a.pin} 脚`);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (to === null) {
|
|
146
|
+
failed.push(`图纸里没有 ${edit.b.ref} 的 ${edit.b.pin} 脚`);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
// 正交走线:同轴直接连,否则在 (to.x, from.y) 拐一次。
|
|
150
|
+
const points = Math.abs(from.x - to.x) < 0.001 || Math.abs(from.y - to.y) < 0.001
|
|
151
|
+
? [from, to]
|
|
152
|
+
: [from, { x: to.x, y: from.y }, to];
|
|
153
|
+
if (wireExists(sheet, points)) {
|
|
154
|
+
skipped += 1;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
additions.push(wireText(points));
|
|
158
|
+
applied += 1;
|
|
159
|
+
notes.push(`连上 ${edit.a.ref}.${edit.a.pin} ↔ ${edit.b.ref}.${edit.b.pin}` +
|
|
160
|
+
(points.length > 2 ? "(拐了一次)" : ""));
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
const at = pinPoint(sheet, edit.at.ref, edit.at.pin);
|
|
164
|
+
if (at === null) {
|
|
165
|
+
failed.push(`图纸里没有 ${edit.at.ref} 的 ${edit.at.pin} 脚`);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (labelExists(sheet, edit.text, at)) {
|
|
169
|
+
skipped += 1;
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
additions.push(labelText(edit.text, at));
|
|
173
|
+
applied += 1;
|
|
174
|
+
notes.push(`给 ${edit.at.ref}.${edit.at.pin} 标名 ${edit.text}`);
|
|
175
|
+
}
|
|
176
|
+
if (additions.length === 0) {
|
|
177
|
+
return { ok: true, error: "", applied, skipped, failed, notes, text };
|
|
178
|
+
}
|
|
179
|
+
// 顶层是一个表达式,最后那个 ')' 就是文档结尾;新图元插在它前面。
|
|
180
|
+
const end = text.lastIndexOf(")");
|
|
181
|
+
if (end < 0) {
|
|
182
|
+
return { ok: false, error: "找不到文档结尾的 ')':文件可能被截断", applied: 0, skipped, failed, notes, text };
|
|
183
|
+
}
|
|
184
|
+
const head = text.slice(0, end).replace(/\s*$/u, "\n");
|
|
185
|
+
const tail = text.slice(end);
|
|
186
|
+
return { ok: true, error: "", applied, skipped, failed, notes, text: head + additions.join("") + tail };
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* 把指令重放到一个构建目录里的所有图纸上(S170)。
|
|
190
|
+
* 工具与 HTTP 路由共用同一份实现:面板点「连起来」和模型调工具要走同一条路。
|
|
191
|
+
*/
|
|
192
|
+
export function replaySchEditsTo(kicadDir, edits, readDir = readdirSync, readText = (file) => readFileSync(file, "utf8"), writeText = (file, text) => writeFileSync(file, text, "utf8")) {
|
|
193
|
+
if (kicadDir === "") {
|
|
194
|
+
return { applied: 0, skipped: 0, failed: [], notes: ["还没有构建产物:指令已存,下次 stm32_ecad_build 时重放"] };
|
|
195
|
+
}
|
|
196
|
+
let applied = 0;
|
|
197
|
+
let skipped = 0;
|
|
198
|
+
const failed = [];
|
|
199
|
+
const notes = [];
|
|
200
|
+
for (const name of readDir(kicadDir)) {
|
|
201
|
+
if (!name.endsWith(".kicad_sch"))
|
|
202
|
+
continue;
|
|
203
|
+
const file = join(kicadDir, name);
|
|
204
|
+
let before;
|
|
205
|
+
try {
|
|
206
|
+
before = readText(file);
|
|
207
|
+
}
|
|
208
|
+
catch (error) {
|
|
209
|
+
notes.push(`读不到 ${name}:${describeError(error)}`);
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
const outcome = applySchEdits(before, file, edits);
|
|
213
|
+
if (!outcome.ok) {
|
|
214
|
+
notes.push(outcome.error);
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
if (outcome.applied > 0)
|
|
218
|
+
writeText(file, outcome.text);
|
|
219
|
+
applied += outcome.applied;
|
|
220
|
+
skipped += outcome.skipped;
|
|
221
|
+
for (const failure of outcome.failed)
|
|
222
|
+
failed.push(failure);
|
|
223
|
+
}
|
|
224
|
+
if (applied > 0)
|
|
225
|
+
notes.push(`已重放 ${String(applied)} 条指令`);
|
|
226
|
+
if (applied === 0 && skipped > 0 && failed.length === 0)
|
|
227
|
+
notes.push("这些指令已经画在图纸上");
|
|
228
|
+
return { applied, skipped, failed, notes };
|
|
229
|
+
}
|