Skip to content

solidworks_mcp.adapters.vba_macro_executor

solidworks_mcp.adapters.vba_macro_executor

VBA macro execution lifecycle management for SolidWorks automation.

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)

Bases: Generic[T]

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.

MacroExecutionRequest dataclass

MacroExecutionRequest(macro_code: str, macro_name: str, subroutine: str = 'Main')

Request to execute a VBA macro.

Attributes:

Name Type Description
macro_code str

The macro code value.

macro_name str

The macro name value.

subroutine str

The subroutine value.

MacroExecutionResult dataclass

MacroExecutionResult(success: bool, macro_name: str, output: str | dict[str, Any] | None = None, error: str | None = None, duration_seconds: float = 0.0)

Result of VBA macro execution.

Attributes:

Name Type Description
duration_seconds float

The duration seconds value.

error str | None

The error value.

macro_name str

The macro name value.

output str | dict[str, Any] | None

The output value.

success bool

The success value.

VbaMacroExecutor

VbaMacroExecutor(temp_macro_dir: Path | None = None)

Manage VBA macro execution with save and tracking.

This executor handles the full lifecycle: code generation, on-disk persistence, execution via the backing adapter, and result tracking.

Parameters:

Name Type Description Default
temp_macro_dir Path | None

The temp macro dir value. Defaults to None.

None

Attributes:

Name Type Description
_temp_macro_dir Any

The temp macro dir value.

Initialize macro executor.

Parameters:

Name Type Description Default
temp_macro_dir Path | None

The temp macro dir value. Defaults to None.

None

Returns:

Name Type Description
None None

None.

Source code in src/solidworks_mcp/adapters/vba_macro_executor.py
def __init__(self, temp_macro_dir: Path | None = None) -> None:
    """Initialize macro executor.

    Args:
        temp_macro_dir (Path | None): The temp macro dir value. Defaults to None.

    Returns:
        None: None.
    """
    self._temp_macro_dir = temp_macro_dir or Path(tempfile.gettempdir())
    self._execution_history: dict[str, MacroExecutionResult] = {}
Functions
execute_macro async
execute_macro(request: MacroExecutionRequest, backing_adapter: Any) -> AdapterResult[MacroExecutionResult]

Provide execute macro support for the vba macro executor.

Parameters:

Name Type Description Default
request MacroExecutionRequest

The request value.

required
backing_adapter Any

The backing adapter value.

required

Returns:

Type Description
AdapterResult[MacroExecutionResult]

AdapterResult[MacroExecutionResult]: The result produced by the operation.

Source code in src/solidworks_mcp/adapters/vba_macro_executor.py
async def execute_macro(
    self,
    request: MacroExecutionRequest,
    backing_adapter: Any,
) -> AdapterResult[MacroExecutionResult]:
    """Provide execute macro support for the vba macro executor.

    Args:
        request (MacroExecutionRequest): The request value.
        backing_adapter (Any): The backing adapter value.

    Returns:
        AdapterResult[MacroExecutionResult]: The result produced by the operation.
    """
    start_time = datetime.utcnow()
    macro_path = self._save_macro_to_disk(request.macro_code, request.macro_name)

    try:
        result = await self._execute_via_adapter(
            macro_path=macro_path,
            subroutine=request.subroutine,
            backing_adapter=backing_adapter,
        )

        duration = (datetime.utcnow() - start_time).total_seconds()
        execution_result = MacroExecutionResult(
            success=result.get("success", False),
            macro_name=request.macro_name,
            output=result.get("output"),
            error=result.get("error"),
            duration_seconds=duration,
        )

        self._execution_history[request.macro_name] = execution_result

        return AdapterResult(
            status=AdapterResultStatus.SUCCESS,
            data=execution_result,
            metadata={
                "macro_path": str(macro_path),
                "macro_name": request.macro_name,
                "duration_seconds": duration,
                "subroutine": request.subroutine,
            },
        )

    except Exception as exc:
        duration = (datetime.utcnow() - start_time).total_seconds()
        execution_result = MacroExecutionResult(
            success=False,
            macro_name=request.macro_name,
            error=str(exc),
            duration_seconds=duration,
        )

        self._execution_history[request.macro_name] = execution_result

        return AdapterResult(
            status=AdapterResultStatus.ERROR,
            error=f"macro execution failed: {exc}",
        )
get_execution_history
get_execution_history(macro_name: str | None = None) -> dict[str, MacroExecutionResult]

Retrieve macro execution history.

Parameters:

Name Type Description Default
macro_name str | None

The macro name value. Defaults to None.

None

Returns:

Type Description
dict[str, MacroExecutionResult]

dict[ str, MacroExecutionResult,

dict[str, MacroExecutionResult]

]: A dictionary containing the resulting values.

Source code in src/solidworks_mcp/adapters/vba_macro_executor.py
def get_execution_history(
    self, macro_name: str | None = None
) -> dict[
    str,
    MacroExecutionResult,
]:
    """Retrieve macro execution history.

    Args:
        macro_name (str | None): The macro name value. Defaults to None.

    Returns:
        dict[
            str,
            MacroExecutionResult,
        ]: A dictionary containing the
                                                                resulting values.
    """
    if macro_name is not None:
        return (
            {
                macro_name: self._execution_history[macro_name],
            }
            if macro_name in self._execution_history
            else {}
        )
    return dict(self._execution_history)