Files
likang 7e01147566
CI / typecheck + test + build (push) Has been cancelled
Initial commit: import VibeMake PCB Studio
2026-11-03 11:20:08 +08:00

108 lines
4.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 ZiroEDA and contributors.
// Portions derived from KiCad, copyright The KiCad Developers. See NOTICE.md.
/**
* Per-item netclass render overrides. Counterparts: `SCH_LINE::GetLineColor` /
* `GetPenWidth` / `GetEffectiveLineStyle` (an item without its own stroke
* falls back to its net's effective netclass) and
* `SCH_JUNCTION::getEffectiveShape` (the settings dot size is clamped to
* ≥170% of the connected wire width).
*
* The maps are keyed by the renderer's refIds; the renderer applies them only
* where the item itself doesn't override (explicit stroke wins, per upstream).
*/
import { computeNetlist, type Netlist } from './connectivity/nets.js';
import { refId } from './tools/hittest.js';
import { type LibSymbol, type Schematic } from './types.js';
import { LINE_STYLE, LINE_STYLE_NAMES } from '@ziroeda/common/stroke_params.js';
import {
IU_PER_MILS,
LINE_STYLES,
junctionDotDiameterIU,
resolveEffectiveNetClass,
type SchematicSetup,
} from './schematic_settings.js';
export interface NetClassOverrides {
/** Line refId -> stroke fallback (wires get wire width, buses bus width). */
lines: ReadonlyMap<string, { color?: string; widthIU?: number; dash?: string }>;
/** Junction refId -> effective dot diameter (settings size clamped to
* ≥ 1.7 × the net's wire width). */
junctions: ReadonlyMap<string, number>;
}
/**
* A net class's line-style *name* -> the renderer's stroke-type token. Built
* from the one upstream table (`lineTypeNames`, common/stroke_params.cpp:39) so
* this cannot drift from the names the netclass grid offers. Solid maps to
* `undefined`: it is the renderer's default and overriding it would be a no-op.
*/
const DASH_TOKENS: Record<string, string | undefined> = Object.fromEntries(
LINE_STYLE_NAMES.map((d) => [d.label, d.style === LINE_STYLE.SOLID ? undefined : d.value]),
);
/** True when no class carries a visual parameter, the whole pass can skip. */
function nothingToApply(s: SchematicSetup): boolean {
return s.netClasses.classes.every(
(c) =>
!c.color &&
c.wireThickness.trim() === '' &&
c.busThickness.trim() === '' &&
(c.lineStyle === 'Solid' || LINE_STYLES.indexOf(c.lineStyle) <= 0),
);
}
/**
* Resolve every net's effective netclass and emit the per-item overrides.
* Pass a precomputed netlist when one is at hand (the editor's memo); plot
* paths let it compute one per document.
*/
export function computeNetClassOverrides(
sch: Schematic,
libById: Map<string, LibSymbol>,
setup: SchematicSetup,
netlist?: Netlist | null,
extraAssignments?: readonly { pattern: string; netClass: string }[],
): NetClassOverrides | undefined {
if (nothingToApply(setup) && !extraAssignments?.length) return undefined;
const nl = netlist ?? computeNetlist(sch, libById);
// refId -> line kind, for wire-vs-bus width selection.
const lineKind = new Map<string, 'wire' | 'bus'>();
sch.lines.forEach((l, i) => {
if (l.kind === 'wire' || l.kind === 'bus') lineKind.set(refId('line', l.uuid, i), l.kind);
});
const junctionIds = new Set<string>();
sch.junctions.forEach((j, i) => junctionIds.add(refId('junction', j.uuid, i)));
const baseDot = junctionDotDiameterIU(setup);
const lines = new Map<string, { color?: string; widthIU?: number; dash?: string }>();
const junctions = new Map<string, number>();
for (const net of nl.nets) {
const eff = resolveEffectiveNetClass(net.name, setup.netClasses, extraAssignments);
const dash = DASH_TOKENS[eff.lineStyle];
const wireIU = eff.wireWidthMils !== undefined ? eff.wireWidthMils * IU_PER_MILS : undefined;
const busIU = eff.busWidthMils !== undefined ? eff.busWidthMils * IU_PER_MILS : undefined;
const hasLineOverride =
eff.color !== undefined || wireIU !== undefined || busIU !== undefined || dash !== undefined;
for (const id of net.items) {
const kind = lineKind.get(id);
if (kind && hasLineOverride) {
const widthIU = kind === 'bus' ? busIU : wireIU;
lines.set(id, {
...(eff.color !== undefined ? { color: eff.color } : {}),
...(widthIU !== undefined ? { widthIU } : {}),
...(dash !== undefined ? { dash } : {}),
});
} else if (junctionIds.has(id) && wireIU !== undefined && baseDot > 1) {
// sch_junction.cpp: diameter 1 ("None") skips the wire-width clamp.
junctions.set(id, Math.max(baseDot, Math.round(wireIU * 1.7)));
}
}
}
if (lines.size === 0 && junctions.size === 0) return undefined;
return { lines, junctions };
}