assign_structural_analysis_model to support batching

This commit is contained in:
Andrej730
2025-01-23 15:57:18 +05:00
parent 70f17c2094
commit 3eeb58dded
5 changed files with 17 additions and 56 deletions
@@ -17,43 +17,22 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import ifcopenshell
import ifcopenshell.api.group
import ifcopenshell.api.owner
import ifcopenshell.guid
from typing import Union
def assign_structural_analysis_model(
file: ifcopenshell.file,
product: ifcopenshell.entity_instance,
products: list[ifcopenshell.entity_instance],
structural_analysis_model: ifcopenshell.entity_instance,
) -> ifcopenshell.entity_instance:
) -> Union[ifcopenshell.entity_instance, None]:
"""Assigns a load or structural member to an analysis model
:param product: The structural element that is part of the analysis.
:type product: ifcopenshell.entity_instance
:param products: The structural elements that is part of the analysis.
:param structural_analysis_model: The IfcStructuralAnalysisModel that
the structural element is related to.
:type structural_analysis_model: ifcopenshell.entity_instance
:return: The IfcRelAssignsToGroup relationship
:rtype: ifcopenshell.entity_instance
"""
settings = {
"product": product,
"structural_analysis_model": structural_analysis_model,
}
if not settings["structural_analysis_model"].IsGroupedBy:
return file.create_entity(
"IfcRelAssignsToGroup",
**{
"GlobalId": ifcopenshell.guid.new(),
"OwnerHistory": ifcopenshell.api.owner.create_owner_history(file),
"RelatedObjects": [settings["product"]],
"RelatingGroup": settings["structural_analysis_model"],
}
)
rel = settings["structural_analysis_model"].IsGroupedBy[0]
related_objects = set(rel.RelatedObjects) or set()
related_objects.add(settings["product"])
rel.RelatedObjects = list(related_objects)
ifcopenshell.api.owner.update_owner_history(file, **{"element": rel})
return rel
return ifcopenshell.api.group.assign_group(file, products, structural_analysis_model)
@@ -17,40 +17,21 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import ifcopenshell
import ifcopenshell.api.group
import ifcopenshell.api.owner
import ifcopenshell.util.element
def unassign_structural_analysis_model(
file: ifcopenshell.file,
product: ifcopenshell.entity_instance,
products: list[ifcopenshell.entity_instance],
structural_analysis_model: ifcopenshell.entity_instance,
) -> None:
"""Removes a relationship between a structural element and the analysis model
:param product: The structural element that is part of the analysis.
:type product: ifcopenshell.entity_instance
:param products: The structural elements that is part of the analysis.
:param structural_analysis_model: The IfcStructuralAnalysisModel that
the structural element is related to.
:type structural_analysis_model: ifcopenshell.entity_instance
:return: None
:rtype: None
"""
settings = {
"product": product,
"structural_analysis_model": structural_analysis_model,
}
if not settings["structural_analysis_model"].IsGroupedBy:
return
rel = settings["structural_analysis_model"].IsGroupedBy[0]
related_objects = set(rel.RelatedObjects) or set()
related_objects.remove(settings["product"])
if len(related_objects):
rel.RelatedObjects = list(related_objects)
ifcopenshell.api.owner.update_owner_history(file, **{"element": rel})
else:
history = rel.OwnerHistory
file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(file, history)
ifcopenshell.api.group.unassign_group(file, products, structural_analysis_model)
@@ -29,12 +29,13 @@ class TestAssignStructuralAnalysisModel(test.bootstrap.IFC4):
)
rel = ifcopenshell.api.structural.assign_structural_analysis_model(
self.file,
product=product,
products=[product],
structural_analysis_model=subject,
)
assert rel
assert rel.is_a("IfcRelAssignsToGroup")
assert rel.RelatingGroup == subject
assert product in rel.RelatedObjects
assert rel.RelatedObjects == (product,)
class TestAssignStructuralAnalysisModelIFC2X3(test.bootstrap.IFC2X3, TestAssignStructuralAnalysisModel):
@@ -29,12 +29,12 @@ class TestUnassignStructuralAnalysisModel(test.bootstrap.IFC4):
)
ifcopenshell.api.structural.assign_structural_analysis_model(
self.file,
product=product,
products=[product],
structural_analysis_model=subject,
)
ifcopenshell.api.structural.unassign_structural_analysis_model(
self.file,
product=product,
products=[product],
structural_analysis_model=subject,
)
models = self.file.by_type("IfcStructuralAnalysisModel")