From 8ca6183313e4692941f9daa9b76bdf8df86d84fb Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Sun, 25 Jul 2021 11:39:11 +1000 Subject: [PATCH] Fix #169. New recipe to fix duplicate and invalid GlobalIds. --- .../ifcpatch/recipes/RegenerateGlobalIds.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/ifcpatch/ifcpatch/recipes/RegenerateGlobalIds.py diff --git a/src/ifcpatch/ifcpatch/recipes/RegenerateGlobalIds.py b/src/ifcpatch/ifcpatch/recipes/RegenerateGlobalIds.py new file mode 100644 index 0000000000..67cd3af170 --- /dev/null +++ b/src/ifcpatch/ifcpatch/recipes/RegenerateGlobalIds.py @@ -0,0 +1,27 @@ +import ifcopenshell + + +class Patcher: + def __init__(self, src, file, logger, args=None): + self.src = src + self.file = file + self.logger = logger + self.args = args + + def patch(self): + if self.args and self.args[0] == "DUPLICATE": + guids = set() + for element in self.file.by_type("IfcRoot"): + if element.GlobalId in guids: + element.GlobalId = ifcopenshell.guid.new() + elif len(element.GlobalId) != 22 or element.GlobalId[0] not in "0123": + element.GlobalId = ifcopenshell.guid.new() + else: + try: + ifcopenshell.guid.expand(element.GlobalId) + except: + element.GlobalId = ifcopenshell.guid.new() + guids.add(element.GlobalId) + else: + for element in self.file.by_type("IfcRoot"): + element.GlobalId = ifcopenshell.guid.new()