tracks: new /tracks generator for IKEA Lillabo / Brio train tracks

Wraps torwanbukaj/ikea-brio-others-compatible-train-tracks-generator
( https://github.com/torwanbukaj/... ) as a third generator alongside
holder and the universal scad playground.

Approach
--------
The upstream .scad ships as "library + one active example call". Used
include<> (rather than use<>) so the library's top-level globals
(track_width, plug/nest dimensions, $fn = 150, etc.) are available
to the modules — `use<>` does NOT propagate variables. Commented out
the upstream `track_tester();` call so include<> doesn't also emit
the tester geometry every time.

Per-render the backend builds a tiny wrapper SCAD on the fly:

    include <scad/train_tracks.scad>
    track(length=100, end1="plug", end2="nest", cutout=true, ...);

and hands it to holder.render_source().

Part types exposed (9)
----------------------
- tester        Calibration block (15-60mm)
- track         Straight (length + chamfers + grooves + plug/nest ends)
- arc           Curved (radius + angle, IKEA/Brio/J'adore radii in help text)
- dogbone       Nest-nest connector
- intersection  Crossing (angle + 2 lengths + 4 ends)
- switch        Turnout (left/right radius+angle, straight branch, common end)
- snake         S-curve (raised default target_length to 200 — the upstream
                modules's natural curvy-span at angle=45/radius=86 is ~150
                and the assert fires below that)
- adapter       BRIO <-> IKEA system adapter (plug + nest side)
- bridge        Multi-part (overview/ground/slope/pillar — value_map
                translates the UI labels to the int 0..3 the SCAD wants)

Files
-----
- scad/train_tracks.scad      Downloaded upstream (57 999 bytes), only
                              modification is the // before the top-level
                              track_tester() call.
- tracks.py                   PART_SCHEMAS dict, build_wrapper_scad,
                              render() that delegates to render_source.
- app.py                      GET /api/tracks/params, POST /api/tracks/render,
                              GET /tracks page route.
- static/tracks.html          Page with part selector + dynamic param form +
                              shared viewer markup. Reuses holder.css.
- static/js/tracks-app.js     Controller. Switching the part select redraws
                              the form (each part has its own schema).
                              Ctrl+Enter renders, Cancel uses AbortController.

Nav
---
Tracks link added to topbar on holder / index / scad.

Smoke test
----------
All 9 parts render with default params on the Manifold backend in
under 0.5s each (output sizes 440 KB - 1.8 MB).
This commit is contained in:
wenil
2026-05-25 13:38:29 +03:00
parent 5d65b0d8a1
commit 102cfcee64
9 changed files with 2124 additions and 0 deletions
+26
View File
@@ -20,6 +20,7 @@ from flask import Flask, Response, jsonify, request, send_from_directory
from busbar_export import WRITERS
import storage
import holder
import tracks
APP_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_DIR = os.path.join(APP_DIR, "static")
@@ -50,6 +51,12 @@ def scad_page():
return send_from_directory(STATIC_DIR, "scad.html")
@app.get("/tracks")
def tracks_page():
"""Wooden-train track generator (IKEA Lillabo / Brio compatible)."""
return send_from_directory(STATIC_DIR, "tracks.html")
@app.get("/api/health")
def health():
return jsonify({"status": "ok", "openscad": holder.openscad_available()})
@@ -104,6 +111,25 @@ def holder_source():
return jsonify({"error": str(e)}), 500
@app.get("/api/tracks/params")
def tracks_params():
return jsonify(tracks.schema_dict())
@app.post("/api/tracks/render")
def tracks_render():
body = request.get_json(silent=True) or {}
part = body.get("part")
params = body.get("params") or {}
try:
data, dims = tracks.render(part, params)
except ValueError as e:
return jsonify({"error": str(e)}), 400
except (FileNotFoundError, RuntimeError) as e:
return jsonify({"error": str(e)}), 500
return _stl_response(data, dims, f"track_{part or 'part'}.stl")
@app.post("/api/scad/render")
def scad_render():
"""Render arbitrary user-supplied OpenSCAD source (no holder schema).