AutoCAD-Agent MCP Server
README.md
# AutoCAD-Agent MCP Server
讓 Claude 等 AI 助理透過 [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) 直接操作 Windows 上的 AutoCAD:畫圖、標註、讀圖、核對字高、輸出圖片檢查,以及插入圖塊和填寫屬性。
專為**建築圖說**工作流程設計:完整支援中文字型(微軟正黑體、標楷體等 TrueType 與 SHX 大字體),可一次設定圖層、文字型式與標註型式,並能整批繪製數百個物件。
> **English summary**
> An MCP server that lets Claude (or any MCP client) drive a running AutoCAD on Windows through COM.
> 29 tools: drawing primitives, hatches, text and dimensions, one-call drawing setup (layers, text and dimension styles), batch drawing with undo grouping, atomic rollback and dry-run validation, entity inspection with text-height statistics, PNG view export for visual verification, block insertion with attributes, and a guarded command or AutoLISP channel. Built for architectural drafting with full CJK font support.
---
## 功能一覽(29 個工具)
| 分類 | 工具 | 說明 |
|---|---|---|
| 繪圖環境 | `setup_drawing` | 一次設定圖層、線型、文字型式、標註型式、系統變數 |
| | `create_or_set_layer`、`create_text_style` | 建立或切換圖層;建立中文文字型式(解決中文變問號) |
| 幾何繪圖 | `draw_line`、`draw_polyline`、`draw_rectangle`、`draw_circle`、`draw_arc`、`draw_ellipse`、`draw_spline` | 對應 LINE、PLINE、REC、CIRCLE、ARC、ELLIPSE、SPLINE |
| 填充與邊界 | `add_hatch`、`create_boundary` | 剖面線填充(SOLID、ANSI31、AR-CONC…)、BOUNDARY |
| 文字與標註 | `add_text`、`add_mtext`、`add_linear_dimension`、`add_aligned_dimension`、`add_radius_dimension` | 單行與多行文字、線性、對齊、半徑標註 |
| **批次繪圖** | `draw_batch` | 一次建立數百個物件(線、聚合線、弧、圓、文字、填實、標註、圖塊)。整批為一個 UNDO 群組;`dry_run` 預檢;`atomic` 失敗全部回滾 |
| **讀圖與檢核** | `inspect_entities` | 依範圍、類型、圖層或 Handle 讀取物件;`summary_only` 會統計「文字型式+字高」組合,可用來檢查字高是否符合規範 |
| | `export_view_png` | 把視圖或指定範圍輸出為 PNG,讓 AI 看圖核對結果 |
| | `get_drawing_info` | 圖面名稱、物件數、圖層、AutoCAD 是否閒置 |
| **圖塊與屬性** | `list_blocks`、`insert_block`、`set_block_attributes` | 列出圖塊與屬性標籤、插入圖塊並填屬性(支援鏡射 `x_scale=-1`)、修改屬性(圖框張號、號碼球) |
| 檢視與復原 | `zoom_extents`、`regen_all`、`undo_last` | ZOOM E、REGENALL、UNDO |
| 檔案 | `new_document` | 以樣板開新圖面 |
| 通用通道 | `execute_cad_command` | 執行任何 AutoCAD 指令或 AutoLISP。**PURGE、SAVE、QSAVE、SAVEAS、CLOSE、QUIT、EXIT、AUDIT、RECOVER** 會被攔截,須經使用者確認後加上 `confirm=True` 才會執行 |
## 系統需求
- Windows 10 或 11
- AutoCAD 完整版(需支援 COM/ActiveX;**AutoCAD LT 不支援**)
- Python 3.10 以上
- 支援 MCP 的客戶端,例如 Claude Desktop、Claude Code
## 安裝
```powershell
git clone https://github.com/topjgapadsup804-pixel/autocad-mcp.git
cd autocad-mcp
python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txt
```
## 設定 Claude Desktop
編輯 `%APPDATA%\Claude\claude_desktop_config.json`,加入(路徑請改成您實際的位置):
```json
{
"mcpServers": {
"autocad": {
"command": "C:\\path\\to\\autocad-mcp\\.venv\\Scripts\\python.exe",
"args": ["C:\\path\\to\\autocad-mcp\\server.py"]
}
}
}
```
存檔後完全關閉再重新開啟 Claude Desktop。使用前請先開啟 AutoCAD,並至少開一張圖面。
### 選用環境變數
| 變數 | 預設值 | 用途 |
|---|---|---|
| `AUTOCAD_MCP_VIEW_DIR` | `server.py` 旁的 `views\` | `export_view_png` 的輸出資料夾 |
| `AUTOCAD_MCP_DUMP_DIR` | `server.py` 旁的 `dumps\` | `inspect_entities(save_json=True)` 的輸出資料夾 |
## 使用範例
直接用中文跟 Claude 說:
- 「開新圖,建立『牆』『門窗』『文字』三個圖層,文字型式用微軟正黑體」
- 「畫一個 6000×4000 的房間,牆厚 150,中間標上『會議室』,字高 300」
- 「讀取 (0,0) 到 (20000,15000) 範圍內的所有文字,統計各字高的數量」
- 「把 (0,0)-(10000,8000) 輸出成圖片,幫我檢查標註有沒有重疊」
- 「插入圖框圖塊,張號填 A-101」
`draw_batch` 的 JSON 格式範例:
```json
[
{"t": "pline", "pts": [[0,0],[6000,0],[6000,4000],[0,4000]], "closed": true, "layer": "牆"},
{"t": "text", "s": "會議室", "p": [3000,2000], "h": 300, "ha": 1, "va": 2, "style": "微軟正黑體", "layer": "文字"},
{"t": "dim", "p1": [0,0], "p2": [6000,0], "at": [3000,-800], "rot": 0, "style": "ISO-25"},
{"t": "block", "name": "號碼球", "p": [7000,2000], "sx": -1, "attrs": {"NO": "1"}}
]
```
建議先用 `dry_run=true` 預檢,確認圖層、型式和圖塊都存在後再實際繪製。
## 運作原理
```
Claude ──MCP (stdio)──> server.py ──COM (pywin32)──> AutoCAD.Application
```
大部分工具直接呼叫 AutoCAD ActiveX API;`execute_cad_command` 和 `create_boundary` 則透過 `SendCommand`,並輪詢 `GetAcadState().IsQuiescent` 確認指令執行完畢。AutoCAD 忙碌而拒絕 COM 呼叫(Call was rejected by callee)時會自動重試。
## 注意事項
- 本工具會直接修改您開啟中的圖面。**請在副本上測試**,重要圖檔請先備份。
- `execute_cad_command` 能執行任意 AutoLISP,等同給 AI 完整的 AutoCAD 操作權限;只在您信任的 MCP 客戶端中使用。
- `inspect_entities` 和 `export_view_png` 指定範圍時會暫時縮放視圖,完成後自動還原。
- 本專案為第三方工具,與 Autodesk 無關。AutoCAD 為 Autodesk, Inc. 的註冊商標。
## 授權
[MIT License](LICENSE)
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues