Skip to content

moikas-code/moicad

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

176 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Releases

No releases published

Packages

 
 
 

Contributors