mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-09 05:46:51 +00:00
Fix #2611. Address multiple situations in the UI where the users could create invalid material data without an indication that it is invalid.
This commit is contained in:
@@ -242,10 +242,11 @@ class RemoveConstituent(bpy.types.Operator, tool.Ifc.Operator):
|
||||
constituent: bpy.props.IntProperty()
|
||||
|
||||
def _execute(self, context):
|
||||
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
|
||||
self.file = IfcStore.get_file()
|
||||
for inverse in tool.Ifc.get().get_inverse(layer):
|
||||
if inverse.is_a("IfcMaterialConstituentSet") and len(inverse.MaterialConstituents) == 1:
|
||||
return
|
||||
ifcopenshell.api.run(
|
||||
"material.remove_constituent", self.file, **{"constituent": self.file.by_id(self.constituent)}
|
||||
"material.remove_constituent", tool.Ifc.get(), constituent=tool.Ifc.get().by_id(self.constituent)
|
||||
)
|
||||
|
||||
|
||||
@@ -276,9 +277,10 @@ class RemoveProfile(bpy.types.Operator, tool.Ifc.Operator):
|
||||
profile: bpy.props.IntProperty()
|
||||
|
||||
def _execute(self, context):
|
||||
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
|
||||
self.file = IfcStore.get_file()
|
||||
ifcopenshell.api.run("material.remove_profile", self.file, **{"profile": self.file.by_id(self.profile)})
|
||||
for inverse in tool.Ifc.get().get_inverse(layer):
|
||||
if inverse.is_a("IfcMaterialProfileSet") and len(inverse.MaterialProfiles) == 1:
|
||||
return
|
||||
ifcopenshell.api.run("material.remove_profile", tool.Ifc.get(), profile=tool.Ifc.get().by_id(self.profile))
|
||||
|
||||
|
||||
class AddLayer(bpy.types.Operator, tool.Ifc.Operator):
|
||||
@@ -333,9 +335,10 @@ class RemoveLayer(bpy.types.Operator, tool.Ifc.Operator):
|
||||
layer: bpy.props.IntProperty()
|
||||
|
||||
def _execute(self, context):
|
||||
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
|
||||
self.file = IfcStore.get_file()
|
||||
ifcopenshell.api.run("material.remove_layer", self.file, **{"layer": self.file.by_id(self.layer)})
|
||||
for inverse in tool.Ifc.get().get_inverse(layer):
|
||||
if inverse.is_a("IfcMaterialLayerSet") and len(inverse.MaterialLayers) == 1:
|
||||
return
|
||||
ifcopenshell.api.run("material.remove_layer", tool.Ifc.get(), layer=tool.Ifc.get().by_id(self.layer))
|
||||
|
||||
|
||||
class AddListItem(bpy.types.Operator, tool.Ifc.Operator):
|
||||
|
||||
@@ -44,6 +44,8 @@ def add_material_set(ifc, material, set_type=None):
|
||||
|
||||
|
||||
def remove_material(ifc, material_tool, style, material=None):
|
||||
if material_tool.is_material_used_in_sets(material):
|
||||
return
|
||||
obj = ifc.get_object(material)
|
||||
ifc.unlink(element=material)
|
||||
ifc.run("material.remove_material", material=material)
|
||||
|
||||
@@ -317,6 +317,7 @@ class Material:
|
||||
def get_name(cls, obj): pass
|
||||
def import_material_definitions(cls, material_type): pass
|
||||
def is_editing_materials(cls): pass
|
||||
def is_material_used_in_sets(cls, material): pass
|
||||
def select_elements(cls, elements): pass
|
||||
|
||||
|
||||
|
||||
@@ -89,6 +89,18 @@ class Material(blenderbim.core.tool.Material):
|
||||
def is_editing_materials(cls):
|
||||
return bpy.context.scene.BIMMaterialProperties.is_editing
|
||||
|
||||
@classmethod
|
||||
def is_material_used_in_sets(cls, material):
|
||||
for inverse in tool.Ifc.get().get_inverse(material):
|
||||
if inverse.is_a() in [
|
||||
"IfcMaterialProfile",
|
||||
"IfcMaterialLayer",
|
||||
"IfcMaterialConstituent",
|
||||
"IfcMaterialList",
|
||||
]:
|
||||
return True
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def select_elements(cls, elements):
|
||||
for element in elements:
|
||||
|
||||
@@ -94,6 +94,7 @@ class TestAddMaterialSet:
|
||||
|
||||
class TestRemoveMaterial:
|
||||
def test_removing_a_material(self, ifc, material, style):
|
||||
material.is_material_used_in_sets("material").should_be_called().will_return(False)
|
||||
ifc.get_object("material").should_be_called().will_return(None)
|
||||
ifc.unlink(element="material").should_be_called()
|
||||
ifc.run("material.remove_material", material="material").should_be_called()
|
||||
@@ -101,6 +102,7 @@ class TestRemoveMaterial:
|
||||
subject.remove_material(ifc, material, style, material="material")
|
||||
|
||||
def test_removing_a_material_and_reloading_imported_materials(self, ifc, material, style):
|
||||
material.is_material_used_in_sets("material").should_be_called().will_return(False)
|
||||
ifc.get_object("material").should_be_called().will_return(None)
|
||||
ifc.unlink(element="material").should_be_called()
|
||||
ifc.run("material.remove_material", material="material").should_be_called()
|
||||
@@ -110,6 +112,7 @@ class TestRemoveMaterial:
|
||||
subject.remove_material(ifc, material, style, material="material")
|
||||
|
||||
def test_removing_a_material_object_if_it_has_no_style(self, ifc, material, style):
|
||||
material.is_material_used_in_sets("material").should_be_called().will_return(False)
|
||||
ifc.get_object("material").should_be_called().will_return("obj")
|
||||
ifc.unlink(element="material").should_be_called()
|
||||
ifc.run("material.remove_material", material="material").should_be_called()
|
||||
@@ -119,6 +122,7 @@ class TestRemoveMaterial:
|
||||
subject.remove_material(ifc, material, style, material="material")
|
||||
|
||||
def test_preserving_a_material_object_if_it_is_still_used_as_a_style(self, ifc, material, style):
|
||||
material.is_material_used_in_sets("material").should_be_called().will_return(False)
|
||||
ifc.get_object("material").should_be_called().will_return("obj")
|
||||
ifc.unlink(element="material").should_be_called()
|
||||
ifc.run("material.remove_material", material="material").should_be_called()
|
||||
@@ -126,6 +130,10 @@ class TestRemoveMaterial:
|
||||
material.is_editing_materials().should_be_called().will_return(False)
|
||||
subject.remove_material(ifc, material, style, material="material")
|
||||
|
||||
def test_not_removing_a_material_if_it_is_used_in_a_material_set(self, ifc, material, style):
|
||||
material.is_material_used_in_sets("material").should_be_called().will_return(True)
|
||||
subject.remove_material(ifc, material, style, material="material")
|
||||
|
||||
|
||||
class TestRemoveMaterialSet:
|
||||
def test_run(self, ifc, material):
|
||||
|
||||
@@ -153,9 +153,22 @@ class TestImportMaterialDefinitions(NewFile):
|
||||
class TestIsEditingMaterials(NewFile):
|
||||
def test_run(self):
|
||||
bpy.context.scene.BIMMaterialProperties.is_editing = False
|
||||
subject.is_editing_materials() is False
|
||||
assert subject.is_editing_materials() is False
|
||||
bpy.context.scene.BIMMaterialProperties.is_editing = True
|
||||
subject.is_editing_materials() is True
|
||||
assert subject.is_editing_materials() is True
|
||||
|
||||
|
||||
class TestIsMaterialUsedInSets(NewFile):
|
||||
def test_run(self):
|
||||
ifc = ifcopenshell.file()
|
||||
tool.Ifc.set(ifc)
|
||||
material_set = ifc.createIfcMaterialLayerSet()
|
||||
material_set_item = ifc.createIfcMaterialLayer()
|
||||
material = ifc.createIfcMaterial()
|
||||
assert subject.is_material_used_in_sets(material) is False
|
||||
material_set.MaterialLayers = [material_set_item]
|
||||
material_set_item.Material = material
|
||||
assert subject.is_material_used_in_sets(material) is True
|
||||
|
||||
|
||||
class TestSelectElements(NewFile):
|
||||
|
||||
Reference in New Issue
Block a user