Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,28 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
def process_step_file(step_file_path):
|
| 8 |
"""
|
| 9 |
-
|
| 10 |
Parameters:
|
| 11 |
-
|
| 12 |
Returns:
|
| 13 |
-
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
# Extract bounding box dimensions
|
| 19 |
-
|
| 20 |
-
length =
|
| 21 |
-
width =
|
| 22 |
-
height =
|
| 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 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 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
|
| 90 |
"""
|
| 91 |
-
|
| 92 |
Parameters:
|
| 93 |
-
|
| 94 |
Returns:
|
| 95 |
-
|
| 96 |
"""
|
| 97 |
-
|
| 98 |
-
|
| 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=
|
| 112 |
-
inputs=gr.File(label="Upload
|
| 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 |
|