Skip to content

AutoCAD Adapter

The main adapter class composed from 11 specialized mixins. Supports AutoCAD, ZWCAD, GstarCAD, and BricsCAD via the same interface.

adapters.autocad_adapter

AutoCAD adapter for multiCAD-MCP.

Implements CADInterface for AutoCAD using Windows COM. Supports AutoCAD, ZWCAD, GstarCAD, and BricsCAD via factory pattern.

Refactored to use mixin classes for better organization and maintainability.

SelectionSetManager

Context manager for safe SelectionSet handling.

Ensures SelectionSet cleanup even on exceptions, preventing orphaned selection sets that can cause issues in AutoCAD.

Example

with SelectionSetManager(document, "TEMP_SS") as ss: ss.Select(...) # ... use ss ...

Auto-deleted on exit

__init__

__init__(document: Any, name: str)

Initialize SelectionSet manager.

PARAMETER DESCRIPTION
document

AutoCAD document object

TYPE: Any

name

Name for the selection set

TYPE: str

__enter__

__enter__() -> Any

Create SelectionSet, deleting existing one if present.

RETURNS DESCRIPTION
Any

Created SelectionSet object

__exit__

__exit__(
    _exc_type: Any, _exc_val: Any, _exc_tb: Any
) -> None

Cleanup SelectionSet on exit.

PARAMETER DESCRIPTION
_exc_type

Exception type if raised

TYPE: Any

_exc_val

Exception value if raised

TYPE: Any

_exc_tb

Exception traceback if raised

TYPE: Any

AutoCADAdapter

Bases: UtilityMixin, ConnectionMixin, DrawingMixin, LayerMixin, FileMixin, ViewMixin, SelectionMixin, EntityMixin, ManipulationMixin, BlockMixin, ExportMixin, CADInterface

Adapter for controlling AutoCAD via COM interface.

[... docstring truncated for brevity ...]

application property writable

application: Any

Get the thread-local application COM proxy.

document property writable

document: Any

Get the thread-local document COM proxy.

__init__

__init__(cad_type: str = 'autocad')

Initialize AutoCAD adapter.

PARAMETER DESCRIPTION
cad_type

Type of CAD (autocad, zwcad, gcad, bricscad)

TYPE: str DEFAULT: 'autocad'

com_session

com_session()

Context manager for safe COM initialization and cleanup.

Ensures CoInitialize/CoUninitialize are always paired, even on exceptions. Use this for all COM operations to prevent thread state leaks.

Example

with com_session(): app = win32com.client.Dispatch("AutoCAD.Application") # ... use app ...

com_safe

com_safe(
    return_type: type = bool,
    operation_name: str = "operation",
)

Decorator for COM operation error handling.

Wraps method with: - Exception catching (pywintypes.com_error) - Operation logging - Automatic error conversion to CADOperationError

PARAMETER DESCRIPTION
return_type

Expected return type (for type hints)

TYPE: type DEFAULT: bool

operation_name

Name of operation (for logging)

TYPE: str DEFAULT: 'operation'