From 15661536dc1f9a40526420dcd859a97ec9ca6665 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Thu, 9 Jun 2022 19:01:32 +1000 Subject: [PATCH] Extract elements recipe now handles spatial elements as an upgrade to the ExtractSpaces recipe --- .../ifcpatch/recipes/ExtractElements.py | 32 ++++++++----- .../ifcpatch/recipes/ExtractSpaces.py | 48 ------------------- 2 files changed, 20 insertions(+), 60 deletions(-) delete mode 100644 src/ifcpatch/ifcpatch/recipes/ExtractSpaces.py diff --git a/src/ifcpatch/ifcpatch/recipes/ExtractElements.py b/src/ifcpatch/ifcpatch/recipes/ExtractElements.py index 69c5b56173..ac35c43117 100644 --- a/src/ifcpatch/ifcpatch/recipes/ExtractElements.py +++ b/src/ifcpatch/ifcpatch/recipes/ExtractElements.py @@ -50,23 +50,31 @@ class Patcher: self.file = self.new def add_element(self, element): - new_element = ifcopenshell.api.run("project.append_asset", self.new, library=self.file, element=element) + new_element = self.append_asset(element) if not new_element: return - for rel in element.ContainedInStructure: + for rel in getattr(element, "ContainedInStructure", []): spatial_element = rel.RelatingStructure - new_spatial_element = self.new.add(spatial_element) + new_spatial_element = self.append_asset(spatial_element) self.contained_ins.setdefault(spatial_element.GlobalId, set()).add(new_element) - self.add_spatial_tree(spatial_element, new_spatial_element) - for rel in element.Decomposes: - ifcopenshell.api.run("project.append_asset", self.new, library=self.file, element=rel.RelatingObject) - self.aggregates.setdefault(rel.RelatingObject.GlobalId, set()).add(new_element) + self.add_decomposition_parents(spatial_element, new_spatial_element) + self.add_decomposition_parents(element, new_element) - def add_spatial_tree(self, spatial_element, new_spatial_element): - for rel in spatial_element.Decomposes: - new = self.new.add(rel.RelatingObject) - self.aggregates.setdefault(rel.RelatingObject.GlobalId, set()).add(new_spatial_element) - self.add_spatial_tree(rel.RelatingObject, new) + def append_asset(self, element): + try: + return self.new.by_guid(element.GlobalId) + except: + pass + if element.is_a("IfcProject"): + return self.new.add(element) + return ifcopenshell.api.run("project.append_asset", self.new, library=self.file, element=element) + + def add_decomposition_parents(self, element, new_element): + for rel in element.Decomposes: + parent = rel.RelatingObject + new_parent = self.append_asset(parent) + self.aggregates.setdefault(parent.GlobalId, set()).add(new_element) + self.add_decomposition_parents(parent, new_parent) def create_spatial_tree(self): for relating_structure, related_elements in self.contained_ins.items(): diff --git a/src/ifcpatch/ifcpatch/recipes/ExtractSpaces.py b/src/ifcpatch/ifcpatch/recipes/ExtractSpaces.py deleted file mode 100644 index ca5077e401..0000000000 --- a/src/ifcpatch/ifcpatch/recipes/ExtractSpaces.py +++ /dev/null @@ -1,48 +0,0 @@ - -# IfcPatch - IFC patching utiliy -# Copyright (C) 2020, 2021 Dion Moult -# -# This file is part of IfcPatch. -# -# IfcPatch is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# IfcPatch is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with IfcPatch. If not, see . - -import ifcopenshell -import ifcopenshell.util.selector - - -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): - self.contained_ins = {} - self.aggregates = {} - self.new = ifcopenshell.file(schema=self.file.wrapped_data.schema) - self.owner_history = None - for owner_history in self.file.by_type("IfcOwnerHistory"): - self.owner_history = self.new.add(owner_history) - break - selector = ifcopenshell.util.selector.Selector() - for space in selector.parse(self.file, ".IfcSpace"): - self.add_element(space) - self.file = self.new - - def add_element(self, element): - self.new.add(element) - for rel_aggregate in element.Decomposes: - self.add_element(rel_aggregate.RelatingObject) - self.new.add(rel_aggregate)