Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
"""``.SchDoc`` -> :class:`model.Schematic` (READ-ONLY) (SPEC §3.2).
An Altium schematic document is an OLE2/CFBF container whose ``FileHeader`` stream
holds the post-header record list (see :mod:`._cfbf` and :mod:`.altium_records`).
This module turns those records into the normalized, format-agnostic model:
* **RECORD 1** -> :class:`model.Component` placement / orientation / mirror /
``UniqueId``;
* **RECORD 2** -> :class:`model.Pin` with ``Electrical`` -> :class:`model.PinType`,
``OwnerPartId``, and the *electrical tip* = ``Location + PinLength*dir`` (the pin
Location is the body/inner end; the wire connects at the far tip);
* **RECORD 34** -> component designator (synthesize ``$U<idx>`` when missing -- a
component is NEVER dropped, so verify-by-re-export keeps ground truth);
* **RECORD 41** -> component parameters (and ``value`` / ``Comment``, where a
``=<ParamName>`` Comment is an *indirection* to that component's own
parameter, not a literal string); an **ownerless** RECORD 41 is a *document*
parameter (Title / Revision / DocumentNumber / Date …) and lands in
:attr:`model.Schematic.title_block`;
* **RECORD 45 / 46** -> footprint: only ``ModelType`` PCBLIB (or absent) is a
footprint link — a ``SIM`` / ``SI`` / ``PCB3DLib`` model is NOT — and an
``IsCurrent=T`` model wins over record order;
* **RECORD 27** wires, **29** junctions, **22** No-ERC, **25 / 17 / 18 / 16**
net-labels / power-ports / ports / sheet-entries -> :class:`model.NetPrimitives`
-> :func:`netbuild.build_nets`.
Every record id this module does NOT consume is counted into a **skipped-record
histogram** and surfaced as a single ``Schematic.warnings`` entry (see
:func:`_skipped_warning`) — an unhandled record never vanishes silently. Buses
(RECORD 26 / bus entry 37) are *not* read: they show up in that histogram.
Canonical coordinate system is origin top-left, **+Y down**, unit **mils**; the
Altium schematic is +Y *up*, so every Y is **negated** on ingest (a uniform
transform that preserves net geometry exactly).
"""
from __future__ import annotations
import os
from collections import Counter
from .. import netbuild
from ..errors import fail
from ..model import (
Component,
NetLabel,
NetPrimitives,
Pin,
PinHandle,
Schematic,
WireSeg,
)
from ..model import Junction as MJunction
from . import _cfbf
from .altium_records import (
RECORD_COMPONENT,
RECORD_DESIGNATOR,
RECORD_IMPL_44,
RECORD_IMPL_48,
RECORD_IMPL_FOOTPRINT,
RECORD_IMPL_MODEL,
RECORD_IMPL_PIN_ASSOC,
RECORD_JUNCTION,
RECORD_NET_LABEL,
RECORD_NO_ERC,
RECORD_PARAMETER,
RECORD_PIN,
RECORD_PORT,
RECORD_POWER_PORT,
RECORD_SHEET_ENTRY,
RECORD_SHEET_SYMBOL,
RECORD_WIRE,
coord,
gi,
parse_records,
pin_electrical_type,
record_name,
)
# PinConglomerate low 2 bits -> orientation unit vector (Altium Location units).
# 0 = right (+X), 1 = up (+Y), 2 = left (-X), 3 = down (-Y).
_DIRS = {0: (1, 0), 1: (0, 1), 2: (-1, 0), 3: (0, -1)}
# Scope of an explicit net name by the record that carried it. RECORD 16
# (sheet entry) is NOT here: real entries carry Name/Side/DistanceFromTop, not
# Text/Location, and are handled structurally by the hierarchy walker.
_LABEL_SCOPE = {
RECORD_NET_LABEL: "local",
RECORD_POWER_PORT: "power",
RECORD_PORT: "port",
}
_TRUE = {"T", "TRUE", "1"}
# ``ModelType`` values that denote a PCB FOOTPRINT link. ``""`` covers legacy /
# converter-written records that omit the field. Deliberately an ALLOW-list:
# SIM / SI / PCB3DLIB (and any future kind) must never reach the footprint field.
_FOOTPRINT_MODEL_TYPES = {"", "PCBLIB"}
# Every RECORD id this module actually consumes (directly, or structurally via an
# OwnerIndex chain: 44/48 are the implementation links walked by `_footprints`).
# Anything else is DROPPED — and must be counted, not silently vanish: buses
# (26 / 37) in particular carry connectivity this reader does not model.
_HANDLED_RECORDS = frozenset({
RECORD_COMPONENT, RECORD_PIN, RECORD_SHEET_SYMBOL, RECORD_SHEET_ENTRY,
RECORD_POWER_PORT, RECORD_PORT, RECORD_NO_ERC, RECORD_NET_LABEL,
RECORD_WIRE, RECORD_JUNCTION, 32, 33, RECORD_DESIGNATOR, RECORD_PARAMETER,
RECORD_IMPL_44, RECORD_IMPL_MODEL, RECORD_IMPL_FOOTPRINT,
RECORD_IMPL_PIN_ASSOC, RECORD_IMPL_48,
})
# Distinct ids named in one warning before it collapses into "+N more id(s)".
_HISTOGRAM_LIMIT = 12
def _parameter_is_dropped(rec: dict, rids: list[int | None]) -> bool:
"""True when this RECORD-41 lands nowhere in the model.
A parameter is *consumed* when it is document-level (no/negative owner ->
the title block, see :func:`_document_parameters`) or owned by a RECORD-1
component (-> ``Component.parameters``). Anything else — the classic case
is a parameter hanging off a RECORD-43 parameter set / blanket directive —
is dropped, and has to show up in the skipped histogram: 41 being in
:data:`_HANDLED_RECORDS` used to make the whole instance invisible, so the
warning under-reported the real loss.
"""
oi = gi(rec, "OwnerIndex")
if oi is None or oi < 0:
return False # document-level: kept as title-block metadata
if not (0 <= oi < len(rids)):
return True # dangling owner: nothing can consume it
return rids[oi] != RECORD_COMPONENT
def _skipped_records(recs: list[dict]) -> Counter[int]:
"""Histogram of RECORD ids present in ``recs`` that this reader drops.
Records with no ``RECORD`` id (the NUL padding tail of a stream) are not
counted — they carry nothing. A *handled* id can still contribute when a
specific instance is dropped (see :func:`_parameter_is_dropped`).
"""
hist: Counter[int] = Counter()
rids = [_rid(r) for r in recs]
for rid, r in zip(rids, recs):
if rid is None:
continue
if rid not in _HANDLED_RECORDS:
hist[rid] += 1
elif rid == RECORD_PARAMETER and _parameter_is_dropped(r, rids):
hist[rid] += 1
return hist
def _skipped_warning(hist: Counter[int]) -> str | None:
"""``"altium: skipped 5 unsupported record(s): 26 (Bus) ×3, 37 (BusEntry) ×2"``.
Deterministic order (count desc, then id asc) so the warning is snapshot-safe.
"""
if not hist:
return None
items = sorted(hist.items(), key=lambda kv: (-kv[1], kv[0]))
shown = items[:_HISTOGRAM_LIMIT]
parts = [f"{record_name(rid)} ×{n}" for rid, n in shown]
if len(items) > len(shown):
parts.append(f"+{len(items) - len(shown)} more id(s)")
return (f"altium: skipped {sum(hist.values())} unsupported record(s): "
+ ", ".join(parts))
def _rid(rec: dict) -> int | None:
"""The integer RECORD id of a record dict, or ``None``."""
return gi(rec, "RECORD")
def _canon(x_mil: float, y_mil: float) -> tuple[float, float]:
"""Negate Y: Altium (+Y up) -> canonical top-left (+Y down)."""
return (x_mil, -y_mil)
def _read_fileheader(path: os.PathLike | str | bytes | bytearray) -> list[dict]:
"""Load the container and frame its ``FileHeader`` stream into records."""
streams = _cfbf.read_streams(path)
fh = streams.get("FileHeader")
if fh is None:
fail("ALTIUM_MALFORMED", "no FileHeader stream in .SchDoc container")
return parse_records(fh, drop_header=True)
# ---------------------------------------------------------------------------
# Component / pin extraction
# ---------------------------------------------------------------------------
def _designators(recs: list[dict]) -> dict[int, str]:
"""Map a component's record index -> its RECORD-34 designator Text."""
out: dict[int, str] = {}
for r in recs:
if _rid(r) == RECORD_DESIGNATOR:
oi = gi(r, "OwnerIndex")
text = r.get("Text")
if oi is not None and text:
out[oi] = text
return out
def _is_footprint_model(rec: dict) -> bool:
"""True when a RECORD-45/46 model link names a PCB **footprint**.
A component links several kinds of model, all through the same record: a
``PCBLIB`` footprint, a ``SIM`` SPICE model, an ``SI`` signal-integrity
model, a ``PCB3DLib`` 3D body. Taking the first one in record order put a
*SPICE model name* into ``Component.footprint`` whenever the sim model was
written first — and because the field was non-empty, ``BOM_MISSING_FOOTPRINT``
stayed quiet and the wrong string flowed into the BOM/PCB. Worse than
reading nothing.
A missing/empty ``ModelType`` counts as a footprint (legacy exports and
converter output omit it, and the field's only historical use was the
footprint link); every other value is excluded, so an unknown future model
kind fails *closed*.
"""
return (rec.get("ModelType", "") or "").strip().upper() in _FOOTPRINT_MODEL_TYPES
def _footprints(recs: list[dict]) -> dict[int, str]:
"""Map a component's record index -> its **current footprint** model name.
The model link is a chain, not a direct child of the component: the model record
(RECORD-45/46, carrying ``ModelName``) is owned by a RECORD-44 *Implementation*,
which in turn is owned by the RECORD-1 *Component*. So the footprint name must be
resolved back to the **component** index by walking the ``OwnerIndex`` chain up to
the first component — keying directly on the model's owner would land on the
RECORD-44 and never match a component (the bug that hid every model-link footprint).
Selection among a component's models (Altium allows many):
1. non-footprint model types are excluded outright (:func:`_is_footprint_model`);
2. a footprint model marked ``IsCurrent=T`` wins — that is the one Altium
actually pushes to the PCB, and it is frequently *not* the first;
3. otherwise the first footprint model in record order;
4. no footprint model at all -> the component keeps an empty footprint, so
``BOM_MISSING_FOOTPRINT`` fires instead of being masked by a SPICE name.
"""
components = {i for i, r in enumerate(recs) if _rid(r) == RECORD_COMPONENT}
n = len(recs)
def owning_component(start: int) -> int | None:
seen: set[int] = set()
cur = start
for _ in range(8): # depth cap; real chains are model -> impl -> component
if cur is None or cur in seen or not (0 <= cur < n):
return None
seen.add(cur)
oi = gi(recs[cur], "OwnerIndex")
if oi is None:
return None
if oi in components:
return oi
cur = oi
return None
# component index -> (rank, name); rank 0 = IsCurrent, 1 = plain footprint.
picked: dict[int, tuple[int, str]] = {}
for i, r in enumerate(recs):
if _rid(r) not in (RECORD_IMPL_MODEL, RECORD_IMPL_FOOTPRINT):
continue
name = r.get("ModelName") or r.get("Name")
if not name or not _is_footprint_model(r):
continue
comp_idx = owning_component(i)
if comp_idx is None:
continue
rank = 0 if (r.get("IsCurrent", "") or "").upper() in _TRUE else 1
prev = picked.get(comp_idx)
if prev is None or rank < prev[0]: # strict: ties keep record order
picked[comp_idx] = (rank, name)
return {idx: name for idx, (_rank, name) in picked.items()}
def _parameters(recs: list[dict]) -> dict[int, dict[str, str]]:
"""Map a component's record index -> its RECORD-41 ``{Name: Text}`` params."""
out: dict[int, dict[str, str]] = {}
for r in recs:
if _rid(r) == RECORD_PARAMETER:
oi = gi(r, "OwnerIndex")
name = r.get("Name")
if oi is None or not name:
continue
out.setdefault(oi, {})[name] = r.get("Text", "")
return out
def _document_parameters(recs: list[dict]) -> dict[str, str]:
"""Document-level RECORD-41 parameters (no owner) -> ``{Name: Text}``.
These are Altium's *document parameters* — ``Title``, ``Revision``,
``DocumentNumber``, ``Date``, ``Organization``, ``SheetNumber``… — which
populate the sheet's title block. They are owned by nothing, so the
component-parameter mapping (which requires an ``OwnerIndex``) dropped
them: the whole title block evaporated without even a histogram entry.
Order-preserving (``dict`` insertion order = record order) so the export
stays byte-stable.
"""
out: dict[str, str] = {}
for r in recs:
if _rid(r) != RECORD_PARAMETER:
continue
oi = gi(r, "OwnerIndex")
if oi is not None and oi >= 0:
continue
name = r.get("Name")
if name:
out[name] = r.get("Text", "")
return out
# Distinct components named in the unresolved-Comment warning before it collapses.
_COMMENT_REF_LIMIT = 12
def _resolve_comment(comment: str | None,
params: dict[str, str]) -> tuple[str | None, str | None]:
"""Resolve Altium's indirect ``Comment="=<ParamName>"`` reference.
``=Value`` on a component does not mean the literal text ``"=Value"``: it is
an indirection to that component's own ``Value`` parameter (Altium's
``=<ParamName>`` special string syntax, resolved case-insensitively).
Importing it literally overwrote the real value with a formula, and the
corruption looked like data.
Returns ``(value, unresolved_ref)`` — ``unresolved_ref`` is non-``None``
only when the reference names a parameter the component does not have, in
which case the literal string is preserved and the caller warns.
"""
if not comment or not comment.startswith("="):
return comment, None
ref = comment[1:].strip()
if not ref:
return comment, None # a bare "=" is not a reference
lowered = {k.lower(): v for k, v in params.items()}
hit = lowered.get(ref.lower())
if hit is None:
return comment, ref
return hit, None
def _comment_ref_warning(refs: list[tuple[str, str]]) -> str | None:
"""One collapsed warning for every ``=<Param>`` Comment that did not resolve."""
if not refs:
return None
shown = refs[:_COMMENT_REF_LIMIT]
parts = [f"{des} {comment!r}" for des, comment in shown]
if len(refs) > len(shown):
parts.append(f"+{len(refs) - len(shown)} more")
return (f"altium: {len(refs)} unresolved Comment parameter reference(s) "
f"(imported literally): " + ", ".join(parts))
def _pin_tip(d: dict) -> tuple[float, float]:
"""Canonical electrical-tip (mils) of a RECORD-2 pin = Location + PinLength*dir."""
loc_x = coord(d, "Location.X")
loc_y = coord(d, "Location.Y")
length = coord(d, "PinLength") if "PinLength" in d else 0.0
# PinLength has no _Frac twin in practice; coord() still assembles it cleanly.
dx, dy = _DIRS[gi(d, "PinConglomerate", 0) & 3]
return _canon(loc_x + length * dx, loc_y + length * dy)
def _build_components(
recs: list[dict],
comment_refs: list[tuple[str, str]] | None = None,
) -> tuple[list[Component], dict[int, Component]]:
"""Build the component list (with pins), keyed by record index.
``comment_refs`` (when given) collects ``(designator, comment)`` pairs for
every ``=<ParamName>`` Comment that could not be resolved, so the caller can
surface one collapsed reader warning.
"""
desig = _designators(recs)
fps = _footprints(recs)
params = _parameters(recs)
by_index: dict[int, Component] = {}
components: list[Component] = []
for idx, r in enumerate(recs):
if _rid(r) != RECORD_COMPONENT:
continue
name = desig.get(idx)
undesignated = name is None
if undesignated:
name = f"$U{idx}" # synthesize -- a component is NEVER dropped
rot = (gi(r, "Orientation", 0) & 3) * 90
mirror = "x" if (r.get("IsMirrored", "") or "").upper() in _TRUE else "none"
comment, unresolved = _resolve_comment(r.get("Comment"), params.get(idx, {}))
if unresolved is not None and comment is not None and comment_refs is not None:
comment_refs.append((name, comment))
comp = Component(
designator=name,
library_ref=r.get("LibReference") or r.get("DesignItemId"),
x_mil=_canon(coord(r, "Location.X"), coord(r, "Location.Y"))[0],
y_mil=_canon(coord(r, "Location.X"), coord(r, "Location.Y"))[1],
rotation=rot,
mirror=mirror,
# Comment may be an indirect "=<ParamName>" reference (resolved above).
value=comment or (params.get(idx, {}).get("Value")),
# RECORD 45/46 model-link first; fall back to the RECORD-41 `Footprint` /
# `Supplier Footprint` parameter (converter-generated parts leave the model-link
# empty but write the footprint as a parameter).
footprint=(
fps.get(idx)
or params.get(idx, {}).get("Footprint")
or params.get(idx, {}).get("Supplier Footprint")
),
unique_id=r.get("UniqueId"),
part_count=gi(r, "PartCount", 1) or 1,
parameters=params.get(idx, {}),
undesignated=undesignated,
)
by_index[idx] = comp
components.append(comp)
# attach pins (RECORD 2) to their owner component
for r in recs:
if _rid(r) != RECORD_PIN:
continue
oi = gi(r, "OwnerIndex")
owner = by_index.get(oi) if oi is not None else None
if owner is None:
continue
tip_x, tip_y = _pin_tip(r)
owner.pins.append(
Pin(
number=r.get("Designator", ""),
name=r.get("Name"),
x_mil=tip_x,
y_mil=tip_y,
electrical_type=pin_electrical_type(r),
owner_part_id=gi(r, "OwnerPartId", 1) or 1,
unique_id=r.get("UniqueId"),
)
)
return components, by_index
# ---------------------------------------------------------------------------
# Net primitives
# ---------------------------------------------------------------------------
def _hier_key(sheet_ns: str, name: str) -> str:
"""Synthetic sheet-entry<->child-port connector text (never names a net)."""
return f"\x02hier:{sheet_ns}:{name}"
def _build_primitives(
recs: list[dict], by_index: dict[int, Component],
prims: NetPrimitives | None = None, sheet: str = "",
hier_mode: bool = False,
) -> NetPrimitives:
"""Emit wires/junctions/labels/power-ports/pins/No-ERC for net inference.
``sheet`` is the geometric namespace (the sheet-instance path; ``""`` root).
``hier_mode`` implements Altium's *Automatic* net-identifier scope: when the
design contains sheet symbols, PORTs stop merging globally by name and
instead pair with their own parent's matching sheet entry (the walker adds
the entry-side connectors); flat designs keep the historical global-port
behavior, so existing reads are unchanged.
"""
prims = prims if prims is not None else NetPrimitives()
for idx, r in enumerate(recs):
rid = _rid(r)
if rid == RECORD_WIRE:
n = gi(r, "LocationCount", 0) or 0
pts = [_canon(coord(r, f"X{k}"), coord(r, f"Y{k}")) for k in range(1, n + 1)]
for a, b in zip(pts, pts[1:]):
prims.wires.append(WireSeg(a=a, b=b, sheet=sheet))
elif rid == RECORD_JUNCTION:
prims.junctions.append(
MJunction(at=_canon(coord(r, "Location.X"), coord(r, "Location.Y")), sheet=sheet)
)
elif rid in _LABEL_SCOPE:
text = r.get("Text")
if text:
scope = _LABEL_SCOPE[rid]
at = _canon(coord(r, "Location.X"), coord(r, "Location.Y"))
if rid == RECORD_PORT and hier_mode:
# hierarchical scope: the port names its net locally and
# pairs ONLY with this sheet's own entry on the parent.
prims.labels.append(NetLabel(at=at, text=text,
scope="local", sheet=sheet))
prims.labels.append(NetLabel(at=at, text=_hier_key(sheet, text),
scope="hier", sheet=sheet))
else:
prims.labels.append(NetLabel(at=at, text=text,
scope=scope, sheet=sheet))
elif rid == RECORD_NO_ERC:
prims.no_erc.append(_canon(coord(r, "Location.X"), coord(r, "Location.Y")))
# pins: PinHandle keyed by (owner designator, pin number) at the electrical tip
for r in recs:
if _rid(r) != RECORD_PIN:
continue
oi = gi(r, "OwnerIndex")
owner = by_index.get(oi) if oi is not None else None
if owner is None:
continue
prims.pins.append(
PinHandle(
ref=(owner.designator, r.get("Designator", "")),
at=_pin_tip(r),
sheet=sheet,
)
)
return prims
# ---------------------------------------------------------------------------
# Public API
# ---------------------------------------------------------------------------
def _metadata(components: list[Component], nets, frac_present: bool) -> dict:
pins = [p for c in components for p in c.pins]
n_pins = len(pins)
from ..model import PinType
passive = sum(1 for p in pins if p.electrical_type is PinType.PASSIVE)
return {
"component_count": len(components),
"pin_count": n_pins,
"passive_pin_ratio": (passive / n_pins) if n_pins else 0.0,
"unnamed_net_count": sum(1 for n in nets if not n.is_named),
"undesignated_count": sum(1 for c in components if c.undesignated),
"frac_present": frac_present,
}
# Depth cap for sheet-symbol recursion (a real design is 2-4 deep).
_MAX_SHEET_DEPTH = 16
# RECORD-16 Side -> which sheet-symbol edge carries the entry.
_SIDE_LEFT, _SIDE_RIGHT, _SIDE_TOP, _SIDE_BOTTOM = 0, 1, 2, 3
def _sheet_children(recs: list[dict]) -> list[dict]:
"""Sheet symbols (RECORD 15) with their name/file (32/33) and entries (16).
Entry positions follow the Altium convention: the symbol's Location is its
TOP-LEFT corner in the +Y-up record frame (the body extends right and
down), and ``DistanceFromTop`` counts from the side's origin in 1/10
Location units. Scale validated against generated fixtures; flagged for
confirmation against a real AD hierarchical design.
"""
from ..units import altium_to_mil
out = []
for idx, r in enumerate(recs):
if _rid(r) != RECORD_SHEET_SYMBOL:
continue
x0 = coord(r, "Location.X")
y0 = coord(r, "Location.Y")
w = coord(r, "XSize")
h = coord(r, "YSize")
child = {"index": idx, "name": None, "file": None, "entries": []}
for r2 in recs:
if gi(r2, "OwnerIndex") != idx:
continue
rid2 = _rid(r2)
if rid2 == 32:
child["name"] = r2.get("Text")
elif rid2 == 33:
child["file"] = r2.get("Text")
elif rid2 == RECORD_SHEET_ENTRY:
name = r2.get("Name") or r2.get("Text")
if not name:
continue
d = altium_to_mil(gi(r2, "DistanceFromTop", 0) * 10, 0)
side = gi(r2, "Side", _SIDE_LEFT)
if side == _SIDE_RIGHT:
pt = (x0 + w, y0 - d)
elif side == _SIDE_TOP:
pt = (x0 + d, y0)
elif side == _SIDE_BOTTOM:
pt = (x0 + d, y0 - h)
else:
pt = (x0, y0 - d)
child["entries"].append((name, _canon(*pt)))
out.append(child)
return out
def _read_hier(
path: os.PathLike | str,
*,
power_priority: bool = False,
) -> Schematic:
"""Read a ``.SchDoc``, recursing into sheet symbols' child documents."""
from pathlib import Path as _P
root_path = _P(os.fspath(path))
loaded: list[tuple[str, _P, list[dict], list[dict]]] = [] # ns, path, recs, children
def _walk(p: _P, ns: str, ancestors: tuple[str, ...]) -> None:
recs = _read_fileheader(p)
children = _sheet_children(recs)
loaded.append((ns, p, recs, children))
for child in children:
fname = child["file"]
if not fname:
continue
child_path = (p.parent / fname.replace("\\", "/")).resolve()
child_ns = f"{ns}/s{child['index']}"
child["ns"] = child_ns
if len(ancestors) >= _MAX_SHEET_DEPTH:
fail("ALTIUM_MALFORMED",
f"sheet nesting deeper than {_MAX_SHEET_DEPTH} at {fname!r}")
if str(child_path) in ancestors:
fail("ALTIUM_MALFORMED",
f"sheet recursion: {child_path} includes itself via {p}")
if not child_path.exists():
raise FileNotFoundError(
f"{child_path} (sheet {child['name'] or fname!r} referenced from {p})")
_walk(child_path, child_ns, ancestors + (str(child_path),))
_walk(root_path, "", (str(root_path.resolve()),))
hier_mode = any(c["entries"] or c["file"] for _, _, _, cs in loaded for c in cs)
components: list[Component] = []
prims = NetPrimitives()
prims.power_priority = power_priority
sheet_names: list[str] = []
frac_present = False
skipped: Counter[int] = Counter()
comment_refs: list[tuple[str, str]] = []
# `loaded[0]` is the root sheet (depth-first walk starts there); its document
# parameters are the design's title block. Child sheets carry their own.
title_block = _document_parameters(loaded[0][2]) if loaded else {}
for ns, _p, recs, children in loaded:
skipped += _skipped_records(recs)
comps, by_index = _build_components(recs, comment_refs)
for c in comps:
c.sheet = ns
components.extend(comps)
_build_primitives(recs, by_index, prims, sheet=ns, hier_mode=hier_mode)
frac_present = frac_present or any(k.endswith("_Frac") for r in recs for k in r)
for child in children:
if child.get("name"):
sheet_names.append(child["name"])
child_ns = child.get("ns")
for name, pt in child["entries"]:
# An entry is a CONNECTOR, not a net label: emit only the
# never-naming hier pair to the child's same-named PORT. (A
# raw-name local label here would wrongly merge same-named
# entries of two different children on the parent sheet.)
if child_ns:
prims.labels.append(NetLabel(at=pt,
text=_hier_key(child_ns, name),
scope="hier", sheet=ns))
nets = netbuild.build_nets(prims)
warnings = [w for w in (_skipped_warning(skipped),
_comment_ref_warning(comment_refs)) if w]
return Schematic(
source_path=str(path),
source_format="altium",
components=components,
nets=nets,
sheets=sheet_names,
no_erc_points=list(prims.no_erc),
warnings=warnings,
metadata=_metadata(components, nets, frac_present),
title_block=title_block,
)
def read(
path: os.PathLike | str | bytes | bytearray,
*,
power_priority: bool = False,
) -> Schematic:
"""Read a ``.SchDoc`` into a normalized :class:`model.Schematic`.
A path input recurses into sheet symbols (RECORD 15/16/32/33): child
documents load relative to the parent file, each sheet instance is its own
geometric namespace, and connectivity crosses sheets through sheet-entry↔
child-port pairs (Altium's *Automatic* scope: PORTs merge globally only in
designs WITHOUT sheet symbols), global labels excluded, power ports always
global. Bytes input stays single-sheet (no filesystem to recurse into).
"""
if isinstance(path, (str, os.PathLike)):
return _read_hier(path, power_priority=power_priority)
recs = _read_fileheader(path)
comment_refs: list[tuple[str, str]] = []
components, by_index = _build_components(recs, comment_refs)
prims = _build_primitives(recs, by_index)
prims.power_priority = power_priority
nets = netbuild.build_nets(prims)
frac_present = any(k.endswith("_Frac") for r in recs for k in r)
warnings = [w for w in (_skipped_warning(_skipped_records(recs)),
_comment_ref_warning(comment_refs)) if w]
return Schematic(
source_path="<bytes>",
source_format="altium",
components=components,
nets=nets,
sheets=[],
no_erc_points=list(prims.no_erc),
warnings=warnings,
metadata=_metadata(components, nets, frac_present),
title_block=_document_parameters(recs),
)
def read_primitives(path: os.PathLike | str | bytes | bytearray) -> NetPrimitives:
"""Read a ``.SchDoc`` into raw :class:`model.NetPrimitives` (pre-netbuild)."""
recs = _read_fileheader(path)
_, by_index = _build_components(recs)
return _build_primitives(recs, by_index)
__all__ = ["read", "read_primitives"]
2126c3284This file is pinned to the selected release. Download the original to open it in your design tool.