blender-gdsimporter/gds/library.py

58 lines
1.6 KiB
Python

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
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)