-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathvase.py
More file actions
191 lines (163 loc) · 4.67 KB
/
Copy pathvase.py
File metadata and controls
191 lines (163 loc) · 4.67 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
# /// script
# requires-python = "==3.12"
# dependencies = [
# "build123d==0.10.0",
# "marimo>=0.19.7",
# "marimo-cad==0.1.0",
# ]
# ///
import marimo
__generated_with = "0.19.11"
app = marimo.App(width="medium")
with app.setup:
from marimo_cad import export_stl
import marimo as mo
import marimo_cad as cad
import tempfile
from build123d import ( Axis, BuildLine, BuildPart, BuildSketch, Line, Plane, Spline, make_face, revolve, )
from pathlib import Path
@app.cell
def _():
mo.md(r"""
# Parametric Vase
A smooth, organic vase created with spline curves and revolution.
Adjust the sliders - camera position is preserved!
""")
return
@app.cell
def _():
# Shape parameters
height = mo.ui.slider(80, 200, value=120, step=10, label="Height (mm)")
base_radius = mo.ui.slider(20, 50, value=30, step=5, label="Base Radius (mm)")
belly_radius = mo.ui.slider(30, 80, value=50, step=5, label="Belly Radius (mm)")
neck_radius = mo.ui.slider(15, 40, value=25, step=5, label="Neck Radius (mm)")
top_radius = mo.ui.slider(20, 60, value=35, step=5, label="Top Radius (mm)")
# Profile control
belly_height = mo.ui.slider(20, 80, value=40, step=5, label="Belly Height (%)")
neck_height = mo.ui.slider(60, 95, value=80, step=5, label="Neck Height (%)")
# Wall thickness
wall = mo.ui.slider(2, 6, value=3, step=0.5, label="Wall Thickness (mm)")
return (
base_radius,
belly_height,
belly_radius,
height,
neck_height,
neck_radius,
top_radius,
wall,
)
@app.cell
def _():
# Create viewer once - it persists across slider changes
viewer = cad.Viewer()
return (viewer,)
@app.function
def build_vase(
h: float,
r_base: float,
r_belly: float,
r_neck: float,
r_top: float,
belly_pct: float,
neck_pct: float,
thickness: float,
):
"""Build a smooth vase using spline revolution."""
h_belly = h * (belly_pct / 100)
h_neck = h * (neck_pct / 100)
inner_r_base = max(r_base - thickness, 2)
inner_r_belly = max(r_belly - thickness, 2)
inner_r_neck = max(r_neck - thickness, 2)
inner_r_top = r_top - thickness
with BuildLine(Plane.XZ) as outer_profile:
Line((0, 0), (r_base, 0))
Spline(
(r_base, 0),
(r_belly, h_belly),
(r_neck, h_neck),
(r_top, h),
)
Line((r_top, h), (inner_r_top, h))
Spline(
(inner_r_top, h),
(inner_r_neck, h_neck),
(inner_r_belly, h_belly),
(inner_r_base, thickness),
)
Line((inner_r_base, thickness), (0, thickness))
Line((0, thickness), (0, 0))
with BuildSketch(Plane.XZ) as profile_sketch:
make_face(outer_profile.wires()[0])
with BuildPart() as vase_part:
revolve(profile_sketch.sketch, axis=Axis.Z, revolution_arc=360)
vase = vase_part.part
parts = [{"shape": vase, "name": "Vase", "color": "#E8D5B7"}]
return vase, parts
@app.cell
def _(
base_radius,
belly_height,
belly_radius,
height,
neck_height,
neck_radius,
top_radius,
wall,
):
vase_shape, vase_parts = build_vase(
h=height.value,
r_base=base_radius.value,
r_belly=belly_radius.value,
r_neck=neck_radius.value,
r_top=top_radius.value,
belly_pct=belly_height.value,
neck_pct=neck_height.value,
thickness=wall.value,
)
return vase_parts, vase_shape
@app.cell
def _(
base_radius,
belly_height,
belly_radius,
height,
neck_height,
neck_radius,
top_radius,
vase_parts,
vase_shape,
viewer,
wall,
):
# Update viewer - camera stays put!
viewer.render(vase_parts)
def _get_stl_bytes():
with tempfile.TemporaryDirectory() as tmpdir:
stl_path = Path(tmpdir) / "vase.stl"
export_stl(vase_shape, stl_path)
return stl_path.read_bytes()
download_btn = mo.download(
data=_get_stl_bytes,
filename="vase.stl",
mimetype="model/stl",
label="Download STL",
)
mo.vstack(
[
mo.hstack(
[
mo.vstack([mo.md("**Dimensions**"), height, base_radius, wall]),
mo.vstack([mo.md("**Shape**"), belly_radius, neck_radius, top_radius]),
mo.vstack([mo.md("**Profile**"), belly_height, neck_height]),
mo.vstack([mo.md("**Export**"), download_btn]),
],
justify="start",
gap=2,
),
viewer,
]
)
return
if __name__ == "__main__":
app.run()