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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -47
app.py CHANGED
@@ -2,74 +2,79 @@ 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)
66
- area_input = gr.Number(label="Contact Area (m²)", value=0.01)
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
 
 
2
  import cadquery as cq
3
  from cadquery import exporters
4
  import tempfile
 
5
  import trimesh
6
 
7
+ # Generate APDL Script
8
+ def generate_apdl_script(file, press_force, material):
9
  try:
10
+ # Save uploaded STEP file temporarily
11
  with tempfile.NamedTemporaryFile(delete=False, suffix=".step") as temp_file:
12
  temp_file.write(file.read())
13
+ step_file_path = temp_file.name
14
 
15
+ # Load STEP file with CadQuery
16
+ model = cq.importers.importStep(step_file_path)
17
+
18
+ # Export as STL for meshing
19
  stl_file = "temp_model.stl"
20
  exporters.export(model, stl_file)
21
 
22
+ # Load STL with trimesh
23
+ mesh = trimesh.load(stl_file)
24
+ volume = mesh.volume
25
+ surface_area = mesh.area
 
 
 
26
 
27
+ # Placeholder stress calculation
28
+ stress = press_force / surface_area if surface_area > 0 else "Undefined (Area is zero)"
 
29
 
30
+ # Generate APDL script
31
+ apdl_script = f"""\
32
+ /PREP7
33
+ ! Importing Geometry
34
+ /import, '{step_file_path}', geom, STEP
35
+ ! Material Properties
36
+ MP, EX, 1, {material['elastic_modulus']}
37
+ MP, PRXY, 1, {material['poisson']}
38
+ ! Load and Boundary Conditions
39
+ F, NODE, ALL, FX, {press_force}
40
+ ! Solve
41
+ /SOLU
42
+ SOLVE
43
+ /POST1
44
+ *GET, stress, NODE, 0, S, MAX
45
+ /EXIT, SAVE
46
+ """
47
+ # Save APDL script to file
48
+ apdl_file_path = "simulation_script.inp"
49
+ with open(apdl_file_path, "w") as apdl_file:
50
+ apdl_file.write(apdl_script)
51
 
52
+ return apdl_script, apdl_file_path
 
53
  except Exception as e:
54
+ return f"Error: {str(e)}", None
 
 
 
 
 
 
 
 
 
55
 
56
+ # Gradio App
57
  def main():
58
  with gr.Blocks() as app:
59
+ gr.Markdown("# APDL Script Generator for Press Tool Design")
60
 
61
  with gr.Row():
62
  step_input = gr.File(label="Upload STEP File")
 
 
 
 
 
 
 
 
 
63
  force_input = gr.Number(label="Press Force (N)", value=1000)
64
+ material_input = gr.Textbox(
65
+ label="Material Properties (JSON)",
66
+ value='{"elastic_modulus": 2e11, "poisson": 0.3}'
67
+ )
68
+ script_output = gr.Textbox(label="Generated APDL Script")
69
+ download_button = gr.File(label="Download Script")
70
+ generate_button = gr.Button("Generate APDL Script")
71
 
72
+ # Events
73
+ generate_button.click(
74
+ fn=generate_apdl_script,
75
+ inputs=[step_input, force_input, material_input],
76
+ outputs=[script_output, download_button],
77
+ )
78
 
79
  app.launch()
80