jithenderchoudary commited on
Commit
20d3747
·
verified ·
1 Parent(s): e39ce8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -44
app.py CHANGED
@@ -5,61 +5,71 @@ import trimesh
5
  import gradio as gr
6
  from cadquery.occ_impl.exporters import exportShape
7
 
8
- # Function to create the CAD model
9
  def create_bird_ear_shell(height, radius, thickness):
10
- outer_shell = (
11
- cq.Workplane("XY")
12
- .lineTo(0, height) # Vertical height
13
- .radiusArc((radius, 0), radius) # Curve to base
14
- .close()
15
- .revolve() # Revolve to create 3D shape
16
- )
17
- inner_shell = (
18
- cq.Workplane("XY")
19
- .lineTo(0, height - thickness)
20
- .radiusArc((radius - thickness, 0), radius - thickness)
21
- .close()
22
- .revolve()
23
- )
24
- shell = outer_shell.cut(inner_shell)
25
- return shell
 
 
 
26
 
27
- # Function to generate a 2D visualization
28
  def generate_2d_image(height, radius):
29
- theta = np.linspace(0, 2 * np.pi, 100)
30
- x_outer = radius * np.cos(theta)
31
- y_outer = height * np.sin(theta) / max(np.sin(theta))
 
32
 
33
- plt.figure(figsize=(6, 6))
34
- plt.plot(x_outer, y_outer, label="Outer Shell", color="blue")
35
- plt.fill_between(x_outer, y_outer, color="lightblue", alpha=0.5)
36
- plt.axhline(0, color="black", linewidth=0.5, linestyle="--")
37
- plt.axvline(0, color="black", linewidth=0.5, linestyle="--")
38
- plt.title("2D Cross-Section of Ear-Like Bird Shell")
39
- plt.xlabel("Radius (mm)")
40
- plt.ylabel("Height (mm)")
41
- plt.grid()
42
- plt.legend()
43
- plt.savefig("2d_visualization.png")
44
- return "2d_visualization.png"
 
 
 
45
 
46
- # Function to generate a 3D STL file
47
  def generate_3d_stl(height, radius, thickness):
48
- bird_shell_model = create_bird_ear_shell(height, radius, thickness)
49
- stl_path = "ear_bird_shell.stl"
50
- with open(stl_path, "wb") as f:
51
- exportShape(bird_shell_model.val(), "STL", f)
52
- return stl_path
 
 
 
 
 
 
53
 
54
- # Gradio interface functions
55
  def generate_visuals(height, radius, thickness):
56
- # Generate 2D image
57
  two_d_image = generate_2d_image(height, radius)
58
- # Generate 3D STL file
59
  three_d_model = generate_3d_stl(height, radius, thickness)
 
 
 
 
60
  return two_d_image, three_d_model
61
 
62
- # Gradio interface
63
  interface = gr.Interface(
64
  fn=generate_visuals,
65
  inputs=[
@@ -75,5 +85,4 @@ interface = gr.Interface(
75
  description="Input the parameters to create an ear-like bird shell model. You can visualize it in 2D and download the 3D STL file for further use."
76
  )
77
 
78
- # Launch the app
79
  interface.launch()
 
5
  import gradio as gr
6
  from cadquery.occ_impl.exporters import exportShape
7
 
 
8
  def create_bird_ear_shell(height, radius, thickness):
9
+ try:
10
+ outer_shell = (
11
+ cq.Workplane("XY")
12
+ .lineTo(0, height)
13
+ .radiusArc((radius, 0), radius)
14
+ .close()
15
+ .revolve()
16
+ )
17
+ inner_shell = (
18
+ cq.Workplane("XY")
19
+ .lineTo(0, height - thickness)
20
+ .radiusArc((radius - thickness, 0), radius - thickness)
21
+ .close()
22
+ .revolve()
23
+ )
24
+ return outer_shell.cut(inner_shell)
25
+ except Exception as e:
26
+ print(f"Error in creating shell geometry: {e}")
27
+ return None
28
 
 
29
  def generate_2d_image(height, radius):
30
+ try:
31
+ theta = np.linspace(0, 2 * np.pi, 100)
32
+ x_outer = radius * np.cos(theta)
33
+ y_outer = height * np.sin(theta) / max(np.sin(theta))
34
 
35
+ plt.figure(figsize=(6, 6))
36
+ plt.plot(x_outer, y_outer, label="Outer Shell", color="blue")
37
+ plt.fill_between(x_outer, y_outer, color="lightblue", alpha=0.5)
38
+ plt.axhline(0, color="black", linewidth=0.5, linestyle="--")
39
+ plt.axvline(0, color="black", linewidth=0.5, linestyle="--")
40
+ plt.title("2D Cross-Section of Ear-Like Bird Shell")
41
+ plt.xlabel("Radius (mm)")
42
+ plt.ylabel("Height (mm)")
43
+ plt.grid()
44
+ plt.legend()
45
+ plt.savefig("2d_visualization.png")
46
+ return "2d_visualization.png"
47
+ except Exception as e:
48
+ print(f"Error in generating 2D image: {e}")
49
+ return None
50
 
 
51
  def generate_3d_stl(height, radius, thickness):
52
+ try:
53
+ bird_shell_model = create_bird_ear_shell(height, radius, thickness)
54
+ if bird_shell_model is None:
55
+ raise ValueError("Invalid CAD geometry.")
56
+ stl_path = "ear_bird_shell.stl"
57
+ with open(stl_path, "wb") as f:
58
+ exportShape(bird_shell_model.val(), "STL", f)
59
+ return stl_path
60
+ except Exception as e:
61
+ print(f"Error in generating 3D STL: {e}")
62
+ return None
63
 
 
64
  def generate_visuals(height, radius, thickness):
 
65
  two_d_image = generate_2d_image(height, radius)
 
66
  three_d_model = generate_3d_stl(height, radius, thickness)
67
+ if two_d_image is None:
68
+ two_d_image = "Error: Unable to generate 2D visualization."
69
+ if three_d_model is None:
70
+ three_d_model = "Error: Unable to generate 3D STL file."
71
  return two_d_image, three_d_model
72
 
 
73
  interface = gr.Interface(
74
  fn=generate_visuals,
75
  inputs=[
 
85
  description="Input the parameters to create an ear-like bird shell model. You can visualize it in 2D and download the 3D STL file for further use."
86
  )
87
 
 
88
  interface.launch()