mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-19 22:50:21 +00:00
Add suggestions for relating types in enum property search (#6962)
This commit is contained in:
@@ -360,6 +360,8 @@ def prop_with_search(
|
|||||||
prop_name: str,
|
prop_name: str,
|
||||||
should_click_ok: bool = False,
|
should_click_ok: bool = False,
|
||||||
original_operator_path: Optional[str] = None,
|
original_operator_path: Optional[str] = None,
|
||||||
|
enable_relating_type_suggestions: bool = True,
|
||||||
|
search_threshold: int = 10,
|
||||||
*,
|
*,
|
||||||
button_kwargs: Union[dict[str, Any], None] = None,
|
button_kwargs: Union[dict[str, Any], None] = None,
|
||||||
**kwargs: Any,
|
**kwargs: Any,
|
||||||
@@ -367,23 +369,26 @@ def prop_with_search(
|
|||||||
"""
|
"""
|
||||||
Draw a row with enum prop and enum search operator.
|
Draw a row with enum prop and enum search operator.
|
||||||
|
|
||||||
Search operator appears only in case if there's more than 10 item in enum.
|
Search operator appears only in case if there's more than `search_threshold` items in enum.
|
||||||
|
|
||||||
:arg button_kwargs: kwargs to pass to ``UILayout.operator()``.
|
:arg button_kwargs: kwargs to pass to ``UILayout.operator()``.
|
||||||
:arg kwargs: kwargs to pass to ``UILayout.prop()``.
|
:arg kwargs: kwargs to pass to ``UILayout.prop()``.
|
||||||
|
:arg enable_relating_type_suggestions: Enable additional suggestions for relating type properties.
|
||||||
|
:arg search_threshold: Minimum number of enum items required to show search button. Default is 10.
|
||||||
:return: Added row.
|
:return: Added row.
|
||||||
"""
|
"""
|
||||||
# kwargs are layout.prop arguments (text, icon, etc.)
|
# kwargs are layout.prop arguments (text, icon, etc.)
|
||||||
row = layout.row(align=True)
|
row = layout.row(align=True)
|
||||||
row.prop(data, prop_name, **kwargs)
|
row.prop(data, prop_name, **kwargs)
|
||||||
try:
|
try:
|
||||||
if len(get_enum_items(data, prop_name, original_operator_path=original_operator_path)) > 10:
|
if len(get_enum_items(data, prop_name, original_operator_path=original_operator_path)) > search_threshold:
|
||||||
# Magick courtesy of https://blender.stackexchange.com/a/203443/86891
|
# Magick courtesy of https://blender.stackexchange.com/a/203443/86891
|
||||||
row.context_pointer_set(name="data", data=data)
|
row.context_pointer_set(name="data", data=data)
|
||||||
op = row.operator("bim.enum_property_search", text="", icon="VIEWZOOM", **(button_kwargs or {}))
|
op = row.operator("bim.enum_property_search", text="", icon="VIEWZOOM", **(button_kwargs or {}))
|
||||||
op.prop_name = prop_name
|
op.prop_name = prop_name
|
||||||
op.should_click_ok = should_click_ok
|
op.should_click_ok = should_click_ok
|
||||||
op.original_operator_path = original_operator_path or ""
|
op.original_operator_path = original_operator_path or ""
|
||||||
|
op.enable_relating_type_suggestions = enable_relating_type_suggestions
|
||||||
except TypeError: # Prop is not iterable
|
except TypeError: # Prop is not iterable
|
||||||
pass
|
pass
|
||||||
return row
|
return row
|
||||||
|
|||||||
@@ -1030,6 +1030,7 @@ class BIM_OT_enum_property_search(bpy.types.Operator):
|
|||||||
prop_name: bpy.props.StringProperty()
|
prop_name: bpy.props.StringProperty()
|
||||||
should_click_ok: bpy.props.BoolProperty(default=False)
|
should_click_ok: bpy.props.BoolProperty(default=False)
|
||||||
original_operator_path: bpy.props.StringProperty(name="Original Operator Path", default="", options={"SKIP_SAVE"})
|
original_operator_path: bpy.props.StringProperty(name="Original Operator Path", default="", options={"SKIP_SAVE"})
|
||||||
|
enable_relating_type_suggestions: bpy.props.BoolProperty(default=True)
|
||||||
|
|
||||||
identifiers: list[str]
|
identifiers: list[str]
|
||||||
|
|
||||||
@@ -1100,6 +1101,38 @@ class BIM_OT_enum_property_search(bpy.types.Operator):
|
|||||||
predefined_type=predefined_type,
|
predefined_type=predefined_type,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if self.enable_relating_type_suggestions:
|
||||||
|
self.add_relating_type_suggestions()
|
||||||
|
|
||||||
|
def add_relating_type_suggestions(self) -> None:
|
||||||
|
ifc_file = tool.Ifc.get()
|
||||||
|
if not ifc_file:
|
||||||
|
return
|
||||||
|
|
||||||
|
type_elements = []
|
||||||
|
for identifier in self.identifiers:
|
||||||
|
element = ifc_file.by_id(int(identifier))
|
||||||
|
if element and element.is_a().endswith("Type"):
|
||||||
|
type_elements.append(element)
|
||||||
|
|
||||||
|
for element in type_elements:
|
||||||
|
base_name = element.Name or "Unnamed"
|
||||||
|
element_step_id = str(element.id())
|
||||||
|
|
||||||
|
attributes = []
|
||||||
|
for attr_name in ["Description", "PredefinedType", "ElementType", "ObjectType"]:
|
||||||
|
value = getattr(element, attr_name, None)
|
||||||
|
if value:
|
||||||
|
attributes.append(value)
|
||||||
|
|
||||||
|
if attributes:
|
||||||
|
concatenated_name = f"{base_name} > {' > '.join(attributes)}"
|
||||||
|
self.add_item(
|
||||||
|
identifier=element_step_id,
|
||||||
|
name=concatenated_name,
|
||||||
|
predefined_type=element.PredefinedType or "",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class BIM_OT_select_entity(bpy.types.Operator):
|
class BIM_OT_select_entity(bpy.types.Operator):
|
||||||
bl_idname = "bim.select_entity"
|
bl_idname = "bim.select_entity"
|
||||||
|
|||||||
Reference in New Issue
Block a user