Skip to content

solidworks_mcp.adapters.vba_adapter

solidworks_mcp.adapters.vba_adapter

VBA adapter path for complex operations with generated macro metadata.

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.

ExtrusionParameters

Bases: BaseModel

Parameters for extrusion operations.

Attributes:

Name Type Description
auto_select bool

The auto select value.

both_directions bool

The both directions value.

depth float

The depth value.

draft_angle float

The draft angle value.

end_condition str

The end condition value.

feature_scope bool

The feature scope value.

merge_result bool

The merge result value.

reverse_direction bool

The reverse direction value.

thin_feature bool

The thin feature value.

thin_thickness float | None

The thin thickness value.

up_to_surface str | None

The up to surface value.

LoftParameters

Bases: BaseModel

Parameters for loft operations.

Attributes:

Name Type Description
end_tangent str | None

The end tangent value.

guide_curves list[str] | None

The guide curves value.

merge_result bool

The merge result value.

profiles list[str]

The profiles value.

start_tangent str | None

The start tangent 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.

RevolveParameters

Bases: BaseModel

Parameters for revolve operations.

Attributes:

Name Type Description
angle float

The angle value.

both_directions bool

The both directions value.

merge_result bool

The merge result value.

reverse_direction bool

The reverse direction value.

thin_feature bool

The thin feature value.

thin_thickness float | None

The thin thickness value.

SweepParameters

Bases: BaseModel

Parameters for sweep operations.

Attributes:

Name Type Description
merge_result bool

The merge result value.

path str

The path value.

twist_along_path bool

The twist along path value.

twist_angle float

The twist angle value.

VbaGeneratorAdapter

VbaGeneratorAdapter(backing_adapter: Any, macro_executor: VbaMacroExecutor | None = None)

Adapter that executes complex operations through VBA-oriented flow.

This adapter currently uses the wrapped COM adapter for final execution, but annotates responses as VBA-routed and can be extended to execute generated macros directly in future iterations.

Parameters:

Name Type Description Default
backing_adapter Any

The backing adapter value.

required
macro_executor VbaMacroExecutor | None

The macro executor value. Defaults to None.

None

Attributes:

Name Type Description
_backing_adapter Any

The backing adapter value.

_macro_executor Any

The macro executor value.

config Any

The config value.

Initialize the vba generator adapter.

Parameters:

Name Type Description Default
backing_adapter Any

The backing adapter value.

required
macro_executor VbaMacroExecutor | None

The macro executor value. Defaults to None.

None

Returns:

Name Type Description
None None

None.

Source code in src/solidworks_mcp/adapters/vba_adapter.py
def __init__(
    self,
    backing_adapter: Any,
    macro_executor: VbaMacroExecutor | None = None,
) -> None:
    """Initialize the vba generator adapter.

    Args:
        backing_adapter (Any): The backing adapter value.
        macro_executor (VbaMacroExecutor | None): The macro executor value. Defaults to
                                                  None.

    Returns:
        None: None.
    """
    self._backing_adapter = backing_adapter
    self._macro_executor = macro_executor or VbaMacroExecutor()
    self.config = getattr(backing_adapter, "config", None)
Functions
__getattr__
__getattr__(item: str) -> Any

Delegate unknown members to backing adapter.

Parameters:

Name Type Description Default
item str

The item value.

required

Returns:

Name Type Description
Any Any

The result produced by the operation.

Source code in src/solidworks_mcp/adapters/vba_adapter.py
def __getattr__(self, item: str) -> Any:
    """Delegate unknown members to backing adapter.

    Args:
        item (str): The item value.

    Returns:
        Any: The result produced by the operation.
    """
    return getattr(self._backing_adapter, item)
connect async
connect() -> None

Connect to SolidWorks using wrapped adapter.

Returns:

Name Type Description
None None

None.

Source code in src/solidworks_mcp/adapters/vba_adapter.py
async def connect(self) -> None:
    """Connect to SolidWorks using wrapped adapter.

    Returns:
        None: None.
    """
    await self._backing_adapter.connect()
create_extrusion async
create_extrusion(params: ExtrusionParameters) -> AdapterResult[Any]

Create the extrusion.

Parameters:

Name Type Description Default
params ExtrusionParameters

The params value.

required

Returns:

Type Description
AdapterResult[Any]

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

Source code in src/solidworks_mcp/adapters/vba_adapter.py
async def create_extrusion(
    self,
    params: ExtrusionParameters,
) -> AdapterResult[Any]:
    """Create the extrusion.

    Args:
        params (ExtrusionParameters): The params value.

    Returns:
        AdapterResult[Any]: The result produced by the operation.
    """
    return await self._run_with_vba_metadata(
        operation="create_extrusion",
        payload=params,
        com_call=self._backing_adapter.create_extrusion,
        vba_code=self._generate_extrusion_vba(params),
    )
create_loft async
create_loft(params: LoftParameters) -> AdapterResult[Any]

Create the loft.

Parameters:

Name Type Description Default
params LoftParameters

The params value.

required

Returns:

Type Description
AdapterResult[Any]

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

Source code in src/solidworks_mcp/adapters/vba_adapter.py
async def create_loft(
    self,
    params: LoftParameters,
) -> AdapterResult[Any]:
    """Create the loft.

    Args:
        params (LoftParameters): The params value.

    Returns:
        AdapterResult[Any]: The result produced by the operation.
    """
    return await self._run_with_vba_metadata(
        operation="create_loft",
        payload=params,
        com_call=self._backing_adapter.create_loft,
        vba_code=self._generate_loft_vba(params),
    )
create_revolve async
create_revolve(params: RevolveParameters) -> AdapterResult[Any]

Create the revolve.

Parameters:

Name Type Description Default
params RevolveParameters

The params value.

required

Returns:

Type Description
AdapterResult[Any]

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

Source code in src/solidworks_mcp/adapters/vba_adapter.py
async def create_revolve(
    self,
    params: RevolveParameters,
) -> AdapterResult[Any]:
    """Create the revolve.

    Args:
        params (RevolveParameters): The params value.

    Returns:
        AdapterResult[Any]: The result produced by the operation.
    """
    return await self._run_with_vba_metadata(
        operation="create_revolve",
        payload=params,
        com_call=self._backing_adapter.create_revolve,
        vba_code=self._generate_revolve_vba(params),
    )
create_sweep async
create_sweep(params: SweepParameters) -> AdapterResult[Any]

Create the sweep.

Parameters:

Name Type Description Default
params SweepParameters

The params value.

required

Returns:

Type Description
AdapterResult[Any]

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

Source code in src/solidworks_mcp/adapters/vba_adapter.py
async def create_sweep(
    self,
    params: SweepParameters,
) -> AdapterResult[Any]:
    """Create the sweep.

    Args:
        params (SweepParameters): The params value.

    Returns:
        AdapterResult[Any]: The result produced by the operation.
    """
    return await self._run_with_vba_metadata(
        operation="create_sweep",
        payload=params,
        com_call=self._backing_adapter.create_sweep,
        vba_code=self._generate_sweep_vba(params),
    )
disconnect async
disconnect() -> None

Disconnect wrapped adapter.

Returns:

Name Type Description
None None

None.

Source code in src/solidworks_mcp/adapters/vba_adapter.py
async def disconnect(self) -> None:
    """Disconnect wrapped adapter.

    Returns:
        None: None.
    """
    await self._backing_adapter.disconnect()
execute_macro async
execute_macro(macro_code: str, macro_name: str = 'GeneratedMacro', subroutine: str = 'Main') -> AdapterResult[Any]

Provide execute macro support for the vba generator adapter.

Parameters:

Name Type Description Default
macro_code str

The macro code value.

required
macro_name str

The macro name value. Defaults to "GeneratedMacro".

'GeneratedMacro'
subroutine str

The subroutine value. Defaults to "Main".

'Main'

Returns:

Type Description
AdapterResult[Any]

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

Source code in src/solidworks_mcp/adapters/vba_adapter.py
async def execute_macro(
    self,
    macro_code: str,
    macro_name: str = "GeneratedMacro",
    subroutine: str = "Main",
) -> AdapterResult[Any]:
    """Provide execute macro support for the vba generator adapter.

    Args:
        macro_code (str): The macro code value.
        macro_name (str): The macro name value. Defaults to "GeneratedMacro".
        subroutine (str): The subroutine value. Defaults to "Main".

    Returns:
        AdapterResult[Any]: The result produced by the operation.
    """
    request = MacroExecutionRequest(
        macro_code=macro_code,
        macro_name=macro_name,
        subroutine=subroutine,
    )
    return await self._macro_executor.execute_macro(
        request=request,
        backing_adapter=self._backing_adapter,
    )
get_macro_execution_history
get_macro_execution_history(macro_name: str | None = None) -> dict[str, Any]

Retrieve VBA 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, Any]

dict[ str, Any,

dict[str, Any]

]: A dictionary containing the resulting values.

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

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

    Returns:
        dict[
            str,
            Any,
        ]: A dictionary containing the resulting values.
    """
    history = self._macro_executor.get_execution_history(macro_name)
    return {key: value.__dict__ for key, value in history.items()}
health_check async
health_check() -> Any

Return wrapped adapter health with VBA route marker.

Returns:

Name Type Description
Any Any

The result produced by the operation.

Source code in src/solidworks_mcp/adapters/vba_adapter.py
async def health_check(self) -> Any:
    """Return wrapped adapter health with VBA route marker.

    Returns:
        Any: The result produced by the operation.
    """
    health = await self._backing_adapter.health_check()
    if hasattr(health, "metrics"):
        metrics = dict(health.metrics or {})
        metrics["route"] = "vba"
        health.metrics = metrics
    return health
is_connected
is_connected() -> bool

Return wrapped adapter connection state.

Returns:

Name Type Description
bool bool

True if connected, otherwise False.

Source code in src/solidworks_mcp/adapters/vba_adapter.py
def is_connected(self) -> bool:
    """Return wrapped adapter connection state.

    Returns:
        bool: True if connected, otherwise False.
    """
    return bool(self._backing_adapter.is_connected())

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)