You can now associate annotations with objects for smart annotations in IFC. See #1153.

This commit is contained in:
Dion Moult
2022-01-14 18:00:08 +11:00
parent 258ca7fb8c
commit f8c7f48674
15 changed files with 311 additions and 73 deletions
@@ -0,0 +1,43 @@
import ifcopenshell
import ifcopenshell.api
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {
"relating_product": None,
"related_object": None,
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
if self.settings["related_object"].HasAssignments:
for assignment in self.settings["related_object"].HasAssignments:
if (
assignment.is_a("IfcRelAssignsToProduct")
and assignment.RelatingProduct == self.settings["relating_product"]
):
return
referenced_by = None
if self.settings["relating_product"].ReferencedBy:
referenced_by = self.settings["relating_product"].ReferencedBy[0]
if referenced_by:
related_objects = list(referenced_by.RelatedObjects)
related_objects.append(self.settings["related_object"])
referenced_by.RelatedObjects = related_objects
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": referenced_by})
else:
referenced_by = self.file.create_entity(
"IfcRelAssignsToProduct",
**{
"GlobalId": ifcopenshell.guid.new(),
"OwnerHistory": ifcopenshell.api.run("owner.create_owner_history", self.file),
"RelatedObjects": [self.settings["related_object"]],
"RelatingProduct": self.settings["relating_product"],
}
)
return referenced_by
@@ -0,0 +1,25 @@
import ifcopenshell
import ifcopenshell.api
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {
"relating_product": None,
"related_object": None,
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for rel in self.settings["related_object"].HasAssignments or []:
if not rel.is_a("IfcRelAssignsToProduct") or rel.RelatingProduct != self.settings["relating_product"]:
continue
if len(rel.RelatedObjects) == 1:
return self.file.remove(rel)
related_objects = list(rel.RelatedObjects)
related_objects.remove(self.settings["related_object"])
rel.RelatedObjects = related_objects
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": rel})
return rel
@@ -0,0 +1,20 @@
import test.bootstrap
import ifcopenshell.api
class TestAssignProduct(test.bootstrap.IFC4):
def test_assigning_a_product(self):
wall = self.file.createIfcWall()
label = self.file.createIfcAnnotation()
label2 = self.file.createIfcAnnotation()
ifcopenshell.api.run("drawing.assign_product", self.file, relating_product=wall, related_object=label)
assert wall.ReferencedBy[0].RelatedObjects == (label,)
ifcopenshell.api.run("drawing.assign_product", self.file, relating_product=wall, related_object=label2)
assert wall.ReferencedBy[0].RelatedObjects == (label, label2)
def test_not_assigning_twice(self):
wall = self.file.createIfcWall()
label = self.file.createIfcAnnotation()
ifcopenshell.api.run("drawing.assign_product", self.file, relating_product=wall, related_object=label)
ifcopenshell.api.run("drawing.assign_product", self.file, relating_product=wall, related_object=label)
assert wall.ReferencedBy[0].RelatedObjects == (label,)
@@ -0,0 +1,11 @@
import test.bootstrap
import ifcopenshell.api
class TestUnassignProduct(test.bootstrap.IFC4):
def test_unassigning_a_product(self):
wall = self.file.createIfcWall()
label = self.file.createIfcAnnotation()
ifcopenshell.api.run("drawing.assign_product", self.file, relating_product=wall, related_object=label)
ifcopenshell.api.run("drawing.unassign_product", self.file, relating_product=wall, related_object=label)
assert len(self.file.by_type("IfcRelAssignsToProduct")) == 0