Fix bug where adding a representation to type instances didn't ensure it was added to a geometric type

This commit is contained in:
Dion Moult
2021-11-05 15:14:10 +11:00
parent 0da5d807b8
commit 07bee4acfa
4 changed files with 94 additions and 23 deletions
@@ -1,4 +1,5 @@
import ifcopenshell.api
import ifcopenshell.util.element
class Usecase:
@@ -10,13 +11,16 @@ class Usecase:
def execute(self):
if self.settings["product"].is_a("IfcProduct"):
definition = self.settings["product"].Representation
if not definition:
definition = self.file.createIfcProductDefinitionShape()
self.settings["product"].Representation = definition
representations = list(definition.Representations) if definition.Representations else []
representations.append(self.settings["representation"])
definition.Representations = representations
product_type = ifcopenshell.util.element.get_type(self.settings["product"])
if (
product_type
and product_type.RepresentationMaps
and self.settings["representation"].RepresentationType != "MappedRepresentation"
):
self.settings["product"] = product_type
if self.settings["product"].is_a("IfcProduct"):
self.assign_product_representation(self.settings["product"], self.settings["representation"])
elif self.settings["product"].is_a("IfcTypeProduct"):
if self.settings["product"].RepresentationMaps:
maps = list(self.settings["product"].RepresentationMaps)
@@ -44,9 +48,14 @@ class Usecase:
mapped_representation = ifcopenshell.api.run(
"geometry.map_representation", self.file, **{"representation": self.settings["representation"]}
)
ifcopenshell.api.run(
"geometry.assign_representation",
self.file,
**{"product": element, "representation": mapped_representation}
)
self.assign_product_representation(element, mapped_representation)
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": self.settings["product"]})
def assign_product_representation(self, product, representation):
definition = product.Representation
if not definition:
definition = self.file.createIfcProductDefinitionShape()
product.Representation = definition
representations = list(definition.Representations) if definition.Representations else []
representations.append(representation)
definition.Representations = representations
@@ -20,7 +20,6 @@ class Usecase:
if self.settings["related_object"].Representation:
representations = self.settings["related_object"].Representation.Representations
for representation in representations:
print('for each rep', representation)
ifcopenshell.api.run(
"geometry.unassign_representation",
self.file,
@@ -0,0 +1,73 @@
import test.bootstrap
import ifcopenshell.api
class TestAssignRepresentation(test.bootstrap.IFC4):
def test_assigning_to_a_product(self):
wall = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
rep = self.file.createIfcShapeRepresentation()
ifcopenshell.api.run("geometry.assign_representation", self.file, product=wall, representation=rep)
assert wall.Representation.Representations == (rep,)
def test_assigning_to_a_product_with_existing_representations(self):
wall = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
rep = self.file.createIfcShapeRepresentation()
rep2 = self.file.createIfcShapeRepresentation()
ifcopenshell.api.run("geometry.assign_representation", self.file, product=wall, representation=rep)
ifcopenshell.api.run("geometry.assign_representation", self.file, product=wall, representation=rep2)
assert wall.Representation.Representations == (rep, rep2)
def test_assigning_to_a_type_product(self):
walltype = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
rep = self.file.createIfcShapeRepresentation()
rep2 = self.file.createIfcShapeRepresentation()
ifcopenshell.api.run("geometry.assign_representation", self.file, product=walltype, representation=rep)
ifcopenshell.api.run("geometry.assign_representation", self.file, product=walltype, representation=rep2)
assert walltype.RepresentationMaps[0].MappedRepresentation == rep
assert walltype.RepresentationMaps[1].MappedRepresentation == rep2
def test_assigning_to_a_type_will_map_representations_to_instances(self):
wall = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
walltype = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
ifcopenshell.api.run("type.assign_type", self.file, related_object=wall, relating_type=walltype)
context = self.file.createIfcGeometricRepresentationContext()
rep = self.file.createIfcShapeRepresentation(ContextOfItems=context)
rep2 = self.file.createIfcShapeRepresentation(ContextOfItems=context)
ifcopenshell.api.run("geometry.assign_representation", self.file, product=walltype, representation=rep)
ifcopenshell.api.run("geometry.assign_representation", self.file, product=walltype, representation=rep2)
assert wall.Representation.Representations[0].RepresentationType == "MappedRepresentation"
assert wall.Representation.Representations[0].Items[0].MappingSource.MappedRepresentation == rep
assert wall.Representation.Representations[0].Items[0].MappingSource == walltype.RepresentationMaps[0]
assert wall.Representation.Representations[1].RepresentationType == "MappedRepresentation"
assert wall.Representation.Representations[1].Items[0].MappingSource.MappedRepresentation == rep2
assert wall.Representation.Representations[1].Items[0].MappingSource == walltype.RepresentationMaps[1]
def test_assigning_to_an_instance_with_a_geometric_type_adds_it_to_both_the_instance_and_type(self):
context = self.file.createIfcGeometricRepresentationContext()
rep = self.file.createIfcShapeRepresentation(ContextOfItems=context)
rep2 = self.file.createIfcShapeRepresentation(ContextOfItems=context)
walltype = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
ifcopenshell.api.run("geometry.assign_representation", self.file, product=walltype, representation=rep)
wall = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("type.assign_type", self.file, related_object=wall, relating_type=walltype)
ifcopenshell.api.run("geometry.assign_representation", self.file, product=wall, representation=rep2)
assert wall.Representation.Representations[0].RepresentationType == "MappedRepresentation"
assert wall.Representation.Representations[0].Items[0].MappingSource.MappedRepresentation == rep
assert walltype.RepresentationMaps[0].MappedRepresentation == rep
assert wall.Representation.Representations[1].RepresentationType == "MappedRepresentation"
assert wall.Representation.Representations[1].Items[0].MappingSource.MappedRepresentation == rep2
assert walltype.RepresentationMaps[1].MappedRepresentation == rep2
def test_assigning_to_an_instance_with_a_nongeometric_type_only_adds_it_to_the_instance(self):
context = self.file.createIfcGeometricRepresentationContext()
rep = self.file.createIfcShapeRepresentation(ContextOfItems=context)
walltype = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
wall = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("type.assign_type", self.file, related_object=wall, relating_type=walltype)
ifcopenshell.api.run("geometry.assign_representation", self.file, product=wall, representation=rep)
assert wall.Representation.Representations[0].RepresentationType != "MappedRepresentation"
assert wall.Representation.Representations[0] == rep
assert not walltype.RepresentationMaps