给AI用的CAD操作软件使用说明


软件位置: 在当前文件夹找到 .\给AI用的CAD接口.exe


给 AI 用的 CAD 操作软件使用说明

一、工具概述

本软件是一个 AutoCAD / CASS HTTP API 桥接服务,通过 aardio 的 com.cad COM 接口连接 AutoCAD,对外暴露 RESTful HTTP API,让任何语言(Python、PowerShell、curl 等)或 AI Agent 都能通过 HTTP JSON 请求操作 AutoCAD。

核心架构:

┌──────────────────────────────────┐
│  Python / PowerShell / curl / AI │
│         HTTP JSON 请求           │
│              ▼                   │
│  ┌────────────────────────────┐  │
│  │  CAD Bridge (aardio)       │  │
│  │  127.0.0.1:18900           │  │
│  │       com.cad() COM        │  │
│  │           ▼                │  │
│  │  ┌──────────────────────┐  │  │
│  │  │  AutoCAD + CASS      │  │  │
│  │  └──────────────────────┘  │  │
│  └────────────────────────────┘  │
└──────────────────────────────────┘

二、核心固定信息

  1. 软件路径.\给AI用的CAD接口.exe
  2. 服务地址http://127.0.0.1:18900(仅本地访问)
  3. 前置条件必须先启动 AutoCAD(已测试通过 AutoCAD 2022 + CASS11)
  4. 通信方式:HTTP RESTful,JSON 格式
  5. 启动方式:可以用户自己双击 exe 即可,控制台窗口显示 🌐 API: http://127.0.0.1:18900 表示启动成功,也可以用Start-Process ".\给AI用的CAD接口.exe" 让AI自己启动

三、⚠️ 最重要的注意事项

CAD 忙碌时操作会失败!

AutoCAD 在执行命令期间(isQuiescent = false),COM 调用会返回 500 错误。

正确做法:每次操作前先检查状态,CAD 空闲再调用。

# 检查 CAD 状态
$status = Invoke-RestMethod "http://127.0.0.1:18900/api/status"
if ($status.isQuiescent -eq $true) {
    # 安全,可以操作
} else {
    # 等待...
    Start-Sleep -Seconds 2
}

封装建议: 写一个带重试的调用函数:

function Invoke-CadApi {
    param([string]$Path, [string]$Method = "Get", [object]$Body = $null)
    $base = "http://127.0.0.1:18900"
    $uri = "$base$Path"
    
    # 等待 CAD 空闲(最多 30 秒)
    for ($i = 0; $i -lt 15; $i++) {
        $s = Invoke-RestMethod "$base/api/status" -TimeoutSec 3
        if ($s.isQuiescent) { break }
        Start-Sleep -Seconds 2
    }
    
    if ($Method -eq "Get") {
        return Invoke-RestMethod $uri -Method Get -TimeoutSec 10
    } else {
        return Invoke-RestMethod $uri -Method Post -ContentType "application/json" -Body ($Body | ConvertTo-Json -Compress) -TimeoutSec 10
    }
}

四、API 完整列表

4.1 状态查询

端点 方法 说明
/api/status GET 检查 CAD 连接状态

返回示例:

{
  "connected": true,
  "version": "24.1s (LMS Tech)",
  "hwnd": 5443382,
  "isQuiescent": true,
  "docCount": 1,
  "docName": "Drawing1.dwg"
}

4.2 绘图操作

端点 方法 必填参数 可选参数
/api/draw/circle POST x, y, r z, color, layer
/api/draw/line POST x1, y1, x2, y2 z1, z2, color, layer
/api/draw/text POST text, x, y z, height(默认30), color, layer, rotation
/api/draw/arc POST x, y, r z, startAngle(默认0), endAngle(默认π), color, layer
/api/draw/rectangle POST x1, y1, x2, y2 color, layer
/api/draw/polyline POST points(二维数组) closed(bool), color, layer

参数说明:

  • 坐标为 CAD 世界坐标系
  • color:ACI 颜色号(1=红, 2=黄, 3=绿, 4=青, 5=蓝, 6=洋红, 7=白/黑)
  • layer:图层名称字符串
  • points 格式:[[x1,y1], [x2,y2], [x3,y3]],至少 2 个点
  • startAngle / endAngle:弧度制

4.3 命令执行

端点 方法 必填参数 说明
/api/command POST cmd SendCommand 同步执行 CAD 命令
/api/lisp POST code 执行 LISP 表达式

4.4 查询操作

端点 方法 参数 说明
/api/entities GET filter(可选) 列出实体,filter 按类型过滤
/api/layers GET - 列出所有图层
/api/layers POST name, color(可选) 创建新图层
/api/variables GET names(逗号分隔) 读取系统变量
/api/variables POST {key: value} 设置系统变量

4.5 文件操作

端点 方法 必填参数 说明
/api/save POST - 保存当前文档
/api/saveas POST path 另存为指定路径
/api/open POST path 打开 DWG 文件

4.6 CASS 专用

端点 方法 必填参数 说明
/api/cass/command POST cmd 调用 CASS 命令(如 dd, ver)

五、调用示例

PowerShell

$base = "http://127.0.0.1:18900"

# 1. 检查状态
$status = Invoke-RestMethod "$base/api/status"

# 2. 画圆(红色,半径 80)
Invoke-RestMethod "$base/api/draw/circle" -Method Post `
    -ContentType "application/json" `
    -Body '{"x":200,"y":200,"r":80,"color":1}'

# 3. 画线(黄色)
Invoke-RestMethod "$base/api/draw/line" -Method Post `
    -ContentType "application/json" `
    -Body '{"x1":0,"y1":0,"x2":300,"y2":300,"color":2}'

# 4. 写文字(青色,字高 30)
Invoke-RestMethod "$base/api/draw/text" -Method Post `
    -ContentType "application/json" `
    -Body '{"text":"Hello CAD","x":100,"y":100,"height":30,"color":4}'

# 5. 画矩形(蓝色)
Invoke-RestMethod "$base/api/draw/rectangle" -Method Post `
    -ContentType "application/json" `
    -Body '{"x1":400,"y1":200,"x2":600,"y2":350,"color":5}'

# 6. 画多段线(闭合五边形,洋红色)
Invoke-RestMethod "$base/api/draw/polyline" -Method Post `
    -ContentType "application/json" `
    -Body '{"points":[[500,500],[600,450],[700,500],[650,600],[550,600]],"closed":true,"color":6}'

# 7. 画圆弧(绿色,半圆)
Invoke-RestMethod "$base/api/draw/arc" -Method Post `
    -ContentType "application/json" `
    -Body '{"x":800,"y":300,"r":50,"startAngle":0,"endAngle":3.14159,"color":3}'

# 8. 创建图层
Invoke-RestMethod "$base/api/layers" -Method Post `
    -ContentType "application/json" `
    -Body '{"name":"MyLayer","color":5}'

# 9. 查看实体列表
$ents = Invoke-RestMethod "$base/api/entities"
Write-Output "实体总数: $($ents.total)"

# 10. 查看图层
$layers = Invoke-RestMethod "$base/api/layers"
Write-Output "图层数: $($layers.count)"

# 11. 读取系统变量
$vars = Invoke-RestMethod "$base/api/variables?names=CLAYER,CECOLOR,TEXTSIZE"
Write-Output "当前图层: $($vars.variables.CLAYER)"

# 12. 执行 CAD 命令(全图缩放)
Invoke-RestMethod "$base/api/command" -Method Post `
    -ContentType "application/json" `
    -Body '{"cmd":"_.ZOOM E "}'

# 13. CASS 命令
Invoke-RestMethod "$base/api/cass/command" -Method Post `
    -ContentType "application/json" `
    -Body '{"cmd":"dd"}'

# 14. 保存文件
Invoke-RestMethod "$base/api/save" -Method Post

# 15. 另存为
Invoke-RestMethod "$base/api/saveas" -Method Post `
    -ContentType "application/json" `
    -Body '{"path":"C:/Users/Public/output.dwg"}'

Python

import requests
BASE = "http://127.0.0.1:18900"

# 状态检查
r = requests.get(f"{BASE}/api/status")
print(r.json())

# 画圆
requests.post(f"{BASE}/api/draw/circle", json={"x": 200, "y": 200, "r": 80, "color": 1})

# 画线
requests.post(f"{BASE}/api/draw/line", json={"x1": 0, "y1": 0, "x2": 300, "y2": 300, "color": 2})

# 写文字
requests.post(f"{BASE}/api/draw/text", json={"text": "Hello", "x": 100, "y": 100, "height": 30})

# 画多段线(闭合)
requests.post(f"{BASE}/api/draw/polyline", json={
    "points": [[500,500],[600,450],[700,500],[650,600],[550,600]],
    "closed": True, "color": 6
})

# 执行 CAD 命令
requests.post(f"{BASE}/api/command", json={"cmd": "_.ZOOM E "})

# CASS 命令
requests.post(f"{BASE}/api/cass/command", json={"cmd": "dd"})

curl

# 状态
curl http://127.0.0.1:18900/api/status

# 画圆
curl -X POST http://127.0.0.1:18900/api/draw/circle \
  -H "Content-Type: application/json" \
  -d '{"x":200,"y":200,"r":80,"color":1}'

# 执行命令
curl -X POST http://127.0.0.1:18900/api/command \
  -H "Content-Type: application/json" \
  -d '{"cmd":"_.ZOOM E "}'

六、颜色对照表

ACI 颜色号 颜色
0 BYBLOCK
1 红色
2 黄色
3 绿色
4 青色
5 蓝色
6 洋红色
7 白色/黑色(取决于背景)
256 BYLAYER

七、已知限制

问题 说明 规避方法
CAD 忙碌时操作失败 isQuiescent=false 时 COM 返回 500 操作前检查状态,等空闲再调用
LISP eval 返回原始代码 求值结果未正确返回 暂用 /api/command 执行 LISP 命令
部分系统变量读取失败 ACADVER 等返回 N/A 仅读取可靠变量(CLAYER, CECOLOR, TEXTSIZE)
仅本地访问 监听 127.0.0.1 安全设计,如需远程需修改程序配置
多段线用命令行方式 COM 的 AddLightWeightPolyline 有兼容问题 通过 SendCommand("_.PLINE ...") 实现

八、远程启动 AutoCAD(AI 可自动完成)

即使 AutoCAD 没有打开,AI 也可以通过命令自动启动,无需人工干预。

AutoCAD 安装路径(从注册表自动获取),以下面的软件为例: cad软件:D:\Program Files\Autodesk\AutoCAD 2022\acad.exe 给AI用的CAD接口:E:\trae\aardio\aaidiojihe\给AI用的CAD接口\dist\给AI用的CAD接口.exe

一键启动脚本:

# 1. 启动 AutoCAD
Start-Process "D:\Program Files\Autodesk\AutoCAD 2022\acad.exe"

# 2. 等待 CAD 完全加载(约 15-20 秒,COM 接口才就绪)
Start-Sleep -Seconds 20

# 3. 启动 CAD Bridge
Start-Process "E:\trae\aardio\aaidiojihe\给AI用的CAD接口\dist\给AI用的CAD接口.exe"
Start-Sleep -Seconds 5

# 4. 验证连接
$r = Invoke-RestMethod "http://127.0.0.1:18900/api/status" -TimeoutSec 5
if ($r.connected) { Write-Output "CAD Bridge 就绪!" }

⚠️ 注意: CAD 启动后需要等待 15-20 秒,COM 接口才会就绪,太早调用会连接失败。

九、完整调用流程(推荐)

1. 启动 AutoCAD(手动双击 或 AI 远程 Start-Process)
2. 等待 15-20 秒让 COM 接口就绪
3. 启动 CAD Bridge(双击 exe 或 Start-Process)
4. 调用 /api/status 确认 connected=true
5. 每次操作前检查 isQuiescent=true
6. 执行绘图/查询/文件操作
7. 操作完成后 /api/save 保存

十、常见问题

Q: 启动后提示连接失败?
A: 确保先启动 AutoCAD,再启动本软件。

Q: 画图时返回 500 错误?
A: CAD 正在忙,等几秒后重试。参考第三节的状态检查方法。

Q: 如何关闭服务?
A: 关闭控制台窗口即可。

Q: 支持哪些 CAD 版本?
A: 理论上支持所有可通过 COM 自动化接口访问的 AutoCAD 版本,已验证 AutoCAD 2022。


文章作者: 摸鱼
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 摸鱼 !
  目录