from datetime import datetime from .elements import SRef from .reader import ProgressGetter class Library(object): def __init__(self): self.version = 0 self.name = "NONAME" self.last_access = datetime.now() self.last_mod = datetime.now() # unit setup self.units_per_dbunit = 1 self.meters_per_unit = 1 self.structures = {} class LinkError(Exception): element = None pass """ normalizes given coordinate according to library units """ def normalize_coord(self, coord, to_meters=False): fact = self.units_per_dbunit if to_meters: fact *= self.meters_per_unit return (coord[0] * fact, coord[1] * fact) def link_refs(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.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)