mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 01:41:57 +00:00
material.unassign_material - support batching #4474
This commit is contained in:
@@ -79,15 +79,14 @@ ARGUMENTS_DEPRECATION = {
|
||||
"type.unassign_type": partial(
|
||||
batching_argument_deprecation, prev_argument="related_object", new_argument="related_objects"
|
||||
),
|
||||
"system.assign_system": partial(
|
||||
batching_argument_deprecation, prev_argument="product", new_argument="products"
|
||||
),
|
||||
"system.unassign_system": partial(
|
||||
batching_argument_deprecation, prev_argument="product", new_argument="products"
|
||||
),
|
||||
"system.assign_system": partial(batching_argument_deprecation, prev_argument="product", new_argument="products"),
|
||||
"system.unassign_system": partial(batching_argument_deprecation, prev_argument="product", new_argument="products"),
|
||||
"material.assign_material": partial(
|
||||
batching_argument_deprecation, prev_argument="product", new_argument="products"
|
||||
),
|
||||
"material.unassign_material": partial(
|
||||
batching_argument_deprecation, prev_argument="product", new_argument="products"
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -155,10 +155,9 @@ class Usecase:
|
||||
return
|
||||
|
||||
# NOTE: we always reassign material, even if it might be assigned before
|
||||
for product in self.products:
|
||||
material = ifcopenshell.util.element.get_material(product)
|
||||
if material:
|
||||
ifcopenshell.api.run("material.unassign_material", self.file, product=product)
|
||||
products_to_unassign_material = [p for p in self.products if ifcopenshell.util.element.get_material(p)]
|
||||
if products_to_unassign_material:
|
||||
ifcopenshell.api.run("material.unassign_material", self.file, products=products_to_unassign_material)
|
||||
|
||||
if self.settings["type"] == "IfcMaterial" or (
|
||||
self.settings["material"] and not self.settings["material"].is_a("IfcMaterial")
|
||||
|
||||
@@ -17,12 +17,13 @@
|
||||
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import ifcopenshell
|
||||
import ifcopenshell.api
|
||||
import ifcopenshell.util.element
|
||||
|
||||
|
||||
class Usecase:
|
||||
def __init__(self, file, product=None):
|
||||
"""Removes any material relationship with a product
|
||||
def __init__(self, file: ifcopenshell.file, products: list[ifcopenshell.entity_instance]):
|
||||
"""Removes any material relationship with the list of products
|
||||
|
||||
A product can only have one material assigned to it, which is why it is
|
||||
not necessary to specify the material to unassign. The material is not
|
||||
@@ -30,8 +31,8 @@ class Usecase:
|
||||
|
||||
If the product does not have a material, nothing happens.
|
||||
|
||||
:param product: The IfcProduct that may or may not have a material
|
||||
:type product: ifcopenshell.entity_instance.entity_instance
|
||||
:param products: The list IfcProducts that may or may not have a material
|
||||
:type product: list[ifcopenshell.entity_instance.entity_instance]
|
||||
:return: None
|
||||
:rtype: None
|
||||
|
||||
@@ -49,20 +50,34 @@ class Usecase:
|
||||
# Let's change our mind and remove the concrete assignment. The
|
||||
# concrete material still exists, but the bench is no longer made
|
||||
# out of concrete now.
|
||||
ifcopenshell.api.run("material.unassign_material", model, product=bench_type)
|
||||
ifcopenshell.api.run("material.unassign_material", model, products=[bench_type])
|
||||
"""
|
||||
self.file = file
|
||||
self.settings = {"product": product}
|
||||
self.settings = {"products": products}
|
||||
|
||||
def execute(self):
|
||||
if self.settings["product"].is_a("IfcTypeObject"):
|
||||
material = ifcopenshell.util.element.get_material(self.settings["product"])
|
||||
def execute(self) -> None:
|
||||
self.products = set(self.settings["products"])
|
||||
if not self.products:
|
||||
return
|
||||
|
||||
self.remove_material_usages_from_types()
|
||||
self.unassign_materials()
|
||||
|
||||
def remove_material_usages_from_types(self) -> None:
|
||||
# remove material usages from types
|
||||
for product in self.products:
|
||||
if not product.is_a("IfcTypeObject"):
|
||||
continue
|
||||
material = ifcopenshell.util.element.get_material(product)
|
||||
if not material:
|
||||
continue
|
||||
if material.is_a() in ["IfcMaterialLayerSet", "IfcMaterialProfileSet"]:
|
||||
# Remove set usages
|
||||
for inverse in self.file.get_inverse(material):
|
||||
if self.file.schema == "IFC2X3":
|
||||
if not inverse.is_a("IfcMaterialLayerSetUsage"):
|
||||
continue
|
||||
# in IFC2X3 there is no .AssociatedTo
|
||||
for inverse2 in self.file.get_inverse(inverse):
|
||||
if inverse2.is_a("IfcRelAssociatesMaterial"):
|
||||
history = inverse2.OwnerHistory
|
||||
@@ -73,21 +88,40 @@ class Usecase:
|
||||
if not inverse.is_a("IfcMaterialUsageDefinition"):
|
||||
continue
|
||||
for rel in inverse.AssociatedTo:
|
||||
history = rel.OwnerHistory
|
||||
self.file.remove(rel)
|
||||
if history:
|
||||
ifcopenshell.util.element.remove_deep2(self.file, history)
|
||||
self.file.remove(inverse)
|
||||
|
||||
for rel in self.settings["product"].HasAssociations:
|
||||
if rel.is_a("IfcRelAssociatesMaterial"):
|
||||
if rel.RelatingMaterial.is_a() in ["IfcMaterialLayerSetUsage", "IfcMaterialProfileSetUsage"]:
|
||||
def unassign_materials(self) -> None:
|
||||
associations: set[ifcopenshell.entity_instance] = set()
|
||||
for product in self.products:
|
||||
associations.update(product.HasAssociations)
|
||||
|
||||
# we ensure that `associations` won't have removed elements
|
||||
# to avoid crash during `material_inverses.issubset(associations)`
|
||||
while associations:
|
||||
rel = next(iter(associations))
|
||||
|
||||
if not rel.is_a("IfcRelAssociatesMaterial"):
|
||||
associations.remove(rel)
|
||||
else:
|
||||
material = rel.RelatingMaterial
|
||||
related_objects = set(rel.RelatedObjects) - self.products
|
||||
|
||||
if material.is_a() in ["IfcMaterialLayerSetUsage", "IfcMaterialProfileSetUsage"]:
|
||||
# Warning: this may leave the model in a non-compliant state.
|
||||
if self.file.get_total_inverses(rel.RelatingMaterial) == 1 and len(rel.RelatedObjects) == 1:
|
||||
self.file.remove(rel.RelatingMaterial)
|
||||
if len(rel.RelatedObjects) == 1:
|
||||
material_inverses = set(self.file.get_inverse(material))
|
||||
if material_inverses.issubset(associations) and not related_objects:
|
||||
self.file.remove(material)
|
||||
associations.remove(rel)
|
||||
|
||||
if not related_objects:
|
||||
history = rel.OwnerHistory
|
||||
self.file.remove(rel)
|
||||
if history:
|
||||
ifcopenshell.util.element.remove_deep2(self.file, history)
|
||||
continue
|
||||
related_objects = set(rel.RelatedObjects)
|
||||
related_objects.remove(self.settings["product"])
|
||||
rel.RelatedObjects = list(related_objects)
|
||||
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": rel})
|
||||
|
||||
@@ -126,7 +126,7 @@ class Usecase:
|
||||
pset=inverse.RelatingPropertyDefinition,
|
||||
)
|
||||
elif inverse.is_a("IfcRelAssociatesMaterial"):
|
||||
ifcopenshell.api.run("material.unassign_material", self.file, product=self.settings["product"])
|
||||
ifcopenshell.api.run("material.unassign_material", self.file, products=[self.settings["product"]])
|
||||
elif inverse.is_a("IfcRelDefinesByType"):
|
||||
if inverse.RelatingType == self.settings["product"]:
|
||||
ifcopenshell.api.run("type.unassign_type", self.file, related_objects=inverse.RelatedObjects)
|
||||
|
||||
Reference in New Issue
Block a user