mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-09 09:21:46 +00:00
Optimise the optimisation recipe. See #944.
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user