Fix a variety of IFC validation errors in entity creation and asset appending

This commit is contained in:
Dion Moult
2022-10-14 18:19:26 +11:00
parent 10bde73c1a
commit a34c0cb35c
9 changed files with 217 additions and 1395 deletions
@@ -79,4 +79,6 @@ class Usecase:
RelatedElement=self.settings["related_element"],
RelatingConnectionType=self.settings["relating_connection"],
RelatedConnectionType=self.settings["related_connection"],
RelatingPriorities=[],
RelatedPriorities=[],
)
@@ -66,14 +66,7 @@ class Usecase:
for e in self.file.traverse(element.HasRepresentation[0])
if e.is_a("IfcGeometricRepresentationContext")
]
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)
self.reuse_existing_contexts(added_contexts)
return element
def append_cost_schedule(self):
@@ -101,14 +94,7 @@ class Usecase:
self.existing_contexts = self.file.by_type("IfcGeometricRepresentationContext")
element = self.add_element(self.settings["element"])
added_contexts = [e for e in self.file.traverse(element) if e.is_a("IfcGeometricRepresentationContext")]
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)
self.reuse_existing_contexts(added_contexts)
return element
def append_product(self):
@@ -125,14 +111,7 @@ class Usecase:
self.existing_contexts = self.file.by_type("IfcGeometricRepresentationContext")
element = self.add_element(self.settings["element"])
added_contexts = [e for e in self.file.traverse(element) if e.is_a("IfcGeometricRepresentationContext")]
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_deep2(self.file, added_context)
self.reuse_existing_contexts(added_contexts)
element_type = ifcopenshell.util.element.get_type(self.settings["element"])
if element_type:
@@ -159,6 +138,7 @@ class Usecase:
new = self.file.add(element)
self.added_elements[element.id()] = new
for subelement in self.settings["library"].traverse(element):
self.added_elements[subelement.id()] = self.file.add(subelement)
self.check_inverses(subelement)
return new
@@ -206,6 +186,16 @@ class Usecase:
return True
return False
def reuse_existing_contexts(self, added_contexts):
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_deep2(self.file, added_context)
def get_equivalent_existing_context(self, added_context):
for context in self.existing_contexts:
if context.is_a() != added_context.is_a():
@@ -50,14 +50,46 @@ class Usecase:
element.ObjectType = self.settings["predefined_type"]
elif hasattr(element, "ElementType"):
element.ElementType = self.settings["predefined_type"]
elif hasattr(element, "ProcessType"):
element.ProcessType = self.settings["predefined_type"]
elif hasattr(element, "ObjectType"):
element.ObjectType = self.settings["predefined_type"]
if self.file.schema == "IFC2X3":
self.handle_2x3_defaults(element)
elif self.file.schema == "IFC4":
self.handle_4_defaults(element)
return element
def handle_2x3_defaults(self, element):
if element.is_a("IfcElement") or element.is_a("IfcElementType"):
if not element.PredefinedType:
element.PredefinedType = "NOTDEFINED"
if element.is_a("IfcSpatialStructureElement"):
element.CompositionType = "ELEMENT"
elif element.is_a("IfcRoof"):
element.ShapeType = "NOTDEFINED"
elif element.is_a("IfcFurnitureType"):
element.AssemblyPlace = "NOTDEFINED"
elif element.is_a("IfcDoorStyle") or element.is_a("IfcWindowStyle"):
element.OperationType = "NOTDEFINED"
element.ConstructionType = "NOTDEFINED"
element.ParameterTakesPrecedence = False
element.Sizeable = False
def handle_4_defaults(self, element):
if element.is_a("IfcElement") or element.is_a("IfcElementType"):
if not element.PredefinedType:
element.PredefinedType = "NOTDEFINED"
if element.is_a("IfcDoorStyle") or element.is_a("IfcWindowStyle"):
element.OperationType = "NOTDEFINED"
element.ConstructionType = "NOTDEFINED"
element.ParameterTakesPrecedence = False
element.Sizeable = False
elif element.is_a("IfcDoorType"):
element.OperationType = "NOTDEFINED"
elif element.is_a("IfcWindowType"):
element.PartitioningType = "NOTDEFINED"
elif element.is_a("IfcFurnitureType"):
element.AssemblyPlace = "NOTDEFINED"
@@ -69,7 +69,8 @@ class TestAppendAsset(test.bootstrap.IFC4):
library.createIfcStyledItem(Item=item)
mapped_rep = library.createIfcShapeRepresentation(Items=[item])
element.RepresentationMaps = [library.createIfcRepresentationMap(MappedRepresentation=mapped_rep)]
ifcopenshell.api.run("project.append_asset", self.file, library=library, element=element)
new = ifcopenshell.api.run("project.append_asset", self.file, library=library, element=element)
assert len(new.RepresentationMaps[0].MappedRepresentation.Items[0].StyledByItem) == 1
assert self.file.by_type("IfcStyledItem")[0].Item == self.file.by_type("IfcBoundingBox")[0]
def test_append_a_material(self):
@@ -108,6 +109,27 @@ class TestAppendAsset(test.bootstrap.IFC4):
assert len(self.file.by_type("IfcGeometricRepresentationContext")) == 1
context = self.file.by_type("IfcMaterial")[0].HasRepresentation[0].Representations[0].ContextOfItems
assert context == file_context
assert context.WorldCoordinateSystem
def test_append_a_material_with_a_representation_and_reuse_an_existing_subcontext(self):
ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
file_context = ifcopenshell.api.run("context.add_context", self.file, context_type="Model")
file_subcontext = ifcopenshell.api.run("context.add_context", self.file, parent=file_context, context_type="Model", context_identifier="Body", target_view="MODEL_VIEW")
library = ifcopenshell.api.run("project.create_file")
ifcopenshell.api.run("root.create_entity", library, ifc_class="IfcProject")
material = ifcopenshell.api.run("material.add_material", library, name="Material")
style = ifcopenshell.api.run("style.add_style", library)
context = ifcopenshell.api.run("context.add_context", library, context_type="Model")
subcontext = ifcopenshell.api.run("context.add_context", library, parent=context, context_type="Model", context_identifier="Body", target_view="MODEL_VIEW")
ifcopenshell.api.run("style.assign_material_style", library, material=material, style=style, context=subcontext)
ifcopenshell.api.run("project.append_asset", self.file, library=library, element=material)
assert len(self.file.by_type("IfcMaterial")) == 1
assert len(self.file.by_type("IfcGeometricRepresentationContext", include_subtypes=False)) == 1
assert len(self.file.by_type("IfcGeometricRepresentationSubContext", include_subtypes=False)) == 1
subcontext = self.file.by_type("IfcMaterial")[0].HasRepresentation[0].Representations[0].ContextOfItems
assert subcontext == file_subcontext
assert subcontext.ParentContext.WorldCoordinateSystem
def test_append_a_material_with_a_representation_and_reuse_an_existing_context_by_a_new_subcontext(self):
ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject")
@@ -0,0 +1,72 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2022 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/>.
import test.bootstrap
import ifcopenshell.api
class TestCreateEntity(test.bootstrap.IFC4):
def test_creating_a_simple_entity_with_automatic_global_id(self):
wall = ifcopenshell.api.run(
"root.create_entity", self.file, ifc_class="IfcWall", predefined_type="SOLIDWALL", name="Foo"
)
assert wall.is_a() == "IfcWall"
assert len(wall.GlobalId) == 22
assert wall.Name == "Foo"
assert wall.PredefinedType == "SOLIDWALL"
def test_handling_predefined_types(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall", name="Foo")
assert element.PredefinedType == "NOTDEFINED"
element = ifcopenshell.api.run(
"root.create_entity", self.file, ifc_class="IfcWall", predefined_type="SHEAR", name="Foo"
)
assert element.PredefinedType == "SHEAR"
element = ifcopenshell.api.run(
"root.create_entity", self.file, ifc_class="IfcWall", predefined_type="Foobar", name="Foo"
)
assert element.PredefinedType == "USERDEFINED"
assert element.ObjectType == "Foobar"
element = ifcopenshell.api.run(
"root.create_entity", self.file, ifc_class="IfcWallType", predefined_type="Foobar", name="Foo"
)
assert element.PredefinedType == "USERDEFINED"
assert element.ElementType == "Foobar"
element = ifcopenshell.api.run(
"root.create_entity", self.file, ifc_class="IfcTaskType", predefined_type="Foobar", name="Foo"
)
assert element.PredefinedType == "USERDEFINED"
assert element.ProcessType == "Foobar"
def test_setting_default_values_for_validity(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcDoorStyle", name="Foo")
assert element.OperationType == "NOTDEFINED"
assert element.ConstructionType == "NOTDEFINED"
assert element.ParameterTakesPrecedence == False
assert element.Sizeable == False
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWindowStyle", name="Foo")
assert element.OperationType == "NOTDEFINED"
assert element.ConstructionType == "NOTDEFINED"
assert element.ParameterTakesPrecedence == False
assert element.Sizeable == False
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcDoorType", name="Foo")
assert element.OperationType == "NOTDEFINED"
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWindowType", name="Foo")
assert element.PartitioningType == "NOTDEFINED"
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcFurnitureType", name="Foo")
assert element.AssemblyPlace == "NOTDEFINED"