-
Notifications
You must be signed in to change notification settings - Fork 187
/
add_padding.py
122 lines (100 loc) · 3.44 KB
/
add_padding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
49
50
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from __future__ import annotations
from functools import partial
import gdsfactory as gf
from gdsfactory.cell import container
from gdsfactory.component import Component
from gdsfactory.typings import ComponentSpec, LayerSpec
def get_padding_points(
component: Component,
default: float = 50.0,
top: float | None = None,
bottom: float | None = None,
right: float | None = None,
left: float | None = None,
) -> list[float]:
"""Returns padding points for a component outline.
Args:
component: to add padding.
default: default padding in um.
top: north padding in um.
bottom: south padding in um.
right: east padding in um.
left: west padding in um.
"""
c = component
top = top if top is not None else default
bottom = bottom if bottom is not None else default
right = right if right is not None else default
left = left if left is not None else default
return [
[c.xmin - left, c.ymin - bottom],
[c.xmax + right, c.ymin - bottom],
[c.xmax + right, c.ymax + top],
[c.xmin - left, c.ymax + top],
]
def add_padding(
component: ComponentSpec = "mmi2x2",
layers: tuple[LayerSpec, ...] = ("PADDING",),
**kwargs,
) -> Component:
"""Adds padding layers to component. Returns same component.
Args:
component: to add padding.
layers: list of layers.
keyword Args:
default: default padding.
top: north padding in um.
bottom: south padding in um.
right: east padding in um.
left: west padding in um.
"""
component = gf.get_component(component)
points = get_padding_points(component, **kwargs)
for layer in layers:
component.add_polygon(points, layer=layer)
return component
def add_padding_to_size(
component: ComponentSpec,
layers: tuple[LayerSpec, ...] = ("PADDING",),
xsize: float | None = None,
ysize: float | None = None,
left: float = 0,
bottom: float = 0,
) -> Component:
"""Returns component with padding layers on each side.
New size is multiple of grid size.
Args:
component: to add padding.
layers: list of layers.
xsize: x size to fill up in um.
ysize: y size to fill up in um.
left: left padding in um to fill up in um.
bottom: bottom padding in um to fill up in um.
"""
component = gf.get_component(component)
c = component
top = abs(ysize - component.ysize) if ysize else 0
right = abs(xsize - component.xsize) if xsize else 0
points = [
[c.xmin - left, c.ymin - bottom],
[c.xmax + right, c.ymin - bottom],
[c.xmax + right, c.ymax + top],
[c.xmin - left, c.ymax + top],
]
for layer in layers:
component.add_polygon(points, layer=layer)
return component
add_padding_container = partial(container, function=add_padding)
add_padding_to_size_container = partial(container, function=add_padding_to_size)
if __name__ == "__main__":
# test_container()
# p = partial(add_padding, layers=((1, 0)))
c = gf.components.straight(length=10)
# c1 = add_padding_to_size_container(c, xsize=100, ysize=100)
# c2 = add_padding_to_size_container(c, xsize=100, ysize=100)
# print(c1.name)
# print(c2.name)
# c2 = add_padding_container(component=c, default=1)
c2 = add_padding_to_size_container(component=c, ysize=20, bottom=10)
# c2 = c.apply(add_padding)
c2.show()