Skip to content

Packing

packing

Utilities for packing KCells and Instances.

pack_instances

pack_instances(
    target: KCell,
    instances: Sequence[Instance],
    max_width: int | None = None,
    max_height: int | None = None,
    spacing: int = 0,
) -> InstanceGroup

Pack KCells.

Parameters:

Name Type Description Default
target KCell

KCell to place the packed instances in.

required
instances Sequence[Instance]

Sequence of Instances to pack.

required
max_width int | None

Maximum width of the packed rectangle

None
max_height int | None

Maximum height of the packed rectangle

None
spacing int

Spacing between the instance bboxes

0
Source code in kfactory/packing.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def pack_instances(
    target: KCell,
    instances: Sequence[Instance],
    max_width: int | None = None,
    max_height: int | None = None,
    spacing: int = 0,
) -> InstanceGroup:
    """Pack KCells.

    Args:
        target: KCell to place the packed instances in.
        instances: Sequence of Instances to pack.
        max_width: Maximum width of the packed rectangle
        max_height: Maximum height of the packed rectangle
        spacing: Spacing between the instance bboxes
    """
    bbs = [(inst, inst.bbox()) for inst in instances]

    packed_bbs = rpack.pack(
        ((bb[1].width() + spacing, bb[1].height() + spacing) for bb in bbs),
        max_width=max_width,
        max_height=max_height,
    )

    for inst_bb, bb in zip(bbs, packed_bbs, strict=False):
        inst, _bb = inst_bb
        inst.transform(
            kdb.Trans(
                bb[0] + spacing // 2 - _bb.left, bb[1] + spacing // 2 - _bb.bottom
            )
        )

    return InstanceGroup(insts=instances)

pack_kcells

pack_kcells(
    target: KCell,
    kcells: Sequence[KCell],
    max_width: int | None = None,
    max_height: int | None = None,
    spacing: int = 0,
) -> InstanceGroup

Pack KCells.

Parameters:

Name Type Description Default
target KCell

KCell to place the packed instances in.

required
kcells Sequence[KCell]

Sequence of KCells from which to create instances.

required
max_width int | None

Maximum width of the packed rectangle

None
max_height int | None

Maximum height of the packed rectangle

None
spacing int

Spacing between the instance bboxes

0
Source code in kfactory/packing.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def pack_kcells(
    target: KCell,
    kcells: Sequence[KCell],
    max_width: int | None = None,
    max_height: int | None = None,
    spacing: int = 0,
) -> InstanceGroup:
    """Pack KCells.

    Args:
        target: KCell to place the packed instances in.
        kcells: Sequence of KCells from which to create instances.
        max_width: Maximum width of the packed rectangle
        max_height: Maximum height of the packed rectangle
        spacing: Spacing between the instance bboxes
    """
    insts = [target << kc for kc in kcells]
    bbs = [(inst, inst.bbox()) for inst in insts]

    packed_bbs = rpack.pack(
        ((bb[1].width() + spacing, bb[1].height() + spacing) for bb in bbs),
        max_width=max_width,
        max_height=max_height,
    )

    for inst_bb, bb in zip(bbs, packed_bbs, strict=False):
        inst, _bb = inst_bb
        inst.transform(
            kdb.Trans(
                bb[0] + spacing // 2 - _bb.left, bb[1] + spacing // 2 - _bb.bottom
            )
        )

    return InstanceGroup(insts=insts)