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,903 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L3:电路设计工具(S97)——用 tscircuit 画板,用 kicad-cli 验收,导出 STEP。
|
|
3
|
+
*
|
|
4
|
+
* 四个工具的分工:
|
|
5
|
+
* stm32_ecad_init 一次性的工作区初始化(circuit/ 骨架与四个目录)
|
|
6
|
+
* stm32_ecad_build 跑 tsci build:tsx → circuit.json + svg + 真 KiCad 工程
|
|
7
|
+
* stm32_ecad_check 跑 kicad-cli ERC/DRC:把违规项收敛成结构化清单
|
|
8
|
+
* stm32_ecad_step 从 KiCad 板导出 STEP,并报告缺失的 3D 模型
|
|
9
|
+
*
|
|
10
|
+
* 写者只有一个:`.kicad_sch` / `.kicad_pcb` 由 tscircuit 生成,本层不直接改它们。
|
|
11
|
+
*/
|
|
12
|
+
import { existsSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
13
|
+
import { basename, dirname, join } from "node:path";
|
|
14
|
+
import { exportStep, findUnnamedNets, runDrc, runErc } from "../engine/ecad/checks.js";
|
|
15
|
+
import { checkDfm, druRulesForLayers, fabRulesForLayers, writeDru } from "../engine/ecad/dfm.js";
|
|
16
|
+
import { countCopperLayers } from "../engine/ecad/pcb/rules.js";
|
|
17
|
+
import { exportEmbeddedLibraries } from "../engine/ecad/libs.js";
|
|
18
|
+
import { runEcadDesign } from "../engine/ecad/design.js";
|
|
19
|
+
import { ensureProjectRuntime } from "../engine/ecad/runtime.js";
|
|
20
|
+
import { exportFabrication } from "../engine/ecad/fabrication.js";
|
|
21
|
+
import { precheckFootprints } from "../engine/ecad/padprecheck.js";
|
|
22
|
+
import { locateEcadToolchain } from "../engine/ecad/toolchain.js";
|
|
23
|
+
import { mergeCoincidentPads } from "../engine/ecad/padmerge.js";
|
|
24
|
+
import { ensureProjectDatasheets } from "../engine/datasheet/auto.js";
|
|
25
|
+
import { dirSizeBytes, initEcadWorkspace, listCircuitEntries, planEcadWorkspace, runTsciBuild, } from "../engine/ecad/tsci.js";
|
|
26
|
+
import { resolveProjectDir } from "./project-dir.js";
|
|
27
|
+
import { resolveProjectLayout } from "../engine/project-layout.js";
|
|
28
|
+
export const ECAD_INIT_TOOL = "stm32_ecad_init";
|
|
29
|
+
export const ECAD_DESIGN_TOOL = "stm32_ecad_design";
|
|
30
|
+
export const ECAD_BUILD_TOOL = "stm32_ecad_build";
|
|
31
|
+
export const ECAD_CHECK_TOOL = "stm32_ecad_check";
|
|
32
|
+
export const ECAD_STEP_TOOL = "stm32_ecad_step";
|
|
33
|
+
export const ECAD_LIBS_TOOL = "stm32_ecad_libs";
|
|
34
|
+
export const ECAD_EXPORT_TOOL = "stm32_ecad_export";
|
|
35
|
+
export const ECAD_PACKAGE_TOOL = "stm32_ecad_package";
|
|
36
|
+
const fileSizeSchema = {
|
|
37
|
+
type: "object",
|
|
38
|
+
additionalProperties: false,
|
|
39
|
+
properties: {
|
|
40
|
+
name: { type: "string" },
|
|
41
|
+
bytes: { type: "number" },
|
|
42
|
+
},
|
|
43
|
+
required: ["name", "bytes"],
|
|
44
|
+
};
|
|
45
|
+
const findingSchema = {
|
|
46
|
+
type: "object",
|
|
47
|
+
additionalProperties: false,
|
|
48
|
+
properties: {
|
|
49
|
+
rule: { type: "string" },
|
|
50
|
+
severity: { type: "string" },
|
|
51
|
+
message: { type: "string" },
|
|
52
|
+
items: { type: "array", items: { type: "string" } },
|
|
53
|
+
},
|
|
54
|
+
required: ["rule", "severity", "message", "items"],
|
|
55
|
+
};
|
|
56
|
+
const checkSummarySchema = {
|
|
57
|
+
type: "object",
|
|
58
|
+
additionalProperties: false,
|
|
59
|
+
properties: {
|
|
60
|
+
errors: { type: "number" },
|
|
61
|
+
warnings: { type: "number" },
|
|
62
|
+
findings: { type: "array", items: findingSchema },
|
|
63
|
+
},
|
|
64
|
+
required: ["errors", "warnings", "findings"],
|
|
65
|
+
};
|
|
66
|
+
/** 工程根与 ECAD 工作区(`hardware/`),找不到硬件目录时按约定落到 `<工程>/hardware`。 */
|
|
67
|
+
function workspaceOf(projectDir) {
|
|
68
|
+
const layout = resolveProjectLayout(projectDir);
|
|
69
|
+
// 空串交给 planEcadWorkspace 处理:它会落到 <工程>/hardware,而不是把工程根当硬件目录。
|
|
70
|
+
return planEcadWorkspace(projectDir, layout.hardwareDir);
|
|
71
|
+
}
|
|
72
|
+
async function projectDirOf(ctx, raw, exec) {
|
|
73
|
+
const resolved = await resolveProjectDir(ctx, typeof raw === "string" ? raw : "", exec);
|
|
74
|
+
if (resolved.projectDir === "") {
|
|
75
|
+
return {
|
|
76
|
+
ok: false,
|
|
77
|
+
dir: "",
|
|
78
|
+
error: "projectDir 不能为空:从工具栏「工程」下拉里选一个,或先打开调试会话",
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
return { ok: true, dir: resolved.projectDir, error: "" };
|
|
82
|
+
}
|
|
83
|
+
function formatReport(name, report) {
|
|
84
|
+
const lines = [name + ":" + String(report.summary.errors) + " error / " + String(report.summary.warnings) + " warning"];
|
|
85
|
+
for (const finding of report.summary.findings.slice(0, 12)) {
|
|
86
|
+
const items = finding.items.length > 0 ? "(" + finding.items.join(";") + ")" : "";
|
|
87
|
+
lines.push(" [" + finding.severity + "] " + finding.rule + " " + finding.message + items);
|
|
88
|
+
}
|
|
89
|
+
if (report.summary.findings.length > 12)
|
|
90
|
+
lines.push(" …还有 " + String(report.summary.findings.length - 12) + " 条");
|
|
91
|
+
if (report.reportPath !== "")
|
|
92
|
+
lines.push(" 报告:" + report.reportPath);
|
|
93
|
+
if (!report.ok && report.error !== "")
|
|
94
|
+
lines.push(" 命令输出:" + report.error.split(/\r?\n/u)[0]);
|
|
95
|
+
return lines;
|
|
96
|
+
}
|
|
97
|
+
export function registerEcadTools(ctx) {
|
|
98
|
+
ctx.tools.register({
|
|
99
|
+
name: ECAD_INIT_TOOL,
|
|
100
|
+
description: "初始化硬件工作区:在 hardware/ 下建 circuit/(tscircuit 工程)、kicad/、libs/、out/ 并写 package.json。不装依赖,装依赖按提示执行 npm install。",
|
|
101
|
+
parameters: {
|
|
102
|
+
type: "object",
|
|
103
|
+
additionalProperties: false,
|
|
104
|
+
properties: {
|
|
105
|
+
projectDir: { type: "string", description: "工程根目录;缺省为会话工作目录。" },
|
|
106
|
+
},
|
|
107
|
+
required: [],
|
|
108
|
+
},
|
|
109
|
+
output: {
|
|
110
|
+
schema: {
|
|
111
|
+
type: "object",
|
|
112
|
+
additionalProperties: false,
|
|
113
|
+
properties: {
|
|
114
|
+
ok: { type: "boolean" },
|
|
115
|
+
error: { type: "string" },
|
|
116
|
+
root: { type: "string" },
|
|
117
|
+
circuitDir: { type: "string" },
|
|
118
|
+
created: { type: "array", items: { type: "string" } },
|
|
119
|
+
entries: { type: "array", items: { type: "string" } },
|
|
120
|
+
nodeModulesReady: { type: "boolean" },
|
|
121
|
+
notes: { type: "array", items: { type: "string" } },
|
|
122
|
+
},
|
|
123
|
+
required: ["ok", "error", "root", "circuitDir", "created", "entries", "nodeModulesReady", "notes"],
|
|
124
|
+
},
|
|
125
|
+
render: (_args, value) => {
|
|
126
|
+
const result = value;
|
|
127
|
+
if (!result.ok)
|
|
128
|
+
return [{ type: "text", text: "初始化失败:" + result.error }];
|
|
129
|
+
const lines = ["工作区:" + result.root, "tscircuit 工程:" + result.circuitDir];
|
|
130
|
+
lines.push(result.nodeModulesReady ? "依赖:已安装" : "依赖:未安装,先在该目录执行 npm install(约 730 MB)");
|
|
131
|
+
if (result.created.length > 0)
|
|
132
|
+
lines.push("新建:" + result.created.join("、"));
|
|
133
|
+
lines.push(result.entries.length > 0 ? "入口:" + result.entries.join("、") : "入口:还没有 *.circuit.tsx");
|
|
134
|
+
for (const note of result.notes)
|
|
135
|
+
lines.push("- " + note);
|
|
136
|
+
return [{ type: "text", text: lines.join("\n") }];
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
timeoutMs: 60_000,
|
|
140
|
+
isConcurrencySafe: () => false,
|
|
141
|
+
execute: async (args, exec) => {
|
|
142
|
+
const project = await projectDirOf(ctx, args.projectDir, exec);
|
|
143
|
+
if (!project.ok) {
|
|
144
|
+
return {
|
|
145
|
+
ok: false,
|
|
146
|
+
error: project.error,
|
|
147
|
+
root: "",
|
|
148
|
+
circuitDir: "",
|
|
149
|
+
created: [],
|
|
150
|
+
entries: [],
|
|
151
|
+
nodeModulesReady: false,
|
|
152
|
+
notes: [],
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
const toolchain = locateEcadToolchain();
|
|
156
|
+
const workspace = workspaceOf(project.dir);
|
|
157
|
+
const init = initEcadWorkspace(workspace);
|
|
158
|
+
const installed = existsSync(join(workspace.circuitDir, "node_modules", "tscircuit"));
|
|
159
|
+
const notes = [...toolchain.toolchain?.notes ?? []];
|
|
160
|
+
if (toolchain.toolchain === null)
|
|
161
|
+
notes.push(toolchain.error);
|
|
162
|
+
if (installed)
|
|
163
|
+
notes.push("node_modules 占用 " + String(Math.round(dirSizeBytes(join(workspace.circuitDir, "node_modules")) / 1048576)) + " MB");
|
|
164
|
+
return {
|
|
165
|
+
ok: init.ok,
|
|
166
|
+
error: init.error,
|
|
167
|
+
root: workspace.root,
|
|
168
|
+
circuitDir: workspace.circuitDir,
|
|
169
|
+
created: [...init.created],
|
|
170
|
+
entries: listCircuitEntries(workspace.circuitDir),
|
|
171
|
+
nodeModulesReady: installed,
|
|
172
|
+
notes,
|
|
173
|
+
};
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
ctx.tools.register({
|
|
177
|
+
name: ECAD_BUILD_TOOL,
|
|
178
|
+
description: "构建电路:用 KiCad 自带的 bun 跑 tscircuit build,把 hardware/circuit 下的 .circuit.tsx 生成 circuit.json、原理图/PCB 的 SVG 与真 KiCad 工程(hardware/kicad)。不装依赖。",
|
|
179
|
+
parameters: {
|
|
180
|
+
type: "object",
|
|
181
|
+
additionalProperties: false,
|
|
182
|
+
properties: {
|
|
183
|
+
projectDir: { type: "string", description: "工程根目录;缺省为会话工作目录。" },
|
|
184
|
+
entry: { type: "string", description: "入口文件(相对 hardware/circuit);缺省让 tscircuit 自己找。" },
|
|
185
|
+
svgs: { type: "boolean", description: "是否同时生成 SVG;默认 true。" },
|
|
186
|
+
},
|
|
187
|
+
required: [],
|
|
188
|
+
},
|
|
189
|
+
output: {
|
|
190
|
+
schema: {
|
|
191
|
+
type: "object",
|
|
192
|
+
additionalProperties: false,
|
|
193
|
+
properties: {
|
|
194
|
+
ok: { type: "boolean" },
|
|
195
|
+
error: { type: "string" },
|
|
196
|
+
durationMs: { type: "number" },
|
|
197
|
+
kicadDir: { type: "string" },
|
|
198
|
+
circuitJson: { type: "string" },
|
|
199
|
+
pcbSvg: { type: "string" },
|
|
200
|
+
schematicSvg: { type: "string" },
|
|
201
|
+
exitCode: { type: "number" },
|
|
202
|
+
notes: { type: "array", items: { type: "string" } },
|
|
203
|
+
stderrTail: { type: "string" },
|
|
204
|
+
},
|
|
205
|
+
required: ["ok", "error", "durationMs", "kicadDir", "circuitJson", "pcbSvg", "schematicSvg", "exitCode", "notes", "stderrTail"],
|
|
206
|
+
},
|
|
207
|
+
render: (_args, value) => {
|
|
208
|
+
const result = value;
|
|
209
|
+
const lines = [
|
|
210
|
+
result.ok
|
|
211
|
+
? "构建完成(" + String(Math.round(result.durationMs / 100) / 10) + " s)"
|
|
212
|
+
: "构建失败(exit " + String(result.exitCode) + ")",
|
|
213
|
+
];
|
|
214
|
+
if (result.kicadDir !== "")
|
|
215
|
+
lines.push("KiCad 工程:" + result.kicadDir);
|
|
216
|
+
if (result.circuitJson !== "")
|
|
217
|
+
lines.push("circuit.json:" + result.circuitJson);
|
|
218
|
+
if (result.pcbSvg !== "")
|
|
219
|
+
lines.push("PCB 预览:" + result.pcbSvg);
|
|
220
|
+
if (result.schematicSvg !== "")
|
|
221
|
+
lines.push("原理图预览:" + result.schematicSvg);
|
|
222
|
+
if (!result.ok && result.error !== "")
|
|
223
|
+
lines.push("原因:" + result.error.split(/\r?\n/u).slice(0, 6).join("\n"));
|
|
224
|
+
for (const note of result.notes)
|
|
225
|
+
lines.push("- " + note);
|
|
226
|
+
return [{ type: "text", text: lines.join("\n") }];
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
timeoutMs: 330_000,
|
|
230
|
+
isConcurrencySafe: () => false,
|
|
231
|
+
execute: async (args, exec) => {
|
|
232
|
+
const project = await projectDirOf(ctx, args.projectDir, exec);
|
|
233
|
+
if (!project.ok) {
|
|
234
|
+
return {
|
|
235
|
+
ok: false,
|
|
236
|
+
error: project.error,
|
|
237
|
+
durationMs: 0,
|
|
238
|
+
kicadDir: "",
|
|
239
|
+
circuitJson: "",
|
|
240
|
+
pcbSvg: "",
|
|
241
|
+
schematicSvg: "",
|
|
242
|
+
exitCode: -1,
|
|
243
|
+
notes: [],
|
|
244
|
+
stderrTail: "",
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
const located = locateEcadToolchain();
|
|
248
|
+
if (located.toolchain === null) {
|
|
249
|
+
return {
|
|
250
|
+
ok: false,
|
|
251
|
+
error: located.error,
|
|
252
|
+
durationMs: 0,
|
|
253
|
+
kicadDir: "",
|
|
254
|
+
circuitJson: "",
|
|
255
|
+
pcbSvg: "",
|
|
256
|
+
schematicSvg: "",
|
|
257
|
+
exitCode: -1,
|
|
258
|
+
notes: [],
|
|
259
|
+
stderrTail: "",
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
const workspace = workspaceOf(project.dir);
|
|
263
|
+
// 依赖先就位:共享运行时 + 工程联接,缺依赖时给出「怎么装」而不是让 bun 半路失败(S136)。
|
|
264
|
+
const runtime = await ensureProjectRuntime({ subprocess: ctx.subprocess, circuitDir: workspace.circuitDir, signal: exec.signal });
|
|
265
|
+
if (!runtime.ok) {
|
|
266
|
+
return {
|
|
267
|
+
ok: false,
|
|
268
|
+
error: runtime.error,
|
|
269
|
+
kicadDir: "",
|
|
270
|
+
exitCode: -1,
|
|
271
|
+
notes: [...runtime.notes],
|
|
272
|
+
stderrTail: "",
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
// 布线前先看封装:同坐标焊盘会让自动布线器放弃整板,降级为只摆件(S130)。
|
|
276
|
+
const entryName = (args.entry ?? "").trim() === "" ? "" : (args.entry ?? "").trim();
|
|
277
|
+
const entries = listCircuitEntries(workspace.circuitDir);
|
|
278
|
+
const tsxFile = join(workspace.circuitDir, entryName === "" ? (entries[0] ?? "") : entryName);
|
|
279
|
+
const precheck = entryName === "" && entries.length === 0
|
|
280
|
+
? { ok: true, error: "", checked: 0, blockers: [], notes: [] }
|
|
281
|
+
: await precheckFootprints(tsxFile, { signal: exec.signal });
|
|
282
|
+
const routingDisabled = precheck.blockers.length > 0;
|
|
283
|
+
const built = await runTsciBuild({
|
|
284
|
+
subprocess: ctx.subprocess,
|
|
285
|
+
toolchain: located.toolchain,
|
|
286
|
+
workspace,
|
|
287
|
+
...(args.entry === undefined ? {} : { entry: args.entry }),
|
|
288
|
+
...(args.svgs === undefined ? {} : { svgs: args.svgs }),
|
|
289
|
+
...(routingDisabled ? { routingDisabled: true } : {}),
|
|
290
|
+
signal: exec.signal,
|
|
291
|
+
});
|
|
292
|
+
const notes = [...precheck.notes, ...built.notes];
|
|
293
|
+
// 合并焊盘要在导出之后做:tscircuit 的 footprint 对象不生成 port,布线器也不认多编号
|
|
294
|
+
// 焊盘,所以只能在 .kicad_pcb 里把同坐标、同网络的焊盘并成一个(S131)。
|
|
295
|
+
if (built.ok && args.mergePads === true && built.kicadProjectDir !== "") {
|
|
296
|
+
const pcbName = readdirSync(built.kicadProjectDir).find((name) => name.endsWith(".kicad_pcb"));
|
|
297
|
+
if (pcbName !== undefined) {
|
|
298
|
+
const pcbFile = join(built.kicadProjectDir, pcbName);
|
|
299
|
+
const merged = mergeCoincidentPads(readFileSync(pcbFile, "utf8"));
|
|
300
|
+
if (merged.merged.length > 0)
|
|
301
|
+
writeFileSync(pcbFile, merged.text, "utf8");
|
|
302
|
+
for (const group of merged.merged) {
|
|
303
|
+
notes.push("合并焊盘:" + group.names.join("/") + "(" + group.footprint + ")");
|
|
304
|
+
}
|
|
305
|
+
for (const skip of merged.skipped) {
|
|
306
|
+
notes.push("未合并:" + skip.names.join("/") + "(" + skip.footprint + ")——" + skip.reason);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
// 构建成功就把本次用到的器件手册补齐:后面读引脚、电气参数、时序都要靠它(S120)。
|
|
311
|
+
if (built.ok) {
|
|
312
|
+
const ds = await ensureProjectDatasheets(project.dir, {
|
|
313
|
+
offline: (process.env["DSH_STM32_OFFLINE"] ?? "").trim() === "1",
|
|
314
|
+
signal: exec.signal,
|
|
315
|
+
});
|
|
316
|
+
if (ds.codes.length > 0) {
|
|
317
|
+
notes.push("数据手册:" + String(ds.cached) + " 份已在缓存,本次新抓 " + String(ds.fetched.length) + " 份" +
|
|
318
|
+
(ds.failed.length > 0 ? "," + String(ds.failed.length) + " 份失败(可用 stm32_ds_fetch file=… 手工导入)" : ""));
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return {
|
|
322
|
+
ok: built.ok,
|
|
323
|
+
error: built.error,
|
|
324
|
+
durationMs: built.durationMs,
|
|
325
|
+
kicadDir: built.kicadProjectDir,
|
|
326
|
+
circuitJson: built.circuitJson,
|
|
327
|
+
pcbSvg: built.pcbSvg,
|
|
328
|
+
schematicSvg: built.schematicSvg,
|
|
329
|
+
exitCode: built.exitCode ?? -1,
|
|
330
|
+
notes,
|
|
331
|
+
stderrTail: built.stderrTail,
|
|
332
|
+
};
|
|
333
|
+
},
|
|
334
|
+
});
|
|
335
|
+
ctx.tools.register({
|
|
336
|
+
name: ECAD_CHECK_TOOL,
|
|
337
|
+
description: "检查电路:对生成的 KiCad 工程跑 kicad-cli ERC 与 DRC(含未连接项与原理图/PCB 一致性),把违规项收敛成清单。只读。",
|
|
338
|
+
parameters: {
|
|
339
|
+
type: "object",
|
|
340
|
+
additionalProperties: false,
|
|
341
|
+
properties: {
|
|
342
|
+
projectDir: { type: "string", description: "工程根目录;缺省为会话工作目录。" },
|
|
343
|
+
severity: {
|
|
344
|
+
type: "string",
|
|
345
|
+
enum: ["all", "warning", "error"],
|
|
346
|
+
description: "只返回不低于该级别的发现;默认 all。",
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
required: [],
|
|
350
|
+
},
|
|
351
|
+
output: {
|
|
352
|
+
schema: {
|
|
353
|
+
type: "object",
|
|
354
|
+
additionalProperties: false,
|
|
355
|
+
properties: {
|
|
356
|
+
ok: { type: "boolean" },
|
|
357
|
+
error: { type: "string" },
|
|
358
|
+
kicadDir: { type: "string" },
|
|
359
|
+
schematicFile: { type: "string" },
|
|
360
|
+
pcbFile: { type: "string" },
|
|
361
|
+
erc: checkSummarySchema,
|
|
362
|
+
drc: checkSummarySchema,
|
|
363
|
+
libsFootprints: { type: "number" },
|
|
364
|
+
libsSymbols: { type: "number" },
|
|
365
|
+
unnamedNets: { type: "array", items: { type: "string" } },
|
|
366
|
+
dfm: {
|
|
367
|
+
type: "object",
|
|
368
|
+
additionalProperties: false,
|
|
369
|
+
properties: {
|
|
370
|
+
ok: { type: "boolean" },
|
|
371
|
+
widthMm: { type: "number" },
|
|
372
|
+
heightMm: { type: "number" },
|
|
373
|
+
layers: { type: "number" },
|
|
374
|
+
minTrackMm: { type: "number" },
|
|
375
|
+
minDrillMm: { type: "number" },
|
|
376
|
+
approxCny: { type: "number" },
|
|
377
|
+
basis: { type: "array", items: { type: "string" } },
|
|
378
|
+
surcharges: { type: "array", items: { type: "string" } },
|
|
379
|
+
druFile: { type: "string" },
|
|
380
|
+
findings: {
|
|
381
|
+
type: "array",
|
|
382
|
+
items: {
|
|
383
|
+
type: "object",
|
|
384
|
+
additionalProperties: false,
|
|
385
|
+
properties: {
|
|
386
|
+
rule: { type: "string" },
|
|
387
|
+
severity: { type: "string", enum: ["error", "warning", "info"] },
|
|
388
|
+
message: { type: "string" },
|
|
389
|
+
fix: { type: "string" },
|
|
390
|
+
},
|
|
391
|
+
required: ["rule", "severity", "message", "fix"],
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
},
|
|
395
|
+
required: ["ok", "widthMm", "heightMm", "layers", "minTrackMm", "minDrillMm", "approxCny", "basis", "surcharges", "druFile", "findings"],
|
|
396
|
+
},
|
|
397
|
+
},
|
|
398
|
+
required: ["ok", "error", "kicadDir", "schematicFile", "pcbFile", "erc", "drc", "libsFootprints", "libsSymbols", "unnamedNets", "dfm"],
|
|
399
|
+
},
|
|
400
|
+
render: (_args, value) => {
|
|
401
|
+
const result = value;
|
|
402
|
+
if (!result.ok)
|
|
403
|
+
return [{ type: "text", text: "检查未执行:" + result.error }];
|
|
404
|
+
const erc = result.erc;
|
|
405
|
+
const drc = result.drc;
|
|
406
|
+
const lines = [
|
|
407
|
+
...formatReport("ERC", { ok: true, error: "", file: result.schematicFile, reportPath: "", summary: erc, raw: "", notes: [] }),
|
|
408
|
+
...formatReport("DRC", { ok: true, error: "", file: result.pcbFile, reportPath: "", summary: drc, raw: "", notes: [] }),
|
|
409
|
+
"库:导出 " + String(result.libsFootprints) + " 个封装库 / " + String(result.libsSymbols) + " 个符号库(fp-lib-table / sym-lib-table 已补齐)",
|
|
410
|
+
];
|
|
411
|
+
if (result.unnamedNets.length > 0) {
|
|
412
|
+
lines.push("未命名网络 " + String(result.unnamedNets.length) + " 条:" + result.unnamedNets.slice(0, 3).join(";"));
|
|
413
|
+
lines.push(" 修法:在 tscircuit 里写 <net name=\"VDD\" />,trace 上加 net=\"VDD\"");
|
|
414
|
+
}
|
|
415
|
+
const dfm = result.dfm;
|
|
416
|
+
lines.push("可制造性:板 " + String(dfm.widthMm) + "×" + String(dfm.heightMm) + " mm、" + String(dfm.layers) + " 层、" +
|
|
417
|
+
"最细走线 " + String(dfm.minTrackMm) + " mm、最小孔径 " + String(dfm.minDrillMm) + " mm" +
|
|
418
|
+
(dfm.druFile !== "" ? "(设计规则已写入 " + dfm.druFile + ")" : ""));
|
|
419
|
+
for (const finding of dfm.findings.slice(0, 8)) {
|
|
420
|
+
const tag = finding.severity === "error" ? "错误" : finding.severity === "warning" ? "注意" : "提示";
|
|
421
|
+
lines.push(" [" + tag + "] " + finding.message);
|
|
422
|
+
lines.push(" 修法:" + finding.fix);
|
|
423
|
+
}
|
|
424
|
+
lines.push("打样成本:约 " + String(dfm.approxCny) + " 元(" + dfm.basis.join(";") + ")");
|
|
425
|
+
if (dfm.surcharges.length > 0)
|
|
426
|
+
lines.push("加价项:" + dfm.surcharges.join(";"));
|
|
427
|
+
return [{ type: "text", text: lines.join("\n") }];
|
|
428
|
+
},
|
|
429
|
+
},
|
|
430
|
+
timeoutMs: 240_000,
|
|
431
|
+
isConcurrencySafe: () => true,
|
|
432
|
+
execute: async (args, exec) => {
|
|
433
|
+
const empty = { errors: 0, warnings: 0, findings: [] };
|
|
434
|
+
const blank = {
|
|
435
|
+
ok: false,
|
|
436
|
+
error: "",
|
|
437
|
+
kicadDir: "",
|
|
438
|
+
schematicFile: "",
|
|
439
|
+
pcbFile: "",
|
|
440
|
+
erc: empty,
|
|
441
|
+
drc: empty,
|
|
442
|
+
libsFootprints: 0,
|
|
443
|
+
libsSymbols: 0,
|
|
444
|
+
unnamedNets: [],
|
|
445
|
+
dfm: {
|
|
446
|
+
ok: false,
|
|
447
|
+
widthMm: 0,
|
|
448
|
+
heightMm: 0,
|
|
449
|
+
layers: 0,
|
|
450
|
+
minTrackMm: 0,
|
|
451
|
+
minDrillMm: 0,
|
|
452
|
+
approxCny: 0,
|
|
453
|
+
basis: [],
|
|
454
|
+
surcharges: [],
|
|
455
|
+
druFile: "",
|
|
456
|
+
findings: [],
|
|
457
|
+
},
|
|
458
|
+
};
|
|
459
|
+
const project = await projectDirOf(ctx, args.projectDir, exec);
|
|
460
|
+
if (!project.ok)
|
|
461
|
+
return { ...blank, error: project.error };
|
|
462
|
+
const located = locateEcadToolchain();
|
|
463
|
+
if (located.toolchain === null)
|
|
464
|
+
return { ...blank, error: located.error };
|
|
465
|
+
const workspace = workspaceOf(project.dir);
|
|
466
|
+
const { findLatestKicadBuild } = await import("../engine/ecad/tsci.js");
|
|
467
|
+
const latest = findLatestKicadBuild(workspace);
|
|
468
|
+
if (!latest.ok)
|
|
469
|
+
return { ...blank, error: latest.error };
|
|
470
|
+
const sch = latest.schematicFile;
|
|
471
|
+
const pcb = latest.pcbFile;
|
|
472
|
+
// 先补齐库表:否则 DRC 会报「封装库 'tscircuit' 不在配置中」,那是在查环境而不是查电路。
|
|
473
|
+
const libs = exportEmbeddedLibraries(workspace, pcb, sch);
|
|
474
|
+
const pcbText = readFileSync(pcb, "utf8");
|
|
475
|
+
const unnamedNets = findUnnamedNets(pcbText);
|
|
476
|
+
// 工艺档按铜层数取(S149),与布线器 fab_tiers 的 standard 档一致。
|
|
477
|
+
const layered = fabRulesForLayers(countCopperLayers(latest.kicadDir));
|
|
478
|
+
const dru = writeDru(latest.kicadDir, druRulesForLayers(countCopperLayers(latest.kicadDir)));
|
|
479
|
+
const dfm = checkDfm(pcbText, layered);
|
|
480
|
+
const [erc, drc] = await Promise.all([
|
|
481
|
+
runErc(ctx.subprocess, located.toolchain, sch, exec.signal),
|
|
482
|
+
runDrc(ctx.subprocess, located.toolchain, pcb, exec.signal),
|
|
483
|
+
]);
|
|
484
|
+
return {
|
|
485
|
+
ok: erc.ok && drc.ok,
|
|
486
|
+
error: erc.ok ? drc.error : erc.error,
|
|
487
|
+
kicadDir: latest.kicadDir,
|
|
488
|
+
schematicFile: sch,
|
|
489
|
+
pcbFile: pcb,
|
|
490
|
+
erc: erc.summary,
|
|
491
|
+
drc: drc.summary,
|
|
492
|
+
libsFootprints: libs.footprintLibs.length,
|
|
493
|
+
libsSymbols: libs.symbolLibs.length,
|
|
494
|
+
unnamedNets,
|
|
495
|
+
dfm: {
|
|
496
|
+
ok: dfm.ok,
|
|
497
|
+
widthMm: dfm.facts.widthMm,
|
|
498
|
+
heightMm: dfm.facts.heightMm,
|
|
499
|
+
layers: dfm.facts.layers,
|
|
500
|
+
minTrackMm: dfm.facts.minTrackMm,
|
|
501
|
+
minDrillMm: dfm.facts.minDrillMm,
|
|
502
|
+
approxCny: dfm.cost.approxCny,
|
|
503
|
+
basis: [...dfm.cost.basis],
|
|
504
|
+
surcharges: [...dfm.cost.surcharges],
|
|
505
|
+
druFile: dru.file,
|
|
506
|
+
findings: dfm.findings.map((finding) => ({ ...finding })),
|
|
507
|
+
},
|
|
508
|
+
};
|
|
509
|
+
},
|
|
510
|
+
});
|
|
511
|
+
ctx.tools.register({
|
|
512
|
+
name: ECAD_LIBS_TOOL,
|
|
513
|
+
description: "导出内嵌库:把生成的 KiCad 板与原理图里内嵌的封装/符号抽成 hardware/libs 下的真库,并补齐 fp-lib-table 与 sym-lib-table。不改 .kicad_pcb / .kicad_sch。",
|
|
514
|
+
parameters: {
|
|
515
|
+
type: "object",
|
|
516
|
+
additionalProperties: false,
|
|
517
|
+
properties: {
|
|
518
|
+
projectDir: { type: "string", description: "工程根目录;缺省为会话工作目录。" },
|
|
519
|
+
},
|
|
520
|
+
required: [],
|
|
521
|
+
},
|
|
522
|
+
output: {
|
|
523
|
+
schema: {
|
|
524
|
+
type: "object",
|
|
525
|
+
additionalProperties: false,
|
|
526
|
+
properties: {
|
|
527
|
+
ok: { type: "boolean" },
|
|
528
|
+
error: { type: "string" },
|
|
529
|
+
footprints: {
|
|
530
|
+
type: "array",
|
|
531
|
+
items: {
|
|
532
|
+
type: "object",
|
|
533
|
+
additionalProperties: false,
|
|
534
|
+
properties: { lib: { type: "string" }, count: { type: "number" } },
|
|
535
|
+
required: ["lib", "count"],
|
|
536
|
+
},
|
|
537
|
+
},
|
|
538
|
+
symbols: {
|
|
539
|
+
type: "array",
|
|
540
|
+
items: {
|
|
541
|
+
type: "object",
|
|
542
|
+
additionalProperties: false,
|
|
543
|
+
properties: { lib: { type: "string" }, count: { type: "number" } },
|
|
544
|
+
required: ["lib", "count"],
|
|
545
|
+
},
|
|
546
|
+
},
|
|
547
|
+
fpLibTable: { type: "string" },
|
|
548
|
+
symLibTable: { type: "string" },
|
|
549
|
+
notes: { type: "array", items: { type: "string" } },
|
|
550
|
+
},
|
|
551
|
+
required: ["ok", "error", "footprints", "symbols", "fpLibTable", "symLibTable", "notes"],
|
|
552
|
+
},
|
|
553
|
+
render: (_args, value) => {
|
|
554
|
+
const result = value;
|
|
555
|
+
if (!result.ok)
|
|
556
|
+
return [{ type: "text", text: "库导出失败:" + result.error }];
|
|
557
|
+
const lines = [];
|
|
558
|
+
lines.push("封装库:" +
|
|
559
|
+
(result.footprints.length === 0
|
|
560
|
+
? "无内嵌封装"
|
|
561
|
+
: result.footprints.map((item) => item.lib + "(" + String(item.count) + ")").join("、")));
|
|
562
|
+
lines.push("符号库:" +
|
|
563
|
+
(result.symbols.length === 0
|
|
564
|
+
? "无内嵌符号"
|
|
565
|
+
: result.symbols.map((item) => item.lib + "(" + String(item.count) + ")").join("、")));
|
|
566
|
+
for (const note of result.notes)
|
|
567
|
+
lines.push("- " + note);
|
|
568
|
+
return [{ type: "text", text: lines.join("\n") }];
|
|
569
|
+
},
|
|
570
|
+
},
|
|
571
|
+
timeoutMs: 60_000,
|
|
572
|
+
isConcurrencySafe: () => false,
|
|
573
|
+
execute: async (args, exec) => {
|
|
574
|
+
const blank = { ok: false, error: "", footprints: [], symbols: [], fpLibTable: "", symLibTable: "", notes: [] };
|
|
575
|
+
const project = await projectDirOf(ctx, args.projectDir, exec);
|
|
576
|
+
if (!project.ok)
|
|
577
|
+
return { ...blank, error: project.error };
|
|
578
|
+
const workspace = workspaceOf(project.dir);
|
|
579
|
+
const { findLatestKicadBuild } = await import("../engine/ecad/tsci.js");
|
|
580
|
+
const latest = findLatestKicadBuild(workspace);
|
|
581
|
+
if (!latest.ok)
|
|
582
|
+
return { ...blank, error: latest.error };
|
|
583
|
+
const exported = exportEmbeddedLibraries(workspace, latest.pcbFile, latest.schematicFile);
|
|
584
|
+
return {
|
|
585
|
+
ok: exported.ok,
|
|
586
|
+
error: exported.error,
|
|
587
|
+
footprints: exported.footprintLibs.map((item) => ({ lib: item.lib, count: item.count })),
|
|
588
|
+
symbols: exported.symbolLibs.map((item) => ({ lib: item.lib, count: item.count })),
|
|
589
|
+
fpLibTable: exported.fpLibTable,
|
|
590
|
+
symLibTable: exported.symLibTable,
|
|
591
|
+
notes: [...exported.notes],
|
|
592
|
+
};
|
|
593
|
+
},
|
|
594
|
+
});
|
|
595
|
+
ctx.tools.register({
|
|
596
|
+
name: ECAD_EXPORT_TOOL,
|
|
597
|
+
description: "导出制造文件:Gerber、钻孔、贴片坐标与 STEP 一起写到 hardware/out。缺 3D 模型或钻孔失败只记提示,不阻断其它产物。",
|
|
598
|
+
parameters: {
|
|
599
|
+
type: "object",
|
|
600
|
+
additionalProperties: false,
|
|
601
|
+
properties: {
|
|
602
|
+
projectDir: { type: "string", description: "工程根目录;缺省为会话工作目录。" },
|
|
603
|
+
},
|
|
604
|
+
required: [],
|
|
605
|
+
},
|
|
606
|
+
output: {
|
|
607
|
+
schema: {
|
|
608
|
+
type: "object",
|
|
609
|
+
additionalProperties: false,
|
|
610
|
+
properties: {
|
|
611
|
+
ok: { type: "boolean" },
|
|
612
|
+
error: { type: "string" },
|
|
613
|
+
outDir: { type: "string" },
|
|
614
|
+
gerberDir: { type: "string" },
|
|
615
|
+
gerbers: { type: "array", items: fileSizeSchema },
|
|
616
|
+
stepFile: { type: "string" },
|
|
617
|
+
stepBytes: { type: "number" },
|
|
618
|
+
posFile: { type: "string" },
|
|
619
|
+
drillFiles: { type: "array", items: fileSizeSchema },
|
|
620
|
+
notes: { type: "array", items: { type: "string" } },
|
|
621
|
+
},
|
|
622
|
+
required: ["ok", "error", "outDir", "gerberDir", "gerbers", "stepFile", "stepBytes", "posFile", "drillFiles", "notes"],
|
|
623
|
+
},
|
|
624
|
+
render: (_args, value) => {
|
|
625
|
+
const result = value;
|
|
626
|
+
if (!result.ok)
|
|
627
|
+
return [{ type: "text", text: "导出失败:" + result.error }];
|
|
628
|
+
const lines = [
|
|
629
|
+
"Gerber:" + String(result.gerbers.length) + " 个文件 → " + result.gerberDir,
|
|
630
|
+
"钻孔:" + String(result.drillFiles.length) + " 个文件",
|
|
631
|
+
result.stepFile === "" ? "STEP:未导出" : "STEP:" + result.stepFile + "(" + String(Math.round(result.stepBytes / 1024)) + " KB)",
|
|
632
|
+
result.posFile === "" ? "贴片坐标:未导出" : "贴片坐标:" + result.posFile,
|
|
633
|
+
];
|
|
634
|
+
for (const note of result.notes)
|
|
635
|
+
lines.push("- " + note);
|
|
636
|
+
return [{ type: "text", text: lines.join("\n") }];
|
|
637
|
+
},
|
|
638
|
+
},
|
|
639
|
+
timeoutMs: 330_000,
|
|
640
|
+
isConcurrencySafe: () => false,
|
|
641
|
+
execute: async (args, exec) => {
|
|
642
|
+
const blank = {
|
|
643
|
+
ok: false,
|
|
644
|
+
error: "",
|
|
645
|
+
outDir: "",
|
|
646
|
+
gerberDir: "",
|
|
647
|
+
gerbers: [],
|
|
648
|
+
stepFile: "",
|
|
649
|
+
stepBytes: 0,
|
|
650
|
+
posFile: "",
|
|
651
|
+
drillFiles: [],
|
|
652
|
+
notes: [],
|
|
653
|
+
};
|
|
654
|
+
const project = await projectDirOf(ctx, args.projectDir, exec);
|
|
655
|
+
if (!project.ok)
|
|
656
|
+
return { ...blank, error: project.error };
|
|
657
|
+
const located = locateEcadToolchain();
|
|
658
|
+
if (located.toolchain === null)
|
|
659
|
+
return { ...blank, error: located.error };
|
|
660
|
+
const workspace = workspaceOf(project.dir);
|
|
661
|
+
const { findLatestKicadBuild } = await import("../engine/ecad/tsci.js");
|
|
662
|
+
const latest = findLatestKicadBuild(workspace);
|
|
663
|
+
if (!latest.ok)
|
|
664
|
+
return { ...blank, error: latest.error };
|
|
665
|
+
const exported = await exportFabrication(ctx.subprocess, located.toolchain, latest.pcbFile, workspace.outDir, exec.signal);
|
|
666
|
+
return {
|
|
667
|
+
ok: exported.ok,
|
|
668
|
+
error: exported.error,
|
|
669
|
+
outDir: exported.outDir,
|
|
670
|
+
gerberDir: exported.gerberDir,
|
|
671
|
+
gerbers: exported.gerbers.map((item) => ({ name: item.name, bytes: item.bytes })),
|
|
672
|
+
stepFile: exported.stepFile,
|
|
673
|
+
stepBytes: exported.stepBytes,
|
|
674
|
+
posFile: exported.posFile,
|
|
675
|
+
drillFiles: exported.drillFiles.map((item) => ({ name: item.name, bytes: item.bytes })),
|
|
676
|
+
notes: [...exported.notes],
|
|
677
|
+
};
|
|
678
|
+
},
|
|
679
|
+
});
|
|
680
|
+
ctx.tools.register({
|
|
681
|
+
name: ECAD_PACKAGE_TOOL,
|
|
682
|
+
description: "打包硬件交付物:把 hardware/ 下的电路源码、生成的 KiCad 工程、抽出的库与 out/ 制造文件打成一个 zip 写到 hardware/out,跳过 node_modules 与 dist,写完读回自检。",
|
|
683
|
+
parameters: {
|
|
684
|
+
type: "object",
|
|
685
|
+
additionalProperties: false,
|
|
686
|
+
properties: {
|
|
687
|
+
projectDir: { type: "string", description: "工程根目录;缺省为会话工作目录。" },
|
|
688
|
+
},
|
|
689
|
+
required: [],
|
|
690
|
+
},
|
|
691
|
+
output: {
|
|
692
|
+
schema: {
|
|
693
|
+
type: "object",
|
|
694
|
+
additionalProperties: false,
|
|
695
|
+
properties: {
|
|
696
|
+
ok: { type: "boolean" },
|
|
697
|
+
error: { type: "string" },
|
|
698
|
+
file: { type: "string" },
|
|
699
|
+
bytes: { type: "number" },
|
|
700
|
+
files: { type: "number" },
|
|
701
|
+
skipped: { type: "array", items: { type: "string" } },
|
|
702
|
+
notes: { type: "array", items: { type: "string" } },
|
|
703
|
+
},
|
|
704
|
+
required: ["ok", "error", "file", "bytes", "files", "skipped", "notes"],
|
|
705
|
+
},
|
|
706
|
+
render: (_args, value) => {
|
|
707
|
+
const result = value;
|
|
708
|
+
if (!result.ok)
|
|
709
|
+
return [{ type: "text", text: "打包失败:" + result.error }];
|
|
710
|
+
const lines = [
|
|
711
|
+
"交付包:" + result.file,
|
|
712
|
+
"大小:" + String(Math.round(result.bytes / 1024)) + " KB,文件 " + String(result.files) + " 个",
|
|
713
|
+
];
|
|
714
|
+
for (const note of result.notes)
|
|
715
|
+
lines.push("- " + note);
|
|
716
|
+
return [{ type: "text", text: lines.join("\n") }];
|
|
717
|
+
},
|
|
718
|
+
},
|
|
719
|
+
timeoutMs: 120_000,
|
|
720
|
+
isConcurrencySafe: () => false,
|
|
721
|
+
execute: async (args, exec) => {
|
|
722
|
+
void exec;
|
|
723
|
+
const project = await projectDirOf(ctx, args.projectDir, exec);
|
|
724
|
+
if (!project.ok)
|
|
725
|
+
return { ok: false, error: project.error, file: "", bytes: 0, files: 0, skipped: [], notes: [] };
|
|
726
|
+
const workspace = workspaceOf(project.dir);
|
|
727
|
+
const { packageHardware, verifyPackage } = await import("../engine/ecad/package.js");
|
|
728
|
+
const projectName = basename(dirname(workspace.root));
|
|
729
|
+
const packed = packageHardware(workspace.root, projectName);
|
|
730
|
+
if (!packed.ok) {
|
|
731
|
+
return { ok: false, error: packed.error, file: "", bytes: 0, files: 0, skipped: packed.skipped, notes: [] };
|
|
732
|
+
}
|
|
733
|
+
const check = verifyPackage(packed.file);
|
|
734
|
+
return {
|
|
735
|
+
ok: check.ok,
|
|
736
|
+
error: check.ok ? "" : check.error,
|
|
737
|
+
file: packed.file,
|
|
738
|
+
bytes: packed.bytes,
|
|
739
|
+
files: packed.files,
|
|
740
|
+
skipped: packed.skipped,
|
|
741
|
+
notes: check.ok ? packed.notes : [...packed.notes, "读回自检失败,交付包可能损坏"],
|
|
742
|
+
};
|
|
743
|
+
},
|
|
744
|
+
});
|
|
745
|
+
ctx.tools.register({
|
|
746
|
+
name: ECAD_STEP_TOOL,
|
|
747
|
+
description: "导出 STEP:对生成的 KiCad 板跑 kicad-cli pcb export step,写到 hardware/out。3D 模型缺失只报告不阻断。",
|
|
748
|
+
parameters: {
|
|
749
|
+
type: "object",
|
|
750
|
+
additionalProperties: false,
|
|
751
|
+
properties: {
|
|
752
|
+
projectDir: { type: "string", description: "工程根目录;缺省为会话工作目录。" },
|
|
753
|
+
outFile: { type: "string", description: "输出文件名(相对 hardware/out);默认 board.step。" },
|
|
754
|
+
},
|
|
755
|
+
required: [],
|
|
756
|
+
},
|
|
757
|
+
output: {
|
|
758
|
+
schema: {
|
|
759
|
+
type: "object",
|
|
760
|
+
additionalProperties: false,
|
|
761
|
+
properties: {
|
|
762
|
+
ok: { type: "boolean" },
|
|
763
|
+
error: { type: "string" },
|
|
764
|
+
stepFile: { type: "string" },
|
|
765
|
+
bytes: { type: "number" },
|
|
766
|
+
durationMs: { type: "number" },
|
|
767
|
+
missingModels: { type: "array", items: { type: "string" } },
|
|
768
|
+
},
|
|
769
|
+
required: ["ok", "error", "stepFile", "bytes", "durationMs", "missingModels"],
|
|
770
|
+
},
|
|
771
|
+
render: (_args, value) => {
|
|
772
|
+
const result = value;
|
|
773
|
+
if (!result.ok)
|
|
774
|
+
return [{ type: "text", text: "STEP 导出失败:" + result.error }];
|
|
775
|
+
const lines = ["STEP:" + result.stepFile + "(" + String(Math.round(result.bytes / 1024)) + " KB)"];
|
|
776
|
+
lines.push(result.missingModels.length > 0
|
|
777
|
+
? "缺 3D 模型:" + result.missingModels.join("、")
|
|
778
|
+
: "3D 模型齐全");
|
|
779
|
+
return [{ type: "text", text: lines.join("\n") }];
|
|
780
|
+
},
|
|
781
|
+
},
|
|
782
|
+
timeoutMs: 330_000,
|
|
783
|
+
isConcurrencySafe: () => false,
|
|
784
|
+
execute: async (args, exec) => {
|
|
785
|
+
const project = await projectDirOf(ctx, args.projectDir, exec);
|
|
786
|
+
if (!project.ok) {
|
|
787
|
+
return { ok: false, error: project.error, stepFile: "", bytes: 0, durationMs: 0, missingModels: [] };
|
|
788
|
+
}
|
|
789
|
+
const located = locateEcadToolchain();
|
|
790
|
+
if (located.toolchain === null) {
|
|
791
|
+
return { ok: false, error: located.error, stepFile: "", bytes: 0, durationMs: 0, missingModels: [] };
|
|
792
|
+
}
|
|
793
|
+
const workspace = workspaceOf(project.dir);
|
|
794
|
+
const { findLatestKicadBuild } = await import("../engine/ecad/tsci.js");
|
|
795
|
+
const latest = findLatestKicadBuild(workspace);
|
|
796
|
+
if (!latest.ok) {
|
|
797
|
+
return { ok: false, error: latest.error, stepFile: "", bytes: 0, durationMs: 0, missingModels: [], models3d: null };
|
|
798
|
+
}
|
|
799
|
+
const name = (args.outFile ?? "board.step").replace(/[\\/:*?"<>|]/gu, "_");
|
|
800
|
+
const exported = await exportStep(ctx.subprocess, located.toolchain, latest.pcbFile, join(workspace.outDir, name), exec.signal, { libsDir: workspace.libsDir });
|
|
801
|
+
return {
|
|
802
|
+
ok: exported.ok,
|
|
803
|
+
error: exported.error,
|
|
804
|
+
stepFile: exported.stepFile,
|
|
805
|
+
bytes: exported.bytes,
|
|
806
|
+
durationMs: exported.durationMs,
|
|
807
|
+
missingModels: [...exported.missingModels],
|
|
808
|
+
};
|
|
809
|
+
},
|
|
810
|
+
});
|
|
811
|
+
ctx.tools.register({
|
|
812
|
+
name: ECAD_DESIGN_TOOL,
|
|
813
|
+
description: "一键设计:构建 → 合并焊盘(可选)→ 导库 → ERC/DRC → 可制造性与成本 → STEP 与制造文件,一次跑完整条流水线并逐步报告。",
|
|
814
|
+
parameters: {
|
|
815
|
+
type: "object",
|
|
816
|
+
additionalProperties: false,
|
|
817
|
+
properties: {
|
|
818
|
+
projectDir: { type: "string", description: "工程根目录;缺省为会话工作目录。" },
|
|
819
|
+
entry: { type: "string", description: "入口文件(相对 hardware/circuit);缺省取第一个 *.circuit.tsx。" },
|
|
820
|
+
mergePads: { type: "boolean", description: "把同一封装内同坐标、同网络的焊盘合并成一个多编号焊盘;一键设计默认合并,传 false 可关。" },
|
|
821
|
+
pcb: { type: "boolean", description: "构建后自动布线(需要预装 KiCadRoutingTools);默认 false,缺引擎时该步跳过而不是失败。" },
|
|
822
|
+
artifacts: { type: "boolean", description: "是否顺带导出 STEP 与 Gerber/钻孔/坐标;默认导出。" },
|
|
823
|
+
},
|
|
824
|
+
required: [],
|
|
825
|
+
},
|
|
826
|
+
output: {
|
|
827
|
+
schema: {
|
|
828
|
+
type: "object",
|
|
829
|
+
additionalProperties: false,
|
|
830
|
+
properties: {
|
|
831
|
+
ok: { type: "boolean" },
|
|
832
|
+
error: { type: "string" },
|
|
833
|
+
kicadDir: { type: "string" },
|
|
834
|
+
pcbFile: { type: "string" },
|
|
835
|
+
schematicFile: { type: "string" },
|
|
836
|
+
steps: {
|
|
837
|
+
type: "array",
|
|
838
|
+
items: {
|
|
839
|
+
type: "object",
|
|
840
|
+
additionalProperties: false,
|
|
841
|
+
properties: {
|
|
842
|
+
step: { type: "string" },
|
|
843
|
+
ok: { type: "boolean" },
|
|
844
|
+
detail: { type: "string" },
|
|
845
|
+
ms: { type: "number" },
|
|
846
|
+
},
|
|
847
|
+
required: ["step", "ok", "detail", "ms"],
|
|
848
|
+
},
|
|
849
|
+
},
|
|
850
|
+
notes: { type: "array", items: { type: "string" } },
|
|
851
|
+
},
|
|
852
|
+
required: ["ok", "error", "kicadDir", "pcbFile", "schematicFile", "steps", "notes"],
|
|
853
|
+
},
|
|
854
|
+
render: (_args, value) => {
|
|
855
|
+
const result = value;
|
|
856
|
+
const lines = ["一键设计:" + (result.ok ? "全部通过" : "有步骤失败")];
|
|
857
|
+
for (const step of result.steps) {
|
|
858
|
+
lines.push((step.ok ? "OK " : "FAIL ") + step.step + " · " + step.detail + "(" + String(step.ms) + " ms)");
|
|
859
|
+
}
|
|
860
|
+
if (result.notes.length > 0) {
|
|
861
|
+
lines.push("");
|
|
862
|
+
for (const note of result.notes)
|
|
863
|
+
lines.push("- " + note);
|
|
864
|
+
}
|
|
865
|
+
return [{ type: "text", text: lines.join("\n") }];
|
|
866
|
+
},
|
|
867
|
+
},
|
|
868
|
+
timeoutMs: 600_000,
|
|
869
|
+
isConcurrencySafe: () => false,
|
|
870
|
+
execute: async (args, exec) => {
|
|
871
|
+
const empty = { ok: false, error: "", kicadDir: "", pcbFile: "", schematicFile: "", steps: [], notes: [] };
|
|
872
|
+
const project = await projectDirOf(ctx, args.projectDir, exec);
|
|
873
|
+
if (!project.ok)
|
|
874
|
+
return { ...empty, error: project.error };
|
|
875
|
+
const located = locateEcadToolchain();
|
|
876
|
+
if (located.toolchain === null)
|
|
877
|
+
return { ...empty, error: located.error };
|
|
878
|
+
const workspace = workspaceOf(project.dir);
|
|
879
|
+
const runtime = await ensureProjectRuntime({ subprocess: ctx.subprocess, circuitDir: workspace.circuitDir, signal: exec.signal });
|
|
880
|
+
if (!runtime.ok)
|
|
881
|
+
return { ...empty, error: runtime.error };
|
|
882
|
+
const result = await runEcadDesign({
|
|
883
|
+
subprocess: ctx.subprocess,
|
|
884
|
+
toolchain: located.toolchain,
|
|
885
|
+
workspace,
|
|
886
|
+
...(args.entry === undefined ? {} : { entry: args.entry }),
|
|
887
|
+
mergePads: args.mergePads !== false,
|
|
888
|
+
pcb: args.pcb === true,
|
|
889
|
+
...(args.artifacts === undefined ? {} : { artifacts: args.artifacts }),
|
|
890
|
+
signal: exec.signal,
|
|
891
|
+
});
|
|
892
|
+
return {
|
|
893
|
+
ok: result.ok,
|
|
894
|
+
error: result.error,
|
|
895
|
+
kicadDir: result.kicadDir,
|
|
896
|
+
pcbFile: result.pcbFile,
|
|
897
|
+
schematicFile: result.schematicFile,
|
|
898
|
+
steps: [...result.steps],
|
|
899
|
+
notes: [...result.notes],
|
|
900
|
+
};
|
|
901
|
+
},
|
|
902
|
+
});
|
|
903
|
+
}
|