811 lines
31 KiB
TypeScript
811 lines
31 KiB
TypeScript
// 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.
|
|
/**
|
|
* Project-file persistence for the Schematic Setup dialog. Counterparts:
|
|
* `eeschema/schematic_settings.cpp` (SCHEMATIC_SETTINGS, the `schematic.*`
|
|
* namespace of `.kicad_pro`), `eeschema/erc/erc_settings.cpp` (ERC_SETTINGS,
|
|
* `erc.*`), `common/project/net_settings.cpp` (NET_SETTINGS, `net_settings.*`)
|
|
* and `common/project/project_file.cpp` (`text_variables`).
|
|
*
|
|
* `readSchematicSetup` hydrates a SchematicSetup from the project's raw
|
|
* `.kicad_pro` (missing keys fall back to KiCad's defaults);
|
|
* `writeSchematicSetupText` merges a SchematicSetup back into the `.kicad_pro`
|
|
* JSON, preserving every key it does not own, so a KiCad-authored project
|
|
* round-trips with only the edited settings changed. `page_layout_descr_file`
|
|
* in particular belongs to projectSheet.ts and is never touched here.
|
|
*
|
|
* Bus aliases persist at `schematic.bus_aliases` (an alias -> members object;
|
|
* current KiCad stores them here, the schematic writer no longer emits
|
|
* `bus_alias` nodes, only the parser still accepts legacy ones). Embedded
|
|
* files stay `.kicad_sch` data (the `embedded_files` section, zstd-compressed
|
|
* blobs preserved losslessly through the AST), listed read-only for now.
|
|
*
|
|
* Units follow the file format: PARAM_SCALED sizes are stored in mils
|
|
* (scale `1 / schIUScale.IU_PER_MILS`), net-class PCB fields in mm and
|
|
* wire/bus widths in mils, the panel grids type file units directly.
|
|
*/
|
|
|
|
import type { ErcCode, ErcSeverityLevel, PinError } from './index.js';
|
|
// Straight from the declaring module, not ./index.js: cross-probing.ts imports
|
|
// this file, and through the barrel's cycle ERC_ITEMS can still be undefined at
|
|
// load time - so severityKeys() reads it on first use.
|
|
import { ERC_ITEMS, PIN_TYPES } from './erc/erc_settings.js';
|
|
import type { RawFile } from '@ziroeda/common';
|
|
import {
|
|
LINE_STYLES,
|
|
UNIT_NOTATION_IDS,
|
|
defaultSchematicSetup,
|
|
type BomFmtPreset,
|
|
type BomPreset,
|
|
type FieldTemplate,
|
|
type NetClass,
|
|
type NetClassAssignment,
|
|
type SchematicSetup,
|
|
type TextVar,
|
|
} from './schematic_settings.js';
|
|
|
|
const PRO_RE = /\.kicad_pro$/i;
|
|
|
|
/** Path basename (project references store a bare file name). */
|
|
function basename(p: string): string {
|
|
return p.split(/[\\/]/).pop() ?? p;
|
|
}
|
|
|
|
/** The project's `.kicad_pro`: prefer `proBase + '.kicad_pro'` when a folder
|
|
* holds several projects (same pinning rule as SchematicEditor's loadProject),
|
|
* else the first one found. */
|
|
export function findProjectPro(files: readonly RawFile[], proBase?: string): RawFile | undefined {
|
|
const want = proBase ? `${proBase}.kicad_pro`.toLowerCase() : null;
|
|
if (want) {
|
|
const exact = files.find((f) => f.name.toLowerCase() === want);
|
|
if (exact) return exact;
|
|
const pinned = files.find(
|
|
(f) => PRO_RE.test(f.name) && basename(f.name).toLowerCase() === want,
|
|
);
|
|
if (pinned) return pinned;
|
|
}
|
|
return files.find((f) => PRO_RE.test(f.name));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// JSON plumbing.
|
|
|
|
type Json = Record<string, unknown>;
|
|
|
|
function isObj(v: unknown): v is Json {
|
|
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
}
|
|
|
|
/** Dotted-path lookup (`'schematic.drawing.default_text_size'`). */
|
|
function getPath(root: unknown, path: string): unknown {
|
|
let cur: unknown = root;
|
|
for (const key of path.split('.')) {
|
|
if (!isObj(cur)) return undefined;
|
|
cur = cur[key];
|
|
}
|
|
return cur;
|
|
}
|
|
|
|
/** Dotted-path set, creating intermediate objects but never clobbering
|
|
* sibling keys (the merge behind "preserve everything we don't own"). */
|
|
function setPath(root: Json, path: string, value: unknown): void {
|
|
const keys = path.split('.');
|
|
let cur = root;
|
|
for (const key of keys.slice(0, -1)) {
|
|
if (!isObj(cur[key])) cur[key] = {};
|
|
cur = cur[key] as Json;
|
|
}
|
|
cur[keys.at(-1)!] = value;
|
|
}
|
|
|
|
function num(v: unknown, dflt: number): number {
|
|
return typeof v === 'number' && Number.isFinite(v) ? v : dflt;
|
|
}
|
|
function bool(v: unknown, dflt: boolean): boolean {
|
|
return typeof v === 'boolean' ? v : dflt;
|
|
}
|
|
function str(v: unknown, dflt: string): string {
|
|
return typeof v === 'string' ? v : dflt;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Value tables.
|
|
|
|
/** The rules `erc.rule_severities` stores, and their keys, every code in
|
|
* ERC_ITEMS *is* its settings key (ERC_ITEM::GetSettingsKey), and ERC_ITEMS is
|
|
* ERC_ITEM::GetItemsWithSeverities, so the file round-trips exactly what KiCad
|
|
* writes. The internal types (`duplicate_pins`, `pin_to_pin_error`) sit past
|
|
* `heading_internal` upstream and are deliberately not serialized. */
|
|
let s_severityKeys: readonly (readonly [ErcCode, string])[] | null = null;
|
|
|
|
/** Read on first use, not at load: the eeschema module graph has cycles through this file, and
|
|
* any of them can reach here before erc_settings.ts has run. */
|
|
function severityKeys(): readonly (readonly [ErcCode, string])[] {
|
|
s_severityKeys ??= ERC_ITEMS.map((item) => [item.code, item.code] as const);
|
|
return s_severityKeys;
|
|
}
|
|
|
|
/** KiCad's "auto" operating-point range sentinels ('~V' / '~A'); the panel
|
|
* shows them as 'Auto'. */
|
|
function opoRangeToFile(ui: string, auto: string): string {
|
|
return ui === 'Auto' ? auto : ui;
|
|
}
|
|
function opoRangeFromFile(file: unknown, auto: string): string {
|
|
const v = str(file, auto);
|
|
return v === auto ? 'Auto' : v;
|
|
}
|
|
|
|
/** COLOR4D's unset marker, what GetSchematicColor/GetPcbColor( true ) emit
|
|
* for COLOR4D::UNSPECIFIED. */
|
|
const KICAD_COLOR_UNSET = 'rgba(0, 0, 0, 0.000)';
|
|
|
|
/** Panel color ('' or '#rrggbb') -> KiCad COLOR4D string. */
|
|
function cssColorToKicad(css: string): string {
|
|
const m = /^#([0-9a-f]{6})$/i.exec(css);
|
|
if (!m) return KICAD_COLOR_UNSET;
|
|
const n = parseInt(m[1]!, 16);
|
|
return `rgb(${(n >> 16) & 255}, ${(n >> 8) & 255}, ${n & 255})`;
|
|
}
|
|
|
|
/** KiCad COLOR4D string -> panel color ('' = unset / transparent). */
|
|
function kicadColorToCss(v: unknown): string {
|
|
if (typeof v !== 'string') return '';
|
|
const m = /^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+)\s*)?\)$/.exec(v.trim());
|
|
if (!m) return /^#[0-9a-f]{6}$/i.test(v.trim()) ? v.trim().toLowerCase() : '';
|
|
if (m[4] !== undefined && parseFloat(m[4]) === 0) return '';
|
|
const hex = (x: string): string => Math.min(255, Number(x)).toString(16).padStart(2, '0');
|
|
return `#${hex(m[1]!)}${hex(m[2]!)}${hex(m[3]!)}`;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Read.
|
|
|
|
/** Read the SchematicSetup from a project's `.kicad_pro` text. Missing or
|
|
* malformed keys fall back to KiCad's defaults (defaultSchematicSetup). */
|
|
export function readSchematicSetupText(proText: string): SchematicSetup {
|
|
const s = defaultSchematicSetup();
|
|
let j: unknown;
|
|
try {
|
|
j = JSON.parse(proText);
|
|
} catch {
|
|
return s;
|
|
}
|
|
if (!isObj(j)) return s;
|
|
|
|
// schematic.drawing.* + schematic.*, SCHEMATIC_SETTINGS.
|
|
const f = s.formatting;
|
|
f.defaultLineWidthMils = num(
|
|
getPath(j, 'schematic.drawing.default_line_thickness'),
|
|
f.defaultLineWidthMils,
|
|
);
|
|
f.defaultTextSizeMils = num(
|
|
getPath(j, 'schematic.drawing.default_text_size'),
|
|
f.defaultTextSizeMils,
|
|
);
|
|
// The file stores raw ratios (0.15); the panel shows percent (15).
|
|
f.labelOffsetRatio =
|
|
num(getPath(j, 'schematic.drawing.text_offset_ratio'), f.labelOffsetRatio / 100) * 100;
|
|
f.labelSizeRatio =
|
|
num(getPath(j, 'schematic.drawing.label_size_ratio'), f.labelSizeRatio / 100) * 100;
|
|
f.overbarOffsetRatio = num(
|
|
getPath(j, 'schematic.drawing.overbar_offset_ratio'),
|
|
f.overbarOffsetRatio,
|
|
);
|
|
f.pinSymbolSizeMils = num(getPath(j, 'schematic.drawing.pin_symbol_size'), f.pinSymbolSizeMils);
|
|
f.junctionDotChoice = num(
|
|
getPath(j, 'schematic.drawing.junction_size_choice'),
|
|
f.junctionDotChoice,
|
|
);
|
|
f.hopOverChoice = num(getPath(j, 'schematic.drawing.hop_over_size_choice'), f.hopOverChoice);
|
|
f.connectionGridMils = num(getPath(j, 'schematic.connection_grid_size'), f.connectionGridMils);
|
|
f.intersheetRefsShow = bool(
|
|
getPath(j, 'schematic.drawing.intersheets_ref_show'),
|
|
f.intersheetRefsShow,
|
|
);
|
|
f.intersheetRefsOwnPage = bool(
|
|
getPath(j, 'schematic.drawing.intersheets_ref_own_page'),
|
|
f.intersheetRefsOwnPage,
|
|
);
|
|
f.intersheetRefsAbbreviated = bool(
|
|
getPath(j, 'schematic.drawing.intersheets_ref_short'),
|
|
f.intersheetRefsAbbreviated,
|
|
);
|
|
f.intersheetRefsPrefix = str(
|
|
getPath(j, 'schematic.drawing.intersheets_ref_prefix'),
|
|
f.intersheetRefsPrefix,
|
|
);
|
|
f.intersheetRefsSuffix = str(
|
|
getPath(j, 'schematic.drawing.intersheets_ref_suffix'),
|
|
f.intersheetRefsSuffix,
|
|
);
|
|
f.dashLengthRatio = num(
|
|
getPath(j, 'schematic.drawing.dashed_lines_dash_length_ratio'),
|
|
f.dashLengthRatio,
|
|
);
|
|
f.gapLengthRatio = num(
|
|
getPath(j, 'schematic.drawing.dashed_lines_gap_length_ratio'),
|
|
f.gapLengthRatio,
|
|
);
|
|
f.opoVPrecision = num(
|
|
getPath(j, 'schematic.drawing.operating_point_overlay_v_precision'),
|
|
f.opoVPrecision,
|
|
);
|
|
f.opoVRange = opoRangeFromFile(
|
|
getPath(j, 'schematic.drawing.operating_point_overlay_v_range'),
|
|
'~V',
|
|
);
|
|
f.opoIPrecision = num(
|
|
getPath(j, 'schematic.drawing.operating_point_overlay_i_precision'),
|
|
f.opoIPrecision,
|
|
);
|
|
f.opoIRange = opoRangeFromFile(
|
|
getPath(j, 'schematic.drawing.operating_point_overlay_i_range'),
|
|
'~A',
|
|
);
|
|
|
|
// Annotation (subpart notation + annotation.* + annotate_start_num).
|
|
const sep = num(getPath(j, 'schematic.subpart_id_separator'), 0);
|
|
const first = num(getPath(j, 'schematic.subpart_first_id'), 65);
|
|
const notation = UNIT_NOTATION_IDS.findIndex(([a, b]) => a === sep && b === first);
|
|
s.annotation.symbolUnitNotation = notation === -1 ? 0 : notation;
|
|
s.annotation.sortOrder = num(getPath(j, 'schematic.annotation.sort_order'), 0) === 1 ? 'y' : 'x';
|
|
const method = num(getPath(j, 'schematic.annotation.method'), 0);
|
|
s.annotation.numbering = method === 1 ? 'sheetX100' : method === 2 ? 'sheetX1000' : 'firstFree';
|
|
s.annotation.firstFreeAfter = num(
|
|
getPath(j, 'schematic.annotate_start_num'),
|
|
s.annotation.firstFreeAfter,
|
|
);
|
|
s.annotation.allowReuse = bool(
|
|
getPath(j, 'schematic.reuse_designators'),
|
|
s.annotation.allowReuse,
|
|
);
|
|
|
|
// Field name templates (schematic.drawing.field_names).
|
|
const fieldNames = getPath(j, 'schematic.drawing.field_names');
|
|
if (Array.isArray(fieldNames)) {
|
|
const templates: FieldTemplate[] = [];
|
|
for (const e of fieldNames) {
|
|
// Upstream skips entries missing any of the three keys.
|
|
if (
|
|
isObj(e) &&
|
|
typeof e.name === 'string' &&
|
|
typeof e.visible === 'boolean' &&
|
|
typeof e.url === 'boolean'
|
|
)
|
|
templates.push({ name: e.name, visible: e.visible, url: e.url });
|
|
}
|
|
s.fieldTemplates = templates;
|
|
}
|
|
|
|
// schematic.bom_presets / bom_fmt_presets (bom_settings.cpp from_json):
|
|
// entries missing a required key are skipped, like upstream's per-item
|
|
// parse; include_excluded_from_bom defaults false (absent before 8.0).
|
|
const bomArr = getPath(j, 'schematic.bom_presets');
|
|
if (Array.isArray(bomArr)) {
|
|
const presets: BomPreset[] = [];
|
|
for (const e of bomArr) {
|
|
if (!isObj(e) || typeof e.name !== 'string' || !Array.isArray(e.fields_ordered)) continue;
|
|
presets.push({
|
|
name: e.name,
|
|
fieldsOrdered: e.fields_ordered
|
|
.filter((f): f is Json => isObj(f))
|
|
.map((f) => ({
|
|
name: str(f.name, ''),
|
|
label: str(f.label, ''),
|
|
show: bool(f.show, false),
|
|
groupBy: bool(f.group_by, false),
|
|
})),
|
|
sortField: str(e.sort_field, 'Reference'),
|
|
sortAsc: bool(e.sort_asc, true),
|
|
filterString: str(e.filter_string, ''),
|
|
groupSymbols: bool(e.group_symbols, false),
|
|
excludeDNP: bool(e.exclude_dnp, false),
|
|
includeExcludedFromBOM: bool(e.include_excluded_from_bom, false),
|
|
});
|
|
}
|
|
s.bomPresets.presets = presets;
|
|
}
|
|
const fmtArr = getPath(j, 'schematic.bom_fmt_presets');
|
|
if (Array.isArray(fmtArr)) {
|
|
const fmt: BomFmtPreset[] = [];
|
|
for (const e of fmtArr) {
|
|
if (!isObj(e) || typeof e.name !== 'string') continue;
|
|
fmt.push({
|
|
name: e.name,
|
|
fieldDelimiter: str(e.field_delimiter, ','),
|
|
stringDelimiter: str(e.string_delimiter, '"'),
|
|
refDelimiter: str(e.ref_delimiter, ','),
|
|
refRangeDelimiter: str(e.ref_range_delimiter, ''),
|
|
keepTabs: bool(e.keep_tabs, false),
|
|
keepLineBreaks: bool(e.keep_line_breaks, false),
|
|
});
|
|
}
|
|
s.bomPresets.fmtPresets = fmt;
|
|
}
|
|
// schematic.bom_settings / bom_fmt_settings, the fields table's current view
|
|
// and output format (SCHEMATIC_SETTINGS m_BomSettings / m_BomFmtSettings).
|
|
const bomCur = getPath(j, 'schematic.bom_settings');
|
|
if (isObj(bomCur) && Array.isArray(bomCur.fields_ordered)) {
|
|
s.bomPresets.settings = {
|
|
name: str(bomCur.name, ''),
|
|
fieldsOrdered: bomCur.fields_ordered
|
|
.filter((f): f is Json => isObj(f))
|
|
.map((f) => ({
|
|
name: str(f.name, ''),
|
|
label: str(f.label, ''),
|
|
show: bool(f.show, false),
|
|
groupBy: bool(f.group_by, false),
|
|
})),
|
|
sortField: str(bomCur.sort_field, 'Reference'),
|
|
sortAsc: bool(bomCur.sort_asc, true),
|
|
filterString: str(bomCur.filter_string, ''),
|
|
groupSymbols: bool(bomCur.group_symbols, false),
|
|
excludeDNP: bool(bomCur.exclude_dnp, false),
|
|
includeExcludedFromBOM: bool(bomCur.include_excluded_from_bom, false),
|
|
};
|
|
}
|
|
const fmtCur = getPath(j, 'schematic.bom_fmt_settings');
|
|
if (isObj(fmtCur)) {
|
|
s.bomPresets.fmtSettings = {
|
|
name: str(fmtCur.name, ''),
|
|
fieldDelimiter: str(fmtCur.field_delimiter, ','),
|
|
stringDelimiter: str(fmtCur.string_delimiter, '"'),
|
|
refDelimiter: str(fmtCur.ref_delimiter, ','),
|
|
refRangeDelimiter: str(fmtCur.ref_range_delimiter, ''),
|
|
keepTabs: bool(fmtCur.keep_tabs, false),
|
|
keepLineBreaks: bool(fmtCur.keep_line_breaks, false),
|
|
};
|
|
}
|
|
const bomFile = getPath(j, 'schematic.bom_export_filename');
|
|
if (typeof bomFile === 'string') s.bomPresets.exportFileName = bomFile;
|
|
|
|
// erc.*, ERC_SETTINGS.
|
|
const sev = getPath(j, 'erc.rule_severities');
|
|
if (isObj(sev)) {
|
|
for (const [code, key] of severityKeys()) {
|
|
const v = sev[key];
|
|
if (v === 'error' || v === 'warning' || v === 'ignore')
|
|
s.erc.severities[code] = v as ErcSeverityLevel;
|
|
}
|
|
}
|
|
const pinMap = getPath(j, 'erc.pin_map');
|
|
if (
|
|
Array.isArray(pinMap) &&
|
|
pinMap.length === PIN_TYPES.length &&
|
|
pinMap.every((row) => Array.isArray(row) && row.length === PIN_TYPES.length)
|
|
) {
|
|
s.erc.pinMap = (pinMap as unknown[][]).map((row) =>
|
|
row.map((v) => (v === 1 || v === 2 ? v : 0) as PinError),
|
|
);
|
|
}
|
|
const excl = getPath(j, 'erc.erc_exclusions');
|
|
if (Array.isArray(excl)) {
|
|
s.ercExclusions = excl
|
|
.map((e) => (Array.isArray(e) ? str(e[0], '') : str(e, '')))
|
|
.filter(Boolean);
|
|
s.ercExclusionComments = {};
|
|
for (const e of excl) {
|
|
if (Array.isArray(e) && typeof e[0] === 'string' && str(e[1], ''))
|
|
s.ercExclusionComments[e[0]] = str(e[1], '');
|
|
}
|
|
}
|
|
|
|
// net_settings.*, NET_SETTINGS.
|
|
const classes = getPath(j, 'net_settings.classes');
|
|
if (Array.isArray(classes)) {
|
|
const numStr = (v: unknown): string =>
|
|
typeof v === 'number' && Number.isFinite(v) ? String(v) : '';
|
|
const read = classes
|
|
.filter((e): e is Json => isObj(e) && typeof e.name === 'string')
|
|
.map((e) => ({
|
|
priority: num(e.priority, Number.MAX_SAFE_INTEGER),
|
|
nc: {
|
|
name: e.name as string,
|
|
clearance: numStr(e.clearance),
|
|
trackWidth: numStr(e.track_width),
|
|
viaSize: numStr(e.via_diameter),
|
|
viaHole: numStr(e.via_drill),
|
|
uviaSize: numStr(e.microvia_diameter),
|
|
uviaHole: numStr(e.microvia_drill),
|
|
dpWidth: numStr(e.diff_pair_width),
|
|
dpGap: numStr(e.diff_pair_gap),
|
|
dpViaGap: numStr(e.diff_pair_via_gap),
|
|
tuningProfile: str(e.tuning_profile, ''),
|
|
pcbColor: kicadColorToCss(e.pcb_color),
|
|
wireThickness: numStr(e.wire_width),
|
|
busThickness: numStr(e.bus_width),
|
|
color: kicadColorToCss(e.schematic_color),
|
|
lineStyle: LINE_STYLES[num(e.line_style, 0)] ?? 'Solid',
|
|
} satisfies NetClass,
|
|
}));
|
|
// Default pinned first (the panel keeps it at row 0), rest by priority.
|
|
const dflt = read.find((r) => r.nc.name === 'Default');
|
|
const rest = read.filter((r) => r !== dflt).sort((a, b) => a.priority - b.priority);
|
|
if (dflt || rest.length)
|
|
s.netClasses.classes = [dflt?.nc ?? s.netClasses.classes[0]!, ...rest.map((r) => r.nc)];
|
|
}
|
|
const patterns = getPath(j, 'net_settings.netclass_patterns');
|
|
if (Array.isArray(patterns)) {
|
|
s.netClasses.assignments = patterns
|
|
.filter((e): e is Json => isObj(e))
|
|
.map((e) => ({ pattern: str(e.pattern, ''), netClass: str(e.netclass, '') }))
|
|
.filter((a): a is NetClassAssignment => Boolean(a.pattern || a.netClass));
|
|
}
|
|
// net_settings.net_colors: a net NAME to colour map, the per-net overrides
|
|
// the Nets tab shows (net_settings.cpp:224-249). Nothing read it, so a board
|
|
// whose project assigns colours opened with every net unspecified.
|
|
const netColors = getPath(j, 'net_settings.net_colors');
|
|
if (isObj(netColors)) {
|
|
const out: Record<string, string> = {};
|
|
for (const [name, v] of Object.entries(netColors)) {
|
|
const css = kicadColorToCss(v);
|
|
if (css) out[name] = css;
|
|
}
|
|
s.netClasses.netColors = out;
|
|
}
|
|
|
|
// text_variables (project-file top level).
|
|
const vars = getPath(j, 'text_variables');
|
|
if (isObj(vars)) {
|
|
s.textVars = Object.entries(vars)
|
|
.filter((e): e is [string, string] => typeof e[1] === 'string')
|
|
.map(([name, value]): TextVar => ({ name, value }));
|
|
}
|
|
|
|
// schematic.used_designators (REFDES_TRACKER::Serialize output).
|
|
s.usedDesignators = str(getPath(j, 'schematic.used_designators'), '');
|
|
|
|
// schematic.bus_aliases (project_file.cpp): alias -> member list.
|
|
const aliases = getPath(j, 'schematic.bus_aliases');
|
|
if (isObj(aliases)) {
|
|
s.busAliases = Object.entries(aliases)
|
|
.filter((e): e is [string, unknown[]] => Array.isArray(e[1]))
|
|
.map(([name, members]) => ({
|
|
name,
|
|
members: members.filter((m): m is string => typeof m === 'string'),
|
|
}));
|
|
}
|
|
|
|
return s;
|
|
}
|
|
|
|
/** Read the SchematicSetup from the project's raw files (missing/corrupt
|
|
* `.kicad_pro` -> defaults). */
|
|
export function readSchematicSetup(files: readonly RawFile[], proBase?: string): SchematicSetup {
|
|
const pro = findProjectPro(files, proBase);
|
|
return pro ? readSchematicSetupText(pro.text) : defaultSchematicSetup();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// cvpcb.equivalence_files.
|
|
//
|
|
// `PROJECT_FILE::m_EquivalenceFiles` (project_file.h:207), registered as
|
|
// `PARAM_PATH_LIST( "cvpcb.equivalence_files", … )`
|
|
// (common/project/project_file.cpp:72-73) — the footprint association files
|
|
// the Assign Footprints window's automatic association reads, in the order
|
|
// Manage Footprint Association Files put them in.
|
|
//
|
|
// It lives in the PROJECT file, not in `CVPCB_SETTINGS` and not in
|
|
// `prefs/settings.ts`: upstream reads it as `Prj().GetProjectFile()` from both
|
|
// ends (`auto_associate.cpp:87`, `dialog_config_equfiles.cpp:56` and `:108`),
|
|
// and it is a property of the design rather than of the user. Our project
|
|
// template already writes the key (`home/new_project.ts:76`).
|
|
|
|
/** The `cvpcb.equivalence_files` list of a `.kicad_pro`, in file order. */
|
|
export function readEquivalenceFilesText(proText: string): string[] {
|
|
let j: unknown;
|
|
try {
|
|
j = JSON.parse(proText);
|
|
} catch {
|
|
return [];
|
|
}
|
|
const arr = getPath(j, 'cvpcb.equivalence_files');
|
|
if (!Array.isArray(arr)) return [];
|
|
return arr.filter((e): e is string => typeof e === 'string');
|
|
}
|
|
|
|
/** The same, from the project's raw files. */
|
|
export function readEquivalenceFiles(files: readonly RawFile[], proBase?: string): string[] {
|
|
const pro = findProjectPro(files, proBase);
|
|
return pro ? readEquivalenceFilesText(pro.text) : [];
|
|
}
|
|
|
|
/**
|
|
* `proText` with `cvpcb.equivalence_files` replaced, every other key
|
|
* preserved; null when the JSON cannot be parsed.
|
|
*
|
|
* Backslashes become forward slashes because `PARAM_PATH_LIST::Store` writes
|
|
* `toFileFormat`, which is exactly that substitution (parameters.h:207-212) —
|
|
* the same normalization `DIALOG_CONFIG_EQUFILES::OnAddFiles` applies to a
|
|
* newly added path ("Use unix separators only.").
|
|
*/
|
|
export function writeEquivalenceFilesText(
|
|
proText: string,
|
|
equFiles: readonly string[],
|
|
): string | null {
|
|
let j: unknown;
|
|
try {
|
|
j = JSON.parse(proText);
|
|
} catch {
|
|
return null;
|
|
}
|
|
if (!isObj(j)) return null;
|
|
setPath(
|
|
j,
|
|
'cvpcb.equivalence_files',
|
|
equFiles.map((f) => f.replace(/\\/g, '/')),
|
|
);
|
|
return `${JSON.stringify(j, null, 2)}\n`;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Write.
|
|
|
|
/** Net-class keys owned by the panel grid: cleared when the cell is blank,
|
|
* rewritten otherwise. Anything else on a class object is preserved.
|
|
*
|
|
* `diff_pair_via_gap` is READ into `NetClass.dpViaGap` and deliberately not
|
|
* listed here. KiCad's grid has no column for it either
|
|
* (`panel_setup_netclasses.cpp:63-64`), so the panel does not own it, and a
|
|
* key the panel does not own must not be cleared when its blank cell says
|
|
* nothing — that would delete a value only the file can carry, on the first
|
|
* OK. */
|
|
const OPTIONAL_CLASS_KEYS: readonly (readonly [string, keyof NetClass])[] = [
|
|
['clearance', 'clearance'],
|
|
['track_width', 'trackWidth'],
|
|
['via_diameter', 'viaSize'],
|
|
['via_drill', 'viaHole'],
|
|
['microvia_diameter', 'uviaSize'],
|
|
['microvia_drill', 'uviaHole'],
|
|
['diff_pair_width', 'dpWidth'],
|
|
['diff_pair_gap', 'dpGap'],
|
|
['wire_width', 'wireThickness'],
|
|
['bus_width', 'busThickness'],
|
|
];
|
|
|
|
/** INT_MAX, NET_SETTINGS gives the Default class the lowest priority. */
|
|
const DEFAULT_CLASS_PRIORITY = 2147483647;
|
|
|
|
/** Return `proText` with the SchematicSetup merged in (all unrelated keys
|
|
* preserved), or null when the JSON cannot be parsed. */
|
|
export function writeSchematicSetupText(proText: string, s: SchematicSetup): string | null {
|
|
let j: unknown;
|
|
try {
|
|
j = JSON.parse(proText);
|
|
} catch {
|
|
return null;
|
|
}
|
|
if (!isObj(j)) return null;
|
|
|
|
// schematic.drawing.* + schematic.*, SCHEMATIC_SETTINGS.
|
|
const f = s.formatting;
|
|
setPath(j, 'schematic.drawing.default_line_thickness', f.defaultLineWidthMils);
|
|
setPath(j, 'schematic.drawing.default_text_size', f.defaultTextSizeMils);
|
|
setPath(j, 'schematic.drawing.text_offset_ratio', f.labelOffsetRatio / 100);
|
|
setPath(j, 'schematic.drawing.label_size_ratio', f.labelSizeRatio / 100);
|
|
setPath(j, 'schematic.drawing.overbar_offset_ratio', f.overbarOffsetRatio);
|
|
setPath(j, 'schematic.drawing.pin_symbol_size', f.pinSymbolSizeMils);
|
|
setPath(j, 'schematic.drawing.junction_size_choice', f.junctionDotChoice);
|
|
setPath(j, 'schematic.drawing.hop_over_size_choice', f.hopOverChoice);
|
|
setPath(j, 'schematic.connection_grid_size', f.connectionGridMils);
|
|
setPath(j, 'schematic.drawing.intersheets_ref_show', f.intersheetRefsShow);
|
|
setPath(j, 'schematic.drawing.intersheets_ref_own_page', f.intersheetRefsOwnPage);
|
|
setPath(j, 'schematic.drawing.intersheets_ref_short', f.intersheetRefsAbbreviated);
|
|
setPath(j, 'schematic.drawing.intersheets_ref_prefix', f.intersheetRefsPrefix);
|
|
setPath(j, 'schematic.drawing.intersheets_ref_suffix', f.intersheetRefsSuffix);
|
|
setPath(j, 'schematic.drawing.dashed_lines_dash_length_ratio', f.dashLengthRatio);
|
|
setPath(j, 'schematic.drawing.dashed_lines_gap_length_ratio', f.gapLengthRatio);
|
|
setPath(j, 'schematic.drawing.operating_point_overlay_v_precision', f.opoVPrecision);
|
|
setPath(
|
|
j,
|
|
'schematic.drawing.operating_point_overlay_v_range',
|
|
opoRangeToFile(f.opoVRange, '~V'),
|
|
);
|
|
setPath(j, 'schematic.drawing.operating_point_overlay_i_precision', f.opoIPrecision);
|
|
setPath(
|
|
j,
|
|
'schematic.drawing.operating_point_overlay_i_range',
|
|
opoRangeToFile(f.opoIRange, '~A'),
|
|
);
|
|
|
|
// Annotation.
|
|
const [sep, first] = UNIT_NOTATION_IDS[s.annotation.symbolUnitNotation] ?? UNIT_NOTATION_IDS[0]!;
|
|
setPath(j, 'schematic.subpart_id_separator', sep);
|
|
setPath(j, 'schematic.subpart_first_id', first);
|
|
setPath(j, 'schematic.annotation.sort_order', s.annotation.sortOrder === 'y' ? 1 : 0);
|
|
setPath(
|
|
j,
|
|
'schematic.annotation.method',
|
|
s.annotation.numbering === 'sheetX100' ? 1 : s.annotation.numbering === 'sheetX1000' ? 2 : 0,
|
|
);
|
|
setPath(j, 'schematic.annotate_start_num', s.annotation.firstFreeAfter);
|
|
setPath(j, 'schematic.reuse_designators', s.annotation.allowReuse);
|
|
|
|
// Field name templates.
|
|
setPath(
|
|
j,
|
|
'schematic.drawing.field_names',
|
|
s.fieldTemplates.map((t) => ({ name: t.name, visible: t.visible, url: t.url })),
|
|
);
|
|
|
|
// schematic.bom_presets / bom_fmt_presets (bom_settings.cpp to_json).
|
|
// Read-only built-ins are never persisted, like upstream; a preset whose
|
|
// name existed before keeps any unowned keys via the old-object merge.
|
|
const oldPresetByName = (path: string): Map<string, Json> => {
|
|
const arr = getPath(j, path);
|
|
const map = new Map<string, Json>();
|
|
if (Array.isArray(arr)) {
|
|
for (const e of arr) if (isObj(e) && typeof e.name === 'string') map.set(e.name, e);
|
|
}
|
|
return map;
|
|
};
|
|
const oldBom = oldPresetByName('schematic.bom_presets');
|
|
setPath(
|
|
j,
|
|
'schematic.bom_presets',
|
|
s.bomPresets.presets
|
|
.filter((p) => !p.readOnly)
|
|
.map((p) => {
|
|
const out: Json = { ...(oldBom.get(p.name) ?? {}) };
|
|
out.name = p.name;
|
|
out.sort_field = p.sortField;
|
|
out.sort_asc = p.sortAsc;
|
|
out.filter_string = p.filterString;
|
|
out.group_symbols = p.groupSymbols;
|
|
out.exclude_dnp = p.excludeDNP;
|
|
out.include_excluded_from_bom = p.includeExcludedFromBOM;
|
|
// Upstream only writes fields_ordered when non-empty.
|
|
if (p.fieldsOrdered.length > 0)
|
|
out.fields_ordered = p.fieldsOrdered.map((f) => ({
|
|
name: f.name,
|
|
label: f.label,
|
|
show: f.show,
|
|
group_by: f.groupBy,
|
|
}));
|
|
else delete out.fields_ordered;
|
|
return out;
|
|
}),
|
|
);
|
|
const oldFmt = oldPresetByName('schematic.bom_fmt_presets');
|
|
setPath(
|
|
j,
|
|
'schematic.bom_fmt_presets',
|
|
s.bomPresets.fmtPresets
|
|
.filter((p) => !p.readOnly)
|
|
.map((p) => ({
|
|
...(oldFmt.get(p.name) ?? {}),
|
|
name: p.name,
|
|
field_delimiter: p.fieldDelimiter,
|
|
string_delimiter: p.stringDelimiter,
|
|
ref_delimiter: p.refDelimiter,
|
|
ref_range_delimiter: p.refRangeDelimiter,
|
|
keep_tabs: p.keepTabs,
|
|
keep_line_breaks: p.keepLineBreaks,
|
|
})),
|
|
);
|
|
// The current view/format and the export file name (written whether or not
|
|
// they match a saved preset, like SCHEMATIC_SETTINGS).
|
|
const cur = s.bomPresets.settings;
|
|
const oldCur = getPath(j, 'schematic.bom_settings');
|
|
const oldCurFmt = getPath(j, 'schematic.bom_fmt_settings');
|
|
setPath(j, 'schematic.bom_settings', {
|
|
...(isObj(oldCur) ? oldCur : {}),
|
|
name: cur.name,
|
|
fields_ordered: cur.fieldsOrdered.map((f) => ({
|
|
name: f.name,
|
|
label: f.label,
|
|
show: f.show,
|
|
group_by: f.groupBy,
|
|
})),
|
|
sort_field: cur.sortField,
|
|
sort_asc: cur.sortAsc,
|
|
filter_string: cur.filterString,
|
|
group_symbols: cur.groupSymbols,
|
|
exclude_dnp: cur.excludeDNP,
|
|
include_excluded_from_bom: cur.includeExcludedFromBOM,
|
|
});
|
|
const curFmt = s.bomPresets.fmtSettings;
|
|
setPath(j, 'schematic.bom_fmt_settings', {
|
|
...(isObj(oldCurFmt) ? oldCurFmt : {}),
|
|
name: curFmt.name,
|
|
field_delimiter: curFmt.fieldDelimiter,
|
|
string_delimiter: curFmt.stringDelimiter,
|
|
ref_delimiter: curFmt.refDelimiter,
|
|
ref_range_delimiter: curFmt.refRangeDelimiter,
|
|
keep_tabs: curFmt.keepTabs,
|
|
keep_line_breaks: curFmt.keepLineBreaks,
|
|
});
|
|
setPath(j, 'schematic.bom_export_filename', s.bomPresets.exportFileName);
|
|
|
|
// erc.rule_severities: overwrite our keys, keep unknown rules untouched.
|
|
const oldSev = getPath(j, 'erc.rule_severities');
|
|
const sevOut: Json = isObj(oldSev) ? { ...oldSev } : {};
|
|
for (const [code, key] of severityKeys()) sevOut[key] = s.erc.severities[code];
|
|
setPath(j, 'erc.rule_severities', sevOut);
|
|
|
|
// erc.pin_map (12x12) and erc.erc_exclusions ([signature, comment] pairs;
|
|
// comments of surviving exclusions are preserved).
|
|
setPath(
|
|
j,
|
|
'erc.pin_map',
|
|
s.erc.pinMap.map((row) => [...row]),
|
|
);
|
|
const oldExcl = getPath(j, 'erc.erc_exclusions');
|
|
const comments = new Map<string, string>();
|
|
if (Array.isArray(oldExcl)) {
|
|
for (const e of oldExcl) {
|
|
if (Array.isArray(e) && typeof e[0] === 'string') comments.set(e[0], str(e[1], ''));
|
|
}
|
|
}
|
|
setPath(
|
|
j,
|
|
'erc.erc_exclusions',
|
|
s.ercExclusions.map((sig) => [sig, s.ercExclusionComments[sig] ?? comments.get(sig) ?? '']),
|
|
);
|
|
|
|
// net_settings.classes: Default first at INT_MAX priority, the rest in panel
|
|
// order. A class that existed before keeps its unowned keys.
|
|
const oldClasses = getPath(j, 'net_settings.classes');
|
|
const oldByName = new Map<string, Json>();
|
|
if (Array.isArray(oldClasses)) {
|
|
for (const e of oldClasses) {
|
|
if (isObj(e) && typeof e.name === 'string') oldByName.set(e.name, e);
|
|
}
|
|
}
|
|
const writeClass = (c: NetClass, priority: number): Json => {
|
|
const out: Json = { ...(oldByName.get(c.name) ?? {}) };
|
|
out.name = c.name;
|
|
out.priority = priority;
|
|
out.schematic_color = cssColorToKicad(c.color);
|
|
out.pcb_color = cssColorToKicad(c.pcbColor);
|
|
out.tuning_profile = c.tuningProfile;
|
|
out.line_style = Math.max(0, LINE_STYLES.indexOf(c.lineStyle));
|
|
for (const [key, field] of OPTIONAL_CLASS_KEYS) {
|
|
const v = parseFloat(c[field]);
|
|
if (c[field].trim() === '' || !Number.isFinite(v)) delete out[key];
|
|
else out[key] = v;
|
|
}
|
|
return out;
|
|
};
|
|
setPath(
|
|
j,
|
|
'net_settings.classes',
|
|
s.netClasses.classes.map((c, i) => writeClass(c, i === 0 ? DEFAULT_CLASS_PRIORITY : i - 1)),
|
|
);
|
|
setPath(
|
|
j,
|
|
'net_settings.netclass_patterns',
|
|
s.netClasses.assignments.map((a) => ({ netclass: a.netClass, pattern: a.pattern })),
|
|
);
|
|
setPath(
|
|
j,
|
|
'net_settings.net_colors',
|
|
Object.fromEntries(
|
|
// `?? {}` because a settings object stored before this field existed does
|
|
// not carry it, and reading one back is not a type error the compiler can
|
|
// see — the JSON is `any` by the time it gets here.
|
|
Object.entries(s.netClasses.netColors ?? {}).map(([name, css]) => [
|
|
name,
|
|
cssColorToKicad(css),
|
|
]),
|
|
),
|
|
);
|
|
|
|
// text_variables: fully owned by the panel, rebuild (a deleted row must
|
|
// leave the file).
|
|
const varsOut: Json = {};
|
|
for (const v of s.textVars) if (v.name) varsOut[v.name] = v.value;
|
|
setPath(j, 'text_variables', varsOut);
|
|
|
|
// schematic.used_designators (REFDES_TRACKER state; opaque string).
|
|
setPath(j, 'schematic.used_designators', s.usedDesignators);
|
|
|
|
// schematic.bus_aliases: fully owned by the panel, rebuild.
|
|
const aliasesOut: Json = {};
|
|
for (const a of s.busAliases) if (a.name) aliasesOut[a.name] = [...a.members];
|
|
setPath(j, 'schematic.bus_aliases', aliasesOut);
|
|
|
|
return `${JSON.stringify(j, null, 2)}\n`;
|
|
}
|