Skip to content

solidworks_mcp.adapters.solidworks.default_entities

solidworks_mcp.adapters.solidworks.default_entities

Find the default planes, the origin and the standard views by identity.

Added in 2.2.0 for #262 (German SolidWorks 2025) and #261 (French): it replaces the name tables and the process-wide pywin32 hook.

The tools name these in English ("Top", "Front Plane", "*Isometric"), but their names in a document depend on the template and the install's language: "Plan de dessus" on French, "Ebene oben" on German, and anything at all once a user renames them. Their identity does not change:

  • the first three RefPlane features before the OriginProfileFeature are Front, Top and Right, in that order, in every part, assembly and component, in every language;
  • IModelDoc2.GetModelViewNames lists the ten standard views first, in a fixed order (normal to, front, back, left, right, top, bottom, isometric, trimetric, dimetric).

Every call reads the document it works on, so two open documents with differently named planes each get their own names and nothing is cached.

Attributes

DEFAULT_PLANES module-attribute

DEFAULT_PLANES = ('Front Plane', 'Top Plane', 'Right Plane')

ORIGIN module-attribute

ORIGIN = 'Origin'

STANDARD_VIEWS module-attribute

STANDARD_VIEWS = ('*Normal To', '*Front', '*Back', '*Left', '*Right', '*Top', '*Bottom', '*Isometric', '*Trimetric', '*Dimetric')

_ALIASES module-attribute

_ALIASES: dict[str, int] = {'front': 0, 'front plane': 0, 'xy': 0, 'top': 1, 'top plane': 1, 'xz': 1, 'right': 2, 'right plane': 2, 'yz': 2, 'origin': _ORIGIN_INDEX, '前视': 0, '前視': 0, '前视基准面': 0, '前視基準面': 0, '上视': 1, '上視': 1, '上视基准面': 1, '上視基準面': 1, '右视': 2, '右視': 2, '右视基准面': 2, '右視基準面': 2, '原点': _ORIGIN_INDEX, '原點': _ORIGIN_INDEX}

_ORIGIN_INDEX module-attribute

_ORIGIN_INDEX = 3

_WALK_LIMIT module-attribute

_WALK_LIMIT = 200

Classes

DefaultEntities dataclass

DefaultEntities(planes: tuple[str, ...], origin: str | None)

What the start of one feature tree says about its default entities.

Attributes:

Name Type Description
planes tuple[str, ...]

Every RefPlane before the origin, in tree order; the first three are Front, Top and Right.

origin str | None

The origin feature's name, None when the walk never reached it (the tree couldn't be read).

Attributes
readable property
readable: bool

Whether the walk reached the origin, so the planes are known.

Methods:
name_of
name_of(index: int) -> str | None

The document's name for entity index (0 Front ... 3 Origin).

Source code in src/solidworks_mcp/adapters/solidworks/default_entities.py
def name_of(self, index: int) -> str | None:
    """The document's name for entity ``index`` (0 Front ... 3 Origin)."""
    if index == _ORIGIN_INDEX:
        return self.origin
    return self.planes[index] if len(self.planes) >= 3 else None

Functions:

_owner_for

_owner_for(adapter: Any, suffix: str) -> Any

The tree a name with @suffix lives in: the named component when the active document is an assembly that has it, else the active document.

Source code in src/solidworks_mcp/adapters/solidworks/default_entities.py
def _owner_for(adapter: Any, suffix: str) -> Any:
    """The tree a name with ``@suffix`` lives in: the named component when the
    active document is an assembly that has it, else the active document."""
    model = adapter.currentModel
    component_name = suffix.partition("@")[0]
    if component_name:
        component = attempt(
            adapter, lambda: model.GetComponentByName(component_name), None
        )
        if component is not None:
            return component
    return model

_read

_read(obj: Any, name: str) -> Any

Read a member late binding may expose as a property or a method.

Source code in src/solidworks_mcp/adapters/solidworks/default_entities.py
def _read(obj: Any, name: str) -> Any:
    """Read a member late binding may expose as a property or a method."""
    member = getattr(obj, name)
    return member() if callable(member) else member

as_list

as_list(value: Any) -> list[Any]

Normalise a COM array/scalar/None return into a list.

Source code in src/solidworks_mcp/adapters/solidworks/com_utils.py
def as_list(value: Any) -> list[Any]:
    """Normalise a COM array/scalar/None return into a list."""
    if value is None:
        return []
    if isinstance(value, (list, tuple)):
        return list(value)
    return [value]

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)

default_index

default_index(name: str) -> int | None

Which default entity name means (0 Front, 1 Top, 2 Right, 3 Origin).

Any @component@assembly suffix is ignored; None for any other name.

Source code in src/solidworks_mcp/adapters/solidworks/default_entities.py
def default_index(name: str) -> int | None:
    """Which default entity ``name`` means (0 Front, 1 Top, 2 Right, 3 Origin).

    Any ``@component@assembly`` suffix is ignored; ``None`` for any other name.
    """
    base = str(name or "").partition("@")[0]
    return _ALIASES.get(base.strip().casefold())

entity_name

entity_name(adapter: Any, name: str, owner: Any = None) -> str

The document's own name for a default plane or the origin.

Front, Top, Right, XY, XZ, YZ, Front Plane... and Origin, in any case, are found by their place in the tree; a @component@assembly suffix reads that component's tree and is kept. Any other name is returned unchanged.

Parameters:

Name Type Description Default
adapter Any

A connected adapter with a document open.

required
name str

The name a tool was given.

required
owner Any

The document or component to read; by default the active document, or the component the suffix names.

None

Returns:

Name Type Description
str str

The name to pass to SolidWorks. When the tree can't be read, the

str

English name ("Top Plane"), as before this lookup existed.

Raises:

Type Description
LookupError

The tree reached its origin with fewer than three reference planes before it; the message lists them and the install's language.

Source code in src/solidworks_mcp/adapters/solidworks/default_entities.py
def entity_name(adapter: Any, name: str, owner: Any = None) -> str:
    """The document's own name for a default plane or the origin.

    ``Front``, ``Top``, ``Right``, ``XY``, ``XZ``, ``YZ``, ``Front Plane``...
    and ``Origin``, in any case, are found by their place in the tree; a
    ``@component@assembly`` suffix reads that component's tree and is kept.
    Any other name is returned unchanged.

    Args:
        adapter: A connected adapter with a document open.
        name: The name a tool was given.
        owner: The document or component to read; by default the active
            document, or the component the suffix names.

    Returns:
        str: The name to pass to SolidWorks. When the tree can't be read, the
        English name (``"Top Plane"``), as before this lookup existed.

    Raises:
        LookupError: The tree reached its origin with fewer than three
            reference planes before it; the message lists them and the
            install's language.
    """
    index = default_index(name)
    if index is None:
        return name
    base, sep, suffix = str(name).partition("@")
    target = owner if owner is not None else _owner_for(adapter, suffix)
    defaults = read_defaults(adapter, target)
    english = ORIGIN if index == _ORIGIN_INDEX else DEFAULT_PLANES[index]
    if not defaults.readable:
        return english + sep + suffix
    local = defaults.name_of(index)
    if local is None:
        planes = ", ".join(defaults.planes) or "none"
        raise LookupError(
            f"Default plane {base.strip()!r} not found: the document has "
            f"{len(defaults.planes)} reference plane(s) before its origin "
            f"({planes}), not the three default ones. SolidWorks language: "
            f"{install_language(adapter)}."
        )
    return local + sep + suffix

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)

install_language

install_language(adapter: Any) -> str

The SolidWorks install's language (GetCurrentLanguage), or unknown.

Source code in src/solidworks_mcp/adapters/solidworks/default_entities.py
def install_language(adapter: Any) -> str:
    """The SolidWorks install's language (``GetCurrentLanguage``), or ``unknown``."""
    language = attempt(adapter, lambda: adapter.swApp.GetCurrentLanguage(), None)
    return str(language) if language else "unknown"

read_defaults

read_defaults(adapter: Any, owner: Any) -> DefaultEntities

Walk owner's tree (a ModelDoc2 or Component2) up to its origin.

Parameters:

Name Type Description Default
adapter Any

The adapter, for _attempt.

required
owner Any

The document or component whose tree to read.

required

Returns:

Name Type Description
DefaultEntities DefaultEntities

The planes before the origin, and the origin.

Source code in src/solidworks_mcp/adapters/solidworks/default_entities.py
def read_defaults(adapter: Any, owner: Any) -> DefaultEntities:
    """Walk ``owner``'s tree (a ModelDoc2 or Component2) up to its origin.

    Args:
        adapter: The adapter, for ``_attempt``.
        owner: The document or component whose tree to read.

    Returns:
        DefaultEntities: The planes before the origin, and the origin.
    """
    flag(adapter, owner, "FirstFeature")
    feature = attempt(adapter, lambda: _read(owner, "FirstFeature"))
    planes: list[str] = []
    for _ in range(_WALK_LIMIT):
        if feature is None:
            break
        flag(adapter, feature, "GetTypeName2", "GetNextFeature")
        type_name = attempt(adapter, lambda f=feature: _read(f, "GetTypeName2"), "")
        name = str(attempt(adapter, lambda f=feature: f.Name, "") or "")
        if type_name == "OriginProfileFeature":
            return DefaultEntities(tuple(planes), name)
        if type_name == "RefPlane":
            planes.append(name)
        feature = attempt(adapter, lambda f=feature: _read(f, "GetNextFeature"))
    return DefaultEntities(tuple(planes), None)

standard_view_name

standard_view_name(adapter: Any, model: Any, english: str) -> str

The model's own name for a standard view ("*Isometric" ...).

Read from model.GetModelViewNames by the view's position; the English name when english isn't a standard view, or when the list is too short or its first ten aren't all *-named views.

Source code in src/solidworks_mcp/adapters/solidworks/default_entities.py
def standard_view_name(adapter: Any, model: Any, english: str) -> str:
    """The model's own name for a standard view (``"*Isometric"`` ...).

    Read from ``model.GetModelViewNames`` by the view's position; the
    English name when ``english`` isn't a standard view, or when the list is
    too short or its first ten aren't all ``*``-named views.
    """
    if english not in STANDARD_VIEWS:
        return english
    position = STANDARD_VIEWS.index(english)
    flag(adapter, model, "GetModelViewNames")
    names = [
        str(n)
        for n in as_list(attempt(adapter, lambda: _read(model, "GetModelViewNames")))
    ]
    count = len(STANDARD_VIEWS)
    if len(names) < count or not all(n.startswith("*") for n in names[:count]):
        return english
    return names[position]