Skip to content

Latest commit

Β 

History

History
406 lines (317 loc) Β· 11.8 KB

File metadata and controls

406 lines (317 loc) Β· 11.8 KB

moicad - Modern OpenSCAD CAD Engine

A high-performance, web-based CAD engine supporting both OpenSCAD and JavaScript/TypeScript. Built with modern technologies and powered by manifold-3d for guaranteed manifold geometry.

🎯 Dual-Language Support

Write CAD models in your preferred language:

OpenSCAD (Traditional)

difference() {
  cube([20, 20, 10]);
  translate([10, 10, 0])
    sphere(8, $fn=32);
}

JavaScript/TypeScript (NEW! ⚑ 10-20x faster)

import { Shape } from 'moicad';

const box = Shape.cube([20, 20, 10]);
const hole = Shape.sphere(8, { $fn: 32 }).translate([10, 10, 0]);

export default box.subtract(hole);

πŸ“š Complete JavaScript API Documentation: See JAVASCRIPT_API.md

πŸš€ Architecture

Modern Bun Monorepo (Restructured January 2026)

moicad/
β”œβ”€β”€ packages/
β”‚   β”œβ”€β”€ sdk/                    # @moicad/sdk - Core CAD engine (publishable npm package)
β”‚   β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”‚   β”œβ”€β”€ shape.ts        # Fluent API: Shape.cube(10)
β”‚   β”‚   β”‚   β”œβ”€β”€ functional.ts   # Functional API: cube(10)
β”‚   β”‚   β”‚   β”œβ”€β”€ scad/           # OpenSCAD parser & evaluator
β”‚   β”‚   β”‚   β”œβ”€β”€ manifold/       # Manifold-3d CSG integration
β”‚   β”‚   β”‚   β”œβ”€β”€ viewport/       # Three.js viewport component
β”‚   β”‚   β”‚   β”œβ”€β”€ animation/      # GIF/video export (NEW!)
β”‚   β”‚   β”‚   β”œβ”€β”€ interactive/    # Click/hover interactions (NEW!)
β”‚   β”‚   β”‚   └── plugins/        # Extensibility system
β”‚   β”‚   └── dist/               # Built npm package
β”‚   β”‚
β”‚   β”œβ”€β”€ landing/                # @moicad/landing - Next.js 16 web app
β”‚   β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”‚   β”œβ”€β”€ page.tsx        # Landing page
β”‚   β”‚   β”‚   β”œβ”€β”€ demo/           # Interactive CAD editor
β”‚   β”‚   β”‚   β”œβ”€β”€ docs/           # Auto-generated API docs
β”‚   β”‚   β”‚   └── api/            # API routes (evaluate, parse, export)
β”‚   β”‚   β”œβ”€β”€ components/demo/    # UI components (Editor, Viewport, TopMenu, etc.)
β”‚   β”‚   β”œβ”€β”€ hooks/              # React hooks (useAnimation, useInteraction)
β”‚   β”‚   β”œβ”€β”€ lib/                # Three.js utilities, API client
β”‚   β”‚   └── scripts/            # Build scripts (Bun module fix)
β”‚   β”‚
β”‚   β”œβ”€β”€ desktop/                # Tauri desktop app (optional)
β”‚   └── shared/                 # Minimal shared types
β”‚
β”œβ”€β”€ backend/                    # ⚠️ NEEDS MIGRATION - Bun server (currently broken)
β”‚   β”œβ”€β”€ core/                   # Server infrastructure
β”‚   β”œβ”€β”€ mcp/                    # MCP server & collaboration
β”‚   └── middleware/             # HTTP middleware
β”‚
└── examples/                   # Example code
    β”œβ”€β”€ javascript/             # JavaScript API examples
    └── openscad/               # OpenSCAD examples

Tech Stack:
- Runtime: Bun (TypeScript/JavaScript) + Node.js (Vercel)
- Languages: OpenSCAD + JavaScript/TypeScript ⚑
- CSG Engine: manifold-3d (WebAssembly npm package)
- SDK: Publishable npm package (@moicad/sdk v0.1.8)
- Backend: API routes in Next.js (backend/ needs SDK migration)
- Frontend: Next.js 16 + React 19 + Three.js
- Desktop: Tauri (optional)
- Deployment: Vercel (with npm workaround for Bun bug)

✨ Features

πŸ†• JavaScript/TypeScript API (NEW!)

Modern CAD programming with 10-20x better performance:

  • βœ… Full API: All OpenSCAD features + classes, async/await, npm packages
  • βœ… Type Safety: Complete TypeScript definitions with IntelliSense
  • βœ… Two Styles: Fluent (OOP) and Functional (FP) APIs
  • βœ… Performance: 1.6ms vs 32ms for simple cube (20x faster!)
  • βœ… Modern: ES6+, imports/exports, parametric classes
  • βœ… Documented: 400+ line API guide + 6 working examples

Quick Example:

import { Shape } from 'moicad';

class Bolt {
  constructor(length, diameter) {
    this.length = length;
    this.diameter = diameter;
  }

  build() {
    const shaft = Shape.cylinder(this.length, this.diameter / 2);
    const head = Shape.cylinder(this.diameter * 0.7, this.diameter * 0.9, { $fn: 6 })
      .translate([0, 0, this.length]);
    return shaft.union(head);
  }
}

export default new Bolt(20, 6).build();

πŸ“š Learn More:

OpenSCAD Compatibility (98-99%)

Primitives

  • βœ… cube, sphere, cylinder, cone
  • βœ… circle, square, polygon, polyhedron
  • βœ… text (ASCII characters, basic Latin)
  • βœ… surface (heightmap import)

Transformations

  • βœ… translate, rotate, scale, mirror
  • βœ… multmatrix (4x4 custom transforms)

CSG Operations

  • βœ… union, difference, intersection
  • βœ… hull (convex hull)
  • βœ… minkowski (Minkowski sum)

2D Operations

  • βœ… linear_extrude, rotate_extrude
  • βœ… offset (expand/contract polygons)
  • βœ… projection (3D β†’ 2D)

Language Features

  • βœ… Variables, functions, modules
  • βœ… Conditionals (if/else), loops (for)
  • βœ… Expressions, operators (arithmetic, logical, ternary)
  • βœ… List comprehensions
  • βœ… Built-in functions (math, array, string)
  • βœ… File imports (include, use)
  • βœ… Special variables ($fn, $fa, $fs, $t, $vpr, $vpt, $preview, etc.)
  • βœ… OpenSCAD modifiers (#, %, !, *)

Interactive Features

  • βœ… Real-time hover highlighting
  • βœ… Click selection, multi-select
  • βœ… Code-to-geometry mapping
  • βœ… Professional Blender-style UI

AI Integration (MCP Server)

Model Context Protocol (MCP)

  • Expose moicad as an MCP server for Claude Desktop
  • AI agents can evaluate OpenSCAD code
  • Real-time geometry generation from natural language
  • Integration with other AI tools

πŸ› οΈ Quick Start

Prerequisites

  • Bun v1.0+ (for development)
  • Node.js v18+ (for Vercel deployment)

Installation

# Clone repository
git clone https://github.com/yourusername/moicad.git
cd moicad

# Install dependencies (monorepo root)
bun install

Development

# Build SDK first (required)
cd packages/sdk
bun run build

# Start web app (http://localhost:3000)
cd ../landing
bun run dev

# Or build SDK + start landing in one command
cd packages/landing
bun run build  # prebuild hook builds SDK automatically

Build for Production

# Build SDK
cd packages/sdk
bun run build

# Build landing (Next.js)
cd ../landing
bun run build

# Start production server
bun run start

Deployment to Vercel

See VERCEL_DEPLOYMENT.md for detailed instructions.

Quick setup:

  1. Import repo to Vercel
  2. Set root directory to packages/landing
  3. Vercel auto-detects Next.js and uses npm (via vercel.json)
  4. Build succeeds automatically!

Note: A postinstall script automatically fixes Bun's module installation bug on both local and Vercel builds.

Testing

# Test SDK
cd packages/sdk
bun test

# Test landing
cd packages/landing
bun run test

# Full test suite (from root)
bun run test:all

πŸ“‘ API Endpoints

REST API

POST /api/parse Parse OpenSCAD code to AST.

Request: { "code": "cube(10);" }
Response: { "ast": [...], "errors": [], "success": true }

POST /api/evaluate Parse and evaluate to 3D geometry.

Request: { "code": "sphere(10);" }
Response: {
  "geometry": {
    "vertices": [...],
    "indices": [...],
    "normals": [...],
    "bounds": { "min": [...], "max": [...] },
    "stats": { "vertexCount": N, "faceCount": N, "volume": V }
  },
  "errors": [],
  "success": true,
  "executionTime": 45.2
}

POST /api/export Export geometry to STL or OBJ.

Request: { "geometry": {...}, "format": "stl" }
Response: Binary STL file

WebSocket

WS /ws Real-time code evaluation.

Client β†’ Server: { "type": "evaluate", "code": "cube(10);", "requestId": "abc123" }
Server β†’ Client: { "type": "evaluate_response", "requestId": "abc123", "geometry": {...} }

MCP Server

WS /ws/mcp Model Context Protocol for AI integration.

Claude Desktop can connect to moicad as an MCP server to evaluate OpenSCAD code and generate geometry.

πŸ”§ Configuration

MCP Server Setup (Claude Desktop)

Add to your Claude Desktop MCP configuration (~/Library/Application Support/Claude/claude_desktop_config.json):

{
  "mcpServers": {
    "moicad": {
      "command": "bun",
      "args": ["run", "/path/to/moicad/backend/index.ts"],
      "env": {
        "MCP_ENABLED": "true"
      }
    }
  }
}

Now Claude can generate 3D models by writing OpenSCAD code!

Tauri Desktop App (Optional)

# Start Tauri development mode
bun run tauri:dev

# Build desktop executable
bun run tauri:build

πŸ“š Documentation

Core Documentation

API & Features

Architecture & Migration

🎯 Key Design Decisions

Why monorepo with packages/?

  • SDK is publishable to npm independently (@moicad/sdk)
  • Landing app uses SDK as dependency
  • Clear separation: engine vs. UI
  • Desktop app can also use SDK
  • Easier to maintain and test

Why manifold-3d?

  • Guaranteed manifold output (no topology errors)
  • Robust Boolean operations (replaces custom BSP tree)
  • High performance with parallel processing
  • Clean geometry eliminates rendering artifacts
  • Available as npm WebAssembly package

Why Bun + npm hybrid?

  • Bun for development (fast, modern)
  • npm for Vercel deployment (reliable)
  • Bun v1.3.7 has workspace bug (missing package.json in symlinks)
  • Automatic postinstall script fixes Bun's installation
  • Best of both worlds

Why Three.js (not custom WebGL)?

  • Manifold-3d provides clean geometry
  • No BSP artifacts = no custom renderer needed
  • Standard Three.js works perfectly
  • Better ecosystem and community support

Why Next.js API routes (not separate backend)?

  • Simpler deployment (single Vercel project)
  • API routes handle evaluate/parse/export
  • Can add WebSocket via custom server later
  • Backend server currently broken, needs SDK migration

πŸ§ͺ Testing

Comprehensive test suite with 98-99% OpenSCAD compatibility:

  • Unit tests: Primitives, transformations, CSG operations, language features
  • Integration tests: API endpoints, WebSocket, file imports
  • Performance benchmarks: Rendering speed, memory usage
  • Validation tests: OpenSCAD compatibility verification

🀝 Contributing

Contributions welcome! Please read COLLABORATION_GUIDE.md for details.

πŸ“„ License

MIT License - see LICENSE for details.

πŸ™ Acknowledgments


Built with ❀️ using modern JavaScript technologies