karthikmn commited on
Commit
8134dc8
·
verified ·
1 Parent(s): 20338fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -77
app.py CHANGED
@@ -1,44 +1,28 @@
1
  import os
2
  import gradio as gr
3
- import gmsh
4
- from ansys.mapdl.core import launch_mapdl
 
5
 
6
-
7
- def process_step_file(step_file_path):
8
  """
9
- Load STEP file using GMSH and extract geometry.
10
  Parameters:
11
- step_file_path: Path to the uploaded STEP file.
12
  Returns:
13
- STL file path, dimensions (length, width, height).
14
  """
15
- gmsh.initialize()
16
- gmsh.open(step_file_path)
 
17
 
18
  # Extract bounding box dimensions
19
- _, bbox_min, bbox_max = gmsh.model.getBoundingBox(0, 0)
20
- length = bbox_max[0] - bbox_min[0]
21
- width = bbox_max[1] - bbox_min[1]
22
- height = bbox_max[2] - bbox_min[2]
23
-
24
- # Save the model as an STL file
25
- stl_file_path = "/tmp/model.stl"
26
- gmsh.write(stl_file_path)
27
- gmsh.finalize()
28
-
29
- return stl_file_path, length, width, height
30
 
31
-
32
- def generate_apdl_script(length, width, height):
33
- """
34
- Generate APDL script based on dimensions.
35
- Parameters:
36
- length: Length of the model.
37
- width: Width of the model.
38
- height: Height of the model.
39
- Returns:
40
- Path to the APDL script.
41
- """
42
  apdl_script = f"""
43
  /prep7
44
  block, 0, {length:.4f}, 0, {width:.4f}, 0, {height:.4f}
@@ -59,63 +43,29 @@ solve
59
  with open(apdl_path, "w") as f:
60
  f.write(apdl_script)
61
 
62
- return apdl_path
63
-
64
 
65
- def run_simulation(apdl_script_path):
66
- """
67
- Run simulation using ANSYS MAPDL and return results.
68
- Parameters:
69
- apdl_script_path: Path to the generated APDL script.
70
- Returns:
71
- Stress plot and displacement plot paths.
72
- """
73
- # Launch ANSYS MAPDL
74
- mapdl = launch_mapdl()
75
-
76
- # Input the APDL script
77
- mapdl.input(apdl_script_path)
78
-
79
- # Extract results
80
- stress_plot = "/tmp/stress_plot.png"
81
- displacement_plot = "/tmp/displacement_plot.png"
82
- mapdl.post_processing.plot_nodal_stress(0, savefig=stress_plot)
83
- mapdl.post_processing.plot_nodal_displacement(0, savefig=displacement_plot)
84
-
85
- mapdl.exit()
86
- return stress_plot, displacement_plot
87
 
88
 
89
- def process_cad_file(cad_file):
90
  """
91
- Complete workflow for STEP file: Load -> Generate APDL -> Simulate.
92
  Parameters:
93
- cad_file: Uploaded STEP file.
94
  Returns:
95
- STL file, APDL script, stress plot, displacement plot.
96
  """
97
- # Extract geometry from the STEP file
98
- stl_file, length, width, height = process_step_file(cad_file.name)
99
-
100
- # Generate APDL script
101
- apdl_script = generate_apdl_script(length, width, height)
102
-
103
- # Run simulation
104
- stress_plot, displacement_plot = run_simulation(apdl_script)
105
-
106
- return stl_file, apdl_script, stress_plot, displacement_plot
107
 
108
 
109
  # Gradio Interface
110
  app = gr.Interface(
111
- fn=process_cad_file,
112
- inputs=gr.File(label="Upload STEP File"),
113
- outputs=[
114
- gr.File(label="Download STL File"),
115
- gr.File(label="Download APDL Script"),
116
- gr.Image(label="Stress Plot"),
117
- gr.Image(label="Displacement Plot"),
118
- ],
119
  live=True,
120
  )
121
 
 
1
  import os
2
  import gradio as gr
3
+ import FreeCAD
4
+ import FreeCADGui
5
+ import Part
6
 
7
+ def generate_apdl_from_freecad(freecad_file_path):
 
8
  """
9
+ Generate an APDL script from a FreeCAD file.
10
  Parameters:
11
+ freecad_file_path: Path to the FreeCAD file (.FCStd).
12
  Returns:
13
+ Path to the generated APDL script.
14
  """
15
+ # Open the FreeCAD file
16
+ doc = FreeCAD.openDocument(freecad_file_path)
17
+ obj = doc.Objects[0] # Assuming a single object in the file
18
 
19
  # Extract bounding box dimensions
20
+ bbox = obj.Shape.BoundBox
21
+ length = bbox.XLength
22
+ width = bbox.YLength
23
+ height = bbox.ZLength
 
 
 
 
 
 
 
24
 
25
+ # Generate APDL script
 
 
 
 
 
 
 
 
 
 
26
  apdl_script = f"""
27
  /prep7
28
  block, 0, {length:.4f}, 0, {width:.4f}, 0, {height:.4f}
 
43
  with open(apdl_path, "w") as f:
44
  f.write(apdl_script)
45
 
46
+ # Close the FreeCAD document
47
+ FreeCAD.closeDocument(doc.Name)
48
 
49
+ return apdl_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
 
52
+ def process_freecad_file(freecad_file):
53
  """
54
+ Process the FreeCAD file and generate an APDL script.
55
  Parameters:
56
+ freecad_file: Uploaded FreeCAD file (.FCStd).
57
  Returns:
58
+ Path to the generated APDL script.
59
  """
60
+ apdl_script_path = generate_apdl_from_freecad(freecad_file.name)
61
+ return apdl_script_path
 
 
 
 
 
 
 
 
62
 
63
 
64
  # Gradio Interface
65
  app = gr.Interface(
66
+ fn=process_freecad_file,
67
+ inputs=gr.File(label="Upload FreeCAD File (.FCStd)"),
68
+ outputs=gr.File(label="Download Generated APDL Script"),
 
 
 
 
 
69
  live=True,
70
  )
71