-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
199 lines (168 loc) · 6.77 KB
/
Copy pathapp.py
File metadata and controls
199 lines (168 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"""
Web frontend for the Gridfinity bin generator.
Routes:
GET / -- HTML form + Three.js viewer
POST /generate -- JSON params -> STL bytes (model/x.stl-binary)
"""
from __future__ import annotations
import io
import os
import warnings
from collections import OrderedDict
from dataclasses import asdict
from threading import Lock
from flask import Flask, jsonify, render_template, request, send_file
import gridfinity as G
# OCP boolean cleanup warnings are noisy and harmless — silence at the source.
warnings.filterwarnings("ignore", message="Boolean operation unable to clean")
warnings.filterwarnings("ignore", message="Unable to clean")
app = Flask(__name__)
# ---------------------------------------------------------------------------
# LRU cache for STL bytes
# ---------------------------------------------------------------------------
_CACHE_MAX = 32
_cache: "OrderedDict[tuple, bytes]" = OrderedDict()
_cache_lock = Lock()
# Per-key locks so two concurrent requests for the same params share work
# instead of both running build_bin in parallel.
_inflight: "dict[tuple, Lock]" = {}
_inflight_lock = Lock()
def _cache_get(key: tuple) -> bytes | None:
with _cache_lock:
if key in _cache:
_cache.move_to_end(key)
return _cache[key]
return None
def _cache_put(key: tuple, data: bytes) -> None:
with _cache_lock:
_cache[key] = data
_cache.move_to_end(key)
while len(_cache) > _CACHE_MAX:
_cache.popitem(last=False)
# ---------------------------------------------------------------------------
# Param parsing
# ---------------------------------------------------------------------------
def _params_from_request() -> G.GridfinityParams:
"""Build GridfinityParams from a JSON POST body, using defaults for any
missing fields and raising on unknown ones."""
data = request.get_json(silent=True) or {}
defaults = asdict(G.GridfinityParams())
unknown = [k for k in data.keys() if k not in defaults]
if unknown:
raise ValueError(f"Unknown parameter(s): {', '.join(unknown)}")
merged = {**defaults, **data}
# Coerce numeric/bool fields
coerce_int = {"dividers_x", "dividers_y"}
coerce_float = {
"grids_x", "grids_y", "grids_z", "wall_thickness", "magnet_diameter",
"magnet_depth", "label_width", "label_depth", "scoop_radius",
"label_support_density", "ultra_light_floor_thickness",
"brim_ear_diameter", "brim_ear_height",
}
coerce_bool = {
"half_grid_right", "half_grid_top", "half_grid_base",
"ultra_light_base", "ultra_light_labels",
"magnets", "dividers", "labels", "label_for_each_section", "scoops",
"brim_ears",
}
for k in coerce_int:
merged[k] = int(merged[k])
for k in coerce_float:
merged[k] = float(merged[k])
for k in coerce_bool:
v = merged[k]
if isinstance(v, str):
merged[k] = v.lower() in ("1", "true", "yes", "on")
else:
merged[k] = bool(v)
if merged["label_position"] not in ("Full", "Left", "Center", "Right"):
raise ValueError(f"Invalid label_position: {merged['label_position']}")
return G.GridfinityParams(**merged)
# ---------------------------------------------------------------------------
# Routes
# ---------------------------------------------------------------------------
@app.route("/")
def index():
return render_template("index.html", defaults=asdict(G.GridfinityParams()))
def _generate_stl(params: G.GridfinityParams) -> bytes:
"""Build the STL bytes for given params, sharing work between concurrent
requests for the same params."""
key = tuple(sorted(asdict(params).items()))
cached = _cache_get(key)
if cached is not None:
return cached
# Acquire a per-key lock so concurrent identical requests don't double-build
with _inflight_lock:
lk = _inflight.get(key)
if lk is None:
lk = Lock()
_inflight[key] = lk
with lk:
cached = _cache_get(key)
if cached is not None:
return cached
part = G.build_bin(params)
data = G.to_stl_bytes(part)
_cache_put(key, data)
with _inflight_lock:
_inflight.pop(key, None)
return data
def _build_filename(p: G.GridfinityParams, ext: str) -> str:
"""Compose a descriptive filename that encodes the parameters that matter
for telling two bins apart. Mirrors ``buildFilename`` in templates/index.html
— keep them in sync.
"""
parts = [f"gridfinity_{p.grids_x:g}x{p.grids_y:g}x{p.grids_z:g}"]
parts.append(f"w{p.wall_thickness:g}")
if p.half_grid_right: parts.append("hr")
if p.half_grid_top: parts.append("ht")
if p.half_grid_base: parts.append("hb")
if p.ultra_light_base:
parts.append("ulb")
if p.ultra_light_floor_thickness > p.wall_thickness:
parts.append(f"bot{p.ultra_light_floor_thickness:g}")
if p.ultra_light_labels:
parts.append("ull")
if p.label_support_density != 1.0:
parts.append(f"lsd{p.label_support_density:g}")
if p.magnets:
parts.append(f"m{p.magnet_diameter:g}-{p.magnet_depth:g}")
if p.dividers and (p.dividers_x or p.dividers_y):
parts.append(f"div{p.dividers_x}x{p.dividers_y}")
if p.labels:
pos = p.label_position[0] # F/L/C/R
# The "per section" flag only affects geometry when dividers exist on
# the relevant axis: Full uses only Y, the others use both X and Y.
if p.label_for_each_section and p.dividers:
effective = (p.dividers_y > 0) if pos == "F" else (p.dividers_x > 0 or p.dividers_y > 0)
else:
effective = False
a = "a" if effective else ""
parts.append(f"lbl{pos}{a}-{p.label_width:g}x{p.label_depth:g}")
if p.scoops:
parts.append(f"sc{p.scoop_radius:g}")
if p.brim_ears:
parts.append(f"be{p.brim_ear_diameter:g}-{p.brim_ear_height:g}")
return "_".join(parts) + ext
@app.route("/generate", methods=["POST"])
def generate():
"""Build the STL for the given parameters and return its bytes.
Query args:
?download=1 – send as attachment (disposition + filename)
"""
try:
params = _params_from_request()
except (ValueError, TypeError) as e:
return jsonify({"error": str(e)}), 400
data = _generate_stl(params)
download = request.args.get("download") in ("1", "true")
fname = _build_filename(params, ".stl")
return send_file(
io.BytesIO(data),
mimetype="model/stl",
as_attachment=download,
download_name=fname,
)
if __name__ == "__main__":
# threaded=True so previews don't queue behind in-flight generations
app.run(host="127.0.0.1", port=5050, debug=True, threaded=True)