Skip to content

solidworks_mcp.adapters.mock_templates

solidworks_mcp.adapters.mock_templates

The mock adapter's templates: saved in memory, carried into new documents.

A template records the document's units, material and custom properties, which is what a real template carries; create_part with that template starts the new part with them, as SolidWorks' NewDocument does.

Attributes

TEMPLATE_EXTENSIONS module-attribute

TEMPLATE_EXTENSIONS: dict[int, str] = {1: '.prtdot', 2: '.asmdot', 3: '.drwdot'}

_MOCK_TYPES module-attribute

_MOCK_TYPES: dict[str, int] = {'Part': 1, 'Assembly': 2, 'Drawing': 3}

Classes

AdapterResult dataclass

AdapterResult(status: AdapterResultStatus, data: T | None = None, error: str | None = None, execution_time: float | None = None, metadata: dict[str, Any] | None = None)

Result wrapper for adapter operations.

Attributes:

Name Type Description
data T | None

The data value.

error str | None

The error value.

execution_time float | None

The execution time value.

metadata dict[str, Any] | None

The metadata value.

status AdapterResultStatus

The status value.

Attributes
is_error property
is_error: bool

Check if operation had an error.

Returns:

Name Type Description
bool bool

True if error, otherwise False.

is_success property
is_success: bool

Check if operation was successful.

Returns:

Name Type Description
bool bool

True if success, otherwise False.

AdapterResultStatus

Bases: StrEnum

Result status for adapter operations.

Attributes:

Name Type Description
ERROR Any

The error value.

SUCCESS Any

The success value.

TIMEOUT Any

The timeout value.

WARNING Any

The warning value.

MockTemplatesMixin

get_template_folders and save_as_template for the mock.

Methods:
get_template_folders async
get_template_folders() -> AdapterResult[dict[str, Any]]

The mock's template folders and the active document's extension.

Returns:

Type Description
AdapterResult[dict[str, Any]]

AdapterResult[dict[str, Any]]: folders, document_type and

AdapterResult[dict[str, Any]]

extension (both None with no document).

Source code in src/solidworks_mcp/adapters/mock_templates.py
async def get_template_folders(self) -> AdapterResult[dict[str, Any]]:
    """The mock's template folders and the active document's extension.

    Returns:
        AdapterResult[dict[str, Any]]: ``folders``, ``document_type`` and
        ``extension`` (both ``None`` with no document).
    """
    model = self._current_model
    kind = _MOCK_TYPES.get(getattr(model, "type", ""), 0)
    return AdapterResult(
        status=AdapterResultStatus.SUCCESS,
        data={
            "folders": list(self._mock_template_folders),
            "document_type": str(model.type).lower() if kind else None,
            "extension": TEMPLATE_EXTENSIONS.get(kind),
        },
    )
save_as_template async
save_as_template(file_path: str) -> AdapterResult[dict[str, Any]]

Record the active document's settings as a template at file_path.

Parameters:

Name Type Description Default
file_path str

The template's path, with the document's extension.

required

Returns:

Type Description
AdapterResult[dict[str, Any]]

AdapterResult[dict[str, Any]]: The same shape as the real adapter's.

Source code in src/solidworks_mcp/adapters/mock_templates.py
async def save_as_template(self, file_path: str) -> AdapterResult[dict[str, Any]]:
    """Record the active document's settings as a template at ``file_path``.

    Args:
        file_path: The template's path, with the document's extension.

    Returns:
        AdapterResult[dict[str, Any]]: The same shape as the real adapter's.
    """
    model = self._current_model
    if model is None:
        return AdapterResult(
            status=AdapterResultStatus.ERROR, error="No active model"
        )
    extension = TEMPLATE_EXTENSIONS[_MOCK_TYPES[model.type]]
    if not file_path.lower().endswith(extension):
        return AdapterResult(
            status=AdapterResultStatus.ERROR,
            error=f"{model.type} templates are {extension}; "
            f"{file_path!r} isn't one.",
        )
    key = str(model.path)
    adapter: Any = self
    self._mock_templates()[file_path] = {
        "units": (model.properties or {}).get("units"),
        "material": getattr(adapter, "_materials", {}).get(key),
        "properties": dict(
            getattr(adapter, "_custom_properties", {}).get((key, ""), {})
        ),
    }
    # As in SolidWorks, the template is now the open document: its state
    # moves to the template's path.
    for store, old, new in (
        ("_materials", key, file_path),
        ("_custom_properties", (key, ""), (file_path, "")),
        ("_models", key, file_path),
    ):
        held = adapter.__dict__.get(store, {})
        if old in held:
            held[new] = held.pop(old)
    model.path = file_path
    return AdapterResult(
        status=AdapterResultStatus.SUCCESS,
        data={
            "file_path": file_path,
            "document_type": model.type.lower(),
            "in_template_folder": in_folders(
                file_path, list(self._mock_template_folders)
            ),
            "template_folders": list(self._mock_template_folders),
            # Mock documents are never on disk.
            "document_path": None,
            "document_became_template": True,
        },
    )

Functions:

in_folders

in_folders(path: str, folders: list[str]) -> bool

Whether path sits directly in one of folders.

Compared as Windows paths (case and separators ignored), on any OS.

Parameters:

Name Type Description Default
path str

A file path.

required
folders list[str]

Folder paths.

required

Returns:

Name Type Description
bool bool

True when the file's folder is one of them.

Source code in src/solidworks_mcp/adapters/solidworks/templates.py
def in_folders(path: str, folders: list[str]) -> bool:
    """Whether ``path`` sits directly in one of ``folders``.

    Compared as Windows paths (case and separators ignored), on any OS.

    Args:
        path: A file path.
        folders: Folder paths.

    Returns:
        bool: True when the file's folder is one of them.
    """
    parent = ntpath.normcase(ntpath.normpath(ntpath.dirname(path)))
    return any(ntpath.normcase(ntpath.normpath(folder)) == parent for folder in folders)