karthikmn commited on
Commit
584295c
·
verified ·
1 Parent(s): ae511d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -32
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
- # Function to generate CAD model
8
- def generate_cad_model(length, width, height):
9
- # Create a simple box model
10
- model = cq.Workplane("XY").box(length, width, height)
 
 
11
 
12
- # Save the model as a STEP file
13
- step_filename = "customer_model.step"
14
- exporters.export(model, step_filename)
 
15
 
16
- # Generate a preview using trimesh
17
- preview = trimesh.creation.box(extents=(length, width, height))
18
- preview_filename = "preview.png"
19
- preview.export(preview_filename)
20
-
21
- return preview_filename, step_filename
 
 
 
 
 
22
 
23
- # Gradio interface for customer demo
24
- def customer_demo(length, width, height):
25
- preview_image, step_file = generate_cad_model(length, width, height)
26
- return preview_image, step_file
27
 
28
- # Launch the Gradio app
29
  demo = gr.Interface(
30
- fn=customer_demo,
31
  inputs=[
32
- gr.Number(label="Length (mm)", value=100),
33
- gr.Number(label="Width (mm)", value=50),
34
- gr.Number(label="Height (mm)", value=30)
35
- ],
36
- outputs=[
37
- gr.Image(label="CAD Preview"),
38
- gr.File(label="Download STEP File")
39
  ],
40
- title="CAD Model Generator",
41
- description="Input dimensions to generate a CAD model for demo."
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()