Expand Schemeta frontend editor with pin/net/symbol editing and schema updates
This commit is contained in:
1 parent
61814349ed
commit
f2d48cee85
15 files changed
+7078
-244
No files matched your search
+67
-5
@@ -87,29 +87,91 @@ function buildSignalPaths(model) {
|
||||
return [...dedup.values()].sort((a, b) => a.join("/").localeCompare(b.join("/")));
|
||||
}
|
||||
|
||||
function detectBusGroups(model) {
|
||||
const groups = new Map();
|
||||
for (const net of model.nets) {
|
||||
const match = /^([A-Za-z0-9]+)_/.exec(net.name);
|
||||
if (!match) {
|
||||
continue;
|
||||
}
|
||||
const key = match[1].toUpperCase();
|
||||
const list = groups.get(key) ?? [];
|
||||
list.push(net.name);
|
||||
groups.set(key, list);
|
||||
}
|
||||
|
||||
const out = [];
|
||||
for (const [name, nets] of groups.entries()) {
|
||||
if (nets.length >= 2) {
|
||||
out.push({ name, nets: nets.sort() });
|
||||
}
|
||||
}
|
||||
|
||||
out.sort((a, b) => a.name.localeCompare(b.name));
|
||||
return out;
|
||||
}
|
||||
|
||||
function extractTopology(model) {
|
||||
const powerDomains = model.nets
|
||||
.filter((n) => n.class === "power" || n.class === "ground")
|
||||
.map((n) => n.name)
|
||||
.sort();
|
||||
const powerNets = model.nets.filter((n) => n.class === "power" || n.class === "ground");
|
||||
const powerDomains = powerNets.map((n) => n.name).sort();
|
||||
|
||||
const powerDomainConsumers = powerNets
|
||||
.map((net) => {
|
||||
const consumers = net.nodes
|
||||
.filter((n) => {
|
||||
const t = pinTypeFor(model, n.ref, n.pin);
|
||||
return t === "power_in" || t === "ground";
|
||||
})
|
||||
.map((n) => `${n.ref}.${n.pin}`)
|
||||
.sort();
|
||||
|
||||
return {
|
||||
name: net.name,
|
||||
consumers,
|
||||
count: consumers.length
|
||||
};
|
||||
})
|
||||
.sort((a, b) => a.name.localeCompare(b.name));
|
||||
|
||||
const clockSources = new Set();
|
||||
const clockSinks = new Set();
|
||||
|
||||
for (const net of model.nets) {
|
||||
if (net.class !== "clock") {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const node of net.nodes) {
|
||||
const type = pinTypeFor(model, node.ref, node.pin);
|
||||
if (type === "output" || type === "power_out") {
|
||||
clockSources.add(node.ref);
|
||||
} else if (type === "input" || type === "analog") {
|
||||
clockSinks.add(node.ref);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const buses = detectBusGroups(model);
|
||||
const signalPaths = buildSignalPaths(model);
|
||||
const namedSignalPaths = model.nets
|
||||
.filter((n) => n.class !== "power" && n.class !== "ground")
|
||||
.map((n) => {
|
||||
const nodes = n.nodes.map((x) => `${x.ref}.${x.pin}`);
|
||||
return {
|
||||
net: n.name,
|
||||
class: n.class,
|
||||
nodes
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
power_domains: powerDomains,
|
||||
power_domain_consumers: powerDomainConsumers,
|
||||
clock_sources: [...clockSources].sort(),
|
||||
signal_paths: buildSignalPaths(model)
|
||||
clock_sinks: [...clockSinks].sort(),
|
||||
buses,
|
||||
signal_paths: signalPaths,
|
||||
named_signal_paths: namedSignalPaths
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+280
-11
@@ -1,5 +1,6 @@
|
||||
import { analyzeModel } from "./analyze.js";
|
||||
import { renderSvg } from "./render.js";
|
||||
import { layoutAndRoute } from "./layout.js";
|
||||
import { renderSvgFromLayout } from "./render.js";
|
||||
import { validateModel } from "./validate.js";
|
||||
|
||||
function emptyTopology() {
|
||||
@@ -10,36 +11,299 @@ function emptyTopology() {
|
||||
};
|
||||
}
|
||||
|
||||
export function compile(payload) {
|
||||
const validated = validateModel(payload);
|
||||
function emptyLayout() {
|
||||
return {
|
||||
width: 0,
|
||||
height: 0,
|
||||
placed: []
|
||||
};
|
||||
}
|
||||
|
||||
function issueSuggestion(code) {
|
||||
const map = {
|
||||
ground_net_missing: "Add a GND net and connect all ground/reference pins to it.",
|
||||
multi_power_out: "Split this net or insert power management so only one source drives the net.",
|
||||
output_conflict: "Avoid direct output-to-output connection. Insert a buffer/selector or separate nets.",
|
||||
required_power_unconnected: "Connect this pin to the appropriate power or ground domain.",
|
||||
floating_input: "Drive this input from a defined source or add pull-up/pull-down network.",
|
||||
unknown_ref_in_net: "Fix net node reference to an existing component instance.",
|
||||
unknown_pin_in_net: "Fix net node pin name to match the selected symbol pin list.",
|
||||
auto_template_symbol_created:
|
||||
"A common component template was auto-selected (resistor/capacitor/etc). Replace with a custom symbol if needed.",
|
||||
auto_template_symbol_hydrated:
|
||||
"Template symbol fields were auto-filled. You can keep the short form or expand the symbol explicitly.",
|
||||
auto_generic_symbol_created:
|
||||
"A generic symbol was auto-created from net usage. You can replace it with a library symbol later.",
|
||||
auto_generic_symbol_hydrated:
|
||||
"Generic symbol fields were auto-filled from connectivity so minimal JSON remains valid.",
|
||||
auto_generic_pin_created:
|
||||
"A missing generic pin was inferred from net usage. Rename/reposition pins in symbols for cleaner diagrams.",
|
||||
auto_symbol_id_filled:
|
||||
"symbol_id was inferred from the symbol map key to keep the schema concise.",
|
||||
auto_symbol_category_filled:
|
||||
"category was inferred automatically. Set it explicitly if you need strict semantic grouping.",
|
||||
invalid_part_type:
|
||||
"Use a supported built-in part type or provide a full custom symbol definition.",
|
||||
instance_symbol_or_part_missing:
|
||||
"Each instance must define either 'symbol' (custom symbol) or 'part' (built-in shorthand)."
|
||||
};
|
||||
|
||||
return map[code] ?? "Review this issue and adjust net connectivity or symbol definitions.";
|
||||
}
|
||||
|
||||
function parseNetFromIssue(issue) {
|
||||
if (typeof issue.path === "string") {
|
||||
const m = /^nets\.([^\.]+)/.exec(issue.path);
|
||||
if (m) {
|
||||
return m[1];
|
||||
}
|
||||
}
|
||||
|
||||
const m2 = /Net '([^']+)'/.exec(issue.message);
|
||||
if (m2) {
|
||||
return m2[1];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseRefFromIssue(issue) {
|
||||
if (typeof issue.path === "string") {
|
||||
const m = /^instances\.([^\.]+)/.exec(issue.path);
|
||||
if (m) {
|
||||
return m[1];
|
||||
}
|
||||
}
|
||||
|
||||
const m2 = /'([A-Za-z][A-Za-z0-9_]*)\./.exec(issue.message);
|
||||
if (m2) {
|
||||
return m2[1];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseRefPinFromIssue(issue) {
|
||||
const m = /'([A-Za-z][A-Za-z0-9_]*)\.([A-Za-z0-9_]+)'/.exec(issue.message);
|
||||
if (m) {
|
||||
return { ref: m[1], pin: m[2] };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function pinPoint(inst, pin, width, height) {
|
||||
const x0 = inst.placement.x;
|
||||
const y0 = inst.placement.y;
|
||||
|
||||
switch (pin.side) {
|
||||
case "left":
|
||||
return { x: x0, y: y0 + pin.offset };
|
||||
case "right":
|
||||
return { x: x0 + width, y: y0 + pin.offset };
|
||||
case "top":
|
||||
return { x: x0 + pin.offset, y: y0 };
|
||||
case "bottom":
|
||||
return { x: x0 + pin.offset, y: y0 + height };
|
||||
default:
|
||||
return { x: x0, y: y0 };
|
||||
}
|
||||
}
|
||||
|
||||
function bboxFromPoints(points, padding = 18) {
|
||||
if (!points.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const xs = points.map((p) => p.x);
|
||||
const ys = points.map((p) => p.y);
|
||||
const minX = Math.min(...xs) - padding;
|
||||
const maxX = Math.max(...xs) + padding;
|
||||
const minY = Math.min(...ys) - padding;
|
||||
const maxY = Math.max(...ys) + padding;
|
||||
|
||||
return {
|
||||
x: minX,
|
||||
y: minY,
|
||||
w: maxX - minX,
|
||||
h: maxY - minY
|
||||
};
|
||||
}
|
||||
|
||||
function netFocusBbox(layout, netName) {
|
||||
const routed = layout.routed.find((r) => r.net.name === netName);
|
||||
if (!routed) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const points = [];
|
||||
for (const route of routed.routes) {
|
||||
for (const seg of route) {
|
||||
points.push(seg.a, seg.b);
|
||||
}
|
||||
}
|
||||
points.push(...(routed.tiePoints ?? []));
|
||||
points.push(...(routed.junctionPoints ?? []));
|
||||
points.push(...(routed.labelPoints ?? []));
|
||||
|
||||
return bboxFromPoints(points);
|
||||
}
|
||||
|
||||
function componentFocus(layout, model, ref) {
|
||||
const inst = layout.placed.find((x) => x.ref === ref);
|
||||
if (!inst) {
|
||||
return null;
|
||||
}
|
||||
const sym = model.symbols[inst.symbol];
|
||||
if (!sym) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
type: "component",
|
||||
ref,
|
||||
bbox: {
|
||||
x: inst.placement.x - 12,
|
||||
y: inst.placement.y - 12,
|
||||
w: sym.body.width + 24,
|
||||
h: sym.body.height + 24
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function pinFocus(layout, model, ref, pinName) {
|
||||
const inst = layout.placed.find((x) => x.ref === ref);
|
||||
if (!inst) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const sym = model.symbols[inst.symbol];
|
||||
const pin = sym?.pins.find((p) => p.name === pinName);
|
||||
if (!pin) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const p = pinPoint(inst, pin, sym.body.width, sym.body.height);
|
||||
return {
|
||||
type: "pin",
|
||||
ref,
|
||||
pin: pinName,
|
||||
bbox: { x: p.x - 28, y: p.y - 28, w: 56, h: 56 }
|
||||
};
|
||||
}
|
||||
|
||||
function buildFocusMap(model, layout, issues) {
|
||||
const map = {};
|
||||
|
||||
for (const issue of issues) {
|
||||
const byNet = parseNetFromIssue(issue);
|
||||
if (byNet) {
|
||||
map[issue.id] = {
|
||||
type: "net",
|
||||
net: byNet,
|
||||
bbox: netFocusBbox(layout, byNet)
|
||||
};
|
||||
continue;
|
||||
}
|
||||
|
||||
const rp = parseRefPinFromIssue(issue);
|
||||
if (rp) {
|
||||
const pf = pinFocus(layout, model, rp.ref, rp.pin);
|
||||
if (pf) {
|
||||
map[issue.id] = pf;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
const byRef = parseRefFromIssue(issue);
|
||||
if (byRef) {
|
||||
const cf = componentFocus(layout, model, byRef);
|
||||
if (cf) {
|
||||
map[issue.id] = cf;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
map[issue.id] = { type: "global", bbox: { x: 0, y: 0, w: layout.width, h: layout.height } };
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
function annotateIssues(issues, prefix) {
|
||||
return issues.map((issue, idx) => ({
|
||||
...issue,
|
||||
id: `${prefix}${idx}`,
|
||||
suggestion: issueSuggestion(issue.code)
|
||||
}));
|
||||
}
|
||||
|
||||
export function compile(payload, options = {}) {
|
||||
const validated = validateModel(payload, options);
|
||||
|
||||
if (!validated.model) {
|
||||
const errors = validated.issues.filter((x) => x.severity === "error");
|
||||
const warnings = validated.issues.filter((x) => x.severity === "warning");
|
||||
const errors = annotateIssues(validated.issues.filter((x) => x.severity === "error"), "E");
|
||||
const warnings = annotateIssues(validated.issues.filter((x) => x.severity === "warning"), "W");
|
||||
return {
|
||||
ok: false,
|
||||
errors,
|
||||
warnings,
|
||||
topology: emptyTopology(),
|
||||
layout: emptyLayout(),
|
||||
layout_metrics: {
|
||||
segment_count: 0,
|
||||
overlap_edges: 0,
|
||||
crossings: 0,
|
||||
label_collisions: 0,
|
||||
tie_points_used: 0,
|
||||
bus_groups: 0
|
||||
},
|
||||
bus_groups: [],
|
||||
focus_map: {},
|
||||
render_mode_used: options.render_mode ?? "schematic_stub",
|
||||
svg: ""
|
||||
};
|
||||
}
|
||||
|
||||
const analysis = analyzeModel(validated.model, validated.issues);
|
||||
const svg = renderSvg(validated.model);
|
||||
const layout = layoutAndRoute(validated.model, options);
|
||||
const svg = renderSvgFromLayout(validated.model, layout, options);
|
||||
|
||||
const errors = annotateIssues(analysis.errors, "E");
|
||||
const warnings = annotateIssues(analysis.warnings, "W");
|
||||
const issues = [...errors, ...warnings];
|
||||
const focusMap = buildFocusMap(validated.model, layout, issues);
|
||||
|
||||
const placed = layout.placed.map((inst) => ({
|
||||
ref: inst.ref,
|
||||
x: inst.placement.x,
|
||||
y: inst.placement.y,
|
||||
rotation: inst.placement.rotation,
|
||||
locked: inst.placement.locked
|
||||
}));
|
||||
|
||||
return {
|
||||
...analysis,
|
||||
errors,
|
||||
warnings,
|
||||
focus_map: focusMap,
|
||||
layout: {
|
||||
width: layout.width,
|
||||
height: layout.height,
|
||||
placed
|
||||
},
|
||||
layout_metrics: layout.metrics,
|
||||
bus_groups: layout.bus_groups,
|
||||
render_mode_used: layout.render_mode_used,
|
||||
svg
|
||||
};
|
||||
}
|
||||
|
||||
export function analyze(payload) {
|
||||
const validated = validateModel(payload);
|
||||
export function analyze(payload, options = {}) {
|
||||
const validated = validateModel(payload, options);
|
||||
|
||||
if (!validated.model) {
|
||||
const errors = validated.issues.filter((x) => x.severity === "error");
|
||||
const warnings = validated.issues.filter((x) => x.severity === "warning");
|
||||
const errors = annotateIssues(validated.issues.filter((x) => x.severity === "error"), "E");
|
||||
const warnings = annotateIssues(validated.issues.filter((x) => x.severity === "warning"), "W");
|
||||
return {
|
||||
ok: false,
|
||||
errors,
|
||||
@@ -48,5 +312,10 @@ export function analyze(payload) {
|
||||
};
|
||||
}
|
||||
|
||||
return analyzeModel(validated.model, validated.issues);
|
||||
const out = analyzeModel(validated.model, validated.issues);
|
||||
return {
|
||||
...out,
|
||||
errors: annotateIssues(out.errors, "E"),
|
||||
warnings: annotateIssues(out.warnings, "W")
|
||||
};
|
||||
}
|
||||
+1359
-136
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,226 @@
|
||||
import { compile, analyze } from "./compile.js";
|
||||
|
||||
const SERVER_INFO = {
|
||||
name: "schemeta-mcp",
|
||||
version: "0.2.0"
|
||||
};
|
||||
|
||||
let stdinBuffer = Buffer.alloc(0);
|
||||
|
||||
function writeMessage(message) {
|
||||
const json = JSON.stringify(message);
|
||||
const body = Buffer.from(json, "utf8");
|
||||
const header = Buffer.from(`Content-Length: ${body.length}\r\n\r\n`, "utf8");
|
||||
process.stdout.write(Buffer.concat([header, body]));
|
||||
}
|
||||
|
||||
function sendResult(id, result) {
|
||||
writeMessage({ jsonrpc: "2.0", id, result });
|
||||
}
|
||||
|
||||
function sendError(id, code, message, data = undefined) {
|
||||
writeMessage({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
error: { code, message, data }
|
||||
});
|
||||
}
|
||||
|
||||
function decodePayload(value) {
|
||||
if (typeof value === "string") {
|
||||
return JSON.parse(value);
|
||||
}
|
||||
if (value && typeof value === "object") {
|
||||
return value;
|
||||
}
|
||||
throw new Error("payload must be a JSON object or JSON string");
|
||||
}
|
||||
|
||||
function toolListResult() {
|
||||
return {
|
||||
tools: [
|
||||
{
|
||||
name: "schemeta_compile",
|
||||
description: "Compile Schemeta JSON into SVG with ERC, metrics, and topology results.",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
payload: {
|
||||
description: "Schemeta model object or JSON string",
|
||||
anyOf: [{ type: "object" }, { type: "string" }]
|
||||
},
|
||||
options: {
|
||||
description: "Compile options (render_mode, show_labels, generic_symbols)",
|
||||
type: "object"
|
||||
}
|
||||
},
|
||||
required: ["payload"]
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "schemeta_analyze",
|
||||
description: "Analyze Schemeta JSON for ERC and topology without rendering SVG.",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
payload: {
|
||||
description: "Schemeta model object or JSON string",
|
||||
anyOf: [{ type: "object" }, { type: "string" }]
|
||||
},
|
||||
options: {
|
||||
description: "Analyze options (generic_symbols)",
|
||||
type: "object"
|
||||
}
|
||||
},
|
||||
required: ["payload"]
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "schemeta_ui_bundle",
|
||||
description: "Return Schemeta UI bundle metadata for iframe embedding.",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {}
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
function uiBundleDescriptor() {
|
||||
return {
|
||||
name: "schemeta-workspace",
|
||||
version: "0.2.0",
|
||||
entry: "/",
|
||||
title: "Schemeta Workspace",
|
||||
transport: "iframe"
|
||||
};
|
||||
}
|
||||
|
||||
function handleToolCall(name, args) {
|
||||
if (!args || typeof args !== "object") {
|
||||
args = {};
|
||||
}
|
||||
|
||||
if (name === "schemeta_compile") {
|
||||
const payload = decodePayload(args.payload);
|
||||
const result = compile(payload, args.options ?? {});
|
||||
return {
|
||||
content: [{ type: "text", text: JSON.stringify(result) }],
|
||||
structuredContent: result,
|
||||
isError: false
|
||||
};
|
||||
}
|
||||
|
||||
if (name === "schemeta_analyze") {
|
||||
const payload = decodePayload(args.payload);
|
||||
const result = analyze(payload, args.options ?? {});
|
||||
return {
|
||||
content: [{ type: "text", text: JSON.stringify(result) }],
|
||||
structuredContent: result,
|
||||
isError: false
|
||||
};
|
||||
}
|
||||
|
||||
if (name === "schemeta_ui_bundle") {
|
||||
const result = uiBundleDescriptor();
|
||||
return {
|
||||
content: [{ type: "text", text: JSON.stringify(result) }],
|
||||
structuredContent: result,
|
||||
isError: false
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error(`Unknown tool '${name}'`);
|
||||
}
|
||||
|
||||
function handleRequest(message) {
|
||||
const { id, method, params } = message;
|
||||
|
||||
if (method === "initialize") {
|
||||
return sendResult(id, {
|
||||
protocolVersion: "2024-11-05",
|
||||
capabilities: { tools: {} },
|
||||
serverInfo: SERVER_INFO
|
||||
});
|
||||
}
|
||||
|
||||
if (method === "notifications/initialized") {
|
||||
return;
|
||||
}
|
||||
|
||||
if (method === "ping") {
|
||||
return sendResult(id, {});
|
||||
}
|
||||
|
||||
if (method === "tools/list") {
|
||||
return sendResult(id, toolListResult());
|
||||
}
|
||||
|
||||
if (method === "tools/call") {
|
||||
try {
|
||||
const name = params?.name;
|
||||
const args = params?.arguments ?? {};
|
||||
const result = handleToolCall(name, args);
|
||||
return sendResult(id, result);
|
||||
} catch (err) {
|
||||
return sendError(id, -32602, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
if (id !== undefined) {
|
||||
return sendError(id, -32601, `Method not found: ${method}`);
|
||||
}
|
||||
}
|
||||
|
||||
function tryParseBuffer() {
|
||||
while (true) {
|
||||
const headerEnd = stdinBuffer.indexOf("\r\n\r\n");
|
||||
if (headerEnd === -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
const headerText = stdinBuffer.slice(0, headerEnd).toString("utf8");
|
||||
const headers = headerText.split("\r\n");
|
||||
const lengthLine = headers.find((h) => h.toLowerCase().startsWith("content-length:"));
|
||||
if (!lengthLine) {
|
||||
stdinBuffer = Buffer.alloc(0);
|
||||
return;
|
||||
}
|
||||
|
||||
const contentLength = Number(lengthLine.split(":")[1].trim());
|
||||
const totalLength = headerEnd + 4 + contentLength;
|
||||
if (stdinBuffer.length < totalLength) {
|
||||
return;
|
||||
}
|
||||
|
||||
const body = stdinBuffer.slice(headerEnd + 4, totalLength).toString("utf8");
|
||||
stdinBuffer = stdinBuffer.slice(totalLength);
|
||||
|
||||
let message;
|
||||
try {
|
||||
message = JSON.parse(body);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
handleRequest(message);
|
||||
} catch (err) {
|
||||
if (message?.id !== undefined) {
|
||||
sendError(message.id, -32603, "Internal error", err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
process.stdin.on("data", (chunk) => {
|
||||
stdinBuffer = Buffer.concat([stdinBuffer, Buffer.from(chunk)]);
|
||||
tryParseBuffer();
|
||||
});
|
||||
|
||||
process.stdin.on("error", (err) => {
|
||||
process.stderr.write(`stdin error: ${String(err)}\n`);
|
||||
});
|
||||
|
||||
process.stdin.resume();
|
||||
+527
-37
@@ -1,5 +1,17 @@
|
||||
import { layoutAndRoute, netAnchorPoint } from "./layout.js";
|
||||
|
||||
const GRID = 20;
|
||||
|
||||
const NET_COLORS = {
|
||||
power: "#b54708",
|
||||
ground: "#344054",
|
||||
signal: "#1d4ed8",
|
||||
analog: "#0f766e",
|
||||
differential: "#c11574",
|
||||
clock: "#b93815",
|
||||
bus: "#155eef"
|
||||
};
|
||||
|
||||
function esc(text) {
|
||||
return String(text)
|
||||
.replaceAll("&", "&")
|
||||
@@ -8,47 +20,425 @@ function esc(text) {
|
||||
.replaceAll('"', """);
|
||||
}
|
||||
|
||||
export function renderSvg(model) {
|
||||
const layout = layoutAndRoute(model);
|
||||
function normalizeRotation(value) {
|
||||
const n = Number(value ?? 0);
|
||||
if (!Number.isFinite(n)) {
|
||||
return 0;
|
||||
}
|
||||
const snapped = Math.round(n / 90) * 90;
|
||||
let rot = snapped % 360;
|
||||
if (rot < 0) {
|
||||
rot += 360;
|
||||
}
|
||||
return rot;
|
||||
}
|
||||
|
||||
function rotatePoint(point, center, rotation) {
|
||||
if (!rotation) {
|
||||
return point;
|
||||
}
|
||||
const rad = (rotation * Math.PI) / 180;
|
||||
const cos = Math.round(Math.cos(rad));
|
||||
const sin = Math.round(Math.sin(rad));
|
||||
const dx = point.x - center.x;
|
||||
const dy = point.y - center.y;
|
||||
return {
|
||||
x: Math.round(center.x + dx * cos - dy * sin),
|
||||
y: Math.round(center.y + dx * sin + dy * cos)
|
||||
};
|
||||
}
|
||||
|
||||
function rotateSide(side, rotation) {
|
||||
const steps = normalizeRotation(rotation) / 90;
|
||||
const order = ["top", "right", "bottom", "left"];
|
||||
const idx = order.indexOf(side);
|
||||
if (idx < 0) {
|
||||
return side;
|
||||
}
|
||||
return order[(idx + steps) % 4];
|
||||
}
|
||||
|
||||
function truncate(text, max) {
|
||||
const s = String(text ?? "");
|
||||
if (s.length <= max) {
|
||||
return s;
|
||||
}
|
||||
return `${s.slice(0, Math.max(1, max - 1))}...`;
|
||||
}
|
||||
|
||||
function netColor(netClass) {
|
||||
return NET_COLORS[netClass] ?? NET_COLORS.signal;
|
||||
}
|
||||
|
||||
function symbolTemplateKind(sym) {
|
||||
const t = String(sym?.template_name ?? "").toLowerCase();
|
||||
if (["resistor", "capacitor", "inductor", "diode", "led", "connector"].includes(t)) {
|
||||
return t;
|
||||
}
|
||||
const c = String(sym?.category ?? "").toLowerCase();
|
||||
if (c.includes("resistor")) return "resistor";
|
||||
if (c.includes("capacitor")) return "capacitor";
|
||||
if (c.includes("inductor")) return "inductor";
|
||||
if (c.includes("diode")) return "diode";
|
||||
if (c.includes("led")) return "led";
|
||||
if (c.includes("connector")) return "connector";
|
||||
return null;
|
||||
}
|
||||
|
||||
function renderSymbolBody(sym, x, y, width, height) {
|
||||
const kind = symbolTemplateKind(sym);
|
||||
if (!kind) {
|
||||
return `<rect x="${x}" y="${y}" width="${width}" height="${height}" rx="8" fill="#ffffff" stroke="#1f2937" stroke-width="2" />`;
|
||||
}
|
||||
|
||||
const midX = x + width / 2;
|
||||
const midY = y + height / 2;
|
||||
const left = x + 16;
|
||||
const right = x + width - 16;
|
||||
const top = y + 14;
|
||||
const bottom = y + height - 14;
|
||||
const body = [];
|
||||
body.push(`<rect x="${x}" y="${y}" width="${width}" height="${height}" rx="8" fill="#fffdfa" stroke="#1f2937" stroke-width="2" />`);
|
||||
|
||||
if (kind === "resistor") {
|
||||
const y0 = midY;
|
||||
const pts = [
|
||||
[left, y0],
|
||||
[left + 16, y0 - 10],
|
||||
[left + 28, y0 + 10],
|
||||
[left + 40, y0 - 10],
|
||||
[left + 52, y0 + 10],
|
||||
[left + 64, y0 - 10],
|
||||
[right, y0]
|
||||
];
|
||||
body.push(`<polyline points="${pts.map((p) => `${p[0]},${p[1]}`).join(" ")}" fill="none" stroke="#344054" stroke-width="2" />`);
|
||||
} else if (kind === "capacitor") {
|
||||
body.push(`<line x1="${midX - 10}" y1="${top}" x2="${midX - 10}" y2="${bottom}" stroke="#344054" stroke-width="2" />`);
|
||||
body.push(`<line x1="${midX + 10}" y1="${top}" x2="${midX + 10}" y2="${bottom}" stroke="#344054" stroke-width="2" />`);
|
||||
body.push(`<line x1="${left}" y1="${midY}" x2="${midX - 10}" y2="${midY}" stroke="#344054" stroke-width="2" />`);
|
||||
body.push(`<line x1="${midX + 10}" y1="${midY}" x2="${right}" y2="${midY}" stroke="#344054" stroke-width="2" />`);
|
||||
} else if (kind === "inductor") {
|
||||
body.push(`<line x1="${left}" y1="${midY}" x2="${left + 10}" y2="${midY}" stroke="#344054" stroke-width="2" />`);
|
||||
for (let i = 0; i < 4; i += 1) {
|
||||
const cx = left + 18 + i * 16;
|
||||
body.push(`<path d="M ${cx - 8} ${midY} A 8 8 0 0 1 ${cx + 8} ${midY}" fill="none" stroke="#344054" stroke-width="2" />`);
|
||||
}
|
||||
body.push(`<line x1="${right - 10}" y1="${midY}" x2="${right}" y2="${midY}" stroke="#344054" stroke-width="2" />`);
|
||||
} else if (kind === "diode" || kind === "led") {
|
||||
const triLeft = left + 12;
|
||||
const triRight = midX + 6;
|
||||
body.push(`<line x1="${left}" y1="${midY}" x2="${triLeft}" y2="${midY}" stroke="#344054" stroke-width="2" />`);
|
||||
body.push(`<path d="M ${triLeft} ${midY - 14} L ${triLeft} ${midY + 14} L ${triRight} ${midY} Z" fill="none" stroke="#344054" stroke-width="2" />`);
|
||||
body.push(`<line x1="${triRight + 4}" y1="${midY - 16}" x2="${triRight + 4}" y2="${midY + 16}" stroke="#344054" stroke-width="2" />`);
|
||||
body.push(`<line x1="${triRight + 4}" y1="${midY}" x2="${right}" y2="${midY}" stroke="#344054" stroke-width="2" />`);
|
||||
if (kind === "led") {
|
||||
body.push(`<line x1="${triRight - 2}" y1="${midY - 20}" x2="${triRight + 8}" y2="${midY - 30}" stroke="#b42318" stroke-width="1.6" />`);
|
||||
body.push(`<line x1="${triRight + 10}" y1="${midY - 18}" x2="${triRight + 20}" y2="${midY - 28}" stroke="#b42318" stroke-width="1.6" />`);
|
||||
}
|
||||
} else if (kind === "connector") {
|
||||
body.push(`<line x1="${midX}" y1="${top + 6}" x2="${midX}" y2="${bottom - 6}" stroke="#98a2b3" stroke-width="1.6" />`);
|
||||
body.push(`<circle cx="${midX}" cy="${midY - 16}" r="5" fill="#fff" stroke="#344054" stroke-width="1.6" />`);
|
||||
body.push(`<circle cx="${midX}" cy="${midY + 16}" r="5" fill="#fff" stroke="#344054" stroke-width="1.6" />`);
|
||||
}
|
||||
|
||||
return body.join("");
|
||||
}
|
||||
|
||||
function pinNetMap(model) {
|
||||
const map = new Map();
|
||||
for (const net of model.nets) {
|
||||
for (const node of net.nodes) {
|
||||
const key = `${node.ref}.${node.pin}`;
|
||||
const list = map.get(key) ?? [];
|
||||
list.push(net.name);
|
||||
map.set(key, list);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
function renderWirePath(pathD, netName, netClass) {
|
||||
const color = netColor(netClass);
|
||||
return [
|
||||
`<path d="${pathD}" fill="none" stroke="#f8fafc" stroke-width="5" stroke-linecap="round" stroke-linejoin="round" data-net="${esc(netName)}" data-net-class="${esc(netClass)}" />`,
|
||||
`<path d="${pathD}" fill="none" stroke="${color}" stroke-width="2.3" stroke-linecap="round" stroke-linejoin="round" data-net="${esc(netName)}" data-net-class="${esc(netClass)}" />`
|
||||
].join("");
|
||||
}
|
||||
|
||||
function renderNetLabel(x, y, netName, netClass, bold = false) {
|
||||
const color = netColor(netClass);
|
||||
const weight = bold ? "700" : "600";
|
||||
return `<text x="${x}" y="${y}" font-size="10" font-weight="${weight}" fill="${color}" stroke="#f8fafc" stroke-width="3" paint-order="stroke fill" data-net-label="${esc(netName)}">${esc(netName)}</text>`;
|
||||
}
|
||||
|
||||
function isGroundLikeNet(net) {
|
||||
const cls = String(net?.class ?? "").trim().toLowerCase();
|
||||
if (cls === "ground") {
|
||||
return true;
|
||||
}
|
||||
const name = String(net?.name ?? "").trim().toLowerCase();
|
||||
return name === "gnd" || name === "ground" || name.endsWith("_gnd");
|
||||
}
|
||||
|
||||
function tieLabelPoint(point, netClass) {
|
||||
if (netClass === "power") {
|
||||
return { x: point.x + 8, y: point.y - 10 };
|
||||
}
|
||||
return { x: point.x + 8, y: point.y - 8 };
|
||||
}
|
||||
|
||||
function distance(a, b) {
|
||||
return Math.hypot(a.x - b.x, a.y - b.y);
|
||||
}
|
||||
|
||||
function pickLabelPoints(points, maxCount, used, minSpacing, avoidPoints = []) {
|
||||
const accepted = [];
|
||||
|
||||
for (const p of points) {
|
||||
if (accepted.length >= maxCount) {
|
||||
break;
|
||||
}
|
||||
|
||||
let blocked = false;
|
||||
for (const prev of used) {
|
||||
if (distance(p, prev) < minSpacing) {
|
||||
blocked = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (blocked) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const pin of avoidPoints) {
|
||||
if (distance(p, pin) < minSpacing * 0.9) {
|
||||
blocked = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (blocked) {
|
||||
continue;
|
||||
}
|
||||
|
||||
accepted.push(p);
|
||||
used.push(p);
|
||||
}
|
||||
|
||||
return accepted;
|
||||
}
|
||||
|
||||
function renderGroundSymbol(x, y, netName) {
|
||||
const y0 = y + 3;
|
||||
const y1 = y + 7;
|
||||
const y2 = y + 10;
|
||||
const y3 = y + 13;
|
||||
return `
|
||||
<g data-net-tie="${esc(netName)}">
|
||||
<line x1="${x}" y1="${y}" x2="${x}" y2="${y0}" stroke="#344054" stroke-width="1.4" />
|
||||
<line x1="${x - 6}" y1="${y1}" x2="${x + 6}" y2="${y1}" stroke="#344054" stroke-width="1.4" />
|
||||
<line x1="${x - 4}" y1="${y2}" x2="${x + 4}" y2="${y2}" stroke="#344054" stroke-width="1.4" />
|
||||
<line x1="${x - 2}" y1="${y3}" x2="${x + 2}" y2="${y3}" stroke="#344054" stroke-width="1.4" />
|
||||
</g>`;
|
||||
}
|
||||
|
||||
function renderPowerSymbol(x, y, netName) {
|
||||
return `
|
||||
<g data-net-tie="${esc(netName)}">
|
||||
<line x1="${x}" y1="${y}" x2="${x}" y2="${y - 4}" stroke="#b54708" stroke-width="1.6" />
|
||||
<path d="M ${x - 5} ${y + 2} L ${x} ${y - 8} L ${x + 5} ${y + 2} Z" fill="#b54708" stroke="#f8fafc" stroke-width="1" />
|
||||
</g>`;
|
||||
}
|
||||
|
||||
function renderGenericTie(x, y, netName, netClass) {
|
||||
const color = netColor(netClass);
|
||||
return `<circle cx="${x}" cy="${y}" r="3" fill="#ffffff" stroke="${color}" stroke-width="1.3" data-net-tie="${esc(netName)}" />`;
|
||||
}
|
||||
|
||||
function renderTieSymbol(x, y, netName, netClass) {
|
||||
if (netClass === "ground") {
|
||||
return renderGroundSymbol(x, y, netName);
|
||||
}
|
||||
if (netClass === "power") {
|
||||
return renderPowerSymbol(x, y, netName);
|
||||
}
|
||||
return renderGenericTie(x, y, netName, netClass);
|
||||
}
|
||||
|
||||
function representativePoint(routeInfo, netAnchor) {
|
||||
if (routeInfo.labelPoints?.length) {
|
||||
return routeInfo.labelPoints[0];
|
||||
}
|
||||
if (routeInfo.tiePoints?.length) {
|
||||
return routeInfo.tiePoints[0];
|
||||
}
|
||||
if (routeInfo.routes?.length && routeInfo.routes[0].length) {
|
||||
const seg = routeInfo.routes[0][0];
|
||||
return {
|
||||
x: (seg.a.x + seg.b.x) / 2,
|
||||
y: (seg.a.y + seg.b.y) / 2
|
||||
};
|
||||
}
|
||||
return netAnchor;
|
||||
}
|
||||
|
||||
function renderLegend() {
|
||||
const entries = [
|
||||
["power", "Power"],
|
||||
["ground", "Ground"],
|
||||
["clock", "Clock"],
|
||||
["signal", "Signal"],
|
||||
["analog", "Analog"]
|
||||
];
|
||||
|
||||
const rows = entries
|
||||
.map(
|
||||
([cls, label], idx) =>
|
||||
`<g transform="translate(0 ${idx * 14})"><line x1="0" y1="6" x2="16" y2="6" stroke="${netColor(cls)}" stroke-width="2" /><text x="22" y="9" fill="#475467" font-size="10">${label}</text></g>`
|
||||
)
|
||||
.join("");
|
||||
|
||||
return `
|
||||
<g data-layer="legend" transform="translate(14 14)">
|
||||
<rect x="-8" y="-8" width="120" height="82" rx="6" fill="#ffffffd8" stroke="#d0d5dd" />
|
||||
${rows}
|
||||
</g>`;
|
||||
}
|
||||
|
||||
export function renderSvgFromLayout(model, layout, options = {}) {
|
||||
const showLabels = options.show_labels !== false;
|
||||
const pinNets = pinNetMap(model);
|
||||
const netClassByName = new Map((model.nets ?? []).map((n) => [n.name, n.class]));
|
||||
const allPinPoints = [];
|
||||
|
||||
const components = layout.placed
|
||||
.map((inst) => {
|
||||
const sym = model.symbols[inst.symbol];
|
||||
const x = inst.placement.x;
|
||||
const y = inst.placement.y;
|
||||
const rotation = normalizeRotation(inst.placement.rotation ?? 0);
|
||||
const cx = x + sym.body.width / 2;
|
||||
const cy = y + sym.body.height / 2;
|
||||
const templateKind = symbolTemplateKind(sym);
|
||||
const compactLabel = templateKind || sym.body.width <= 140 || sym.body.height <= 90;
|
||||
const legacyShowInstanceNetLabels = Boolean(inst.properties?.show_net_labels);
|
||||
const pinUi =
|
||||
inst.properties?.pin_ui && typeof inst.properties.pin_ui === "object" && !Array.isArray(inst.properties.pin_ui)
|
||||
? inst.properties.pin_ui
|
||||
: {};
|
||||
|
||||
const pinSvg = sym.pins
|
||||
.map((pin) => {
|
||||
let px = x;
|
||||
let py = y;
|
||||
const pinCircles = [];
|
||||
const pinLabels = [];
|
||||
const instanceNetLabels = [];
|
||||
for (const pin of sym.pins) {
|
||||
let px = x;
|
||||
let py = y;
|
||||
|
||||
if (pin.side === "left") {
|
||||
px = x;
|
||||
py = y + pin.offset;
|
||||
} else if (pin.side === "right") {
|
||||
px = x + sym.body.width;
|
||||
py = y + pin.offset;
|
||||
} else if (pin.side === "top") {
|
||||
px = x + pin.offset;
|
||||
py = y;
|
||||
} else {
|
||||
px = x + pin.offset;
|
||||
py = y + sym.body.height;
|
||||
if (pin.side === "left") {
|
||||
px = x;
|
||||
py = y + pin.offset;
|
||||
} else if (pin.side === "right") {
|
||||
px = x + sym.body.width;
|
||||
py = y + pin.offset;
|
||||
} else if (pin.side === "top") {
|
||||
px = x + pin.offset;
|
||||
py = y;
|
||||
} else {
|
||||
px = x + pin.offset;
|
||||
py = y + sym.body.height;
|
||||
}
|
||||
|
||||
pinCircles.push(
|
||||
`<circle cx="${px}" cy="${py}" r="3.2" fill="#111827" data-pin-ref="${esc(inst.ref)}" data-pin-name="${esc(pin.name)}" data-pin-nets="${esc((pinNets.get(`${inst.ref}.${pin.name}`) ?? []).join(","))}" />`
|
||||
);
|
||||
|
||||
const rotated = rotatePoint({ x: px, y: py }, { x: cx, y: cy }, rotation);
|
||||
const rx = rotated.x;
|
||||
const ry = rotated.y;
|
||||
const rotatedSide = rotateSide(pin.side, rotation);
|
||||
|
||||
allPinPoints.push({ x: rx, y: ry });
|
||||
let labelX = rx + 6;
|
||||
let labelY = ry - 4;
|
||||
let textAnchor = "start";
|
||||
|
||||
if (rotatedSide === "right") {
|
||||
labelX = rx - 6;
|
||||
labelY = ry - 4;
|
||||
textAnchor = "end";
|
||||
} else if (rotatedSide === "top") {
|
||||
labelX = rx + 4;
|
||||
labelY = ry + 12;
|
||||
textAnchor = "start";
|
||||
} else if (rotatedSide === "bottom") {
|
||||
labelX = rx + 4;
|
||||
labelY = ry - 8;
|
||||
textAnchor = "start";
|
||||
}
|
||||
|
||||
const showPinLabel = !templateKind || !/^\d+$/.test(pin.name);
|
||||
if (showPinLabel) {
|
||||
pinLabels.push(
|
||||
`<text x="${labelX}" y="${labelY}" text-anchor="${textAnchor}" font-size="10" fill="#475467" data-pin-label="${esc(inst.ref)}.${esc(pin.name)}">${esc(pin.name)}</text>`
|
||||
);
|
||||
}
|
||||
|
||||
const pinUiEntry = pinUi[pin.name];
|
||||
const showPinNetLabel =
|
||||
pinUiEntry && typeof pinUiEntry === "object" && Object.prototype.hasOwnProperty.call(pinUiEntry, "show_net_label")
|
||||
? Boolean(pinUiEntry.show_net_label)
|
||||
: legacyShowInstanceNetLabels;
|
||||
if (showPinNetLabel && showLabels) {
|
||||
const nets = pinNets.get(`${inst.ref}.${pin.name}`) ?? [];
|
||||
const displayNet = nets.find((n) => !isGroundLikeNet({ name: n, class: "" })) ?? nets[0];
|
||||
if (displayNet) {
|
||||
let netX = labelX;
|
||||
let netY = labelY;
|
||||
let netAnchor = textAnchor;
|
||||
|
||||
if (rotatedSide === "left") {
|
||||
netX = rx - 12;
|
||||
netY = ry - 10;
|
||||
netAnchor = "end";
|
||||
} else if (rotatedSide === "right") {
|
||||
netX = rx + 12;
|
||||
netY = ry - 10;
|
||||
netAnchor = "start";
|
||||
} else if (rotatedSide === "top") {
|
||||
netX = rx + 8;
|
||||
netY = ry - 10;
|
||||
netAnchor = "start";
|
||||
} else {
|
||||
netX = rx + 8;
|
||||
netY = ry + 14;
|
||||
netAnchor = "start";
|
||||
}
|
||||
|
||||
instanceNetLabels.push(
|
||||
`<text x="${netX}" y="${netY}" text-anchor="${netAnchor}" font-size="10" font-weight="700" fill="${netColor(netClassByName.get(displayNet))}" stroke="#f8fafc" stroke-width="2.6" paint-order="stroke fill" data-net-label="${esc(displayNet)}" data-ref-net-label="${esc(inst.ref)}">${esc(displayNet)}</text>`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
`<circle cx="${px}" cy="${py}" r="3" fill="#1f2937" data-pin="${esc(pin.name)}" />`,
|
||||
`<text x="${px + 6}" y="${py - 4}" font-size="10" fill="#374151">${esc(pin.name)}</text>`
|
||||
].join("");
|
||||
})
|
||||
.join("");
|
||||
const pinLabelsSvg = [...pinLabels, ...instanceNetLabels].join("");
|
||||
const pinCoreSvg = pinCircles.join("");
|
||||
|
||||
const rotationTransform = rotation ? ` transform="rotate(${rotation} ${cx} ${cy})"` : "";
|
||||
|
||||
const refLabel = truncate(inst.ref, compactLabel ? 6 : 10);
|
||||
const valueLabel = truncate(inst.properties?.value ?? inst.symbol, compactLabel ? 18 : 28);
|
||||
const refY = compactLabel ? y - 6 : y + 18;
|
||||
const valueY = compactLabel ? y + sym.body.height + 14 : y + 34;
|
||||
|
||||
return `
|
||||
<g data-ref="${esc(inst.ref)}" data-symbol="${esc(inst.symbol)}">
|
||||
<rect x="${x}" y="${y}" width="${sym.body.width}" height="${sym.body.height}" rx="8" fill="#ffffff" stroke="#111827" stroke-width="2" />
|
||||
<text x="${x + 10}" y="${y + 18}" font-size="12" font-weight="700" fill="#111827">${esc(inst.ref)}</text>
|
||||
<text x="${x + 10}" y="${y + 34}" font-size="10" fill="#4b5563">${esc(inst.properties?.value ?? inst.symbol)}</text>
|
||||
${pinSvg}
|
||||
<g${rotationTransform}>
|
||||
${renderSymbolBody(sym, x, y, sym.body.width, sym.body.height)}
|
||||
${pinCoreSvg}
|
||||
</g>
|
||||
${pinLabelsSvg}
|
||||
<text x="${x + 8}" y="${refY}" font-size="${compactLabel ? 11 : 12}" font-weight="700" fill="#111827" stroke="#f8fafc" stroke-width="2.2" paint-order="stroke fill">${esc(refLabel)}</text>
|
||||
<text x="${x + 8}" y="${valueY}" font-size="${compactLabel ? 9 : 10}" fill="#667085" stroke="#f8fafc" stroke-width="2" paint-order="stroke fill">${esc(valueLabel)}</text>
|
||||
</g>`;
|
||||
})
|
||||
.join("\n");
|
||||
@@ -59,18 +449,107 @@ export function renderSvg(model) {
|
||||
const path = route
|
||||
.map((seg, idx) => `${idx === 0 ? "M" : "L"} ${seg.a.x} ${seg.a.y} L ${seg.b.x} ${seg.b.y}`)
|
||||
.join(" ");
|
||||
return `<path d="${path}" fill="none" stroke="#2563eb" stroke-width="2" data-net="${esc(rn.net.name)}" />`;
|
||||
return renderWirePath(path, rn.net.name, rn.net.class);
|
||||
})
|
||||
)
|
||||
.join("\n");
|
||||
|
||||
const labels = model.nets
|
||||
.map((net) => {
|
||||
const p = netAnchorPoint(net, model, layout.placed);
|
||||
if (!p) {
|
||||
const junctions = layout.routed
|
||||
.flatMap((rn) =>
|
||||
(rn.junctionPoints ?? []).map((p) => {
|
||||
const color = netColor(rn.net.class);
|
||||
return `<circle cx="${p.x}" cy="${p.y}" r="3" fill="${color}" stroke="#f8fafc" stroke-width="1.3" data-net-junction="${esc(rn.net.name)}" />`;
|
||||
})
|
||||
)
|
||||
.join("\n");
|
||||
|
||||
const tiePoints = layout.routed
|
||||
.flatMap((rn) =>
|
||||
(rn.tiePoints ?? []).map((p) => renderTieSymbol(p.x, p.y, rn.net.name, rn.net.class))
|
||||
)
|
||||
.join("\n");
|
||||
|
||||
const routedByName = new Map(layout.routed.map((r) => [r.net.name, r]));
|
||||
const usedLabelPoints = [];
|
||||
const labels = [];
|
||||
const tieLabels = [];
|
||||
|
||||
for (const net of model.nets) {
|
||||
if (isGroundLikeNet(net)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const routeInfo = routedByName.get(net.name);
|
||||
if (routeInfo?.isBusMember && routeInfo.mode === "label_tie") {
|
||||
continue;
|
||||
}
|
||||
|
||||
const netAnchor = netAnchorPoint(net, model, layout.placed);
|
||||
const candidates = [];
|
||||
|
||||
if (routeInfo?.mode === "label_tie") {
|
||||
candidates.push(...(routeInfo?.labelPoints ?? []));
|
||||
const selected = pickLabelPoints(candidates, 1, usedLabelPoints, GRID * 2.4, allPinPoints);
|
||||
for (const p of selected) {
|
||||
labels.push(renderNetLabel(p.x, p.y, net.name, net.class, true));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (routeInfo?.labelPoints?.length) {
|
||||
candidates.push(...routeInfo.labelPoints);
|
||||
}
|
||||
if (netAnchor) {
|
||||
candidates.push({ x: netAnchor.x + 8, y: netAnchor.y - 8 });
|
||||
}
|
||||
|
||||
const selected = pickLabelPoints(candidates, 1, usedLabelPoints, GRID * 2.4, allPinPoints);
|
||||
for (const p of selected) {
|
||||
labels.push(renderNetLabel(p.x, p.y, net.name, net.class));
|
||||
}
|
||||
}
|
||||
|
||||
if (showLabels) {
|
||||
const usedTieLabels = [];
|
||||
for (const rn of layout.routed) {
|
||||
if (rn.mode !== "label_tie" || isGroundLikeNet(rn.net)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const candidates = (rn.tiePoints ?? []).map((p) => tieLabelPoint(p, rn.net.class));
|
||||
if (!candidates.length) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const maxPerNet = rn.net.class === "power" ? Math.min(6, candidates.length) : Math.min(2, candidates.length);
|
||||
const selected = pickLabelPoints(candidates, maxPerNet, usedTieLabels, GRID * 1.5, allPinPoints);
|
||||
for (const p of selected) {
|
||||
tieLabels.push(renderNetLabel(p.x, p.y, rn.net.name, rn.net.class, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const busLabels = (layout.bus_groups ?? [])
|
||||
.map((group) => {
|
||||
const reps = group.nets
|
||||
.map((netName) => {
|
||||
const net = model.nets.find((n) => n.name === netName);
|
||||
if (!net) {
|
||||
return null;
|
||||
}
|
||||
const anchor = netAnchorPoint(net, model, layout.placed);
|
||||
return representativePoint(routedByName.get(netName), anchor);
|
||||
})
|
||||
.filter(Boolean);
|
||||
|
||||
if (!reps.length) {
|
||||
return "";
|
||||
}
|
||||
return `<text x="${p.x + 8}" y="${p.y - 8}" font-size="10" fill="#0f766e" data-net-label="${esc(net.name)}">${esc(net.name)}</text>`;
|
||||
|
||||
const x = reps.reduce((sum, p) => sum + p.x, 0) / reps.length;
|
||||
const y = reps.reduce((sum, p) => sum + p.y, 0) / reps.length;
|
||||
|
||||
return `<text x="${x + 12}" y="${y - 12}" font-size="11" font-weight="700" fill="#155eef" stroke="#f8fafc" stroke-width="3" paint-order="stroke fill" data-bus-group="${esc(group.name)}">${esc(group.name)} bus</text>`;
|
||||
})
|
||||
.join("\n");
|
||||
|
||||
@@ -82,14 +561,25 @@ export function renderSvg(model) {
|
||||
})
|
||||
.join("\n");
|
||||
|
||||
const labelLayer = showLabels ? [...labels, ...tieLabels].join("\n") : "";
|
||||
|
||||
return `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${layout.width} ${layout.height}" width="${layout.width}" height="${layout.height}" data-engine="schemeta-mvp">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${layout.width} ${layout.height}" width="${layout.width}" height="${layout.height}" data-engine="schemeta-v2">
|
||||
<g data-layer="background">
|
||||
<rect x="0" y="0" width="${layout.width}" height="${layout.height}" fill="#f9fafb" />
|
||||
<rect x="0" y="0" width="${layout.width}" height="${layout.height}" fill="#f8fafc" />
|
||||
</g>
|
||||
<g data-layer="components">${components}</g>
|
||||
<g data-layer="wires">${wires}</g>
|
||||
<g data-layer="net-labels">${labels}</g>
|
||||
<g data-layer="junctions">${junctions}</g>
|
||||
<g data-layer="ties">${tiePoints}</g>
|
||||
<g data-layer="net-labels">${labelLayer}</g>
|
||||
<g data-layer="bus-groups">${busLabels}</g>
|
||||
<g data-layer="annotations">${annotations}</g>
|
||||
${renderLegend()}
|
||||
</svg>`;
|
||||
}
|
||||
|
||||
export function renderSvg(model, options = {}) {
|
||||
const layout = layoutAndRoute(model, options);
|
||||
return renderSvgFromLayout(model, layout, options);
|
||||
}
|
||||
+199
-17
@@ -1,17 +1,62 @@
|
||||
import { createServer } from "node:http";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { extname, join, normalize } from "node:path";
|
||||
import { analyze, compile } from "./compile.js";
|
||||
import { applyLayoutToModel } from "./layout.js";
|
||||
import { validateModel } from "./validate.js";
|
||||
|
||||
const PORT = Number(process.env.PORT ?? "8787");
|
||||
const MAX_BODY_BYTES = Number(process.env.MAX_BODY_BYTES ?? 2 * 1024 * 1024);
|
||||
const CORS_ORIGIN = process.env.CORS_ORIGIN ?? "*";
|
||||
const FRONTEND_ROOT = join(process.cwd(), "frontend");
|
||||
|
||||
const MIME_TYPES = {
|
||||
".html": "text/html; charset=utf-8",
|
||||
".css": "text/css; charset=utf-8",
|
||||
".js": "text/javascript; charset=utf-8",
|
||||
".json": "application/json; charset=utf-8",
|
||||
".svg": "image/svg+xml",
|
||||
".png": "image/png"
|
||||
};
|
||||
|
||||
function setCors(res) {
|
||||
res.setHeader("Access-Control-Allow-Origin", CORS_ORIGIN);
|
||||
res.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
|
||||
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
||||
}
|
||||
|
||||
function json(res, status, payload) {
|
||||
res.writeHead(status, { "Content-Type": "application/json" });
|
||||
setCors(res);
|
||||
res.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
|
||||
res.end(JSON.stringify(payload, null, 2));
|
||||
}
|
||||
|
||||
function errorEnvelope(code, message) {
|
||||
return {
|
||||
ok: false,
|
||||
error: {
|
||||
code,
|
||||
message
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function readBody(req) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const chunks = [];
|
||||
req.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
|
||||
let size = 0;
|
||||
|
||||
req.on("data", (chunk) => {
|
||||
const c = Buffer.from(chunk);
|
||||
size += c.length;
|
||||
if (size > MAX_BODY_BYTES) {
|
||||
reject(Object.assign(new Error("Payload too large"), { code: "PAYLOAD_TOO_LARGE" }));
|
||||
req.destroy();
|
||||
return;
|
||||
}
|
||||
chunks.push(c);
|
||||
});
|
||||
|
||||
req.on("end", () => {
|
||||
if (chunks.length === 0) {
|
||||
resolve({});
|
||||
@@ -21,46 +66,183 @@ function readBody(req) {
|
||||
try {
|
||||
const value = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
||||
resolve(value);
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
} catch {
|
||||
reject(Object.assign(new Error("Malformed JSON"), { code: "INVALID_JSON" }));
|
||||
}
|
||||
});
|
||||
|
||||
req.on("error", reject);
|
||||
});
|
||||
}
|
||||
|
||||
const server = createServer(async (req, res) => {
|
||||
if (!req.url || !req.method) {
|
||||
return json(res, 400, { error: "Invalid request." });
|
||||
function sanitizePath(urlPath) {
|
||||
if (urlPath === "/") {
|
||||
return "index.html";
|
||||
}
|
||||
|
||||
if (req.method === "GET" && req.url === "/health") {
|
||||
const clean = normalize(urlPath).replace(/^\/+/, "");
|
||||
if (clean.includes("..")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return clean;
|
||||
}
|
||||
|
||||
async function serveStatic(urlPath, res) {
|
||||
const cleanPath = sanitizePath(urlPath);
|
||||
if (!cleanPath) {
|
||||
json(res, 400, errorEnvelope("invalid_path", "Invalid static file path."));
|
||||
return true;
|
||||
}
|
||||
|
||||
const filePath = join(FRONTEND_ROOT, cleanPath);
|
||||
|
||||
try {
|
||||
const content = await readFile(filePath);
|
||||
const type = MIME_TYPES[extname(filePath)] ?? "application/octet-stream";
|
||||
setCors(res);
|
||||
res.writeHead(200, { "Content-Type": type });
|
||||
res.end(content);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function parsePayloadOptions(body) {
|
||||
if (body && typeof body === "object" && Object.prototype.hasOwnProperty.call(body, "payload")) {
|
||||
return {
|
||||
payload: body.payload,
|
||||
options: body.options ?? {}
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
payload: body,
|
||||
options: {}
|
||||
};
|
||||
}
|
||||
|
||||
const server = createServer(async (req, res) => {
|
||||
if (!req.url || !req.method) {
|
||||
return json(res, 400, errorEnvelope("invalid_request", "Invalid request."));
|
||||
}
|
||||
|
||||
const pathname = new URL(req.url, "http://localhost").pathname;
|
||||
|
||||
if (req.method === "OPTIONS") {
|
||||
setCors(res);
|
||||
res.writeHead(204);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.method === "GET" && pathname === "/health") {
|
||||
return json(res, 200, {
|
||||
ok: true,
|
||||
service: "schemeta",
|
||||
status: "ok",
|
||||
date: new Date().toISOString()
|
||||
});
|
||||
}
|
||||
|
||||
if (req.method === "POST" && req.url === "/analyze") {
|
||||
if (req.method === "GET" && pathname === "/mcp/ui-bundle") {
|
||||
return json(res, 200, {
|
||||
ok: true,
|
||||
name: "schemeta-workspace",
|
||||
version: "0.2.0",
|
||||
entry: "/",
|
||||
title: "Schemeta Workspace",
|
||||
transport: "iframe"
|
||||
});
|
||||
}
|
||||
|
||||
if (req.method === "POST" && pathname === "/analyze") {
|
||||
try {
|
||||
const body = await readBody(req);
|
||||
return json(res, 200, analyze(body));
|
||||
} catch {
|
||||
return json(res, 400, { error: "Invalid JSON payload." });
|
||||
const parsed = parsePayloadOptions(body);
|
||||
return json(res, 200, analyze(parsed.payload, parsed.options));
|
||||
} catch (err) {
|
||||
if (err?.code === "PAYLOAD_TOO_LARGE") {
|
||||
return json(res, 413, errorEnvelope("payload_too_large", `Request body exceeds ${MAX_BODY_BYTES} bytes.`));
|
||||
}
|
||||
if (err?.code === "INVALID_JSON") {
|
||||
return json(res, 400, errorEnvelope("invalid_json", "Invalid JSON payload."));
|
||||
}
|
||||
return json(res, 500, errorEnvelope("internal_error", "Request failed."));
|
||||
}
|
||||
}
|
||||
|
||||
if (req.method === "POST" && req.url === "/compile") {
|
||||
if (req.method === "POST" && pathname === "/compile") {
|
||||
try {
|
||||
const body = await readBody(req);
|
||||
return json(res, 200, compile(body));
|
||||
} catch {
|
||||
return json(res, 400, { error: "Invalid JSON payload." });
|
||||
const parsed = parsePayloadOptions(body);
|
||||
return json(res, 200, compile(parsed.payload, parsed.options));
|
||||
} catch (err) {
|
||||
if (err?.code === "PAYLOAD_TOO_LARGE") {
|
||||
return json(res, 413, errorEnvelope("payload_too_large", `Request body exceeds ${MAX_BODY_BYTES} bytes.`));
|
||||
}
|
||||
if (err?.code === "INVALID_JSON") {
|
||||
return json(res, 400, errorEnvelope("invalid_json", "Invalid JSON payload."));
|
||||
}
|
||||
return json(res, 500, errorEnvelope("internal_error", "Request failed."));
|
||||
}
|
||||
}
|
||||
|
||||
return json(res, 404, { error: "Not found." });
|
||||
if (req.method === "POST" && pathname === "/layout/auto") {
|
||||
try {
|
||||
const body = await readBody(req);
|
||||
const parsed = parsePayloadOptions(body);
|
||||
const validated = validateModel(parsed.payload, parsed.options);
|
||||
const model = validated.model ?? parsed.payload;
|
||||
const laidOut = applyLayoutToModel(model, { respectLocks: false });
|
||||
return json(res, 200, {
|
||||
ok: true,
|
||||
model: laidOut,
|
||||
compile: compile(laidOut, parsed.options)
|
||||
});
|
||||
} catch (err) {
|
||||
if (err?.code === "PAYLOAD_TOO_LARGE") {
|
||||
return json(res, 413, errorEnvelope("payload_too_large", `Request body exceeds ${MAX_BODY_BYTES} bytes.`));
|
||||
}
|
||||
if (err?.code === "INVALID_JSON") {
|
||||
return json(res, 400, errorEnvelope("invalid_json", "Invalid JSON payload."));
|
||||
}
|
||||
return json(res, 500, errorEnvelope("internal_error", "Layout auto failed."));
|
||||
}
|
||||
}
|
||||
|
||||
if (req.method === "POST" && pathname === "/layout/tidy") {
|
||||
try {
|
||||
const body = await readBody(req);
|
||||
const parsed = parsePayloadOptions(body);
|
||||
const validated = validateModel(parsed.payload, parsed.options);
|
||||
const model = validated.model ?? parsed.payload;
|
||||
const laidOut = applyLayoutToModel(model, { respectLocks: true });
|
||||
return json(res, 200, {
|
||||
ok: true,
|
||||
model: laidOut,
|
||||
compile: compile(laidOut, parsed.options)
|
||||
});
|
||||
} catch (err) {
|
||||
if (err?.code === "PAYLOAD_TOO_LARGE") {
|
||||
return json(res, 413, errorEnvelope("payload_too_large", `Request body exceeds ${MAX_BODY_BYTES} bytes.`));
|
||||
}
|
||||
if (err?.code === "INVALID_JSON") {
|
||||
return json(res, 400, errorEnvelope("invalid_json", "Invalid JSON payload."));
|
||||
}
|
||||
return json(res, 500, errorEnvelope("internal_error", "Layout tidy failed."));
|
||||
}
|
||||
}
|
||||
|
||||
if (req.method === "GET") {
|
||||
const served = await serveStatic(pathname, res);
|
||||
if (served) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
return json(res, 404, errorEnvelope("not_found", "Not found."));
|
||||
});
|
||||
|
||||
server.listen(PORT, () => {
|
||||
|
||||
+516
-8
@@ -18,9 +18,505 @@ const VALID_NET_CLASSES = new Set([
|
||||
"clock",
|
||||
"bus"
|
||||
]);
|
||||
const BUILTIN_PART_TYPES = new Set(["resistor", "capacitor", "inductor", "diode", "led", "connector", "generic"]);
|
||||
|
||||
export function validateModel(model) {
|
||||
const GENERIC_DEFAULT_WIDTH = 160;
|
||||
const GENERIC_MIN_HEIGHT = 120;
|
||||
const GENERIC_PIN_STEP = 18;
|
||||
const TEMPLATE_PIN_STEP = 24;
|
||||
|
||||
const TEMPLATE_DEFS = {
|
||||
resistor: {
|
||||
category: "passive_resistor",
|
||||
body: { width: 120, height: 70 },
|
||||
pins: [
|
||||
{ name: "1", side: "left", offset: 35, type: "passive" },
|
||||
{ name: "2", side: "right", offset: 35, type: "passive" }
|
||||
]
|
||||
},
|
||||
capacitor: {
|
||||
category: "passive_capacitor",
|
||||
body: { width: 120, height: 70 },
|
||||
pins: [
|
||||
{ name: "1", side: "left", offset: 35, type: "passive" },
|
||||
{ name: "2", side: "right", offset: 35, type: "passive" }
|
||||
]
|
||||
},
|
||||
inductor: {
|
||||
category: "passive_inductor",
|
||||
body: { width: 120, height: 70 },
|
||||
pins: [
|
||||
{ name: "1", side: "left", offset: 35, type: "passive" },
|
||||
{ name: "2", side: "right", offset: 35, type: "passive" }
|
||||
]
|
||||
},
|
||||
diode: {
|
||||
category: "passive_diode",
|
||||
body: { width: 120, height: 70 },
|
||||
pins: [
|
||||
{ name: "A", side: "left", offset: 35, type: "passive" },
|
||||
{ name: "K", side: "right", offset: 35, type: "passive" }
|
||||
]
|
||||
},
|
||||
led: {
|
||||
category: "passive_led",
|
||||
body: { width: 120, height: 70 },
|
||||
pins: [
|
||||
{ name: "A", side: "left", offset: 35, type: "passive" },
|
||||
{ name: "K", side: "right", offset: 35, type: "passive" }
|
||||
]
|
||||
},
|
||||
connector: {
|
||||
category: "connector_generic",
|
||||
body: { width: 140, height: 90 },
|
||||
pins: [
|
||||
{ name: "1", side: "left", offset: 24, type: "passive" },
|
||||
{ name: "2", side: "left", offset: 48, type: "passive" },
|
||||
{ name: "3", side: "right", offset: 24, type: "passive" },
|
||||
{ name: "4", side: "right", offset: 48, type: "passive" }
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
function clone(value) {
|
||||
return JSON.parse(JSON.stringify(value));
|
||||
}
|
||||
|
||||
function hasTemplateName(sym) {
|
||||
const name = String(sym?.template_name ?? "").toLowerCase();
|
||||
return Object.prototype.hasOwnProperty.call(TEMPLATE_DEFS, name);
|
||||
}
|
||||
|
||||
function normalizePartName(value) {
|
||||
return String(value ?? "")
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
}
|
||||
|
||||
function symbolIdForPart(partName) {
|
||||
return `__part_${partName}`;
|
||||
}
|
||||
|
||||
function isGenericSymbol(sym) {
|
||||
if (!sym || typeof sym !== "object") {
|
||||
return false;
|
||||
}
|
||||
if (sym.auto_generated === true) {
|
||||
return true;
|
||||
}
|
||||
const category = String(sym.category ?? "").toLowerCase();
|
||||
return category.includes("generic");
|
||||
}
|
||||
|
||||
function pinTypeFromNet(netClass) {
|
||||
if (netClass === "ground") {
|
||||
return "ground";
|
||||
}
|
||||
if (netClass === "power") {
|
||||
return "power_in";
|
||||
}
|
||||
if (netClass === "clock") {
|
||||
return "input";
|
||||
}
|
||||
if (netClass === "analog") {
|
||||
return "analog";
|
||||
}
|
||||
return "passive";
|
||||
}
|
||||
|
||||
function inferSymbolTemplate(inst) {
|
||||
const ref = String(inst.ref ?? "").toUpperCase();
|
||||
const symbol = String(inst.symbol ?? "").toLowerCase();
|
||||
const value = String(inst.properties?.value ?? "").toLowerCase();
|
||||
const haystack = `${symbol} ${value}`;
|
||||
|
||||
if (ref.startsWith("R") || /\bres(istor)?\b/.test(haystack)) {
|
||||
return "resistor";
|
||||
}
|
||||
if (ref.startsWith("C") || /\bcap(acitor)?\b/.test(haystack)) {
|
||||
return "capacitor";
|
||||
}
|
||||
if (ref.startsWith("L") || /\bind(uctor)?\b/.test(haystack)) {
|
||||
return "inductor";
|
||||
}
|
||||
if (ref.startsWith("D") || /\bdiod(e)?\b/.test(haystack)) {
|
||||
return "diode";
|
||||
}
|
||||
if (/\bled\b/.test(haystack)) {
|
||||
return "led";
|
||||
}
|
||||
if (ref.startsWith("J") || ref.startsWith("P") || /\b(conn(ector)?|header)\b/.test(haystack)) {
|
||||
return "connector";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function buildTemplateSymbol(symbolId, templateName) {
|
||||
const template = TEMPLATE_DEFS[templateName];
|
||||
if (!template) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const pins = template.pins.map((p, idx) => ({
|
||||
name: p.name,
|
||||
number: String(idx + 1),
|
||||
side: p.side,
|
||||
offset: p.offset,
|
||||
type: p.type
|
||||
}));
|
||||
|
||||
return {
|
||||
symbol_id: symbolId,
|
||||
category: template.category,
|
||||
auto_generated: true,
|
||||
template_name: templateName,
|
||||
body: {
|
||||
width: template.body.width,
|
||||
height: template.body.height
|
||||
},
|
||||
pins,
|
||||
graphics: {
|
||||
primitives: [
|
||||
{
|
||||
type: "rect",
|
||||
x: 0,
|
||||
y: 0,
|
||||
w: template.body.width,
|
||||
h: template.body.height
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function buildUsageByRef(model) {
|
||||
const usage = new Map();
|
||||
for (const net of model.nets) {
|
||||
for (const node of net.nodes ?? []) {
|
||||
const item = usage.get(node.ref) ?? new Map();
|
||||
const entry = item.get(node.pin) ?? { pin: node.pin, classes: new Set() };
|
||||
entry.classes.add(net.class);
|
||||
item.set(node.pin, entry);
|
||||
usage.set(node.ref, item);
|
||||
}
|
||||
}
|
||||
return usage;
|
||||
}
|
||||
|
||||
function buildUsageBySymbol(model) {
|
||||
const byRef = new Map((model.instances ?? []).map((inst) => [inst.ref, inst]));
|
||||
const usage = new Map();
|
||||
|
||||
for (const net of model.nets ?? []) {
|
||||
for (const node of net.nodes ?? []) {
|
||||
const inst = byRef.get(node.ref);
|
||||
if (!inst) {
|
||||
continue;
|
||||
}
|
||||
const symbolId = inst.symbol;
|
||||
const symbolUsage = usage.get(symbolId) ?? new Map();
|
||||
const entry = symbolUsage.get(node.pin) ?? { pin: node.pin, classes: new Set() };
|
||||
entry.classes.add(net.class);
|
||||
symbolUsage.set(node.pin, entry);
|
||||
usage.set(symbolId, symbolUsage);
|
||||
}
|
||||
}
|
||||
|
||||
return usage;
|
||||
}
|
||||
|
||||
function pinCountToHeight(pinCount) {
|
||||
const rows = Math.max(4, pinCount + 1);
|
||||
return Math.max(GENERIC_MIN_HEIGHT, rows * GENERIC_PIN_STEP);
|
||||
}
|
||||
|
||||
function buildPinsFromUsage(ref, pinUsage) {
|
||||
const names = [...pinUsage.values()].map((x) => x.pin).sort((a, b) => a.localeCompare(b));
|
||||
if (!names.length) {
|
||||
return [
|
||||
{
|
||||
name: "P1",
|
||||
number: "1",
|
||||
side: "left",
|
||||
offset: GENERIC_PIN_STEP,
|
||||
type: "passive"
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
const left = names.filter((_, idx) => idx % 2 === 0);
|
||||
const right = names.filter((_, idx) => idx % 2 === 1);
|
||||
let leftOffset = GENERIC_PIN_STEP;
|
||||
let rightOffset = GENERIC_PIN_STEP;
|
||||
|
||||
const ordered = [...left, ...right];
|
||||
const pins = [];
|
||||
for (let i = 0; i < ordered.length; i += 1) {
|
||||
const name = ordered[i];
|
||||
const classes = [...(pinUsage.get(name)?.classes ?? new Set())].sort();
|
||||
const preferredClass = classes[0] ?? "signal";
|
||||
const side = left.includes(name) ? "left" : "right";
|
||||
const offset = side === "left" ? leftOffset : rightOffset;
|
||||
if (side === "left") {
|
||||
leftOffset += GENERIC_PIN_STEP;
|
||||
} else {
|
||||
rightOffset += GENERIC_PIN_STEP;
|
||||
}
|
||||
|
||||
pins.push({
|
||||
name,
|
||||
number: String(i + 1),
|
||||
side,
|
||||
offset,
|
||||
type: pinTypeFromNet(preferredClass)
|
||||
});
|
||||
}
|
||||
|
||||
return pins;
|
||||
}
|
||||
|
||||
function ensureGenericSymbols(model, issues, enableGenericSymbols) {
|
||||
if (!enableGenericSymbols) {
|
||||
return model;
|
||||
}
|
||||
|
||||
const next = clone(model);
|
||||
next.symbols = next.symbols ?? {};
|
||||
const usageByRef = buildUsageByRef(next);
|
||||
|
||||
for (const inst of next.instances ?? []) {
|
||||
const part = normalizePartName(inst.part);
|
||||
if (!part) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!BUILTIN_PART_TYPES.has(part)) {
|
||||
issues.push({
|
||||
code: "invalid_part_type",
|
||||
message: `Instance '${inst.ref}' uses unsupported part '${inst.part}'.`,
|
||||
severity: "error",
|
||||
path: `instances.${inst.ref}.part`
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
inst.part = part;
|
||||
if (!inst.symbol) {
|
||||
inst.symbol = symbolIdForPart(part);
|
||||
}
|
||||
|
||||
if (next.symbols[inst.symbol]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (part === "generic") {
|
||||
next.symbols[inst.symbol] = {
|
||||
symbol_id: inst.symbol,
|
||||
category: "generic",
|
||||
auto_generated: true
|
||||
};
|
||||
continue;
|
||||
}
|
||||
|
||||
const templateSymbol = buildTemplateSymbol(inst.symbol, part);
|
||||
if (templateSymbol) {
|
||||
next.symbols[inst.symbol] = templateSymbol;
|
||||
}
|
||||
}
|
||||
|
||||
for (const inst of next.instances ?? []) {
|
||||
if (next.symbols[inst.symbol]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const templateName = inferSymbolTemplate(inst);
|
||||
const templated = templateName ? buildTemplateSymbol(inst.symbol, templateName) : null;
|
||||
|
||||
if (templated) {
|
||||
next.symbols[inst.symbol] = templated;
|
||||
issues.push({
|
||||
code: "auto_template_symbol_created",
|
||||
message: `Created '${templateName}' symbol '${inst.symbol}' for instance '${inst.ref}'.`,
|
||||
severity: "warning",
|
||||
path: `instances.${inst.ref}.symbol`
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const pinUsage = usageByRef.get(inst.ref) ?? new Map();
|
||||
const pins = buildPinsFromUsage(inst.ref, pinUsage);
|
||||
|
||||
next.symbols[inst.symbol] = {
|
||||
symbol_id: inst.symbol,
|
||||
category: "generic",
|
||||
auto_generated: true,
|
||||
body: {
|
||||
width: GENERIC_DEFAULT_WIDTH,
|
||||
height: pinCountToHeight(pins.length)
|
||||
},
|
||||
pins,
|
||||
graphics: {
|
||||
primitives: [
|
||||
{
|
||||
type: "rect",
|
||||
x: 0,
|
||||
y: 0,
|
||||
w: GENERIC_DEFAULT_WIDTH,
|
||||
h: pinCountToHeight(pins.length)
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
issues.push({
|
||||
code: "auto_generic_symbol_created",
|
||||
message: `Created generic symbol '${inst.symbol}' for instance '${inst.ref}'.`,
|
||||
severity: "warning",
|
||||
path: `instances.${inst.ref}.symbol`
|
||||
});
|
||||
}
|
||||
|
||||
const usageBySymbol = buildUsageBySymbol(next);
|
||||
for (const [id, sym] of Object.entries(next.symbols)) {
|
||||
if (typeof sym !== "object" || !sym) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!sym.symbol_id) {
|
||||
sym.symbol_id = id;
|
||||
issues.push({
|
||||
code: "auto_symbol_id_filled",
|
||||
message: `Filled missing symbol_id for '${id}'.`,
|
||||
severity: "warning",
|
||||
path: `symbols.${id}.symbol_id`
|
||||
});
|
||||
}
|
||||
|
||||
const templateName = String(sym.template_name ?? "").toLowerCase();
|
||||
if (hasTemplateName(sym)) {
|
||||
const templated = buildTemplateSymbol(id, templateName);
|
||||
let templateHydrated = false;
|
||||
if (!sym.category) {
|
||||
sym.category = templated.category;
|
||||
issues.push({
|
||||
code: "auto_symbol_category_filled",
|
||||
message: `Filled missing category for '${id}' from template '${templateName}'.`,
|
||||
severity: "warning",
|
||||
path: `symbols.${id}.category`
|
||||
});
|
||||
}
|
||||
if (!sym.body || sym.body.width == null || sym.body.height == null) {
|
||||
sym.body = { ...(sym.body ?? {}), ...templated.body };
|
||||
templateHydrated = true;
|
||||
}
|
||||
if (!Array.isArray(sym.pins) || sym.pins.length === 0) {
|
||||
sym.pins = templated.pins;
|
||||
templateHydrated = true;
|
||||
}
|
||||
if (!sym.graphics) {
|
||||
sym.graphics = templated.graphics;
|
||||
}
|
||||
if (templateHydrated) {
|
||||
issues.push({
|
||||
code: "auto_template_symbol_hydrated",
|
||||
message: `Hydrated template fields for '${id}' (${templateName}).`,
|
||||
severity: "warning",
|
||||
path: `symbols.${id}`
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!sym.category) {
|
||||
sym.category = "generic";
|
||||
issues.push({
|
||||
code: "auto_symbol_category_filled",
|
||||
message: `Filled missing category for '${id}' as generic.`,
|
||||
severity: "warning",
|
||||
path: `symbols.${id}.category`
|
||||
});
|
||||
}
|
||||
|
||||
const genericCategory = String(sym.category ?? "").toLowerCase().includes("generic");
|
||||
if (!genericCategory) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let genericHydrated = false;
|
||||
if (!Array.isArray(sym.pins) || sym.pins.length === 0) {
|
||||
const pinUsage = usageBySymbol.get(id) ?? new Map();
|
||||
sym.pins = buildPinsFromUsage(id, pinUsage);
|
||||
genericHydrated = true;
|
||||
}
|
||||
|
||||
if (!sym.body || sym.body.width == null || sym.body.height == null) {
|
||||
sym.body = {
|
||||
width: sym.body?.width ?? GENERIC_DEFAULT_WIDTH,
|
||||
height: Math.max(sym.body?.height ?? GENERIC_MIN_HEIGHT, pinCountToHeight(sym.pins.length))
|
||||
};
|
||||
genericHydrated = true;
|
||||
}
|
||||
|
||||
if (genericHydrated) {
|
||||
issues.push({
|
||||
code: "auto_generic_symbol_hydrated",
|
||||
message: `Hydrated generic fields for '${id}' from net usage.`,
|
||||
severity: "warning",
|
||||
path: `symbols.${id}`
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const net of next.nets ?? []) {
|
||||
for (const node of net.nodes ?? []) {
|
||||
const inst = (next.instances ?? []).find((x) => x.ref === node.ref);
|
||||
if (!inst) {
|
||||
continue;
|
||||
}
|
||||
const sym = next.symbols[inst.symbol];
|
||||
if (!sym || !isGenericSymbol(sym)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const hasPin = Array.isArray(sym.pins) && sym.pins.some((p) => p.name === node.pin);
|
||||
if (hasPin) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const side = (sym.pins?.length ?? 0) % 2 === 0 ? "left" : "right";
|
||||
const sameSideCount = (sym.pins ?? []).filter((p) => p.side === side).length;
|
||||
const pinStep = sym.template_name ? TEMPLATE_PIN_STEP : GENERIC_PIN_STEP;
|
||||
const offset = pinStep + sameSideCount * pinStep;
|
||||
const nextNumber = String((sym.pins?.length ?? 0) + 1);
|
||||
|
||||
sym.pins = sym.pins ?? [];
|
||||
sym.pins.push({
|
||||
name: node.pin,
|
||||
number: nextNumber,
|
||||
side,
|
||||
offset,
|
||||
type: pinTypeFromNet(net.class)
|
||||
});
|
||||
|
||||
sym.body = sym.body ?? { width: GENERIC_DEFAULT_WIDTH, height: GENERIC_MIN_HEIGHT };
|
||||
sym.body.width = sym.body.width ?? GENERIC_DEFAULT_WIDTH;
|
||||
sym.body.height = Math.max(sym.body.height ?? GENERIC_MIN_HEIGHT, pinCountToHeight(sym.pins.length));
|
||||
|
||||
issues.push({
|
||||
code: "auto_generic_pin_created",
|
||||
message: `Added pin '${node.pin}' to generic symbol '${inst.symbol}' from net '${net.name}'.`,
|
||||
severity: "warning",
|
||||
path: `symbols.${inst.symbol}.pins.${node.pin}`
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
export function validateModel(model, options = {}) {
|
||||
const issues = [];
|
||||
const enableGenericSymbols = options.generic_symbols !== false;
|
||||
|
||||
if (!model || typeof model !== "object") {
|
||||
return {
|
||||
@@ -65,8 +561,10 @@ export function validateModel(model) {
|
||||
return { model: null, issues };
|
||||
}
|
||||
|
||||
const symbolIds = new Set(Object.keys(model.symbols));
|
||||
for (const [id, sym] of Object.entries(model.symbols)) {
|
||||
const workingModel = ensureGenericSymbols(model, issues, enableGenericSymbols);
|
||||
const symbolIds = new Set(Object.keys(workingModel.symbols));
|
||||
|
||||
for (const [id, sym] of Object.entries(workingModel.symbols)) {
|
||||
if (sym.symbol_id !== id) {
|
||||
issues.push({
|
||||
code: "symbol_id_mismatch",
|
||||
@@ -111,7 +609,7 @@ export function validateModel(model) {
|
||||
|
||||
const refs = new Set();
|
||||
const instanceSymbol = new Map();
|
||||
for (const inst of model.instances) {
|
||||
for (const inst of workingModel.instances) {
|
||||
if (refs.has(inst.ref)) {
|
||||
issues.push({
|
||||
code: "duplicate_ref",
|
||||
@@ -122,7 +620,14 @@ export function validateModel(model) {
|
||||
}
|
||||
refs.add(inst.ref);
|
||||
|
||||
if (!symbolIds.has(inst.symbol)) {
|
||||
if (!inst.symbol) {
|
||||
issues.push({
|
||||
code: "instance_symbol_or_part_missing",
|
||||
message: `Instance '${inst.ref}' must define either 'symbol' or 'part'.`,
|
||||
severity: "error",
|
||||
path: `instances.${inst.ref}`
|
||||
});
|
||||
} else if (!symbolIds.has(inst.symbol)) {
|
||||
issues.push({
|
||||
code: "unknown_symbol",
|
||||
message: `Instance '${inst.ref}' references unknown symbol '${inst.symbol}'.`,
|
||||
@@ -134,7 +639,7 @@ export function validateModel(model) {
|
||||
}
|
||||
|
||||
const netNames = new Set();
|
||||
for (const net of model.nets) {
|
||||
for (const net of workingModel.nets) {
|
||||
if (netNames.has(net.name)) {
|
||||
issues.push({
|
||||
code: "duplicate_net_name",
|
||||
@@ -175,7 +680,10 @@ export function validateModel(model) {
|
||||
});
|
||||
continue;
|
||||
}
|
||||
const sym = model.symbols[symId];
|
||||
const sym = workingModel.symbols[symId];
|
||||
if (!sym || !Array.isArray(sym.pins)) {
|
||||
continue;
|
||||
}
|
||||
const foundPin = sym.pins.some((p) => p.name === node.pin);
|
||||
if (!foundPin) {
|
||||
issues.push({
|
||||
@@ -189,7 +697,7 @@ export function validateModel(model) {
|
||||
}
|
||||
|
||||
return {
|
||||
model: issues.some((x) => x.severity === "error") ? null : model,
|
||||
model: issues.some((x) => x.severity === "error") ? null : workingModel,
|
||||
issues
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user