Skip to content

PCell to gdsfactory #8

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions zeropdk/pcell.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,59 @@ def place_cell(
transform_into=transform_into,
)

def to_gdsfactory(self, tempfile="./temp", translate_ports=True, port_layer=[1,0]):
"""Converts the PCell to a gdsfactory Component (https://github.com/gdsfactory/gdsfactory)
Useful to access gdsfactory features such as simulation, rendering, etc.

- Maps zeropdk ports to gdsfactory ports
- Can map zeropdk layout.layers to gdsfactory LayerStack
- Default is all ports on [1,0] layer

Returns:
Component
"""

# Import gdsfactory here to avoid it being a prereq
try:
import gdsfactory as gf
except:
raise ImportError("You need to pip install gdsfactory to use the to_gdsfactory feature..")
import klayout.db as pya
import yaml
import math

# Instanciate zeropdk cell
layout = pya.Layout()
cell, ports = self.new_cell(layout)

# Translates zeropdk ports to gdsfactory ports
if translate_ports:
yml_dict = {}
yml_dict["ports"] = {}
for port_name, port in ports.items():
yml_dict["ports"][port_name] = {}
yml_dict["ports"][port_name]["center"] = [port.position.x, port.position.y]
yml_dict["ports"][port_name]["width"] = port.width
yml_dict["ports"][port_name]["orientation"] = math.degrees(math.atan2(port.direction.x, port.direction.y))
yml_dict["ports"][port_name]["port_type"] = port.type
yml_dict["ports"][port_name]["layer"] = port_layer

# Generate metadata
with open(f'{tempfile}.yml', 'w') as outfile:
yaml.dump(yml_dict, outfile, default_flow_style=False)

# Convert by writing with one software, and reading with the other
layout.write(f'{tempfile}.gds')
c = gf.import_gds(gdspath=f'{tempfile}.gds')

# Clean up
os.remove(f'{tempfile}.gds')
if translate_ports:
os.remove(f'{tempfile}.yml')
return c


_zeropdk_cache_store = dict()

_zeropdk_cache_store: Dict[Tuple[str, str, str], Dict[Tuple[str, str, kdb.Layout], kdb.Cell]] = defaultdict(dict)

Expand Down