diff --git a/gds/library.py b/gds/library.py index 9389faf..93a7c51 100644 --- a/gds/library.py +++ b/gds/library.py @@ -1,4 +1,6 @@ from datetime import datetime +from .elements import SRef +from .reader import ProgressGetter class Library(object): version = 0 @@ -11,4 +13,46 @@ class Library(object): units_per_dbunit = 1 meters_per_unit = 1 - structures = {} \ No newline at end of file + structures = {} + + class LinkError(Exception): + element = None + pass + + def link_srefs(self, progress_callback=None): + class Progress(ProgressGetter): + total = 0 + current = 0 + + def __init__(self, lib): + for key, value in lib.structures.items(): + self.total += len(value.references) + + def progress(self): + return float(self.current) / self.total + + def inc(self): + self.current += 1 + + count = Progress(self) + + for key, value in self.structures.items(): + + for element in value.references: + if isinstance(element, SRef) and isinstance(element.structure, str): + # try to resolve link + try: + ref = self.structures[element.structure] + element.structure = ref + except KeyError: + err = LinkError("dangeling sref (structure {} is not defined in library)".format(element.structure)) + err.element = element + + raise err + + count.inc() + + if progress_callback: + progress_callback(count) + + \ No newline at end of file diff --git a/gds/structure.py b/gds/structure.py index a37a653..f79cf1a 100644 --- a/gds/structure.py +++ b/gds/structure.py @@ -6,5 +6,7 @@ class Structure(object): last_mod = datetime.now() name = "NONAME" - # contains all the elements + # contains all the low level elements elements = [] + # contains all sref and aref elements + references = [] \ No newline at end of file