A whole bunch of new usecases for managing IFC library information and references

This commit is contained in:
Dion Moult
2021-11-16 18:30:23 +11:00
parent 41486ba08a
commit 4617d9f234
16 changed files with 246 additions and 0 deletions
@@ -0,0 +1,14 @@
import ifcopenshell
import ifcopenshell.util.schema
import ifcopenshell.util.date
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"name": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
return self.file.create_entity("IfcLibraryInformation", Name=self.settings["name"])
@@ -0,0 +1,14 @@
import ifcopenshell
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {
"library": None,
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
return self.file.createIfcLibraryReference(ReferencedLibrary=self.settings["library"])
@@ -0,0 +1,34 @@
import ifcopenshell
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {
"product": None,
"reference": None,
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
rels = self.settings["reference"].LibraryRefForObjects
if not rels:
return self.file.create_entity(
"IfcRelAssociatesLibrary",
GlobalId=ifcopenshell.guid.new(),
OwnerHistory=ifcopenshell.api.run("owner.create_owner_history", self.file),
RelatedObjects=[self.settings["product"]],
RelatingLibrary=self.settings["reference"],
)
for rel in rels:
if self.settings["product"] in rel.RelatedObjects:
return rel
rel = rels[0]
related_objects = list(rel.RelatedObjects)
related_objects.append(self.settings["product"])
rel.RelatedObjects = related_objects
ifcopenshell.api.run("owner.update_owner_history", self.file, element=rel)
return rel
@@ -0,0 +1,10 @@
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"library": None, "attributes": {}}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for name, value in self.settings["attributes"].items():
setattr(self.settings["library"], name, value)
@@ -0,0 +1,10 @@
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"reference": None, "attributes": {}}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for name, value in self.settings["attributes"].items():
setattr(self.settings["reference"], name, value)
@@ -0,0 +1,14 @@
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"library": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for reference in set(self.settings["library"].HasLibraryReferences or []):
self.file.remove(reference)
self.file.remove(self.settings["library"])
for rel in self.file.by_type("IfcRelAssociatesLibrary"):
if not rel.RelatingLibrary:
self.file.remove(rel)
@@ -0,0 +1,11 @@
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"reference": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for rel in self.settings["reference"].LibraryRefForObjects:
self.file.remove(rel)
self.file.remove(self.settings["reference"])
@@ -0,0 +1,19 @@
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"reference": None, "product": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
rels = self.settings["reference"].LibraryRefForObjects
if not rels:
return
for rel in rels:
if self.settings["product"] in rel.RelatedObjects:
if len(rel.RelatedObjects) == 1:
self.file.remove(rel)
continue
related_objects = list(rel.RelatedObjects)
related_objects.remove(self.settings["product"])
rel.RelatedObjects = related_objects
@@ -0,0 +1,9 @@
import test.bootstrap
import ifcopenshell.api
class TestAddLibrary(test.bootstrap.IFC4):
def test_adding_a_library(self):
library = ifcopenshell.api.run("library.add_library", self.file, name="Name")
assert library.is_a("IfcLibraryInformation")
assert library.Name == "Name"
@@ -0,0 +1,10 @@
import test.bootstrap
import ifcopenshell.api
class TestAddReference(test.bootstrap.IFC4):
def test_adding_a_reference(self):
library = ifcopenshell.api.run("library.add_library", self.file, name="Name")
reference = ifcopenshell.api.run("library.add_reference", self.file, library=library)
assert reference.is_a("IfcLibraryReference")
assert reference.ReferencedLibrary == library
@@ -0,0 +1,20 @@
import test.bootstrap
import ifcopenshell.api
class TestAssignReference(test.bootstrap.IFC4):
def test_assigning_a_reference(self):
reference = self.file.createIfcLibraryReference()
product = self.file.createIfcWall()
product2 = self.file.createIfcWall()
ifcopenshell.api.run("library.assign_reference", self.file, product=product, reference=reference)
assert reference.LibraryRefForObjects[0].RelatedObjects == (product,)
ifcopenshell.api.run("library.assign_reference", self.file, product=product2, reference=reference)
assert reference.LibraryRefForObjects[0].RelatedObjects == (product, product2)
def test_not_assigning_twice(self):
reference = self.file.createIfcLibraryReference()
product = self.file.createIfcWall()
ifcopenshell.api.run("library.assign_reference", self.file, product=product, reference=reference)
ifcopenshell.api.run("library.assign_reference", self.file, product=product, reference=reference)
assert reference.LibraryRefForObjects[0].RelatedObjects == (product,)
@@ -0,0 +1,19 @@
import test.bootstrap
import ifcopenshell.api
class TestEditLibrary(test.bootstrap.IFC4):
def test_editing_a_library(self):
library = self.file.createIfcLibraryInformation()
ifcopenshell.api.run("library.edit_library", self.file, library=library, attributes={
"Name": "Name",
"Version": "Version",
"VersionDate": "VersionDate",
"Location": "Location",
"Description": "Description",
})
assert library.Name == "Name"
assert library.Version == "Version"
assert library.VersionDate == "VersionDate"
assert library.Location == "Location"
assert library.Description == "Description"
@@ -0,0 +1,19 @@
import test.bootstrap
import ifcopenshell.api
class TestEditReference(test.bootstrap.IFC4):
def test_editing_a_reference(self):
reference = self.file.createIfcLibraryReference()
ifcopenshell.api.run("library.edit_reference", self.file, reference=reference, attributes={
"Location": "Location",
"Identification": "Identification",
"Name": "Name",
"Description": "Description",
"Language": "Language",
})
assert reference.Location == "Location"
assert reference.Identification == "Identification"
assert reference.Name == "Name"
assert reference.Description == "Description"
assert reference.Language == "Language"
@@ -0,0 +1,19 @@
import test.bootstrap
import ifcopenshell.api
class TestRemoveLibrary(test.bootstrap.IFC4):
def test_removing_a_library(self):
library = self.file.createIfcLibraryInformation()
ifcopenshell.api.run("library.remove_library", self.file, library=library)
assert len(self.file.by_type("IfcLibraryInformation")) == 0
def test_removing_a_library_and_all_references(self):
library = self.file.createIfcLibraryInformation()
reference1 = self.file.createIfcLibraryReference(ReferencedLibrary=library)
reference2 = self.file.createIfcLibraryReference(ReferencedLibrary=library)
self.file.createIfcRelAssociatesLibrary(GlobalId='foo', RelatingLibrary=library)
self.file.createIfcRelAssociatesLibrary(GlobalId='bar', RelatingLibrary=reference1)
ifcopenshell.api.run("library.remove_library", self.file, library=library)
assert len(self.file.by_type("IfcLibraryReference")) == 0
assert len(self.file.by_type("IfcRelAssociatesLibrary")) == 0
@@ -0,0 +1,12 @@
import test.bootstrap
import ifcopenshell.api
class TestRemoveReference(test.bootstrap.IFC4):
def test_removing_a_reference(self):
reference = self.file.createIfcLibraryReference()
product = self.file.createIfcWall()
ifcopenshell.api.run("library.assign_reference", self.file, product=product, reference=reference)
ifcopenshell.api.run("library.remove_reference", self.file, reference=reference)
assert len(self.file.by_type("IfcLibraryReference")) == 0
assert len(self.file.by_type("IfcRelAssociatesLibrary")) == 0
@@ -0,0 +1,12 @@
import test.bootstrap
import ifcopenshell.api
class TestUnassignReference(test.bootstrap.IFC4):
def test_unassigning_a_reference(self):
reference = self.file.createIfcLibraryReference()
product = self.file.createIfcWall()
product2 = self.file.createIfcWall()
ifcopenshell.api.run("library.assign_reference", self.file, product=product, reference=reference)
ifcopenshell.api.run("library.unassign_reference", self.file, product=product, reference=reference)
assert len(self.file.by_type("IfcRelAssociatesLibrary")) == 0