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 move with string #573

Merged
merged 3 commits into from
Aug 5, 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
57 changes: 43 additions & 14 deletions gdsfactory/component_reference.py
Expand Up @@ -325,17 +325,18 @@ def _transform_point(

def move(
self,
origin: Union[Port, Coordinate] = (0, 0),
destination: Optional[Any] = None,
origin: Union[Port, Coordinate, str] = (0, 0),
destination: Optional[Union[Port, Coordinate, str]] = None,
axis: Optional[str] = None,
) -> "ComponentReference":
"""Move the ComponentReference from the origin point to the destination.

Both origin and destination can be 1x2 array-like, Port, or a key
corresponding to one of the Ports in this device_ref
corresponding to one of the Ports in this device_ref.

Args:
origin: x,y.
destination: x,y.
origin: Port, port_name or Coordinate.
destination: Port, port_name or Coordinate.
axis: for the movemenent.

Returns:
Expand All @@ -347,31 +348,38 @@ def move(
destination = origin
origin = (0, 0)

if hasattr(origin, "center"):
if isinstance(origin, str):
if origin not in self.ports:
raise ValueError(f"{origin} not in {self.ports.keys()}")

origin = self.ports[origin]
origin = cast(Port, origin)
o = origin.center
elif np.array(origin).size == 2:
o = origin
elif origin in self.ports:
origin = self.ports[origin]
elif hasattr(origin, "center"):
origin = cast(Port, origin)
o = origin.center
elif np.array(origin).size == 2:
o = origin
else:
raise ValueError(
f"move(origin={origin})\n"
f"Invalid origin = {origin!r} needs to be"
f"a coordinate, port or port name {list(self.ports.keys())}"
)

if isinstance(destination, str):
if destination not in self.ports:
raise ValueError(f"{destination} not in {self.ports.keys()}")

destination = self.ports[destination]
destination = cast(Port, destination)
d = destination.center
if hasattr(destination, "center"):
destination = cast(Port, destination)
d = destination.center
elif np.array(destination).size == 2:
d = destination
elif destination in self.ports:
destination = self.ports[destination]
destination = cast(Port, destination)
d = destination.center

else:
raise ValueError(
f"{self.parent.name}.move(destination={destination}) \n"
Expand Down Expand Up @@ -446,6 +454,7 @@ def reflect(
p1: Coordinate = (0.0, 1.0),
p2: Coordinate = (0.0, 0.0),
) -> "ComponentReference":
"""TODO. Delete this code and rely on phidl's mirror code."""
if isinstance(p1, Port):
p1 = p1.center
if isinstance(p2, Port):
Expand Down Expand Up @@ -603,3 +612,23 @@ def get_ports_ysize(self, **kwargs) -> float:
ports_cw = self.get_ports_list(clockwise=True, **kwargs)
ports_ccw = self.get_ports_list(clockwise=False, **kwargs)
return snap_to_grid(ports_ccw[0].y - ports_cw[0].y)


def test_move():
import gdsfactory as gf

c = gf.Component()
mzi = c.add_ref(gf.components.mzi())
bend = c.add_ref(gf.components.bend_euler())
bend.move("o1", mzi.ports["o2"])


if __name__ == "__main__":
import gdsfactory as gf

c = gf.Component()
mzi = c.add_ref(gf.components.mzi())
bend = c.add_ref(gf.components.bend_euler())
bend.move("o1", mzi.ports["o2"])
bend.move("o1", "o2")
# c.show()
12 changes: 4 additions & 8 deletions gdsfactory/write_cells.py
Expand Up @@ -38,11 +38,7 @@ def get_script(gdspath: PathType, module: Optional[str] = None) -> str:
cell = clean_name(gdspath.stem)
gdspath = gdspath.stem + gdspath.suffix

if "." in module:
module, submodule = module.split(".")
else:
submodule = module

package = module.split(".")[0] if "." in module else module
if module:
return f"""

Expand All @@ -53,9 +49,9 @@ def {cell}()->gf.Component:
.. plot::
:include-source:

import {module}
import {package}

c = {submodule}.{cell}()
c = {module}.{cell}()
c.plot()
'''
return import_gds({str(gdspath)!r})
Expand Down Expand Up @@ -218,4 +214,4 @@ def test_write_cells():
sample_pdk_cells.write_gds("extra/pdk.gds")
gf.write_cells.write_cells(gdspath="extra/pdk.gds", dirpath="extra/gds")

print(gf.write_cells.get_import_gds_script("extra/gds", module="sky130"))
print(gf.write_cells.get_import_gds_script("extra/gds", module="sky130.components"))