From 918915aeae652f998026ded72dc4d778ca0d64e1 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Sat, 23 Oct 2021 19:55:22 +1100 Subject: [PATCH] Reintroduced feature to copy attributes to selected objects. --- .../bim/module/attribute/__init__.py | 1 + .../bim/module/attribute/operator.py | 41 +++++++++++-------- .../blenderbim/bim/module/attribute/ui.py | 3 ++ src/blenderbim/blenderbim/bim/operator.py | 31 -------------- src/blenderbim/blenderbim/bim/prop.py | 1 - src/blenderbim/blenderbim/core/attribute.py | 26 ++++++++++++ src/blenderbim/pytest.ini | 1 + .../test/bim/feature/attribute.feature | 19 +++++++++ src/blenderbim/test/core/test_attribute.py | 31 ++++++++++++++ 9 files changed, 106 insertions(+), 48 deletions(-) create mode 100644 src/blenderbim/blenderbim/core/attribute.py create mode 100644 src/blenderbim/test/bim/feature/attribute.feature create mode 100644 src/blenderbim/test/core/test_attribute.py diff --git a/src/blenderbim/blenderbim/bim/module/attribute/__init__.py b/src/blenderbim/blenderbim/bim/module/attribute/__init__.py index d91a5ad409..33dc7a9304 100644 --- a/src/blenderbim/blenderbim/bim/module/attribute/__init__.py +++ b/src/blenderbim/blenderbim/bim/module/attribute/__init__.py @@ -24,6 +24,7 @@ classes = ( operator.DisableEditingAttributes, operator.EditAttributes, operator.GenerateGlobalId, + operator.CopyAttributeToSelection, prop.BIMAttributeProperties, ui.BIM_PT_object_attributes, ui.BIM_PT_material_attributes, diff --git a/src/blenderbim/blenderbim/bim/module/attribute/operator.py b/src/blenderbim/blenderbim/bim/module/attribute/operator.py index e29bc69628..f282b81eaf 100644 --- a/src/blenderbim/blenderbim/bim/module/attribute/operator.py +++ b/src/blenderbim/blenderbim/bim/module/attribute/operator.py @@ -20,10 +20,21 @@ import bpy import json import ifcopenshell import ifcopenshell.api +import blenderbim.bim.helper +import blenderbim.bim.handler +import blenderbim.tool as tool +import blenderbim.core.attribute as core from blenderbim.bim.ifc import IfcStore from ifcopenshell.api.attribute.data import Data +class Operator: + def execute(self, context): + IfcStore.execute_ifc_operator(self, context) + blenderbim.bim.handler.refresh_ui_data() + return {"FINISHED"} + + class EnableEditingAttributes(bpy.types.Operator): bl_idname = "bim.enable_editing_attributes" bl_label = "Enable Editing Attributes" @@ -40,22 +51,9 @@ class EnableEditingAttributes(bpy.types.Operator): oprops = obj.BIMObjectProperties props = obj.BIMAttributeProperties props.attributes.clear() - for attribute in Data.products[oprops.ifc_definition_id]: - new = props.attributes.add() - if attribute["type"] == "entity" or (attribute["type"] == "list" and attribute["list_type"] == "entity"): - continue - new.name = attribute["name"] - new.is_null = attribute["is_null"] - if attribute["type"] == "string" or attribute["type"] == "list": - new.string_value = attribute["value"] or "" - elif attribute["type"] == "integer": - new.int_value = attribute["value"] or 0 - elif attribute["type"] == "float": - new.float_value = attribute["value"] or 0.0 - elif attribute["type"] == "enum": - new.enum_items = json.dumps(attribute["enum_items"]) - if attribute["value"]: - new.enum_value = attribute["value"] + if oprops.ifc_definition_id not in Data.products: + Data.load(IfcStore.get_file(), oprops.ifc_definition_id) + blenderbim.bim.helper.import_attributes2(tool.Ifc.get().by_id(oprops.ifc_definition_id), props.attributes) props.is_editing_attributes = True return {"FINISHED"} @@ -139,3 +137,14 @@ class GenerateGlobalId(bpy.types.Operator): global_id.data_type = "string" global_id.string_value = ifcopenshell.guid.new() return {"FINISHED"} + + +class CopyAttributeToSelection(bpy.types.Operator, Operator): + bl_idname = "bim.copy_attribute_to_selection" + bl_label = "Copy Attribute To Selection" + attribute_name: bpy.props.StringProperty() + + def _execute(self, context): + value = context.active_object.BIMAttributeProperties.attributes.get(self.attribute_name).get_value() + for obj in context.selected_objects: + core.copy_attribute_to_selection(tool.Ifc, name=self.attribute_name, value=value, obj=obj) diff --git a/src/blenderbim/blenderbim/bim/module/attribute/ui.py b/src/blenderbim/blenderbim/bim/module/attribute/ui.py index df73d36da1..7b3efc9483 100644 --- a/src/blenderbim/blenderbim/bim/module/attribute/ui.py +++ b/src/blenderbim/blenderbim/bim/module/attribute/ui.py @@ -60,6 +60,9 @@ def draw_ui(context, layout, obj_type): icon="RADIOBUT_OFF" if blender_attribute.is_null else "RADIOBUT_ON", text="", ) + if attribute["name"] != "GlobalId": + op = row.operator("bim.copy_attribute_to_selection", icon="COPYDOWN", text="") + op.attribute_name = attribute["name"] else: row = layout.row() op = row.operator("bim.enable_editing_attributes", icon="GREASEPENCIL", text="Edit") diff --git a/src/blenderbim/blenderbim/bim/operator.py b/src/blenderbim/blenderbim/bim/operator.py index f2ea820184..d7264c8f7a 100644 --- a/src/blenderbim/blenderbim/bim/operator.py +++ b/src/blenderbim/blenderbim/bim/operator.py @@ -456,37 +456,6 @@ class CopyPropertyToSelection(bpy.types.Operator): return {"FINISHED"} -class CopyAttributeToSelection(bpy.types.Operator): - bl_idname = "bim.copy_attribute_to_selection" - bl_label = "Copy Attribute To Selection" - attribute_name: bpy.props.StringProperty() - attribute_value: bpy.props.StringProperty() - - def execute(self, context): - # TODO: this is dead code, awaiting reimplementation. See #1222. - self.schema = ifcopenshell.ifcopenshell_wrapper.schema_by_name(context.scene.BIMProperties.export_schema) - self.applicable_attributes_cache = {} - for obj in context.selected_objects: - if "/" not in obj.name: - continue - attribute = obj.BIMObjectProperties.attributes.get(self.attribute_name) - if not attribute: - applicable_attributes = self.get_applicable_attributes(obj.name.split("/")[0]) - if self.attribute_name not in applicable_attributes: - continue - attribute = obj.BIMObjectProperties.attributes.add() - attribute.name = self.attribute_name - attribute.string_value = self.attribute_value - return {"FINISHED"} - - def get_applicable_attributes(self, ifc_class): - if ifc_class not in self.applicable_attributes_cache: - self.applicable_attributes_cache[ifc_class] = [ - a.name() for a in self.schema.declaration_by_name(ifc_class).all_attributes() - ] - return self.applicable_attributes_cache[ifc_class] - - class ConfigureVisibility(bpy.types.Operator): bl_idname = "bim.configure_visibility" bl_label = "Configure module UI visibility in BlenderBIM" diff --git a/src/blenderbim/blenderbim/bim/prop.py b/src/blenderbim/blenderbim/bim/prop.py index dbea59fcba..03a77f4d79 100644 --- a/src/blenderbim/blenderbim/bim/prop.py +++ b/src/blenderbim/blenderbim/bim/prop.py @@ -196,7 +196,6 @@ class Attribute(PropertyGroup): class ModuleVisibility(PropertyGroup): name: StringProperty(name="Name") is_visible: BoolProperty(name="Value", default=True, update=update_is_visible) - # is_visible: BoolProperty(name="Value", update=update_is_visible) class BIMProperties(PropertyGroup): diff --git a/src/blenderbim/blenderbim/core/attribute.py b/src/blenderbim/blenderbim/core/attribute.py new file mode 100644 index 0000000000..4ad8515e8f --- /dev/null +++ b/src/blenderbim/blenderbim/core/attribute.py @@ -0,0 +1,26 @@ +# BlenderBIM Add-on - OpenBIM Blender Add-on +# Copyright (C) 2021 Dion Moult +# +# This file is part of BlenderBIM Add-on. +# +# BlenderBIM Add-on is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# BlenderBIM Add-on 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with BlenderBIM Add-on. If not, see . + + +def copy_attribute_to_selection(ifc, name=None, value=None, obj=None): + element = ifc.get_entity(obj) + if element: + try: + ifc.run("attribute.edit_attributes", product=element, attributes={name: value}) + except: + pass diff --git a/src/blenderbim/pytest.ini b/src/blenderbim/pytest.ini index 5d251b6622..ae78d9a487 100644 --- a/src/blenderbim/pytest.ini +++ b/src/blenderbim/pytest.ini @@ -1,6 +1,7 @@ [pytest] markers = aggregate + attribute bimtester context geometry diff --git a/src/blenderbim/test/bim/feature/attribute.feature b/src/blenderbim/test/bim/feature/attribute.feature new file mode 100644 index 0000000000..742ad302a9 --- /dev/null +++ b/src/blenderbim/test/bim/feature/attribute.feature @@ -0,0 +1,19 @@ +@attribute +Feature: Attribute + +Scenario: Copy attribute to selected + Given an empty IFC project + And I add a cube + And the object "Cube" is selected + And I set "scene.BIMRootProperties.ifc_class" to "IfcWall" + And I press "bim.assign_class" + And I add a cube + And the object "Cube" is selected + And I set "scene.BIMRootProperties.ifc_class" to "IfcWall" + And I press "bim.assign_class" + And the object "IfcWall/Cube" is selected + And additionally the object "IfcWall/Cube.001" is selected + And I press "bim.enable_editing_attributes(obj='IfcWall/Cube.001', obj_type='Object')" + And I set "active_object.BIMAttributeProperties.attributes[2].string_value" to "Foo" + When I press "bim.copy_attribute_to_selection(attribute_name='Description')" + Then nothing happens diff --git a/src/blenderbim/test/core/test_attribute.py b/src/blenderbim/test/core/test_attribute.py new file mode 100644 index 0000000000..8eee30531e --- /dev/null +++ b/src/blenderbim/test/core/test_attribute.py @@ -0,0 +1,31 @@ +# BlenderBIM Add-on - OpenBIM Blender Add-on +# Copyright (C) 2021 Dion Moult +# +# This file is part of BlenderBIM Add-on. +# +# BlenderBIM Add-on is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# BlenderBIM Add-on 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with BlenderBIM Add-on. If not, see . + +import blenderbim.core.attribute as subject +from test.core.bootstrap import ifc + + +class TestCopyAttributeToSelection: + def test_run(self, ifc): + ifc.get_entity("obj").should_be_called().will_return("element") + ifc.run("attribute.edit_attributes", product="element", attributes={"name": "value"}).should_be_called() + subject.copy_attribute_to_selection(ifc, name="name", value="value", obj="obj") + + def test_do_nothing_if_object_is_not_an_element(self, ifc): + ifc.get_entity("obj").should_be_called().will_return(None) + subject.copy_attribute_to_selection(ifc, name="name", value="value", obj="obj")