Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,49 @@
|
|
| 1 |
-
import cadquery as cq
|
| 2 |
-
from cadquery import exporters
|
| 3 |
-
import trimesh
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
# Generate a
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
# Gradio
|
| 24 |
-
def
|
| 25 |
-
|
| 26 |
-
return
|
| 27 |
|
| 28 |
-
#
|
| 29 |
demo = gr.Interface(
|
| 30 |
-
fn=
|
| 31 |
inputs=[
|
| 32 |
-
gr.Number(label="Length (mm)", value=
|
| 33 |
-
gr.Number(label="Width (mm)", value=
|
| 34 |
-
gr.Number(label="Height (mm)", value=
|
| 35 |
-
],
|
| 36 |
-
outputs=[
|
| 37 |
-
gr.Image(label="CAD Preview"),
|
| 38 |
-
gr.File(label="Download STEP File")
|
| 39 |
],
|
| 40 |
-
|
| 41 |
-
|
| 42 |
)
|
| 43 |
|
| 44 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import trimesh
|
| 3 |
+
import io
|
| 4 |
+
import base64
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from matplotlib import pyplot as plt
|
| 7 |
|
| 8 |
+
def generate_cad_model(base_length, base_width, base_height):
|
| 9 |
+
"""
|
| 10 |
+
Generate a CAD model as a 3D mesh and return a 2D projection image for demo purposes.
|
| 11 |
+
"""
|
| 12 |
+
# Create a 3D box (CAD model representation)
|
| 13 |
+
mesh = trimesh.creation.box(extents=(base_length, base_width, base_height))
|
| 14 |
|
| 15 |
+
# Create an image buffer to save a rendered view
|
| 16 |
+
image_buffer = io.BytesIO()
|
| 17 |
+
# Render the model to an image
|
| 18 |
+
mesh.show(smooth=False)
|
| 19 |
|
| 20 |
+
# Generate a 2D projection image (top view for demo)
|
| 21 |
+
projection = mesh.section(plane_origin=(0, 0, 0), plane_normal=(0, 0, 1))
|
| 22 |
+
projection_img = trimesh.util.grid_2d(projection.to_image(), tile=(1, 1))
|
| 23 |
+
plt.imshow(projection_img, cmap="gray")
|
| 24 |
+
plt.axis("off")
|
| 25 |
+
plt.savefig(image_buffer, format="png")
|
| 26 |
+
image_buffer.seek(0)
|
| 27 |
+
|
| 28 |
+
# Convert to Base64 for Gradio to display as an image
|
| 29 |
+
encoded_image = base64.b64encode(image_buffer.getvalue()).decode("utf-8")
|
| 30 |
+
return f"data:image/png;base64,{encoded_image}"
|
| 31 |
|
| 32 |
+
# Gradio Interface
|
| 33 |
+
def display_cad_model(base_length, base_width, base_height):
|
| 34 |
+
cad_image = generate_cad_model(base_length, base_width, base_height)
|
| 35 |
+
return cad_image
|
| 36 |
|
| 37 |
+
# Hosting the interface with Gradio
|
| 38 |
demo = gr.Interface(
|
| 39 |
+
fn=display_cad_model,
|
| 40 |
inputs=[
|
| 41 |
+
gr.Number(label="Base Length (mm)", value=10),
|
| 42 |
+
gr.Number(label="Base Width (mm)", value=10),
|
| 43 |
+
gr.Number(label="Base Height (mm)", value=5),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
],
|
| 45 |
+
outputs=gr.Image(label="CAD Model Projection"),
|
| 46 |
+
live=True,
|
| 47 |
)
|
| 48 |
|
| 49 |
demo.launch()
|