Skip to content

Add OpenSCAD to JS with ManifoldCAD - #119

Open
codex826 wants to merge 3 commits into
opencax:masterfrom
codex826:master
Open

Add OpenSCAD to JS with ManifoldCAD#119
codex826 wants to merge 3 commits into
opencax:masterfrom
codex826:master

Conversation

@codex826

@codex826 codex826 commented Mar 24, 2026

Copy link
Copy Markdown

Implementation of OpenSCAD-to-Manifold Compiler

Issue no: #117

Description

This PR introduces the first functional version of the OpenSCAD-to-Manifold Compiler, a tool designed to translate OpenSCAD source code (.scad) into executable JavaScript optimized for the Manifold-3D geometry library.

This compiler allows developers to utilize OpenSCAD's familiar declarative syntax while leveraging the performance and topologically robust geometry processing of Manifold in modern JavaScript/TypeScript environments (like Node.js or the browser).

Key Features

  • Full AST Visitor Implementation: Completed all required visitor methods for the openscad-parser AST.
  • Robust Runtime Environment:
    • RuntimeScope: Manages variable, module, and function scopes, supporting both global and nested definitions.
    • Custom Modules & Functions: Full support for declaring and calling custom OpenSCAD modules and functions from within the generated JS.
    • Geometry Collection System: Implemented a results stack to ensure nested transformations and boolean operations are applied correctly to their children.
  • Core OpenSCAD Support:
    • Leaf Geometry: cube, sphere, cylinder.
    • Transformations: translate, rotate, scale.
    • Booleans: union, difference, intersection.
    • Control flow: if-else statements and for loops (including ranges like [0:5:100]).
    • Expressions: Mathematical operations, ternary expressions, array lookups, and built-in functions (sin, cos, sqrt, etc.).
  • CLI Utility: Includes a command-line tool (cli.ts) to easily compile files from the terminal.

Technical Details

  • Language: TypeScript
  • Target: Node.js (ESM)
  • Primary Dependencies: openscad-parser, manifold-3d, yargs.
  • Formatting: The generated code handles named vs positional arguments, ensuring that parameters like cube(size=10) are correctly mapped to { size: 10 } in the target library.

How to Test

1. Compile the tool

npm install
npm run build

2. Run the test bench

npm test

This runs a comprehensive test case in test-compiler.ts and verifies the output.

3. Compile a SCAD file

node cli.js -i example.scad -o example.js

Future Work

  • Support for 2D primitives (square, circle).
  • Implementation of more advanced geometry like polyhedron and hull.
  • Full support for $children and more complex list comprehensions.

Checklist

  • Code compiles without errors
  • All core OpenSCAD modules are implemented
  • Variables, modules, and loops are supported
  • CLI tool is functional
  • Documentation/README is relevant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant