Implement support for removing styles. See #1557.

This commit is contained in:
Dion Moult
2021-07-10 11:43:01 +10:00
parent edd737aa5d
commit af0f0a4f6c
4 changed files with 55 additions and 3 deletions
@@ -4,6 +4,7 @@ from . import ui, operator
classes = (
operator.AddStyle,
operator.EditStyle,
operator.RemoveStyle,
operator.UnlinkStyle,
ui.BIM_PT_style,
)
@@ -29,13 +29,32 @@ class EditStyle(bpy.types.Operator):
def _execute(self, context):
self.file = IfcStore.get_file()
material = bpy.data.objects.get(self.material) if self.material else bpy.context.active_object.active_material
material = bpy.data.materials.get(self.material) if self.material else bpy.context.active_object.active_material
settings = get_colour_settings(material)
settings["style"] = self.file.by_id(material.BIMMaterialProperties.ifc_style_id)
ifcopenshell.api.run("style.edit_style", self.file, **settings)
return {"FINISHED"}
class RemoveStyle(bpy.types.Operator):
bl_idname = "bim.remove_style"
bl_label = "Remove Style"
bl_options = {"REGISTER", "UNDO"}
material: bpy.props.StringProperty()
def execute(self, context):
return IfcStore.execute_ifc_operator(self, context)
def _execute(self, context):
self.file = IfcStore.get_file()
material = bpy.data.materials.get(self.material) if self.material else bpy.context.active_object.active_material
ifcopenshell.api.run(
"style.remove_style", self.file, style=self.file.by_id(material.BIMMaterialProperties.ifc_style_id)
)
material.BIMMaterialProperties.ifc_style_id = 0
return {"FINISHED"}
class AddStyle(bpy.types.Operator):
bl_idname = "bim.add_style"
bl_label = "Add Style"
@@ -50,9 +69,9 @@ class AddStyle(bpy.types.Operator):
material = bpy.data.materials.get(self.material) if self.material else bpy.context.active_object.active_material
settings = get_colour_settings(material)
settings["name"] = material.name
settings["external_definition"] = None # TODO: Implement. See #1222
settings["external_definition"] = None # TODO: Implement. See #1222
style = ifcopenshell.api.run("style.add_style", self.file, **settings)
material.BIMMaterialProperties.ifc_style_id = int(style.id())
material.BIMMaterialProperties.ifc_style_id = style.id()
return {"FINISHED"}
@@ -24,5 +24,7 @@ class BIM_PT_style(Panel):
row.operator("bim.edit_style", icon="GREASEPENCIL")
op = row.operator("bim.unlink_style", icon="UNLINKED", text="")
op.material = context.active_object.active_material.name
op = row.operator("bim.remove_style", icon="X", text="")
op.material = context.active_object.active_material.name
else:
row.operator("bim.add_style", icon="ADD")
@@ -0,0 +1,30 @@
import ifcopenshell.util.element
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"style": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
self.purge_styled_items(self.settings["style"])
ifcopenshell.util.element.remove_deep(self.file, self.settings["style"])
def purge_styled_items(self, style):
for inverse in self.file.get_inverse(style):
if inverse.is_a("IfcStyledItem") and len(inverse.Styles) == 1:
self.purge_styled_representations(inverse)
self.file.remove(inverse)
def purge_styled_representations(self, styled_item):
for inverse in self.file.get_inverse(styled_item):
if inverse.is_a("IfcStyledRepresentation") and len(inverse.Items) == 1:
self.purge_material_definition_representations(inverse)
self.file.remove(inverse)
def purge_material_definition_representations(self, styled_representation):
for inverse in self.file.get_inverse(styled_representation):
if inverse.is_a("IfcMaterialDefinitionRepresentation") and len(inverse.Representations) == 1:
self.file.remove(inverse)