2019-07-01 11:45:02 +02:00
|
|
|
from datetime import datetime
|
2019-07-01 13:08:48 +02:00
|
|
|
from .elements import SRef
|
|
|
|
from .reader import ProgressGetter
|
2019-07-01 11:45:02 +02:00
|
|
|
|
|
|
|
class Library(object):
|
2019-07-02 14:47:36 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.version = 0
|
|
|
|
self.name = "NONAME"
|
2019-07-01 11:45:02 +02:00
|
|
|
|
2019-07-02 14:47:36 +02:00
|
|
|
self.last_access = datetime.now()
|
|
|
|
self.last_mod = datetime.now()
|
2019-07-01 11:45:02 +02:00
|
|
|
|
2019-07-02 14:47:36 +02:00
|
|
|
# unit setup
|
|
|
|
self.units_per_dbunit = 1
|
|
|
|
self.meters_per_unit = 1
|
2019-07-01 11:45:02 +02:00
|
|
|
|
2019-07-02 14:47:36 +02:00
|
|
|
self.structures = {}
|
2019-07-01 13:08:48 +02:00
|
|
|
|
|
|
|
class LinkError(Exception):
|
|
|
|
element = None
|
|
|
|
pass
|
2019-07-02 20:05:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
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)
|
2019-07-01 13:08:48 +02:00
|
|
|
|
2019-07-03 14:14:30 +02:00
|
|
|
def link_refs(self, root, progress_callback=None):
|
|
|
|
for ref in root.references:
|
|
|
|
if isinstance(ref.structure, str):
|
|
|
|
try:
|
|
|
|
element.structure = self.structures[ref.structure]
|
|
|
|
except KeyError:
|
|
|
|
err = LinkError("dangeling sref (structure {} is not defined in library)".format(ref.structure))
|
|
|
|
err.element = root
|
|
|
|
raise err
|
|
|
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
def link_all_refs(self, progress_callback=None):
|
2019-07-01 13:08:48 +02:00
|
|
|
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():
|
2019-07-01 14:21:45 +02:00
|
|
|
for element in value.references:
|
|
|
|
if isinstance(element.structure, str):
|
2019-07-01 13:08:48 +02:00
|
|
|
# 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)
|
|
|
|
|
|
|
|
|