ai-cad
by putaojuju
README.md
# AI-CAD
AI-CAD is a cleaned-up rebuild of an earlier CAD automation prototype. The first stable core focuses on offline DXF parsing, entity extraction, geometry normalization, and stone blank size analysis.
## What Is Included
- DXF parsing with `ezdxf`
- Unified entity dictionaries for lines, arcs, circles, polylines, text, and inserts
- Layer/type filtering and drawing summaries
- Minimum oriented bounding rectangle analysis
- Multi-part stone grouping by endpoint connectivity
- JSON, CSV, and optional Excel export
- Automation-loop contracts for future computer-use and AutoLISP adapters
- AutoCAD current-viewport assistant contracts for MCP/AutoLISP integration
- A small CLI and focused pytest coverage
- An MCP server (`aicad.mcp_server`, built on FastMCP) exposing the DXF tools and, on Windows, timeout- and `IsQuiescent`-guarded AutoCAD COM tools plus a registry-driven catalog for driving external AutoLISP scripts
Heavy visual rendering and experimental tooling are intentionally left outside this rebuilt core. AutoCAD COM execution is now available as an **optional, adapter-style** layer through the MCP server (Windows + `pywin32`), kept behind automation contracts so the geometry/DXF core stays backend-independent. The current-viewport assistant direction is documented in [docs/VIEWPORT_ASSISTANT.md](docs/VIEWPORT_ASSISTANT.md), the general edit/validation loop in [docs/AUTOMATION_LOOP.md](docs/AUTOMATION_LOOP.md), and migration notes in [docs/MIGRATION.md](docs/MIGRATION.md).
## Install
```bash
python -m pip install -e .
```
For Excel export:
```bash
python -m pip install -e ".[excel]"
```
For development:
```bash
python -m pip install -e ".[dev,excel]"
```
## CLI
```bash
aicad summary path/to/file.dxf
aicad extract path/to/file.dxf --layer "stone-*"
aicad blank path/to/file.dxf --margin 2
aicad export path/to/file.dxf --format json --output output/entities.json
```
You can also run the package module directly:
```bash
python -m aicad summary path/to/file.dxf
```
## Python API
```python
from aicad import calculate_blank_size, extract_entities
entities = extract_entities("drawing.dxf", layer_filter="stone-*")
blank = calculate_blank_size("drawing.dxf", margin=2.0)
```
Every public API function returns a response dictionary:
```python
{
"success": True,
"data": ...,
"error": None,
"metadata": {...},
}
```
## MCP Server
`aicad` ships an MCP server (FastMCP) so agents can call the toolkit directly.
Install the extra and run it:
```bash
python -m pip install -e ".[mcp]"
python -m aicad.mcp_server
```
Register it with an MCP client (e.g. a project `.mcp.json`):
```json
{
"mcpServers": {
"ai-cad": { "command": "python", "args": ["-m", "aicad.mcp_server"] }
}
}
```
Tools:
- **DXF (cross-platform):** `aicad_extract_entities`, `aicad_blank_size`, `aicad_validate_dxf`, `aicad_build_viewport_probe`, `aicad_ping`
- **AutoCAD COM (Windows, optional):** `autocad_get_active_document`, `autocad_run_lisp`, `autocad_send_command`, `autocad_probe_viewport`, `autocad_diagnose_access` — every COM call runs on a worker thread with a timeout and an `IsQuiescent` guard, so a busy AutoCAD returns a clean error instead of hanging the server.
- **Script catalog:** `aicad_list_scripts`, `aicad_describe_script`, `autocad_run_script` — a registry-driven layer for discovering and running your own AutoLISP commands through thin headless wrappers. The registry is read at call time, so adding a script needs no server restart.
> **Windows integrity note:** the COM tools require AutoCAD and the MCP process to run at the **same integrity level** (both normal, or both "as administrator"). A mismatch (e.g. AutoCAD elevated, MCP not) blocks COM/ROT access and surfaces as an "invalid class string" error — `autocad_diagnose_access` reports this directly.
## Repository Layout
```text
src/aicad/ Core Python package
tests/ Reproducible tests and generated fixtures
docs/ Migration notes and project context
.codex/ Agent-facing context notes
```
## Automation Direction
The intended editing loop has two modes:
- visible computer-use validation for screenshots, UI inspection, and interactive diagnosis
- quiet AutoLISP/headless automation for deterministic background edits and validation
Both modes should report through `aicad.automation` contracts so the geometry and DXF core stays independent from the CAD execution backend.
The next practical focus is the AutoCAD current-viewport assistant: Codex acts as the reasoning brain, while an MCP/AutoLISP backend reads the active view, selected entities, or a requested world-coordinate window and returns formatted JSON.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessSyncing