Source code for jitxlib.gerber.extract.stackup

"""The stackup: the layer model, and the gbrjob's `MaterialStackup`.

Row types come from `jitx.stackup.Material.representing` where it is set, so
a layer that declares itself soldermask or silkscreen becomes a `SolderMask`
or `Legend` row rather than being mislabeled `Dielectric`, which is what an
"everything that is not a conductor is a dielectric" walk produces. The same
field is what lets `BoardThickness` exclude the soldermask, as the convention
expects.
"""

from __future__ import annotations

from dataclasses import dataclass
from logging import getLogger

from jitx.feature import Finish, Silkscreen, Soldermask
from jitx.inspect import visit
from jitx.proxy import typeof
from jitx.run.runtime import RuntimeDesign
from jitx.stackup import Conductor, Dielectric, Material

from ..format.jobfile import StackupRow, StackupType
from .layers import LayerModel

logger = getLogger(__name__)


[docs] @dataclass class StackupModel: """What the stackup walk produced.""" layers: LayerModel rows: list[StackupRow] thickness: float | None
[docs] def build(design: RuntimeDesign) -> StackupModel: """Walk the stackup materials top to bottom. Raises: ValueError: the stackup declares no conductors, so there is no board to export. """ stackup = design.root.substrate.stackup materials = [material for _trace, material in visit(stackup, Material)] conductors = sum(1 for material in materials if isinstance(material, Conductor)) if conductors == 0: raise ValueError("Stackup has no conductor layers") rows: list[StackupRow] = [] thickness: float | None = 0.0 for material in materials: row_type = _row_type(material) rows.append( StackupRow( row_type, thickness=material.thickness, name=material.material_name or typeof(material).__name__, dielectric_constant=_float( getattr(material, "dielectric_coefficient", None) ), loss_tangent=_float(getattr(material, "loss_tangent", None)), ) ) if material.thickness is None: thickness = None elif thickness is not None and row_type is not StackupType.SOLDERMASK: thickness += material.thickness return StackupModel(LayerModel(conductors), rows, thickness)
def _row_type(material: Material) -> StackupType: """The gbrjob row type for one material. `representing` is consulted first because it is the design's own statement about what the layer is; the material class is only the fallback for a layer that does not declare one. """ match material.representing: case None: pass case represented if represented is Soldermask: return StackupType.SOLDERMASK case represented if represented is Silkscreen: return StackupType.LEGEND case represented if represented is Finish: return StackupType.SURFACE_FINISH case _: logger.debug( "Stackup layer represents %s, which has no gbrjob row type", material.representing, ) if isinstance(material, Conductor): return StackupType.COPPER if isinstance(material, Dielectric): return StackupType.DIELECTRIC return StackupType.DIELECTRIC def _float(value: float | None) -> float | None: return None if value is None else float(value)