Skip to article frontmatterSkip to article content

gdsfactory.pack

gdsfactory.pack(component_list: Sequence[str | Callable[[...], Component] | dict[str, Any] | DKCell], spacing: float = 10.0, aspect_ratio: tuple[float, float] = (1.0, 1.0), max_size: tuple[float | None, float | None] = (None, None), sort_by_area: bool = True, density: float = 1.1, precision: float = 0.01, text: TextFunction | None = None, text_prefix: str = '', text_mirror: bool = False, text_rotation: int = 0, text_offsets: tuple[tuple[float, float], ...] = ((0, 0),), text_anchors: tuple[Literal['ce', 'cw', 'nc', 'ne', 'nw', 'sc', 'se', 'sw', 'center', 'cc'], ...] = ('cc',), name_prefix: str | None = None, rotation: int = 0, h_mirror: bool = False, v_mirror: bool = False, add_ports_prefix: bool = True, add_ports_suffix: bool = False)list[Component]

Pack a list of components into as few Components as possible.

Parameters
  • component_listlist or tuple.

  • spacingMinimum distance between adjacent shapes.

  • aspect_ratio(width, height) ratio of the rectangular bin.

  • max_sizeLimits the size into which the shapes will be packed.

  • sort_by_areaPre-sorts the shapes by area.

  • densityValues closer to 1 pack tighter but require more computation.

  • precisionDesired precision for rounding vertex coordinates.

  • textOptional function to add text labels.

  • text_prefixfor labels. For example. ‘A’ will produce ‘A1’, ‘A2’, …

  • text_mirrorif True mirrors text.

  • text_rotationOptional text rotation.

  • text_offsetsrelative to component size info anchor. Defaults to center.

  • text_anchorsrelative to component (ce cw nc ne nw sc se sw center cc).

  • name_prefixfor each packed component (avoids the Unnamed cells warning). Note that the suffix contains a uuid so the name will not be deterministic.

  • rotationoptional component rotation in degrees.

  • h_mirrorhorizontal mirror in y axis (x, 1) (1, 0). This is the most common.

  • v_mirrorvertical mirror using x axis (1, y) (0, y).

  • add_ports_prefixadds port names with prefix.

  • add_ports_suffixadds port names with suffix.

import gdsfactory as gf
from functools import partial

components = [gf.components.triangle(x=i) for i in range(1, 10)]
c = gf.pack(
    components,
    spacing=20.0,
    max_size=(100, 100),
    text=partial(gf.components.text, justify="center"),
    text_prefix="R",
    name_prefix="demo",
    text_anchors=["nc"],
    text_offsets=[(-10, 0)],
    v_mirror=True,
)
c[0].plot()