Source code for jitxlib.gerber.extract.layers

"""Mapping between JITX conductor indices and Gerber file functions.

`LayerModel` is the single source of truth for what layer a piece of copper
belongs to and which file function names it. It owns no filenames: those are
the naming layer's, and nothing outside `format/naming.py` builds one.
"""

from __future__ import annotations

from dataclasses import dataclass

from ..format.model import Copper, Drill, HoleType, Side


[docs] def normalize_layer(layer: int, conductors: int) -> int: """Resolve a JITX layer index to an absolute one with 0 at the top. Negative indices count from the bottom, as everywhere else in jitx. """ return layer + conductors if layer < 0 else layer
[docs] @dataclass(frozen=True) class LayerModel: """Conductor indices, sides and the file functions that describe them. Args: conductors: number of conductor layers in the stackup. """ conductors: int
[docs] def normalize(self, layer: int) -> int: """Absolute conductor index, 0 at the top. Raises: IndexError: the index is outside the stackup. """ index = normalize_layer(layer, self.conductors) if not 0 <= index < self.conductors: raise IndexError(f"Conductor index out of range: {layer}") return index
[docs] def side(self, layer: int) -> Side: """Whether a conductor is the top, the bottom, or an inner layer.""" index = self.normalize(layer) if index == 0: return Side.TOP if index == self.conductors - 1: return Side.BOTTOM return Side.INNER
[docs] def copper(self, layer: int) -> Copper: """The `Copper,L<n>,<side>` function for a conductor. The index in the attribute is 1-based, counted from the top, while every index this class takes is 0-based; the conversion happens here and nowhere else. """ index = self.normalize(layer) return Copper(index + 1, self.side(index), self.conductors)
[docs] def span(self, start: int, stop: int) -> tuple[int, int]: """A drill span as an ordered pair of absolute indices.""" first, last = self.normalize(start), self.normalize(stop) return (min(first, last), max(first, last))
[docs] def drill(self, span: tuple[int, int], *, plated: bool) -> Drill: """The drill file function for a span. Classification is by reach: a span covering the whole board is PTH or NPTH, one touching exactly one outer layer is Blind, and one touching neither is Buried. The `i,j` fields are 1-based, as the attribute requires. """ first, last = span outer = (first == 0) + (last == self.conductors - 1) if outer == 2: hole = HoleType.PTH if plated else HoleType.NPTH elif outer == 1: hole = HoleType.BLIND else: hole = HoleType.BURIED return Drill(first + 1, last + 1, hole, plated=plated)
@property def through(self) -> tuple[int, int]: """The span crossing the whole board.""" return (0, self.conductors - 1)