Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,80 +2,42 @@ import gradio as gr
|
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
def execute_script():
|
| 6 |
-
"""Function to execute the APDL script using ANSYS."""
|
| 7 |
-
global apdl_script
|
| 8 |
-
|
| 9 |
-
# Save the APDL script to a file
|
| 10 |
-
script_file = "script.dat"
|
| 11 |
-
with open(script_file, "w") as f:
|
| 12 |
-
f.write(apdl_script)
|
| 13 |
-
|
| 14 |
-
# Define ANSYS execution command
|
| 15 |
-
ansys_command = [
|
| 16 |
-
"ansys", # Replace with the full path to your ANSYS executable, e.g., "C:\\Program Files\\ANSYS Inc\\v211\\ansys.exe"
|
| 17 |
-
"-b", # Batch mode
|
| 18 |
-
"-i", script_file, # Input script
|
| 19 |
-
"-o", "output.log" # Output log
|
| 20 |
-
]
|
| 21 |
-
|
| 22 |
-
try:
|
| 23 |
-
# Run the command
|
| 24 |
-
subprocess.run(ansys_command, check=True)
|
| 25 |
-
return "APDL script executed. Check generated views (2D_View.jpg, 3D_View.jpg)."
|
| 26 |
-
except subprocess.CalledProcessError as e:
|
| 27 |
-
return f"Error executing APDL script: {e}"
|
| 28 |
-
|
| 29 |
-
|
| 30 |
# APDL Script for ANSYS
|
| 31 |
apdl_script = """/CLEAR ! Clear any existing database
|
| 32 |
/PREP7 ! Enter preprocessor
|
| 33 |
-
|
| 34 |
! Define material properties
|
| 35 |
MP, EX, 1, 210E3 ! Young's Modulus (e.g., Steel)
|
| 36 |
MP, PRXY, 1, 0.3 ! Poisson's Ratio
|
| 37 |
-
|
| 38 |
! Define geometry (e.g., a rectangular plate)
|
| 39 |
RECTNG, 0, 100, 0, 50 ! Rectangle from (0,0) to (100,50)
|
| 40 |
-
|
| 41 |
! Define element type and mesh
|
| 42 |
ET, 1, PLANE183 ! 2D structural element
|
| 43 |
ESIZE, 5 ! Set element size
|
| 44 |
AMESH, ALL ! Mesh the area
|
| 45 |
-
|
| 46 |
! Apply boundary conditions
|
| 47 |
D, NODE(0, 0), ALL, 0 ! Fix the bottom-left corner
|
| 48 |
D, NODE(100, 0), UY, 0 ! Restrict vertical movement at bottom-right
|
| 49 |
-
|
| 50 |
! Apply loads
|
| 51 |
F, NODE(50, 50), FX, 1000 ! Apply a horizontal load at the top-middle node
|
| 52 |
-
|
| 53 |
! Solve
|
| 54 |
FINISH ! Exit preprocessor
|
| 55 |
/SOLU ! Enter solution processor
|
| 56 |
ANTYPE, 0 ! Static analysis
|
| 57 |
SOLVE ! Solve the system
|
| 58 |
FINISH ! Exit solution processor
|
| 59 |
-
|
| 60 |
! Post-processing for 2D view
|
| 61 |
/POST1 ! Enter post-processor
|
| 62 |
PLNSOL, U, SUM ! Plot nodal displacement (2D view)
|
| 63 |
/IMAGE, SAVE, '2D_View', JPG ! Save 2D view as an image
|
| 64 |
-
|
| 65 |
! Post-processing for 3D view
|
| 66 |
/VIEW, 1, 1, 1, 1 ! Set 3D view (isometric)
|
| 67 |
/DV3D ! Enable dynamic 3D view
|
| 68 |
PLNSOL, S, EQV ! Plot equivalent stress (3D view)
|
| 69 |
/IMAGE, SAVE, '3D_View', JPG ! Save 3D view as an image
|
| 70 |
-
|
| 71 |
! Exit ANSYS
|
| 72 |
FINISH
|
| 73 |
"""
|
| 74 |
|
| 75 |
-
def apdl_script_view():
|
| 76 |
-
"""Function to display the APDL script."""
|
| 77 |
-
return apdl_script
|
| 78 |
-
|
| 79 |
def upload_step_file(file):
|
| 80 |
"""Function to handle STEP file upload."""
|
| 81 |
return f"STEP file '{file.name}' uploaded successfully."
|
|
@@ -87,51 +49,61 @@ def modify_apdl_script(modified_script):
|
|
| 87 |
return "APDL script updated successfully."
|
| 88 |
|
| 89 |
def execute_script():
|
| 90 |
-
"""
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# Gradio interface
|
| 95 |
with gr.Blocks() as app:
|
| 96 |
gr.Markdown("# ANSYS APDL Script for 2D and 3D Views")
|
| 97 |
|
| 98 |
with gr.Row():
|
| 99 |
-
gr.Markdown("## Upload STEP File")
|
| 100 |
step_file = gr.File(label="Upload STEP File")
|
| 101 |
step_output = gr.Text(label="Upload Status")
|
|
|
|
| 102 |
|
| 103 |
with gr.Row():
|
| 104 |
-
gr.Markdown("## View or Modify APDL Script")
|
| 105 |
apdl_view = gr.Textbox(apdl_script, lines=20, label="APDL Script")
|
| 106 |
modify_button = gr.Button("Save Changes")
|
|
|
|
| 107 |
|
| 108 |
with gr.Row():
|
| 109 |
-
gr.Markdown("## Execute APDL Script")
|
| 110 |
execute_button = gr.Button("Run Script")
|
| 111 |
execution_output = gr.Text(label="Execution Status")
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
if os.path.exists("2D_View.jpg") and os.path.exists("3D_View.jpg"):
|
| 120 |
-
print("Images generated successfully.")
|
| 121 |
-
else:
|
| 122 |
-
print("Error: Images not found.")
|
| 123 |
-
def get_generated_images():
|
| 124 |
-
"""Return the generated 2D and 3D view images."""
|
| 125 |
-
return "2D_View.jpg", "3D_View.jpg"
|
| 126 |
-
|
| 127 |
-
# Add this to the Gradio interface
|
| 128 |
-
with gr.Row():
|
| 129 |
-
gr.Markdown("## Generated Images")
|
| 130 |
-
view_2d = gr.Image(label="2D View")
|
| 131 |
-
view_3d = gr.Image(label="3D View")
|
| 132 |
-
refresh_button = gr.Button("Refresh Images")
|
| 133 |
-
|
| 134 |
-
refresh_button.click(get_generated_images, outputs=[view_2d, view_3d])
|
| 135 |
|
| 136 |
-
# Launch the app
|
| 137 |
app.launch()
|
|
|
|
|
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# APDL Script for ANSYS
|
| 6 |
apdl_script = """/CLEAR ! Clear any existing database
|
| 7 |
/PREP7 ! Enter preprocessor
|
|
|
|
| 8 |
! Define material properties
|
| 9 |
MP, EX, 1, 210E3 ! Young's Modulus (e.g., Steel)
|
| 10 |
MP, PRXY, 1, 0.3 ! Poisson's Ratio
|
|
|
|
| 11 |
! Define geometry (e.g., a rectangular plate)
|
| 12 |
RECTNG, 0, 100, 0, 50 ! Rectangle from (0,0) to (100,50)
|
|
|
|
| 13 |
! Define element type and mesh
|
| 14 |
ET, 1, PLANE183 ! 2D structural element
|
| 15 |
ESIZE, 5 ! Set element size
|
| 16 |
AMESH, ALL ! Mesh the area
|
|
|
|
| 17 |
! Apply boundary conditions
|
| 18 |
D, NODE(0, 0), ALL, 0 ! Fix the bottom-left corner
|
| 19 |
D, NODE(100, 0), UY, 0 ! Restrict vertical movement at bottom-right
|
|
|
|
| 20 |
! Apply loads
|
| 21 |
F, NODE(50, 50), FX, 1000 ! Apply a horizontal load at the top-middle node
|
|
|
|
| 22 |
! Solve
|
| 23 |
FINISH ! Exit preprocessor
|
| 24 |
/SOLU ! Enter solution processor
|
| 25 |
ANTYPE, 0 ! Static analysis
|
| 26 |
SOLVE ! Solve the system
|
| 27 |
FINISH ! Exit solution processor
|
|
|
|
| 28 |
! Post-processing for 2D view
|
| 29 |
/POST1 ! Enter post-processor
|
| 30 |
PLNSOL, U, SUM ! Plot nodal displacement (2D view)
|
| 31 |
/IMAGE, SAVE, '2D_View', JPG ! Save 2D view as an image
|
|
|
|
| 32 |
! Post-processing for 3D view
|
| 33 |
/VIEW, 1, 1, 1, 1 ! Set 3D view (isometric)
|
| 34 |
/DV3D ! Enable dynamic 3D view
|
| 35 |
PLNSOL, S, EQV ! Plot equivalent stress (3D view)
|
| 36 |
/IMAGE, SAVE, '3D_View', JPG ! Save 3D view as an image
|
|
|
|
| 37 |
! Exit ANSYS
|
| 38 |
FINISH
|
| 39 |
"""
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def upload_step_file(file):
|
| 42 |
"""Function to handle STEP file upload."""
|
| 43 |
return f"STEP file '{file.name}' uploaded successfully."
|
|
|
|
| 49 |
return "APDL script updated successfully."
|
| 50 |
|
| 51 |
def execute_script():
|
| 52 |
+
"""Function to execute the APDL script using ANSYS."""
|
| 53 |
+
global apdl_script
|
| 54 |
+
|
| 55 |
+
# Save the APDL script to a file
|
| 56 |
+
script_file = "script.dat"
|
| 57 |
+
with open(script_file, "w") as f:
|
| 58 |
+
f.write(apdl_script)
|
| 59 |
+
|
| 60 |
+
# Define ANSYS execution command
|
| 61 |
+
ansys_command = [
|
| 62 |
+
"ansys", # Replace with the full path to your ANSYS executable
|
| 63 |
+
"-b", # Batch mode
|
| 64 |
+
"-i", script_file, # Input script
|
| 65 |
+
"-o", "output.log" # Output log
|
| 66 |
+
]
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
# Run the command
|
| 70 |
+
subprocess.run(ansys_command, check=True)
|
| 71 |
+
return "APDL script executed. Check generated views (2D_View.jpg, 3D_View.jpg)."
|
| 72 |
+
except subprocess.CalledProcessError as e:
|
| 73 |
+
return f"Error executing APDL script: {e}"
|
| 74 |
+
|
| 75 |
+
def get_generated_images():
|
| 76 |
+
"""Return the generated 2D and 3D view images if they exist."""
|
| 77 |
+
if os.path.exists("2D_View.jpg") and os.path.exists("3D_View.jpg"):
|
| 78 |
+
return "2D_View.jpg", "3D_View.jpg"
|
| 79 |
+
else:
|
| 80 |
+
return None, None
|
| 81 |
|
| 82 |
# Gradio interface
|
| 83 |
with gr.Blocks() as app:
|
| 84 |
gr.Markdown("# ANSYS APDL Script for 2D and 3D Views")
|
| 85 |
|
| 86 |
with gr.Row():
|
|
|
|
| 87 |
step_file = gr.File(label="Upload STEP File")
|
| 88 |
step_output = gr.Text(label="Upload Status")
|
| 89 |
+
step_file.upload(upload_step_file, inputs=step_file, outputs=step_output)
|
| 90 |
|
| 91 |
with gr.Row():
|
|
|
|
| 92 |
apdl_view = gr.Textbox(apdl_script, lines=20, label="APDL Script")
|
| 93 |
modify_button = gr.Button("Save Changes")
|
| 94 |
+
modify_button.click(modify_apdl_script, inputs=apdl_view, outputs=step_output)
|
| 95 |
|
| 96 |
with gr.Row():
|
|
|
|
| 97 |
execute_button = gr.Button("Run Script")
|
| 98 |
execution_output = gr.Text(label="Execution Status")
|
| 99 |
+
execute_button.click(execute_script, outputs=execution_output)
|
| 100 |
|
| 101 |
+
with gr.Row():
|
| 102 |
+
gr.Markdown("## Generated Images")
|
| 103 |
+
view_2d = gr.Image(label="2D View", interactive=False)
|
| 104 |
+
view_3d = gr.Image(label="3D View", interactive=False)
|
| 105 |
+
refresh_button = gr.Button("Refresh Images")
|
| 106 |
+
refresh_button.click(get_generated_images, outputs=[view_2d, view_3d])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
|
|
|
| 108 |
app.launch()
|
| 109 |
+
|