diff --git a/src/ifcpatch/ifcpatch.py b/src/ifcpatch/ifcpatch.py index 6def4ce78c..00b88b97af 100644 --- a/src/ifcpatch/ifcpatch.py +++ b/src/ifcpatch/ifcpatch.py @@ -15,12 +15,17 @@ def execute(args, is_library=None): args['input'], ifc_file, logger, args['arguments']) print('# Patching ...') patcher.patch() - if is_library is not None: + ifc_file = patcher.file + if is_library is True: return ifc_file print('# Writing patched file ...') if not args['output']: args['output'] = args['input'] - ifc_file.write(args.output) + if isinstance(ifc_file, str): + with open(args['output'], 'w') as text_file: + text_file.write(ifc_file) + else: + ifc_file.write(args['output']) if __name__ == '__main__': parser = argparse.ArgumentParser( diff --git a/src/ifcpatch/recipes/RecycleNonRootedElements.py b/src/ifcpatch/recipes/RecycleNonRootedElements.py index 6407e24150..48ec179dd5 100644 --- a/src/ifcpatch/recipes/RecycleNonRootedElements.py +++ b/src/ifcpatch/recipes/RecycleNonRootedElements.py @@ -1,3 +1,6 @@ +from collections import deque +import ifcopenshell.util.element + class Patcher: def __init__(self, src, file, logger, args=None): self.src = src @@ -6,15 +9,27 @@ class Patcher: self.args = args def patch(self): - import ifcopenshell.util.element + deleted = [] hashes = {} for element in self.file: if element.is_a('IfcRoot'): continue h = hash(tuple(element)) if h in hashes: - for inverse in f.get_inverse(element): + for inverse in self.file.get_inverse(element): ifcopenshell.util.element.replace_attribute(inverse, element, hashes[h]) - self.file.remove(element) + deleted.append(element.id()) else: hashes[h] = element + deleted.sort() + deleted_q = deque(deleted) + new = '' + for line in self.file.wrapped_data.to_string().split('\n'): + try: + if int(line.split('=')[0][1:]) != deleted_q[0]: + new += (line + '\n') + else: + deleted_q.popleft() + except: + new += (line + '\n') + self.file = new