2022-01-19 12:18:33 +11:00
|
|
|
# IfcOpenShell - IFC toolkit and geometry engine
|
|
|
|
|
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
|
|
|
|
|
#
|
|
|
|
|
# This file is part of IfcOpenShell.
|
|
|
|
|
#
|
|
|
|
|
# IfcOpenShell 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.
|
|
|
|
|
#
|
|
|
|
|
# IfcOpenShell 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 IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
2021-04-21 20:49:58 +10:00
|
|
|
import ifcopenshell
|
|
|
|
|
import ifcopenshell.api
|
2022-02-09 15:16:22 +11:00
|
|
|
import ifcopenshell.api.owner.settings
|
2021-04-21 20:49:58 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Usecase:
|
|
|
|
|
def __init__(self, file, **settings):
|
|
|
|
|
self.file = file
|
2021-06-18 15:54:01 +10:00
|
|
|
self.settings = {"library": None, "element": None}
|
2021-04-21 20:49:58 +10:00
|
|
|
for key, value in settings.items():
|
|
|
|
|
self.settings[key] = value
|
|
|
|
|
|
|
|
|
|
def execute(self):
|
2021-06-18 15:54:01 +10:00
|
|
|
self.added_elements = set()
|
2021-08-14 12:05:59 +10:00
|
|
|
self.whitelisted_inverse_attributes = {}
|
2021-08-08 20:56:31 +10:00
|
|
|
if self.settings["element"].is_a("IfcTypeProduct"):
|
2021-06-18 15:54:01 +10:00
|
|
|
return self.append_type_product()
|
2022-01-18 17:44:50 +11:00
|
|
|
elif self.settings["element"].is_a("IfcProduct"):
|
|
|
|
|
return self.append_product()
|
2021-08-09 11:17:10 +10:00
|
|
|
elif self.settings["element"].is_a("IfcMaterial"):
|
|
|
|
|
return self.append_material()
|
2021-08-10 09:35:09 +10:00
|
|
|
elif self.settings["element"].is_a("IfcCostSchedule"):
|
|
|
|
|
return self.append_cost_schedule()
|
2021-08-14 12:05:59 +10:00
|
|
|
elif self.settings["element"].is_a("IfcProfileDef"):
|
|
|
|
|
return self.append_profile_def()
|
2021-08-10 09:35:09 +10:00
|
|
|
|
2022-01-18 17:44:50 +11:00
|
|
|
def get_existing_element(self):
|
2021-08-10 09:35:09 +10:00
|
|
|
try:
|
2022-01-18 17:44:50 +11:00
|
|
|
return self.file.by_guid(self.settings["element"].GlobalId)
|
2021-08-10 09:35:09 +10:00
|
|
|
except:
|
|
|
|
|
return False
|
2021-08-09 11:17:10 +10:00
|
|
|
|
|
|
|
|
def append_material(self):
|
2021-08-10 09:35:09 +10:00
|
|
|
if [e for e in self.file.by_type("IfcMaterial") if e.Name == self.settings["element"].Name]:
|
|
|
|
|
return
|
2022-07-27 00:13:55 +01:00
|
|
|
self.whitelisted_inverse_attributes = {"IfcMaterial": ["HasProperties", "HasRepresentation"]}
|
2022-09-03 22:16:35 +10:00
|
|
|
self.existing_contexts = self.file.by_type("IfcGeometricRepresentationContext")
|
2022-07-27 00:13:55 +01:00
|
|
|
element = self.add_element(self.settings["element"])
|
|
|
|
|
if not element.HasRepresentation:
|
|
|
|
|
return element
|
|
|
|
|
added_contexts = [
|
|
|
|
|
e
|
|
|
|
|
for e in self.file.traverse(element.HasRepresentation[0])
|
2022-09-03 22:16:35 +10:00
|
|
|
if e.is_a("IfcGeometricRepresentationContext")
|
2022-07-27 00:13:55 +01:00
|
|
|
]
|
|
|
|
|
for added_context in added_contexts:
|
|
|
|
|
equivalent_existing_context = self.get_equivalent_existing_context(added_context)
|
|
|
|
|
if not equivalent_existing_context:
|
|
|
|
|
equivalent_existing_context = self.create_equivalent_context(added_context)
|
|
|
|
|
for inverse in self.file.get_inverse(added_context):
|
|
|
|
|
ifcopenshell.util.element.replace_attribute(inverse, added_context, equivalent_existing_context)
|
|
|
|
|
for added_context in added_contexts:
|
|
|
|
|
ifcopenshell.util.element.remove_deep(self.file, added_context)
|
|
|
|
|
return element
|
2021-06-18 15:54:01 +10:00
|
|
|
|
2021-08-10 09:35:09 +10:00
|
|
|
def append_cost_schedule(self):
|
2022-01-18 17:44:50 +11:00
|
|
|
element = self.get_existing_element()
|
|
|
|
|
if element:
|
|
|
|
|
return element
|
2021-08-10 09:35:09 +10:00
|
|
|
self.whitelisted_inverse_attributes = {"IfcCostSchedule": ["Controls"], "IfcCostItem": ["IsNestedBy"]}
|
|
|
|
|
return self.add_element(self.settings["element"])
|
|
|
|
|
|
2021-08-14 12:05:59 +10:00
|
|
|
def append_profile_def(self):
|
|
|
|
|
if [e for e in self.file.by_type("IfcProfileDef") if e.ProfileName == self.settings["element"].ProfileName]:
|
|
|
|
|
return
|
2021-08-14 13:01:22 +10:00
|
|
|
self.whitelisted_inverse_attributes = {"IfcProfileDef": ["HasProperties"]}
|
2021-08-14 12:05:59 +10:00
|
|
|
return self.add_element(self.settings["element"])
|
|
|
|
|
|
2021-06-18 15:54:01 +10:00
|
|
|
def append_type_product(self):
|
2022-01-18 17:44:50 +11:00
|
|
|
element = self.get_existing_element()
|
|
|
|
|
if element:
|
|
|
|
|
return element
|
2021-06-18 15:54:01 +10:00
|
|
|
self.whitelisted_inverse_attributes = {
|
|
|
|
|
"IfcObjectDefinition": ["HasAssociations"],
|
|
|
|
|
"IfcMaterialDefinition": ["HasExternalReferences", "HasProperties"],
|
|
|
|
|
"IfcRepresentationItem": ["StyledByItem"],
|
|
|
|
|
}
|
2021-04-21 20:49:58 +10:00
|
|
|
self.existing_contexts = self.file.by_type("IfcGeometricRepresentationContext")
|
2022-09-03 22:16:35 +10:00
|
|
|
element = self.add_element(self.settings["element"])
|
|
|
|
|
added_contexts = [e for e in self.file.traverse(element) if e.is_a("IfcGeometricRepresentationContext")]
|
2021-04-21 20:49:58 +10:00
|
|
|
for added_context in added_contexts:
|
|
|
|
|
equivalent_existing_context = self.get_equivalent_existing_context(added_context)
|
|
|
|
|
if not equivalent_existing_context:
|
|
|
|
|
equivalent_existing_context = self.create_equivalent_context(added_context)
|
|
|
|
|
for inverse in self.file.get_inverse(added_context):
|
|
|
|
|
ifcopenshell.util.element.replace_attribute(inverse, added_context, equivalent_existing_context)
|
|
|
|
|
for added_context in added_contexts:
|
2021-08-24 18:08:06 +10:00
|
|
|
ifcopenshell.util.element.remove_deep(self.file, added_context)
|
2021-04-21 20:49:58 +10:00
|
|
|
return element
|
|
|
|
|
|
2022-01-18 17:44:50 +11:00
|
|
|
def append_product(self):
|
|
|
|
|
element = self.get_existing_element()
|
|
|
|
|
if element:
|
|
|
|
|
return element
|
|
|
|
|
self.whitelisted_inverse_attributes = {
|
|
|
|
|
"IfcObjectDefinition": ["HasAssociations"],
|
|
|
|
|
"IfcObject": ["IsDefinedBy.IfcRelDefinesByProperties"],
|
|
|
|
|
"IfcElement": ["HasOpenings"],
|
|
|
|
|
"IfcMaterialDefinition": ["HasExternalReferences", "HasProperties"],
|
|
|
|
|
"IfcRepresentationItem": ["StyledByItem"],
|
|
|
|
|
}
|
|
|
|
|
self.existing_contexts = self.file.by_type("IfcGeometricRepresentationContext")
|
2022-09-03 22:16:35 +10:00
|
|
|
element = self.add_element(self.settings["element"])
|
|
|
|
|
added_contexts = [e for e in self.file.traverse(element) if e.is_a("IfcGeometricRepresentationContext")]
|
2022-01-18 17:44:50 +11:00
|
|
|
for added_context in added_contexts:
|
|
|
|
|
equivalent_existing_context = self.get_equivalent_existing_context(added_context)
|
|
|
|
|
if not equivalent_existing_context:
|
|
|
|
|
equivalent_existing_context = self.create_equivalent_context(added_context)
|
|
|
|
|
for inverse in self.file.get_inverse(added_context):
|
|
|
|
|
ifcopenshell.util.element.replace_attribute(inverse, added_context, equivalent_existing_context)
|
|
|
|
|
for added_context in added_contexts:
|
2022-06-09 18:59:20 +10:00
|
|
|
ifcopenshell.util.element.remove_deep2(self.file, added_context)
|
2022-01-18 17:44:50 +11:00
|
|
|
|
|
|
|
|
element_type = ifcopenshell.util.element.get_type(self.settings["element"])
|
|
|
|
|
if element_type:
|
|
|
|
|
ifcopenshell.api.owner.settings.factory_reset()
|
|
|
|
|
new_type = ifcopenshell.api.run(
|
|
|
|
|
"project.append_asset", self.file, library=self.settings["library"], element=element_type
|
|
|
|
|
)
|
|
|
|
|
ifcopenshell.api.run(
|
|
|
|
|
"type.assign_type",
|
|
|
|
|
self.file,
|
|
|
|
|
should_run_listeners=False,
|
|
|
|
|
related_object=element,
|
|
|
|
|
relating_type=new_type,
|
|
|
|
|
)
|
|
|
|
|
ifcopenshell.api.owner.settings.restore()
|
|
|
|
|
|
|
|
|
|
return element
|
|
|
|
|
|
2021-06-18 15:54:01 +10:00
|
|
|
def add_element(self, element):
|
|
|
|
|
if element.id() == 0 or element.id() in self.added_elements:
|
|
|
|
|
return
|
|
|
|
|
new = self.file.add(element)
|
|
|
|
|
self.added_elements.add(element.id())
|
|
|
|
|
self.add_inverse(element)
|
|
|
|
|
for subelement in self.settings["library"].traverse(element):
|
|
|
|
|
self.add_inverse(subelement)
|
|
|
|
|
return new
|
|
|
|
|
|
|
|
|
|
def add_inverse(self, element):
|
2022-01-18 17:44:50 +11:00
|
|
|
for source_class, attributes in self.whitelisted_inverse_attributes.items():
|
|
|
|
|
if not element.is_a(source_class):
|
|
|
|
|
continue
|
|
|
|
|
for attribute in attributes:
|
|
|
|
|
attribute_class = None
|
|
|
|
|
if "." in attribute:
|
|
|
|
|
attribute, attribute_class = attribute.split(".")
|
|
|
|
|
for inverse in getattr(element, attribute, []):
|
|
|
|
|
if attribute_class and inverse.is_a(attribute_class):
|
|
|
|
|
self.add_element(inverse)
|
|
|
|
|
elif not attribute_class:
|
|
|
|
|
self.add_element(inverse)
|
2021-06-18 15:54:01 +10:00
|
|
|
|
2021-04-21 20:49:58 +10:00
|
|
|
def get_equivalent_existing_context(self, added_context):
|
|
|
|
|
for context in self.existing_contexts:
|
|
|
|
|
if context.is_a() != added_context.is_a():
|
|
|
|
|
continue
|
|
|
|
|
if context.is_a("IfcGeometricRepresentationSubContext"):
|
|
|
|
|
if (
|
|
|
|
|
context.ContextType == added_context.ContextType
|
|
|
|
|
and context.ContextIdentifier == added_context.ContextIdentifier
|
|
|
|
|
and context.TargetView == added_context.TargetView
|
|
|
|
|
):
|
|
|
|
|
return context
|
|
|
|
|
elif (
|
|
|
|
|
context.ContextType == added_context.ContextType
|
|
|
|
|
and context.ContextIdentifier == added_context.ContextIdentifier
|
|
|
|
|
):
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
def create_equivalent_context(self, added_context):
|
|
|
|
|
if added_context.is_a("IfcGeometricRepresentationSubContext"):
|
2022-09-05 11:04:29 +10:00
|
|
|
parent = self.get_equivalent_existing_context(added_context.ParentContext)
|
|
|
|
|
if not parent:
|
|
|
|
|
parent = self.create_equivalent_context(added_context.ParentContext)
|
2021-04-21 20:49:58 +10:00
|
|
|
return ifcopenshell.api.run(
|
|
|
|
|
"context.add_context",
|
2022-09-05 11:04:29 +10:00
|
|
|
self.file,
|
|
|
|
|
parent=parent,
|
|
|
|
|
context_type=added_context.ContextType,
|
|
|
|
|
context_identifier=added_context.ContextIdentifier,
|
2021-04-21 20:49:58 +10:00
|
|
|
target_view=added_context.TargetView,
|
|
|
|
|
)
|
|
|
|
|
return ifcopenshell.api.run(
|
2022-09-05 11:04:29 +10:00
|
|
|
"context.add_context", self.file, context_type=added_context.ContextType,
|
|
|
|
|
context_identifier=added_context.ContextIdentifier
|
2021-04-21 20:49:58 +10:00
|
|
|
)
|