Skip to content

Commit

Permalink
prevent duplicated instances and reload cells before deleting libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroCampana committed Jun 7, 2024
1 parent f40aa85 commit 109d7c4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
25 changes: 9 additions & 16 deletions klayout_package/python/kqcircuits/util/layout_to_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,33 +357,26 @@ def get_node_params(node: Node):
return node_params, elem


def remove_kqc_pcells():
"""Remove all KQCircuits PCells."""

main_window = pya.Application.instance().main_window()
for vid in range(main_window.views()):
top_cell = main_window.view(vid).active_cellview().cell
for inst in top_cell.each_inst():
if isinstance(inst.pcell_declaration(), Element):
inst.delete()


def extract_pcell_data_from_views():
"""Iterate over all KQCircuits PCells and return their data.
"""Iterate over all KQCircuits PCells and return their data and instances.
Returns: A list of lists. Each element corresponds to a view in KLayout and it is a list of
``(type, location, parameters)`` tuples. These tuples completely describe the type, position and
parameters of a single PCell in the "Top Cell" of this view.
Returns: a tuple (views, instances) where
views: a list of lists. Each element corresponds to a view in KLayout and it is a list of
``(type, location, parameters)`` tuples. These tuples completely describe the type, position
and parameters of a single PCell in the "Top Cell" of this view.
instances: flattened list of all instances of KQCircuits PCells found.
"""

views = []
instances = []
main_window = pya.Application.instance().main_window()
for vid in range(main_window.views()):
top_cell = main_window.view(vid).active_cellview().cell
pcells = []
for inst in top_cell.each_inst():
pc = inst.pcell_declaration()
if isinstance(pc, Element):
instances.append(inst)
params = inst.pcell_parameters_by_name()
def_params = pc.__class__.get_schema()
for k, v in def_params.items():
Expand All @@ -392,7 +385,7 @@ def extract_pcell_data_from_views():
pcells.append((pc.__class__, inst.dtrans, params))
views.append(pcells)

return views
return views, instances


def restore_pcells_to_views(views):
Expand Down
4 changes: 3 additions & 1 deletion klayout_package/python/kqcircuits/util/library_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def load_libraries(flush=False, path=""):
Returns:
A dictionary of libraries that have been loaded, keys are library names and values are libraries.
"""
# Try reloading all pcells before deleting libraries, otherwise classes are lost in case of errors
all_pcell_classes = _get_all_pcell_classes(flush, path)

if flush:
delete_all_libraries()
Expand All @@ -95,7 +97,7 @@ def load_libraries(flush=False, path=""):
if lib_path == path:
return {key: value[0] for key, value in _kqc_libraries.items()}

for cls in _get_all_pcell_classes(flush, path):
for cls in all_pcell_classes:

library_name = cls.LIBRARY_NAME
library_path = cls.LIBRARY_PATH
Expand Down
7 changes: 4 additions & 3 deletions klayout_package/python/scripts/macros/0system/0reload.lym
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ Note that this does not reload the ``defaults`` file.
"""

from kqcircuits.util.library_helper import load_libraries
from kqcircuits.util.layout_to_code import remove_kqc_pcells, extract_pcell_data_from_views, restore_pcells_to_views
from kqcircuits.util.layout_to_code import extract_pcell_data_from_views, restore_pcells_to_views

views = extract_pcell_data_from_views()
views, instances = extract_pcell_data_from_views()
try:
load_libraries(flush=True)
except:
raise RuntimeError("Failed to reload KQCircuits libraries, keeping current views.")
remove_kqc_pcells()
for inst in instances:
inst.delete()
restore_pcells_to_views(views)
</text>
</klayout-macro>

0 comments on commit 109d7c4

Please sign in to comment.