Skip to content

Architecture

Monorepo

modulecad/
├── packages/
│ ├── web/ Web app (Vite) — integrates everything
│ ├── desktop/ Electron app (parked)
│ ├── cad-engine/ 3D engine: stores, viewport, snap, mates, parametric, quote, cut plan
│ ├── ui/ React component library + design tokens
│ ├── storage-api/ Persistence interface (parked)
│ └── math/ Vectors, Euler XYZ rotation, units, environment bounds
├── services/api/ FastAPI back end + Postgres
├── design-system/ Visual mirror of the ui package (preview cards)
├── docs/ ADRs, PRDs, runbooks, research
├── infra/ Infrastructure (k3s cluster on Hetzner)
└── documentacao/ This documentation

Dependency graph

web / desktop
├── cad-engine ──┐
├── ui ──────────┼──► math
└── storage-api │
└ (cad-engine and ui don't depend on each other)
  • The library packages (cad-engine, ui, storage-api, math) compile with tsc only, to dist/ (ESM).
  • The apps (web, desktop) use Vite.
  • ui doesn’t import stores: all behaviour comes in through callback props; what connects stores and components is web (mainly packages/web/src/App.tsx).

Packages

@modulecad/cad-engine

Folder Responsibility
store/ Zustand stores (scene, tools, snap, viewport, mates, sketch, parametric, project, environment, shortcuts, units…)
viewport/ R3F components: canvas, parts, gizmos, environment, props, styles, capture
snap/ Face-to-face snap engine
mate/ Mate solver
parametric/ Expression evaluator, variable graph, repeat maths
sketch/ 2D sketch, 2D snap, extrusion
project/ Module instances, bounding box, unbind, environment snap
environment/ Openings, props, environment templates
quote/ Quote engine (computeQuote)
cutplan/ Cut-plan engine (guillotine and MaxRects), run in a Web Worker
mesh/ OBJ import: parser, simplification, encoding, limits
io/ File format, validation, migration, versions (jsondiffpatch), DXF
interaction/ Drag maths, gestures, per-frame throttling

@modulecad/ui

A pure React component library. Radix-based primitives (Button, Dialog, Select, Tooltip, ContextMenu…), layouts (AppLayout, HeaderBar, ToolSidebar, StatusBar), panels (PropertyPanel, SceneTreePanel, MatesPanel, ParametricPanel, EnvironmentPanel…), dialogs (SettingsModal, ModuleFormBuilderModal, ImportPreviewDialog…) and the AI chat. Visual values come exclusively from tokens in src/tokens/tokens.css, with light and dark themes.

@modulecad/math

A dependency-free leaf: Vec3, the Euler XYZ rotation convention (rotateByEuler, inverseRotateByEuler, rotationMatrix), unit conversion and formatting, and the environment’s dimension bounds. All rotation maths lives here.

@modulecad/web

The app: routes (main.tsx), authentication, the Home dashboard, the editor (App.tsx), the quote/cut list/cut plan/labels pages, the API’s HTTP clients, file import, the AI chat orchestration and the app’s translations.

@modulecad/desktop and @modulecad/storage-api (parked)

The Electron app is a minimal shell, kept compiling only — it doesn’t keep up with the web app. storage-api defines a StorageAdapter interface with a single in-memory implementation; real persistence goes through the HTTP API. Both were deliberately parked (ADR-0002).

services/api

A layered FastAPI back end: routers/ (HTTP) → usecases/ (orchestration, authorisation) → services/ (data access) → db/models.py (SQLAlchemy). Module, project and quote documents are stored as opaque JSONB: the server validates their shape, but never recomputes geometry. All authorisation goes through a single point (services/permissions.py).

Web app routes

Route Page Access
/login Sign in Public
/register Sign up Public
/forgot-password Request a password reset Public
/reset-password?token=… Set a new password Public
/verify-email?token=… Email verification Public
/ Home dashboard Signed in
/company Company settings Signed in
/modules/:id Module editor Signed in
/projects/:id Project editor Signed in
/projects/:projectId/quotes/:quoteId Quote Signed in
/projects/:projectId/quotes/:quoteId/cutlist Cut list Signed in
/projects/:projectId/quotes/:quoteId/cutplan Cut plan (?piece=N) Signed in
/projects/:projectId/quotes/:quoteId/labels Labels Signed in

/projects and /modules redirect to / with the matching tab; any other route goes to /.

Architecture patterns

  • Flat scene: Map<string, SceneNode> with parentId references; O(1) lookup by id.
  • One Zustand store per domain — never a “mega-store”.
  • Command pattern: every undoable mutation creates a Command with execute() and undo().
  • Live / commit: during a drag, set*Live() updates without history; on release, commit*() pushes exactly one Command.
  • Removal cascade: scene/removalCascade.ts is the only place that declares what is removed along with a part (mates, sketches, bindings, repeats).
  • Pure engines: snap, mates, quote, cut plan, DXF and meshes are pure functions, with no React or stores — testable in Node.
  • Client computes, server validates: quotes and cut plans are computed in the browser; the server validates shape and totals and persists them.
  • Performance: pointer events limited to one per frame; each part subscribes only to its own node; nothing is allocated inside useFrame.

Recorded decisions (ADRs)

ADR Decision
0001 The back end’s use-case layer stays thin.
0002 storage-api stays a hypothetical interface while the desktop app is parked.
0003 The world unit is the millimetre; the display unit is a presentation preference.
0004 Library permissions are “envelope” only; sharing between companies is deferred.
0005 A separate control plane and shared workers on the infrastructure cluster.

The full texts are in docs/adr/.