You can now assign and unassign styles to materials

This commit is contained in:
Dion Moult
2023-10-21 15:23:43 +11:00
parent ca1925cc9f
commit f9236f8b42
8 changed files with 286 additions and 20 deletions
@@ -0,0 +1,54 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2023 Dion Moult <dion@thinkmoult.com>
#
# This file is part of IfcOpenShell.
#
# IfcOpenShell is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IfcOpenShell is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
class Usecase:
def __init__(self, file, representation=None):
"""Removes a styled representation
Styled representations are typically associated with materials. This
removes the representation but not the underlying styles.
:param representation: The IfcStyledRepresentation to remove.
:type representation: ifcopenshell.entity_instance.entity_instance
:return: None
:rtype: None
Example:
.. code:: python
# Remove a styled representation
ifcopenshell.api.run("style.remove_styled_representation", model, representation=representation)
"""
self.file = file
self.settings = {"representation": representation}
def execute(self):
for inverse in self.file.get_inverse(self.settings["representation"]):
if inverse.is_a("IfcMaterialDefinitionRepresentation") and len(inverse.Representations) == 1:
self.file.remove(inverse)
for item in self.settings["representation"].Items:
if item.is_a("IfcStyledItem") and self.file.get_total_inverses(item) == 1:
for style in item.Styles:
if style.is_a("IfcPresentationStyleAssignment"):
self.file.remove(style)
self.file.remove(item)
self.file.remove(self.settings["representation"])
@@ -0,0 +1,70 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2023 Dion Moult <dion@thinkmoult.com>
#
# This file is part of IfcOpenShell.
#
# IfcOpenShell is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IfcOpenShell is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
class Usecase:
def __init__(self, file, material=None, style=None, context=None):
"""Unassigns a style to a material
This does the inverse of assign_material_style.
:param material: The IfcMaterial which you want to unassign the style from.
:type material: ifcopenshell.entity_instance.entity_instance
:param style: The IfcPresentationStyle (typically IfcSurfaceStyle) that
you want to unassign from material. This will then be applied to all
objects that have that material.
:type style: ifcopenshell.entity_instance.entity_instance
:param context: The IfcGeometricRepresentationSubContext at which this
style should be unassigned. Typically this is the Model BODY context.
:type context: ifcopenshell.entity_instance.entity_instance
:return: None
:rtype: None
Example:
.. code:: python
ifcopenshell.api.run("style.unassign_material_style", model, material=concrete, style=style, context=body)
"""
self.file = file
self.settings = {
"material": material,
"style": style,
"context": context,
}
def execute(self):
to_delete = set()
for definition in self.settings["material"].HasRepresentation:
for representation in definition.Representations:
if not representation.is_a("IfcStyledRepresentation"):
continue
if representation.ContextOfItems != self.settings["context"]:
continue
for item in representation.Items:
if not item.is_a("IfcStyledItem"):
continue
styles = [s for s in item.Styles if s != self.settings["style"]]
if not styles:
self.file.remove(item)
elif len(styles) != len(item.Styles):
item.Styles = styles
if not representation.Items:
self.file.remove(representation)
if not definition.Representations:
self.file.remove(definition)