Source code for jitxlib.jlcpcb.part
"""LCSC catalogue part annotation for JLCPCB assembly."""
import re
from dataclasses import dataclass
from typing import Any
_PARTNUMBER = re.compile(r"C\d+")
[docs]
@dataclass(frozen=True)
class LCSCPart:
"""LCSC catalogue part for a component, for JLCPCB assembly.
Attach one to a component to name the part JLCPCB should place. The
exporter finds it with ``decompose(component, LCSCPart)``, so it must be a
field of the component itself.
>>> class Decoupler(jitx.Component):
... lcsc = LCSCPart("C91183")
A part library may attach it on the user's behalf; none does today, so it
is written by hand.
Args:
partnumber: the LCSC C-number, such as "C91183".
footprint: package code for the BOM, when the one derived from the
landpattern is wrong or cannot be determined.
orientation: rotation correction in degrees, counter-clockwise, added
to the placement rotation in the pick-and-place file. Use it when
the part's tape orientation differs from the landpattern's.
"""
partnumber: str
footprint: str | None = None
orientation: float | None = None
def __post_init__(self):
# A mistyped C-number orders the wrong part, which is the failure this
# exporter must not introduce silently, and it is free to catch.
if not _PARTNUMBER.fullmatch(self.partnumber):
raise ValueError(
f"{self.partnumber!r} is not an LCSC part number (expected C<digits>)"
)
[docs]
class AmbiguousPart(ValueError):
"""A component carrying more than one `LCSCPart`."""
[docs]
def part_of(component: Any) -> LCSCPart | None:
"""The component's `LCSCPart`, if it has exactly one.
Components only. A landpattern is a shape shared by many parts and a
circuit is a grouping, so a catalogue part number on either would be wrong
as soon as the design is reused; `decompose` looks no further by design.
Raises:
AmbiguousPart: the component carries more than one.
"""
from jitx.inspect import decompose
found = list(decompose(component, LCSCPart))
if not found:
return None
if len(found) > 1:
numbers = ", ".join(sorted(p.partnumber for p in found))
raise AmbiguousPart(
f"carries more than one LCSCPart ({numbers}); which part to order "
"is ambiguous"
)
return found[0]