Skip to content

Commit

Permalink
Merge pull request #854 from gdsfactory/fix_add_port_from_labels
Browse files Browse the repository at this point in the history
Fix add port from labels
  • Loading branch information
joamatab committed Nov 12, 2022
2 parents 74dd5b7 + 9c0bc8e commit 30c741c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 14 deletions.
17 changes: 17 additions & 0 deletions fixme/flexpath.py
@@ -0,0 +1,17 @@
"""fixme."""

import numpy as np
import gdstk

path = gdstk.FlexPath(width=0.5, points=[(-0.1, 0), (+0.1, 0)])
# print(path.points)
# AttributeError: 'gdstk.FlexPath' object has no attribute 'points'


polygons = path.to_polygons()
polygon = polygons[0]
p = polygon.points
center = np.sum(p, 0) / 2

p1 = np.sum(p[:2], 0) / 2
p2 = np.sum(p[2:], 0) / 2
4 changes: 2 additions & 2 deletions gdsfactory/add_pins.py
Expand Up @@ -564,7 +564,7 @@ def add_instance_label(

def add_pins_and_outline(
component: "Component",
reference: "ComponentReference",
reference: Optional["ComponentReference"] = None,
add_outline_function: Optional[Callable] = add_outline,
add_pins_function: Optional[Callable] = add_pins,
add_settings_function: Optional[Callable] = add_settings_label,
Expand Down Expand Up @@ -606,7 +606,7 @@ def add_pins_and_outline(
# p2 = len(c2.get_polygons())
# assert p2 == p1 + 2
# c1 = gf.components.straight_heater_metal(length=2)
c = gf.components.ring_single()
c = gf.components.straight(decorator=add_pins)
# cc.show(show_ports=False)
c.show(show_subports=True)
c.show(show_ports=True)
28 changes: 17 additions & 11 deletions gdsfactory/add_ports.py
Expand Up @@ -306,7 +306,7 @@ def add_ports_from_labels(

xc = xcenter or component.x
for i, label in enumerate(component.labels):
x, y = label.position
x, y = label.origin

if layer_label and (
layer_label[0] != label.layer or layer_label[1] != label.texttype
Expand Down Expand Up @@ -362,7 +362,7 @@ def add_ports_from_siepic_pins(
pin_layer_electrical: LayerSpec = "PORTE",
port_layer_electrical: Optional[LayerSpec] = None,
) -> Component:
"""Add ports from SiEPIC-type cells.
"""Add ports from SiEPIC-type cells, where the pins are defined as paths.
Looks for label, path pairs.
Expand All @@ -379,23 +379,29 @@ def add_ports_from_siepic_pins(
labels = c.get_labels()

for path in c.paths:
p1, p2 = path.points

# Find the center of the path
center = (p1 + p2) / 2
polygons = path.to_polygons()
polygon = polygons[0]
p = polygon.points
center = np.sum(p, 0) / 4
p1 = np.sum(p[:2], 0) / 2
p2 = np.sum(p[2:], 0) / 2

# Find the label closest to the pin
label = None
for i, l in enumerate(labels):
if (
all(isclose(l.position, center))
or all(isclose(l.position, p1))
or all(isclose(l.position, p2))
all(isclose(l.origin, center))
or all(isclose(l.origin, p1))
or all(isclose(l.origin, p2))
):
label = l
labels.pop(i)
# else:
# print(f"Warning: label in {l.origin} in center={center} p1={p1} p2={p2}")
if label is None:
print(f"Warning: label not found for path: ({p1}, {p2})")
print(
f"Warning: label not found for path: in center={center} p1={p1} p2={p2}"
)
continue
if pin_layer_optical[0] in path.layers:
port_type = "optical"
Expand All @@ -418,7 +424,7 @@ def add_ports_from_siepic_pins(
port = Port(
name=port_name,
center=center,
width=path.widths[0][0],
width=path.widths()[0][0],
orientation=angle,
layer=port_layer or pin_layers[port_type],
port_type=port_type,
Expand Down
32 changes: 31 additions & 1 deletion gdsfactory/tests/test_add_ports.py
@@ -1,4 +1,9 @@
import gdsfactory as gf
from gdsfactory.add_pins import add_pins, add_pins_siepic
from gdsfactory.add_ports import (
add_ports_from_markers_inside,
add_ports_from_siepic_pins,
)


def test_add_ports_dict() -> None:
Expand All @@ -15,6 +20,31 @@ def test_add_ports_list() -> None:
assert len(c.ports) == 2, len(c.ports)


def test_add_ports_from_pins(data_regression):
c = gf.components.straight(decorator=add_pins)
gdspath = c.write_gds()
c2 = gf.import_gds(gdspath, decorator=add_ports_from_markers_inside)
data_regression.check(c2.to_dict(with_ports=True))


def test_add_ports_from_pins_siepic(data_regression):
c = gf.components.straight(decorator=add_pins_siepic)
gdspath = c.write_gds()
c2 = gf.import_gds(gdspath, decorator=add_ports_from_siepic_pins)
data_regression.check(c2.to_dict(with_ports=True))


if __name__ == "__main__":
test_add_ports_list()
# test_add_ports_list()
# test_add_ports_dict()

c = gf.components.straight(decorator=add_pins)
gdspath = c.write_gds()
c2 = gf.import_gds(gdspath, decorator=add_ports_from_markers_inside)

assert len(c2.ports) == 2

x1, y1 = c.ports["o1"].center
x2, y2 = c2.ports["o1"].center
assert x1 == x2, f"{x1} {x2}"
assert y1 == y2, f"{y1} {y2}"
21 changes: 21 additions & 0 deletions gdsfactory/tests/test_add_ports/test_add_ports_from_pins.yml
@@ -0,0 +1,21 @@
name: straight_1514d1a3
ports:
o1:
center:
- 0.0
- 0.0
layer: PORT
name: o1
orientation: 180
port_type: optical
width: 0.5
o2:
center:
- 10.0
- 0.0
layer: PORT
name: o2
orientation: 0
port_type: optical
width: 0.5
settings: {}
@@ -0,0 +1,3 @@
name: straight_0c623a86
ports: {}
settings: {}

0 comments on commit 30c741c

Please sign in to comment.