From c7272065e6f7ac4e9e20fc2e779684d3cb8c59bc Mon Sep 17 00:00:00 2001 From: Gorgious56 Date: Mon, 4 Jul 2022 23:09:03 +0200 Subject: [PATCH] Improve prop search v2 (#2262) * Minor fixes * Fix #2257 : enums with a different id and name now correctly show the name in the search field * Add prop search to template_type --- src/blenderbim/blenderbim/bim/__init__.py | 1 + .../bim/module/pset_template/prop.py | 102 +++++++++--------- .../blenderbim/bim/module/pset_template/ui.py | 3 +- .../blenderbim/bim/module/root/__init__.py | 1 - .../blenderbim/bim/module/root/operator.py | 33 +----- .../blenderbim/bim/module/root/prop.py | 1 - src/blenderbim/blenderbim/bim/operator.py | 39 +++++++ 7 files changed, 98 insertions(+), 82 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/__init__.py b/src/blenderbim/blenderbim/bim/__init__.py index c29a3f9298..3ad281d07d 100644 --- a/src/blenderbim/blenderbim/bim/__init__.py +++ b/src/blenderbim/blenderbim/bim/__init__.py @@ -93,6 +93,7 @@ classes = [ operator.SelectSchemaDir, operator.SelectURIAttribute, prop.StrProperty, + operator.BIM_OT_enum_property_search, # /!\ Register AFTER prop.StrProperty prop.ObjProperty, prop.Attribute, prop.ModuleVisibility, diff --git a/src/blenderbim/blenderbim/bim/module/pset_template/prop.py b/src/blenderbim/blenderbim/bim/module/pset_template/prop.py index 4554c229c4..eb648969d0 100644 --- a/src/blenderbim/blenderbim/bim/module/pset_template/prop.py +++ b/src/blenderbim/blenderbim/bim/module/pset_template/prop.py @@ -94,65 +94,70 @@ def get_primary_measure_type(self, context): return PsetTemplatesData.data["primary_measure_type"] +def get_template_type(self, context): + return [ + ( + "PSET_TYPEDRIVENONLY", + "Pset - IfcTypeObject", + "The property sets defined by this IfcPropertySetTemplate can only be assigned to subtypes of IfcTypeObject.", + ), + ( + "PSET_TYPEDRIVENOVERRIDE", + "Pset - IfcTypeObject - Override", + "The property sets defined by this IfcPropertySetTemplate can only be assigned to subtypes of IfcTypeObject.", + ), + ( + "PSET_OCCURRENCEDRIVEN", + "Pset - IfcObject", + "The property sets defined by this IfcPropertySetTemplate can only be assigned to subtypes of IfcObject.", + ), + ( + "PSET_PERFORMANCEDRIVEN", + "Pset - IfcPerformanceHistory", + "The property sets defined by this IfcPropertySetTemplate can only be assigned to IfcPerformanceHistory.", + ), + ( + "QTO_TYPEDRIVENONLY", + "Qto - IfcTypeObject", + "The element quantity defined by this IfcPropertySetTemplate can only be assigned to subtypes of IfcTypeObject.", + ), + ( + "QTO_TYPEDRIVENOVERRIDE", + "Qto - IfcTypeObject - Override", + "The element quantity defined by this IfcPropertySetTemplate can be assigned to subtypes of IfcTypeObject and can be overridden by an element quantity with same name at subtypes of IfcObject.", + ), + ( + "QTO_OCCURRENCEDRIVEN", + "Qto - IfcObject", + "The element quantity defined by this IfcPropertySetTemplate can only be assigned to subtypes of IfcObject.", + ), + ( + "NOTDEFINED", + "Not defined", + "No restriction provided, the property sets defined by this IfcPropertySetTemplate can be assigned to any entity, if not otherwise restricted by the ApplicableEntity attribute.", + ), + ] + + class PsetTemplate(PropertyGroup): global_id: StringProperty(name="Global ID") name: StringProperty(name="Name") description: StringProperty(name="Description") - template_type: EnumProperty( - items=[ - ( - "PSET_TYPEDRIVENONLY", - "Pset - IfcTypeObject", - "The property sets defined by this IfcPropertySetTemplate can only be assigned to subtypes of IfcTypeObject.", - ), - ( - "PSET_TYPEDRIVENOVERRIDE", - "Pset - IfcTypeObject - Override", - "The property sets defined by this IfcPropertySetTemplate can only be assigned to subtypes of IfcTypeObject.", - ), - ( - "PSET_OCCURRENCEDRIVEN", - "Pset - IfcObject", - "The property sets defined by this IfcPropertySetTemplate can only be assigned to subtypes of IfcObject.", - ), - ( - "PSET_PERFORMANCEDRIVEN", - "Pset - IfcPerformanceHistory", - "The property sets defined by this IfcPropertySetTemplate can only be assigned to IfcPerformanceHistory.", - ), - ( - "QTO_TYPEDRIVENONLY", - "Qto - IfcTypeObject", - "The element quantity defined by this IfcPropertySetTemplate can only be assigned to subtypes of IfcTypeObject.", - ), - ( - "QTO_TYPEDRIVENOVERRIDE", - "Qto - IfcTypeObject - Override", - "The element quantity defined by this IfcPropertySetTemplate can be assigned to subtypes of IfcTypeObject and can be overridden by an element quantity with same name at subtypes of IfcObject.", - ), - ( - "QTO_OCCURRENCEDRIVEN", - "Qto - IfcObject", - "The element quantity defined by this IfcPropertySetTemplate can only be assigned to subtypes of IfcObject.", - ), - ( - "NOTDEFINED", - "Not defined", - "No restriction provided, the property sets defined by this IfcPropertySetTemplate can be assigned to any entity, if not otherwise restricted by the ApplicableEntity attribute.", - ), - ], - name="Template Type", - ) + template_type: EnumProperty(items=get_template_type, name="Template Type") applicable_entity: StringProperty(name="Applicable Entity") + getter_enum = { + "template_type": lambda self, context: get_template_type(self, context), + } + class EnumerationValues(PropertyGroup): string_value: StringProperty(name="Value") bool_value: BoolProperty(name="Value") int_value: IntProperty(name="Value") float_value: FloatProperty(name="Value") - - + + class PropTemplate(PropertyGroup): global_id: StringProperty(name="Global ID") name: StringProperty(name="Name") @@ -170,7 +175,6 @@ class PropTemplate(PropertyGroup): "primary_measure_type": get_primary_measure_type, } - def get_value_name(self): ifc_data_type = IfcStore.get_schema().declaration_by_name(self.primary_measure_type) data_type = ifcopenshell.util.attribute.get_primitive_type(ifc_data_type) @@ -194,7 +198,7 @@ class BIMPsetTemplateProperties(PropertyGroup): active_pset_template: PointerProperty(type=PsetTemplate) active_prop_template: PointerProperty(type=PropTemplate) new_template_filename: StringProperty("New TemplateFileName") - + getter_enum = { "pset_template_files": getPsetTemplateFiles, "pset_templates": getPsetTemplates, diff --git a/src/blenderbim/blenderbim/bim/module/pset_template/ui.py b/src/blenderbim/blenderbim/bim/module/pset_template/ui.py index 858f856711..1c914f83d3 100644 --- a/src/blenderbim/blenderbim/bim/module/pset_template/ui.py +++ b/src/blenderbim/blenderbim/bim/module/pset_template/ui.py @@ -72,8 +72,7 @@ class BIM_PT_pset_template(Panel): row.prop(props.active_pset_template, "name") row = layout.row() row.prop(props.active_pset_template, "description") - row = layout.row() - row.prop(props.active_pset_template, "template_type") + prop_with_search(layout, props.active_pset_template, "template_type") row = layout.row() row.prop(props.active_pset_template, "applicable_entity") else: diff --git a/src/blenderbim/blenderbim/bim/module/root/__init__.py b/src/blenderbim/blenderbim/bim/module/root/__init__.py index 523f3b77cd..00a36e0817 100644 --- a/src/blenderbim/blenderbim/bim/module/root/__init__.py +++ b/src/blenderbim/blenderbim/bim/module/root/__init__.py @@ -26,7 +26,6 @@ classes = ( operator.EnableReassignClass, operator.ReassignClass, operator.UnlinkObject, - operator.BIM_OT_enum_property_search, prop.BIMRootProperties, ui.BIM_PT_class, ) diff --git a/src/blenderbim/blenderbim/bim/module/root/operator.py b/src/blenderbim/blenderbim/bim/module/root/operator.py index ee57c74f0f..b2c39b7b44 100644 --- a/src/blenderbim/blenderbim/bim/module/root/operator.py +++ b/src/blenderbim/blenderbim/bim/module/root/operator.py @@ -31,8 +31,6 @@ import blenderbim.core.root as core import blenderbim.tool as tool from ifcopenshell.api.void.data import Data as VoidData from blenderbim.bim.ifc import IfcStore -from blenderbim.bim.module.root.prop import get_contexts -from blenderbim.bim.prop import StrProperty class Operator: @@ -128,12 +126,13 @@ class AssignClass(bpy.types.Operator, Operator): ifc_representation_class: bpy.props.StringProperty() def _execute(self, context): + props = context.scene.BIMRootProperties objects = [bpy.data.objects.get(self.obj)] if self.obj else context.selected_objects - ifc_class = self.ifc_class or context.scene.BIMRootProperties.ifc_class + ifc_class = self.ifc_class or props.ifc_class predefined_type = self.userdefined_type if self.predefined_type == "USERDEFINED" else self.predefined_type ifc_context = self.context_id - if not ifc_context and get_contexts(self, context): - ifc_context = int(context.scene.BIMRootProperties.contexts or "0") or None + if not ifc_context and props.getter_enum["contexts"](props, context): + ifc_context = int(props.contexts or "0") or None if ifc_context: ifc_context = tool.Ifc.get().by_id(ifc_context) active_object = context.active_object @@ -190,27 +189,3 @@ class CopyClass(bpy.types.Operator, Operator): for obj in objects: core.copy_class(tool.Ifc, tool.Collector, tool.Geometry, tool.Root, obj=obj) blenderbim.bim.handler.purge_module_data() - - -class BIM_OT_enum_property_search(bpy.types.Operator): - bl_idname = "bim.enum_property_search" - bl_label = "Search For Property" - bl_options = {"REGISTER", "UNDO"} - prop_name: bpy.props.StringProperty() - collection: bpy.props.CollectionProperty(type=StrProperty) - - def invoke(self, context, event): - self.data = context.data - getter = self.data.getter_enum.get(self.prop_name, None) - if getter is None: - return {"FINISHED"} - self.collection.clear() - for item in getter(self.data, context): - self.collection.add().name = item[0] - return context.window_manager.invoke_props_dialog(self) - - def execute(self, context): - return {"FINISHED"} - - def draw(self, context): - self.layout.prop_search(self.data, self.prop_name, self, "collection") diff --git a/src/blenderbim/blenderbim/bim/module/root/prop.py b/src/blenderbim/blenderbim/bim/module/root/prop.py index 8e3fbeca94..2b009a551b 100644 --- a/src/blenderbim/blenderbim/bim/module/root/prop.py +++ b/src/blenderbim/blenderbim/bim/module/root/prop.py @@ -21,7 +21,6 @@ import ifcopenshell import ifcopenshell.util.schema from blenderbim.bim.module.root.data import IfcClassData from blenderbim.bim.ifc import IfcStore -from blenderbim.bim.prop import StrProperty from bpy.types import PropertyGroup from bpy.props import ( PointerProperty, diff --git a/src/blenderbim/blenderbim/bim/operator.py b/src/blenderbim/blenderbim/bim/operator.py index 7b171961e0..17ec6ed626 100644 --- a/src/blenderbim/blenderbim/bim/operator.py +++ b/src/blenderbim/blenderbim/bim/operator.py @@ -25,6 +25,7 @@ import blenderbim.bim.handler import blenderbim.tool as tool from . import schema from blenderbim.bim.ifc import IfcStore +from blenderbim.bim.prop import StrProperty from blenderbim.bim.ui import IFCFileSelector from mathutils import Vector, Matrix, Euler from math import radians @@ -523,3 +524,41 @@ class ConfigureVisibility(bpy.types.Operator): def execute(self, context): return {"FINISHED"} + + +def update_enum_property_search_prop(self, context): + for i, prop in enumerate(self.collection_name): + if prop.name == self.dummy_name: + setattr(context.data, self.prop_name, self.collection_identifier[i].name) + + +class BIM_OT_enum_property_search(bpy.types.Operator): + bl_idname = "bim.enum_property_search" + bl_label = "Search For Property" + bl_options = {"REGISTER", "UNDO"} + dummy_name: bpy.props.StringProperty(name="Property", update=update_enum_property_search_prop) + collection_name: bpy.props.CollectionProperty(type=StrProperty) + collection_identifier: bpy.props.CollectionProperty(type=StrProperty) + prop_name: bpy.props.StringProperty() + + def invoke(self, context, event): + self.data = context.data + getter = self.data.getter_enum.get(self.prop_name, None) + if getter is None: + return {"FINISHED"} + self.collection_name.clear() + self.collection_identifier.clear() + for item in getter(self.data, context): + self.collection_identifier.add().name = item[0] + if item[0] == getattr(self.data, self.prop_name): + self.dummy_name = item[1] # We found the current enum value + self.collection_name.add().name = item[1] + return context.window_manager.invoke_props_dialog(self) + + def execute(self, context): + return {"FINISHED"} + + def draw(self, context): + # Mandatory to access context.data in update : + self.layout.context_pointer_set(name="data", data=self.data) + self.layout.prop_search(self, "dummy_name", self, "collection_name")