Skip to content

solidworks_mcp.adapters.solidworks.feature_walk

solidworks_mcp.adapters.solidworks.feature_walk

The one walk of a document's top-level feature tree.

list_features and list_model_dimensions both read the tree through :func:walk_feature_tree, so they always see the same features in the same order.

SolidWorks can stop IFeature.GetNextFeature at the system folders even though later sketches and features exist, so the walk always makes a second pass with IModelDoc2.FeatureByPositionReverse. Both passes number a feature by its place in the tree (the forward pass counts from 0, the reverse pass uses count - reverse_position - 1), so a feature found by both gets one number, and sorting by it gives tree order, oldest first.

Attributes

WALK_MEMBERS module-attribute

WALK_MEMBERS = ('GetNextFeature', 'GetTypeName2', 'IsSuppressed', 'IsSuppressed2')

_MAX_FORWARD_STEPS module-attribute

_MAX_FORWARD_STEPS = 10000

Classes

TreeFeature dataclass

TreeFeature(position: int, name: str, type_name: str, feature: Any)

One top-level feature found by the walk.

Attributes:

Name Type Description
position int

Its place in the tree, 0 for the first.

name str

The feature's name.

type_name str

Its GetTypeName2 type, "Unknown" when unreadable.

feature Any

The IFeature dispatch, for reading more from it.

Functions:

_describe

_describe(adapter: Any, feature: Any, position: int) -> TreeFeature

Read one feature's name and type into a :class:TreeFeature.

Parameters:

Name Type Description Default
adapter Any

The adapter, for _attempt and _get_attr_or_call.

required
feature Any

The IFeature dispatch.

required
position int

Its place in the tree.

required

Returns:

Name Type Description
TreeFeature TreeFeature

The feature with its name and type.

Source code in src/solidworks_mcp/adapters/solidworks/feature_walk.py
def _describe(adapter: Any, feature: Any, position: int) -> TreeFeature:
    """Read one feature's name and type into a :class:`TreeFeature`.

    Args:
        adapter: The adapter, for ``_attempt`` and ``_get_attr_or_call``.
        feature: The ``IFeature`` dispatch.
        position: Its place in the tree.

    Returns:
        TreeFeature: The feature with its name and type.
    """
    type_name = attempt(
        adapter,
        lambda: adapter._get_attr_or_call(feature, "GetTypeName2"),
        "Unknown",
    )
    return TreeFeature(
        position=position,
        name=str(getattr(feature, "Name", "")),
        type_name=str(type_name),
        feature=feature,
    )

attempt

attempt(adapter: Any, fn: Any, default: Any = None) -> Any

Run fn through the adapter's _attempt (exceptions -> default).

Source code in src/solidworks_mcp/adapters/solidworks/com_utils.py
def attempt(adapter: Any, fn: Any, default: Any = None) -> Any:
    """Run ``fn`` through the adapter's ``_attempt`` (exceptions -> default)."""
    return adapter._attempt(fn, default=default)

flag

flag(adapter: Any, obj: Any, *names: str) -> None

Flag just names as methods on obj (no-op for None/fakes).

Flagging a whole interface costs ~27 ms per fresh dispatch (see WALK_MEMBERS in feature_walk); SolidWorks hands back a fresh dispatch on most calls, so flag only what is about to be read.

Source code in src/solidworks_mcp/adapters/solidworks/com_utils.py
def flag(adapter: Any, obj: Any, *names: str) -> None:
    """Flag just ``names`` as methods on ``obj`` (no-op for ``None``/fakes).

    Flagging a whole interface costs ~27 ms per fresh dispatch (see
    ``WALK_MEMBERS`` in feature_walk); SolidWorks hands back a fresh
    dispatch on most calls, so flag only what is about to be read.
    """
    attempt(adapter, lambda: _sw_type_info.flag_members(obj, *names), 0)

walk_feature_tree

walk_feature_tree(adapter: Any, document: Any) -> list[TreeFeature]

Every top-level feature of document, once each, in tree order.

Parameters:

Name Type Description Default
adapter Any

The adapter, for _attempt and _get_attr_or_call.

required
document Any

The IModelDoc2 dispatch to walk.

required

Returns:

Type Description
list[TreeFeature]

list[TreeFeature]: The features, sorted by tree position. A feature

list[TreeFeature]

found by both passes (same name and type) is kept once.

Source code in src/solidworks_mcp/adapters/solidworks/feature_walk.py
def walk_feature_tree(adapter: Any, document: Any) -> list[TreeFeature]:
    """Every top-level feature of ``document``, once each, in tree order.

    Args:
        adapter: The adapter, for ``_attempt`` and ``_get_attr_or_call``.
        document: The ``IModelDoc2`` dispatch to walk.

    Returns:
        list[TreeFeature]: The features, sorted by tree position. A feature
        found by both passes (same name and type) is kept once.
    """
    found: list[TreeFeature] = []
    seen: set[tuple[str, str]] = set()

    def _keep(feature: Any, position: int) -> None:
        item = _describe(adapter, feature, position)
        key = (item.name, item.type_name)
        if key not in seen:
            seen.add(key)
            found.append(item)

    feature = attempt(adapter, lambda: document.FirstFeature())
    position = 0
    while feature is not None and position < _MAX_FORWARD_STEPS:
        flag(adapter, feature, *WALK_MEMBERS)
        _keep(feature, position)
        position += 1
        feature = attempt(adapter, lambda f=feature: f.GetNextFeature())

    feature_manager = getattr(document, "FeatureManager", None)
    count = attempt(
        adapter,
        lambda: int(feature_manager.GetFeatureCount(True) or 0),  # type: ignore[union-attr]
        0,
    )
    # FeatureByPositionReverse is zero-based: 0 is the newest feature.
    for reverse_position in range(count):
        feature = attempt(
            adapter, lambda p=reverse_position: document.FeatureByPositionReverse(p)
        )
        if feature is not None:
            _keep(feature, count - reverse_position - 1)

    found.sort(key=lambda item: item.position)
    return found