Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,52 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
|
|
|
|
|
|
| 4 |
from ansys.mapdl.core import launch_mapdl
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
"""
|
| 8 |
-
|
| 9 |
Parameters:
|
| 10 |
-
|
| 11 |
Returns:
|
| 12 |
-
|
| 13 |
"""
|
| 14 |
-
# Load the
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
apdl_script = f"""
|
| 25 |
/prep7
|
| 26 |
block, 0, {length:.4f}, 0, {width:.4f}, 0, {height:.4f}
|
|
@@ -37,67 +63,64 @@ f, 2, fy, -1000
|
|
| 37 |
solve
|
| 38 |
/exit
|
| 39 |
"""
|
| 40 |
-
|
| 41 |
-
|
| 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
|
| 47 |
|
| 48 |
|
| 49 |
-
def run_simulation(
|
| 50 |
"""
|
| 51 |
Run simulation using ANSYS MAPDL and return results.
|
| 52 |
Parameters:
|
| 53 |
-
|
| 54 |
-
apdl_script_path: Generated APDL script path.
|
| 55 |
Returns:
|
| 56 |
-
|
| 57 |
"""
|
| 58 |
# Launch ANSYS MAPDL
|
| 59 |
mapdl = launch_mapdl()
|
| 60 |
|
| 61 |
-
#
|
| 62 |
mapdl.input(apdl_script_path)
|
| 63 |
|
| 64 |
# Extract results
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 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
|
| 76 |
|
| 77 |
|
| 78 |
-
def
|
| 79 |
"""
|
| 80 |
-
Complete
|
| 81 |
Parameters:
|
| 82 |
-
|
| 83 |
Returns:
|
| 84 |
-
APDL script
|
| 85 |
"""
|
|
|
|
|
|
|
|
|
|
| 86 |
# Generate APDL script
|
| 87 |
-
|
| 88 |
|
| 89 |
# Run simulation
|
| 90 |
-
stress_plot, displacement_plot = run_simulation(
|
| 91 |
|
| 92 |
-
return
|
| 93 |
|
| 94 |
|
| 95 |
# Gradio Interface
|
| 96 |
app = gr.Interface(
|
| 97 |
-
fn=
|
| 98 |
-
inputs=gr.File(label="Upload
|
| 99 |
outputs=[
|
| 100 |
-
gr.File(label="Download
|
|
|
|
| 101 |
gr.Image(label="Stress Plot"),
|
| 102 |
gr.Image(label="Displacement Plot"),
|
| 103 |
],
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from OCC.Core.STEPControl import STEPControl_Reader
|
| 4 |
+
from OCC.Core.BRepTools import breptools_Write
|
| 5 |
+
from OCC.Extend.DataExchange import write_stl_file
|
| 6 |
from ansys.mapdl.core import launch_mapdl
|
| 7 |
|
| 8 |
+
|
| 9 |
+
def load_step_and_extract_geometry(step_file_path):
|
| 10 |
"""
|
| 11 |
+
Load a STEP file and extract geometry information using pythonocc-core.
|
| 12 |
Parameters:
|
| 13 |
+
step_file_path: Path to the uploaded STEP file.
|
| 14 |
Returns:
|
| 15 |
+
STL file path, dimensions (length, width, height).
|
| 16 |
"""
|
| 17 |
+
# Load the STEP file
|
| 18 |
+
step_reader = STEPControl_Reader()
|
| 19 |
+
status = step_reader.ReadFile(step_file_path)
|
| 20 |
+
if status != 1:
|
| 21 |
+
raise ValueError("Failed to load the STEP file.")
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Transfer the file into shapes
|
| 24 |
+
step_reader.TransferRoots()
|
| 25 |
+
shape = step_reader.OneShape()
|
| 26 |
+
|
| 27 |
+
# Save the model as an STL file
|
| 28 |
+
stl_file_path = "/tmp/model.stl"
|
| 29 |
+
write_stl_file(shape, stl_file_path, linear_deflection=0.01)
|
| 30 |
+
|
| 31 |
+
# Calculate dimensions (bounding box)
|
| 32 |
+
bbox = shape.BoundingBox()
|
| 33 |
+
length = bbox.XMax() - bbox.XMin()
|
| 34 |
+
width = bbox.YMax() - bbox.YMin()
|
| 35 |
+
height = bbox.ZMax() - bbox.ZMin()
|
| 36 |
+
|
| 37 |
+
return stl_file_path, length, width, height
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def generate_apdl_script(length, width, height):
|
| 41 |
+
"""
|
| 42 |
+
Generate an APDL script based on dimensions.
|
| 43 |
+
Parameters:
|
| 44 |
+
length: Length of the model.
|
| 45 |
+
width: Width of the model.
|
| 46 |
+
height: Height of the model.
|
| 47 |
+
Returns:
|
| 48 |
+
Path to the APDL script.
|
| 49 |
+
"""
|
| 50 |
apdl_script = f"""
|
| 51 |
/prep7
|
| 52 |
block, 0, {length:.4f}, 0, {width:.4f}, 0, {height:.4f}
|
|
|
|
| 63 |
solve
|
| 64 |
/exit
|
| 65 |
"""
|
| 66 |
+
apdl_path = "/tmp/generated_apdl.txt"
|
| 67 |
+
with open(apdl_path, "w") as f:
|
|
|
|
|
|
|
| 68 |
f.write(apdl_script)
|
| 69 |
|
| 70 |
+
return apdl_path
|
| 71 |
|
| 72 |
|
| 73 |
+
def run_simulation(apdl_script_path):
|
| 74 |
"""
|
| 75 |
Run simulation using ANSYS MAPDL and return results.
|
| 76 |
Parameters:
|
| 77 |
+
apdl_script_path: Path to the generated APDL script.
|
|
|
|
| 78 |
Returns:
|
| 79 |
+
Stress plot and displacement plot paths.
|
| 80 |
"""
|
| 81 |
# Launch ANSYS MAPDL
|
| 82 |
mapdl = launch_mapdl()
|
| 83 |
|
| 84 |
+
# Input the APDL script
|
| 85 |
mapdl.input(apdl_script_path)
|
| 86 |
|
| 87 |
# Extract results
|
| 88 |
+
stress_plot = "/tmp/stress_plot.png"
|
| 89 |
+
displacement_plot = "/tmp/displacement_plot.png"
|
| 90 |
+
mapdl.post_processing.plot_nodal_stress(0, savefig=stress_plot)
|
| 91 |
+
mapdl.post_processing.plot_nodal_displacement(0, savefig=displacement_plot)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
mapdl.exit()
|
| 94 |
+
return stress_plot, displacement_plot
|
| 95 |
|
| 96 |
|
| 97 |
+
def process_step_file(step_file):
|
| 98 |
"""
|
| 99 |
+
Complete workflow for STEP file: Load -> Generate APDL -> Simulate.
|
| 100 |
Parameters:
|
| 101 |
+
step_file: Uploaded STEP file.
|
| 102 |
Returns:
|
| 103 |
+
STL file, APDL script, stress plot, displacement plot.
|
| 104 |
"""
|
| 105 |
+
# Extract geometry from the STEP file
|
| 106 |
+
stl_file, length, width, height = load_step_and_extract_geometry(step_file.name)
|
| 107 |
+
|
| 108 |
# Generate APDL script
|
| 109 |
+
apdl_script = generate_apdl_script(length, width, height)
|
| 110 |
|
| 111 |
# Run simulation
|
| 112 |
+
stress_plot, displacement_plot = run_simulation(apdl_script)
|
| 113 |
|
| 114 |
+
return stl_file, apdl_script, stress_plot, displacement_plot
|
| 115 |
|
| 116 |
|
| 117 |
# Gradio Interface
|
| 118 |
app = gr.Interface(
|
| 119 |
+
fn=process_step_file,
|
| 120 |
+
inputs=gr.File(label="Upload STEP File"),
|
| 121 |
outputs=[
|
| 122 |
+
gr.File(label="Download STL File"),
|
| 123 |
+
gr.File(label="Download APDL Script"),
|
| 124 |
gr.Image(label="Stress Plot"),
|
| 125 |
gr.Image(label="Displacement Plot"),
|
| 126 |
],
|