jithenderchoudary commited on
Commit
526af34
·
verified ·
1 Parent(s): ae2b345

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -30
app.py CHANGED
@@ -1,57 +1,65 @@
1
  import gradio as gr
2
  import cadquery as cq
3
  from cadquery import exporters
4
- import numpy as np
5
  import pyvista as pv
6
- from pyvistaqt import BackgroundPlotter
7
  import trimesh
8
 
9
- # Load the STL file
10
- mesh = trimesh.load('/mnt/data/Tile_3_3.stl')
11
-
12
- # Display basic information
13
- print(mesh)
14
- print("Volume:", mesh.volume)
15
- print("Surface Area:", mesh.area)
16
-
17
-
18
- # Function to load a STEP file and display it
19
  def load_and_display_step(file):
20
  try:
21
- # Load STEP file
22
- model = cq.importers.importStep(file.name)
 
23
 
24
- # Export as STL for visualization
25
  stl_file = "temp_model.stl"
26
  exporters.export(model, stl_file)
27
 
28
  # Render with PyVista
29
  mesh = pv.read(stl_file)
30
- plotter = BackgroundPlotter()
 
31
  plotter.add_mesh(mesh, color="lightblue")
32
- plotter.show()
 
 
 
 
 
33
 
34
- return "File loaded and displayed successfully!"
 
 
 
 
 
 
 
35
  except Exception as e:
36
  return f"Error: {str(e)}"
37
 
38
- # Function to run a simple stress analysis (placeholder logic)
39
  def stress_analysis(press_force, area):
40
  try:
41
- stress = press_force / area # Stress = Force / Area
42
- return f"Calculated Stress: {stress} N/m²"
43
- except ZeroDivisionError:
44
- return "Error: Area cannot be zero."
 
 
45
 
46
- # Create Gradio Interface
47
  def main():
48
  with gr.Blocks() as app:
49
  gr.Markdown("# Press Tool Design Simulation")
50
 
51
  with gr.Row():
52
- file_input = gr.File(label="Upload STEP File")
53
- file_output = gr.Textbox(label="File Status")
54
- file_submit = gr.Button("Load and Display STEP File")
 
 
 
 
 
55
 
56
  with gr.Row():
57
  force_input = gr.Number(label="Press Force (N)", value=1000)
@@ -59,12 +67,11 @@ def main():
59
  stress_output = gr.Textbox(label="Stress Analysis Result")
60
  stress_submit = gr.Button("Calculate Stress")
61
 
62
- # Events
63
- file_submit.click(fn=load_and_display_step, inputs=file_input, outputs=file_output)
64
  stress_submit.click(fn=stress_analysis, inputs=[force_input, area_input], outputs=stress_output)
65
 
66
  app.launch()
67
 
68
- # Run the app
69
  if __name__ == "__main__":
70
  main()
 
1
  import gradio as gr
2
  import cadquery as cq
3
  from cadquery import exporters
4
+ import tempfile
5
  import pyvista as pv
 
6
  import trimesh
7
 
 
 
 
 
 
 
 
 
 
 
8
  def load_and_display_step(file):
9
  try:
10
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".step") as temp_file:
11
+ temp_file.write(file.read())
12
+ temp_path = temp_file.name
13
 
14
+ model = cq.importers.importStep(temp_path)
15
  stl_file = "temp_model.stl"
16
  exporters.export(model, stl_file)
17
 
18
  # Render with PyVista
19
  mesh = pv.read(stl_file)
20
+ plotter = pv.Plotter(off_screen=True)
21
+ image_file = "rendered_output.png"
22
  plotter.add_mesh(mesh, color="lightblue")
23
+ plotter.screenshot(image_file)
24
+ plotter.close()
25
+
26
+ return f"STEP file processed successfully! Render saved as {image_file}"
27
+ except Exception as e:
28
+ return f"Error: {str(e)}"
29
 
30
+ def analyze_stl(file):
31
+ try:
32
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".stl") as temp_file:
33
+ temp_file.write(file.read())
34
+ temp_path = temp_file.name
35
+
36
+ mesh = trimesh.load(temp_path)
37
+ return f"Volume: {mesh.volume:.3f} m³\nSurface Area: {mesh.area:.3f} m²"
38
  except Exception as e:
39
  return f"Error: {str(e)}"
40
 
 
41
  def stress_analysis(press_force, area):
42
  try:
43
+ if area <= 0:
44
+ return "Error: Area must be greater than zero."
45
+ stress = press_force / area
46
+ return f"Calculated Stress: {stress:.2f} N/m²"
47
+ except Exception as e:
48
+ return f"Error: {str(e)}"
49
 
 
50
  def main():
51
  with gr.Blocks() as app:
52
  gr.Markdown("# Press Tool Design Simulation")
53
 
54
  with gr.Row():
55
+ step_input = gr.File(label="Upload STEP File")
56
+ step_output = gr.Textbox(label="STEP File Status")
57
+ step_submit = gr.Button("Load and Display STEP File")
58
+
59
+ with gr.Row():
60
+ stl_input = gr.File(label="Upload STL File")
61
+ stl_output = gr.Textbox(label="STL Analysis Result")
62
+ stl_submit = gr.Button("Analyze STL File")
63
 
64
  with gr.Row():
65
  force_input = gr.Number(label="Press Force (N)", value=1000)
 
67
  stress_output = gr.Textbox(label="Stress Analysis Result")
68
  stress_submit = gr.Button("Calculate Stress")
69
 
70
+ step_submit.click(fn=load_and_display_step, inputs=step_input, outputs=step_output)
71
+ stl_submit.click(fn=analyze_stl, inputs=stl_input, outputs=stl_output)
72
  stress_submit.click(fn=stress_analysis, inputs=[force_input, area_input], outputs=stress_output)
73
 
74
  app.launch()
75
 
 
76
  if __name__ == "__main__":
77
  main()