cadbridge
# cadbridge
给 LLM harness(DeepSeek 等)用的 CAD 操作插件:**一句话 → SolidWorks 建模并导出**,**一句话 → Proteus 放件、连网、跑仿真**。
不是"再包一层 GUI 截图 + 点坐标"的套壳。两个硬性指标(效率/准确、省 token)是设计约束,不是宣传语:
| 指标 | 落地手段 | 实测(不是估算) |
|---|---|---|
| Token 省 | 1 次调用 = 1 个完整模型;返回体只有短键 JSON;硬截断 900 字符 | 单次建模往返 **541 字符 ≈ 216 tokens**(请求 345 + 响应 196);工具目录 3447 字符,仅首轮加载 |
| 效率 | 特征计划离线生成,无逐步对话;Proteus 用网标连接替代画线 | 5 步特征 1 次调用;Proteus N 个元件 + P 个引脚 → N+P+1 步,不缩放不画线 |
| 准确 | 每次建模后校验体积/包围盒;每步 GUI 操作后断言;失败带步号 | 30 条断言用例覆盖失败路径 |
| 可运维 | 错误码 + 一句修复建议;不把异常原文 200 行甩给模型 | 全链路错误码 E1xx/E2xx |
## 为什么这两个软件要分别对待
- **SolidWorks 有 COM API**,走 `pywin32` 直连,稳定且快。风险在于各版本 `FeatureExtrusion3` / `FeatureCut4` 等方法的参数个数会漂移,所以适配层内置**签名变体表 + 自动探测缓存**(`~/.cadbridge/sw-sig.json`):某个变体报 COM 错误就换下一个,成功的那个记住,下次直接用。
- **Proteus 没有公开自动化 API**(官方只有 GUI)。任何声称"Proteus API 直连"的方案都是假的。本插件用的是两条真正可靠的 GUI 自动化原则:
1. **按角色寻址,不按坐标**:一次性探针把 `ok_btn` / `lib_search` 这类语义角色映射到控件 ID 并缓存到 `~/.cadbridge/proteus-map.json`。对话框改布局不会导致误点,只会 probe 时报 missing。
2. **网标连接,不画线**:Proteus 的连通性由网络名决定,所以同名 terminal 即同网。省掉缩放/平移/对准引脚这一整套最贵的操作,也消除了线头虚接。
3. 每步之后断言(控件仍在 / 窗标题匹配),失败立即停在确定步号。
## 安装(Windows 主机,SolidWorks / Proteus 所在机器)
```powershell
git clone https://github.com/yu-dev-love/cadbridge.git
cd cadbridge
pip install -e ".[win]"
```
无需 `pip` 之外的运行时依赖:MCP 服务端是纯标准库的 stdio JSON-RPC,不依赖 `mcp` 包,避免版本地狱。
## 接入 harness:两条路,选一条
### A. MCP(推荐,DeepSeek/Claude/Cursor 等支持 MCP 的客户端通用)
`harness/deepseek_mcp.json` 直接可用:
```json
{
"mcpServers": {
"cadbridge": {
"command": "python",
"args": ["-m", "cadbridge.mcp_server"],
"env": { "CADBRIDGE_BUDGET": "900" }
}
}
}
```
### B. OpenAI 兼容 function calling(无 MCP 的 harness)
`harness/openai_tools.json` 是 9 个工具的 schema,直接塞进 `tools=[...]`。执行侧把 `function.name` / `arguments` 转成本地调用:
```python
from cadbridge.mcp_server import call_tool
result = call_tool(name, json.loads(arguments)) # 同一个实现,无第二套代码
```
### C. 给模型的技能说明
`skills/cadbridge/SKILL.md` 是可选的提示词资产(约 2.1k 字符),告诉模型:先 `cad_env` 探一次、一次一个完整 job、失败按 `fix` 修一轮不要超过两次、Proteus 优先 netlist 而不是逐步 recipe。把它放进 harness 的 system prompt 或 skill 目录即可。
## 用法
### SolidWorks:一个 job 建完一个零件
```json
{
"name": "flange",
"units": "mm",
"plane": "front",
"ops": [
{"t": "rect", "w": 60, "h": 40},
{"t": "extrude", "d": 8},
{"t": "hole", "x": 10, "y": 10, "dia": 6, "nx": 2, "ny": 2, "dx": 40, "dy": 20},
{"t": "fillet", "r": 2, "sel": {"dir": "z", "len_min": 30}}
],
"export": ["step", "stl"],
"out": "D:/cad/out",
"bbox": [60, 40, 8]
}
```
支持的 op:`rect` `circle` `slot` `poly` `line`(草图)/ `extrude` `cut` `hole` `fillet` `chamfer` `pattern`(实体)。
`hole` 用 `nx/ny + dx/dy` 直接阵列,不必写 4 个圆。`sel` 按几何筛边(`dir` 轴向、`len_min/len_max` 长度、`r` 圆角半径),不靠"第 3 条边"这种脆弱索引。
`bbox` 是可选的验收断言,超差直接 E107 而不是交给你一个错模型。
返回(196 字符,无废话):
```json
{"ok":1,"id":"1987e6507b","steps":5,"v":19080.0,"m":149.778,"bb":[60.0,40.0,8.0],
"files":{"step":"D:/cad/out/flange.step","stl":"D:/cad/out/flange.stl"},"ms":842}
```
其他工具:`cad_sw_export`(导出已有模型)、`cad_sw_query`(体积/质量/包围盒/特征数)、`cad_sw_selftest`。
### Proteus:网表优先
```json
{
"comp": [
{"id": "R1", "lib": "RES", "x": 50, "y": 40},
{"id": "D1", "lib": "LED", "x": 60, "y": 40}
],
"net": [
{"n": "A", "pins": ["R1:1", "D1:A"]}
]
}
```
`proteus_netlist` 会:放件 → 按环状槽位算终端的格点坐标(不重叠、对齐 2.54 栅格)→ 打网标。
其余情况用 `proteus_recipe` 写声明式步骤,`do` 取 `focus` `keys` `click` `set` `assert` `wait` `place` `label`:
```json
[{"do":"focus"},
{"do":"keys","seq":"^N"},
{"do":"assert","re":"proteus"},
{"do":"set","k":"lib_search","text":"LM358"},
{"do":"click","k":"ok_btn"}]
```
`k` 是语义角色,`id` 是原始控件 ID(已知时更省一步)。
## 验证(三步,约 1 分钟)
```powershell
python tests/test_all.py # 30 条断言,Linux/Windows 都能跑,不需要装 CAD
python tools/mcp_smoke.py # 真实 stdio 框架握手 + 建一个零件,打印 token 账单
```
然后在装有 SolidWorks/Proteus 的机器上各跑一次真机自检:
```
cad_sw_selftest # 60x40x8 板 + 4 孔 + 圆角 + 导出 STEP,校验体积与包围盒
proteus_probe # 建立/刷新控件角色表
```
`cad_sw_selftest` 返回 `ok=1` 且 `bb=[60,40,8]`,说明这台机器的 COM 签名表已自动对齐,后续建模不会再猜。若返回 E104 且 `d` 里带步号,把该步号和 SW 版本提给维护者,签名变体表补一条即可。
## 错误码
| 码 | 含义 | 处置 |
|---|---|---|
| E101 | job 结构不合法 | 按 `fix` 改字段,别重试原样参数 |
| E102 | SolidWorks 不可达 | 启动 SW / 装 pywin32 |
| E103 | 模板或文件不存在 | 查路径 |
| E104 | 特征构建失败 | 看 `d` 步号;跑 selftest 对齐签名 |
| E105 | 选边为空 | 放宽 `sel`(`dir:"any"`) |
| E106 | 导出失败 | 目录可写、文件没被占用 |
| E107 | 校验不通过 | 尺寸与 `bbox` 不符 |
| E201 | Proteus 不可达 | 启动 Proteus |
| E202 | 角色表缺失/过期 | `proteus_probe force=true` |
| E203 | 步骤执行失败 | `d` 里有步号 |
| E204 | 断言失败 | 界面没到位,重 probe |
## 边界(先说清楚,免得你踩)
- **装配体**:仅支持"新建 + 导出",mate 约束尚未覆盖。零件级自动化是完整的。
- **Proteus 版本差异**:控件命名随版本变,换版本后跑一次 `proteus_probe force=true` 即可,不要手改缓存。
- **Proteus 库关键字**:`lib` 字段是库里可检索的关键字(如 `ATMEGA328P`、`LM358`),不是封装名;搜不到会 E203 停在该步。
- **真机验收**:CI 跑的是几何/流程层面的断言,真实 COM/UIA 行为必须在你的机器上过一次 `selftest`——这是唯一诚实的验证方式,本仓库不假装跨版本免验证。
- **执行位置**:全部在本机进程内完成,不联网、不回传模型几何。
## 目录
```
cadbridge/
core.py 错误码、紧凑信封、硬预算截断
dsl.py job 校验 → 确定性特征计划(纯函数)
solidworks.py 执行器 + 几何选边 + 校验(可注入 API,故可离线测试)
proteus.py 角色表、recipe 引擎、网表→网标布局
backend_win.py 真实 COM / UIA 后端(惰性导入)
fake.py 确定性假后端(几何真实,CI 与首次试跑用)
mcp_server.py stdio MCP,9 个工具,零第三方依赖
tests/ 30 条断言
tools/ 真实 stdio 冒烟 + token 计量
skills/ 给模型的精简技能说明
harness/ MCP 与 function-calling 两种接法
```
MIT License.
TDQS
Scored across 9 tools
Most tools target clearly distinct operations across SolidWorks and Proteus, and descriptions distinguish build, query, export, diagnostics, discovery, recipes, netlisting, simulation, and environment checks. Minor overlap exists between cad_sw_job and cad_sw_selftest, both of which build and export a model, and between the generic proteus_recipe and specialized proteus_netlist, but the descriptions make the intended boundaries reasonably clear.
Names consistently use snake_case with domain prefixes (cad_sw_, proteus_, cad_env), making grouping predictable. The suffixes are mostly noun-like (job, query, selftest, recipe, netlist) rather than a strict verb_noun pattern, but the convention is still coherent and readable.
Nine tools is well-scoped for a dual-domain CAD/EDA automation bridge, with roughly balanced coverage of SolidWorks and Proteus operations. No tool appears to be filler, and the surface is not so large that selection becomes unwieldy.
The surface covers the core lifecycle: build/query/export/diagnose SolidWorks models, discover/control Proteus, run recipes, place nets, simulate, and report environment status. Minor gaps exist around explicit model modification, document management, or cleanup operations, but the generic job/recipe mechanisms provide workarounds for most workflows.