Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix import_gds works with arrays #905

Merged
merged 4 commits into from Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions gdsfactory/component.py
Expand Up @@ -1899,6 +1899,8 @@ def copy(D: Component) -> Component:
magnification=ref.magnification,
x_reflection=ref.x_reflection,
name=ref.name,
v1=ref.v1,
v2=ref.v2,
)
D_copy.add(new_ref)

Expand Down
34 changes: 28 additions & 6 deletions gdsfactory/component_reference.py
Expand Up @@ -148,6 +148,8 @@ def __init__(
rows: int = 1,
spacing=None,
name: Optional[str] = None,
v1: Optional[Tuple[float, float]] = None,
v2: Optional[Tuple[float, float]] = None,
) -> None:
"""Initialize the ComponentReference object."""
self._reference = gdstk.Reference(
Expand All @@ -160,15 +162,15 @@ def __init__(
rows=rows,
spacing=spacing,
)
if v1 or v2:
self._reference.repetition = gdstk.Repetition(
columns=columns, rows=rows, v1=v1, v2=v2
)

self.ref_cell = component
self._owner = None
self._name = name

self.rows = rows
self.columns = columns
self.spacing = spacing

# The ports of a ComponentReference have their own unique id (uid),
# since two ComponentReferences of the same parent Component can be
# in different locations and thus do not represent the same port
Expand All @@ -178,6 +180,26 @@ def __init__(
self.visual_label = visual_label
# self.uid = str(uuid.uuid4())[:8]

@property
def v1(self) -> Optional[Tuple[float, float]]:
return self._reference.repetition.v1

@property
def v2(self) -> Optional[Tuple[float, float]]:
return self._reference.repetition.v2

@property
def rows(self) -> int:
return self._reference.repetition.rows or 1

@property
def columns(self) -> int:
return self._reference.repetition.columns or 1

@property
def spacing(self) -> Optional[Tuple[float, float]]:
return self._reference.repetition.spacing

@property
def parent(self):
return self.ref_cell
Expand All @@ -191,7 +213,7 @@ def origin(self, value):
self._reference.origin = value

@property
def magnification(self):
def magnification(self) -> float:
return self._reference.magnification

@magnification.setter
Expand All @@ -207,7 +229,7 @@ def rotation(self, value):
self._reference.rotation = np.deg2rad(value)

@property
def x_reflection(self):
def x_reflection(self) -> bool:
return self._reference.x_reflection

@x_reflection.setter
Expand Down
27 changes: 19 additions & 8 deletions gdsfactory/read/import_gds.py
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Optional, Union, cast
from typing import Optional, Union

import gdstk
from omegaconf import OmegaConf
Expand Down Expand Up @@ -85,14 +85,23 @@ def import_gds(
for c, D in cell_to_device.items():
for e in c.references:
ref_device = cell_to_device[e.cell]
ref = ComponentReference(component=ref_device)

ref = ComponentReference(
component=ref_device,
origin=e.origin,
rotation=e.rotation,
magnification=e.magnification,
x_reflection=e.x_reflection,
columns=e.repetition.columns or 1,
rows=e.repetition.rows or 1,
spacing=e.repetition.spacing,
v1=e.repetition.v1,
v2=e.repetition.v2,
)
D._register_reference(ref)
D._references.append(ref)
ref._reference = e

component = cell_to_device[topcell]
cast(Component, component)

if read_metadata and metadata_filepath.exists():
logger.info(f"Read YAML metadata from {metadata_filepath}")
Expand Down Expand Up @@ -121,10 +130,13 @@ def import_gds(
if __name__ == "__main__":
import gdsfactory as gf

c = gf.components.mzi()
gdspath = c.write_oas()
c = gf.components.array()
gdspath = c.write_gds()
c.show(show_ports=True)

c2 = import_gds(gdspath)
gf.clear_cache()
c = import_gds(gdspath)
c.show(show_ports=True)

# gdspath = CONFIG["gdsdir"] / "mzi2x2.gds"
# c = import_gds(gdspath, flatten=True, name="TOP")
Expand All @@ -133,4 +145,3 @@ def import_gds(
# c = import_gds(gdspath, flatten=False, polarization="te")
# c = import_gds("/home/jmatres/gdsfactory/gdsfactory/gdsdiff/gds_diff_git.py")
# print(c.hash_geometry())
c.show(show_ports=True)
6 changes: 3 additions & 3 deletions gdsfactory/samples/demo/pcell.py
Expand Up @@ -16,6 +16,6 @@ def mzi_with_bend(radius: float = 10):


if __name__ == "__main__":
c = mzi_with_bend(radius=5)
cc = gf.routing.add_fiber_single(c)
cc.show(show_ports=True)
c = mzi_with_bend(radius=25)
# c = gf.routing.add_fiber_single(c)
c.show(show_ports=True)
18 changes: 16 additions & 2 deletions gdsfactory/tests/test_import_gds.py
Expand Up @@ -44,10 +44,24 @@ def test_import_ports_inside() -> gf.Component:
# return c1


def test_import_gds_array() -> gf.Component:
"""Make sure you can import a GDS with arrays."""
c0 = gf.components.array(
gf.components.rectangle, rows=2, columns=2, spacing=(10, 10)
)
gdspath = c0.write_gds()

gf.clear_cache()
c1 = import_gds(gdspath)
assert len(c1.get_polygons()) == 4
return c1


if __name__ == "__main__":
# c = test_import_gds_hierarchy()
c = test_import_ports_inside()
c.show()
# c = test_import_ports_inside()
c = test_import_gds_array()
c.show(show_ports=True)

# c = test_import_ports()
# c = test_import_gds_add_padding()
Expand Down
3 changes: 2 additions & 1 deletion gdsfactory/types.py
Expand Up @@ -39,7 +39,7 @@
from typing_extensions import Literal

from gdsfactory.component import Component, ComponentReference
from gdsfactory.cross_section import CrossSection
from gdsfactory.cross_section import CrossSection, Section
from gdsfactory.layers import LayerColor, LayerColors
from gdsfactory.port import Port
from gdsfactory.tech import LayerLevel, LayerStack
Expand Down Expand Up @@ -252,6 +252,7 @@ class Array(np.ndarray, metaclass=ArrayMeta):
"LayerLevel",
"LayerColor",
"LayerColors",
"Section",
)


Expand Down