library: add function to link structure references
This commit is contained in:
		
							parent
							
								
									73af3adb34
								
							
						
					
					
						commit
						06bc11fb17
					
				@ -1,4 +1,6 @@
 | 
				
			|||||||
from datetime import datetime
 | 
					from datetime import datetime
 | 
				
			||||||
 | 
					from .elements import SRef
 | 
				
			||||||
 | 
					from .reader import ProgressGetter
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Library(object):
 | 
					class Library(object):
 | 
				
			||||||
    version = 0
 | 
					    version = 0
 | 
				
			||||||
@ -11,4 +13,46 @@ class Library(object):
 | 
				
			|||||||
    units_per_dbunit = 1
 | 
					    units_per_dbunit = 1
 | 
				
			||||||
    meters_per_unit  = 1
 | 
					    meters_per_unit  = 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    structures = {}
 | 
					    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)
 | 
				
			||||||
 | 
					                    
 | 
				
			||||||
 | 
					                    
 | 
				
			||||||
@ -6,5 +6,7 @@ class Structure(object):
 | 
				
			|||||||
    last_mod      = datetime.now()
 | 
					    last_mod      = datetime.now()
 | 
				
			||||||
    name          = "NONAME"
 | 
					    name          = "NONAME"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # contains all the elements
 | 
					    # contains all the low level elements
 | 
				
			||||||
    elements = []
 | 
					    elements = []
 | 
				
			||||||
 | 
					    # contains all sref and aref elements
 | 
				
			||||||
 | 
					    references = [] 
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user