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