Skip to content

Commit

Permalink
Merge pull request #216 from tvt173/improve-dataprep
Browse files Browse the repository at this point in the history
Improve dataprep
  • Loading branch information
joamatab authored Nov 2, 2023
2 parents c7ea171 + d6ba6b9 commit a41eb42
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
33 changes: 27 additions & 6 deletions gplugins/klayout/dataprep/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ def copy(region: kdb.Region) -> kdb.Region:
return region.dup()


def _is_layer(value: any) -> bool:
try:
layer, datatype = value
except Exception:
return False
if isinstance(layer, int) and isinstance(datatype, int):
return True
else:
return False


def _assert_is_layer(value: any) -> None:
if not _is_layer(value):
raise ValueError(f"Layer must be a tuple of two integers. Got {value!r}")


class Region(kdb.Region):
def __iadd__(self, offset) -> kdb.Region:
"""Adds an offset to the layer."""
Expand Down Expand Up @@ -81,8 +97,7 @@ def __init__(self, gdspath, cell_name: str | None = None) -> None:
self.regions = {}

def __getitem__(self, layer: tuple[int, int]) -> Region:
if len(layer) != 2:
raise ValueError(f"Layer must be a tuple of two integers. Got {layer!r}")
_assert_is_layer(layer)

if layer in self.regions:
return self.regions[layer]
Expand All @@ -94,10 +109,15 @@ def __getitem__(self, layer: tuple[int, int]) -> Region:
return region

def __setitem__(self, layer: tuple[int, int], region: Region) -> None:
if len(layer) != 2:
raise ValueError(f"Layer must be a tuple of two integers. Got {layer!r}")
_assert_is_layer(layer)
self.regions[layer] = region

def __contains__(self, item):
# checks if the layout contains the given layer
_assert_is_layer(item)
layer, datatype = item
return self.lib.find_layer(layer, datatype) is not None

def write_gds(self, gdspath: PathType = GDSDIR_TEMP / "out.gds", **kwargs) -> None:
"""Write gds.
Expand Down Expand Up @@ -134,10 +154,11 @@ def get_kcell(
c = kf.KCell(cellname, self.lib)
if keep_original:
c.copy_tree(self.layout)
c.flatten()
for layer in self.regions:
layer_id = self.lib.layer(layer[0], layer[1])
self.lib.layout.clear_layer(layer_id)

for layer, region in self.regions.items():
c.shapes(self.lib.layer(layer[0], layer[1])).clear()
c.shapes(self.lib.layer(layer[0], layer[1])).insert(region)
return c

Expand Down
4 changes: 4 additions & 0 deletions gplugins/klayout/tests/test_dataprep_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ def test_RegionCollection_init(region_collection) -> None:
assert region_collection[(1, 0)]


def test_region_collection_contains(region_collection) -> None:
assert l.FLOORPLAN in region_collection


if __name__ == "__main__":
import pathlib

Expand Down

0 comments on commit a41eb42

Please sign in to comment.