Skip to content

gui_selection

freecad.datamanager_wb.ui.gui_selection

GUI selection helpers for the DataManager workbench.

This module contains small utilities for selecting FreeCAD objects referenced by expression items.

The helper functions route FreeCAD/FreeCADGui access through FreeCadPort so they can be tested (or at least imported) outside FreeCAD.

select_object_from_expression_item(expression_item, *, ctx=None)

Select the FreeCAD object referenced by an expression item.

The expressions list in the panel can provide either: - an ExpressionItem instance, or - its display string (e.g. "Object.Property = expr").

This function resolves the owning object name and selects it in the model tree.

Parameters:

Name Type Description Default
expression_item ExpressionItem | str

Expression item object or expression display string.

required
Source code in freecad/datamanager_wb/ui/gui_selection.py
def select_object_from_expression_item(
    expression_item: ExpressionItem | str,
    *,
    ctx: FreeCadContext | None = None,
) -> None:
    """Select the FreeCAD object referenced by an expression item.

    The expressions list in the panel can provide either:
    - an `ExpressionItem` instance, or
    - its display string (e.g. ``"Object.Property = expr"``).

    This function resolves the owning object name and selects it in the model
    tree.

    Args:
        expression_item: Expression item object or expression display string.
    """
    obj_name = _resolve_object_name(expression_item)
    if obj_name is None:
        return

    port = get_port(ctx)
    doc_and_obj = _get_active_doc_and_object(obj_name=obj_name, ctx=ctx)
    if doc_and_obj is None:
        return

    doc, obj = doc_and_obj
    names = _get_doc_and_object_internal_names(doc=doc, obj=obj)
    if names is None:
        return

    doc_name, obj_internal_name = names
    port.clear_selection()
    port.add_selection(doc_name=doc_name, obj_name=obj_internal_name)