Extract elements recipe now handles spatial elements as an upgrade to the ExtractSpaces recipe

This commit is contained in:
Dion Moult
2022-06-09 19:01:32 +10:00
parent f451c51451
commit 15661536dc
2 changed files with 20 additions and 60 deletions
@@ -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():
@@ -1,48 +0,0 @@
# IfcPatch - IFC patching utiliy
# Copyright (C) 2020, 2021 Dion Moult <dion@thinkmoult.com>
#
# 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 <http://www.gnu.org/licenses/>.
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)