karthikmn commited on
Commit
ed199a0
·
verified ·
1 Parent(s): 7372fba

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import trimesh
4
+ from ansys.mapdl.core import launch_mapdl
5
+
6
+ def generate_apdl_from_cad(cad_file_path):
7
+ """
8
+ Generate APDL script from a CAD file.
9
+ Parameters:
10
+ cad_file_path: Path to the uploaded CAD file (STEP/STL).
11
+ Returns:
12
+ Path to the generated APDL script.
13
+ """
14
+ # Load the CAD model using Trimesh
15
+ cad_model = trimesh.load(cad_file_path)
16
+
17
+ # Get the bounding box dimensions
18
+ bbox = cad_model.bounds
19
+ length = bbox[1][0] - bbox[0][0]
20
+ width = bbox[1][1] - bbox[0][1]
21
+ height = bbox[1][2] - bbox[0][2]
22
+
23
+ # Generate APDL script
24
+ apdl_script = f"""
25
+ /prep7
26
+ block, 0, {length:.4f}, 0, {width:.4f}, 0, {height:.4f}
27
+ /mesh
28
+ esize, 10
29
+ vmesh, all
30
+ allsel, all
31
+ /solu
32
+ antype, static
33
+ d, 1, ux, 0
34
+ d, 1, uy, 0
35
+ d, 1, uz, 0
36
+ f, 2, fy, -1000
37
+ solve
38
+ /exit
39
+ """
40
+
41
+ # Save the APDL script
42
+ apdl_file_path = "/tmp/generated_apdl.txt"
43
+ with open(apdl_file_path, "w") as f:
44
+ f.write(apdl_script)
45
+
46
+ return apdl_file_path
47
+
48
+
49
+ def run_simulation(cad_file, apdl_script_path):
50
+ """
51
+ Run simulation using ANSYS MAPDL and return results.
52
+ Parameters:
53
+ cad_file: CAD file path.
54
+ apdl_script_path: Generated APDL script path.
55
+ Returns:
56
+ Simulation results (stress and displacement plots).
57
+ """
58
+ # Launch ANSYS MAPDL
59
+ mapdl = launch_mapdl()
60
+
61
+ # Import the APDL script
62
+ mapdl.input(apdl_script_path)
63
+
64
+ # Extract results
65
+ stress = mapdl.post_processing.nodal_stress(0)
66
+ displacement = mapdl.post_processing.nodal_displacement(0)
67
+
68
+ # Save stress and displacement plots
69
+ stress_plot_path = "/tmp/stress_plot.png"
70
+ displacement_plot_path = "/tmp/displacement_plot.png"
71
+ mapdl.post_processing.plot_nodal_stress(0, savefig=stress_plot_path)
72
+ mapdl.post_processing.plot_nodal_displacement(0, savefig=displacement_plot_path)
73
+
74
+ mapdl.exit()
75
+ return stress_plot_path, displacement_plot_path
76
+
77
+
78
+ def process_cad_file(cad_file):
79
+ """
80
+ Complete process: CAD to APDL to Simulation.
81
+ Parameters:
82
+ cad_file: Uploaded CAD file (STEP/STL).
83
+ Returns:
84
+ APDL script and simulation results.
85
+ """
86
+ # Generate APDL script
87
+ apdl_script_path = generate_apdl_from_cad(cad_file.name)
88
+
89
+ # Run simulation
90
+ stress_plot, displacement_plot = run_simulation(cad_file.name, apdl_script_path)
91
+
92
+ return apdl_script_path, stress_plot, displacement_plot
93
+
94
+
95
+ # Gradio Interface
96
+ app = gr.Interface(
97
+ fn=process_cad_file,
98
+ inputs=gr.File(label="Upload CAD File (STEP/STL)"),
99
+ outputs=[
100
+ gr.File(label="Download Generated APDL Script"),
101
+ gr.Image(label="Stress Plot"),
102
+ gr.Image(label="Displacement Plot"),
103
+ ],
104
+ live=True,
105
+ )
106
+
107
+ if __name__ == "__main__":
108
+ app.launch()