Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,127 +2,72 @@ import gradio as gr
|
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
ET, 1,
|
| 15 |
-
ESIZE,
|
| 16 |
-
AMESH, ALL
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
D,
|
| 20 |
-
|
| 21 |
-
F, NODE(
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
/
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"""
|
| 40 |
-
|
| 41 |
-
def upload_step_file(file):
|
| 42 |
-
"""Function to handle STEP file upload."""
|
| 43 |
-
return f"STEP file '{file.name}' uploaded successfully."
|
| 44 |
-
|
| 45 |
-
def modify_apdl_script(modified_script):
|
| 46 |
-
"""Function to modify and save the APDL script."""
|
| 47 |
-
global apdl_script
|
| 48 |
-
apdl_script = modified_script
|
| 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 |
-
"C:\\Program Files\\ANSYS Inc\\v211\\ansys.exe",
|
| 63 |
-
"-b",
|
| 64 |
-
"-i", script_file,
|
| 65 |
-
"-o", "output.log"
|
| 66 |
]
|
| 67 |
|
| 68 |
try:
|
| 69 |
-
# Run the ANSYS command
|
| 70 |
subprocess.run(ansys_command, check=True)
|
| 71 |
-
|
| 72 |
-
# Check if 2D and 3D images exist
|
| 73 |
if os.path.exists("2D_View.jpg") and os.path.exists("3D_View.jpg"):
|
| 74 |
-
return "
|
| 75 |
else:
|
| 76 |
-
return "
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
return f"Error executing APDL script: {str(e)}. Check output.log for details."
|
| 80 |
-
except FileNotFoundError:
|
| 81 |
-
return "ANSYS executable not found. Verify the path in ansys_command."
|
| 82 |
-
|
| 83 |
|
| 84 |
-
def get_generated_images():
|
| 85 |
-
"""Return the generated 2D and 3D view images if they exist."""
|
| 86 |
-
if os.path.exists("2D_View.jpg") and os.path.exists("3D_View.jpg"):
|
| 87 |
-
return "2D_View.jpg", "3D_View.jpg"
|
| 88 |
-
else:
|
| 89 |
-
return None, None
|
| 90 |
-
|
| 91 |
-
def read_output_log():
|
| 92 |
-
"""Read and return the contents of output.log."""
|
| 93 |
-
if os.path.exists("output.log"):
|
| 94 |
-
with open("output.log", "r") as f:
|
| 95 |
-
return f.read()
|
| 96 |
-
else:
|
| 97 |
-
return "No output log found."
|
| 98 |
-
|
| 99 |
-
# Gradio interface
|
| 100 |
with gr.Blocks() as app:
|
| 101 |
-
gr.Markdown("# ANSYS
|
| 102 |
|
| 103 |
with gr.Row():
|
| 104 |
step_file = gr.File(label="Upload STEP File")
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
with gr.Row():
|
| 109 |
-
apdl_view = gr.Textbox(apdl_script, lines=20, label="APDL Script")
|
| 110 |
-
modify_button = gr.Button("Save Changes")
|
| 111 |
-
modify_button.click(modify_apdl_script, inputs=apdl_view, outputs=step_output)
|
| 112 |
|
| 113 |
with gr.Row():
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
with gr.Row():
|
| 121 |
-
gr.Markdown("## Generated Images")
|
| 122 |
-
view_2d = gr.Image(label="2D View", interactive=False)
|
| 123 |
-
view_3d = gr.Image(label="3D View", interactive=False)
|
| 124 |
-
refresh_button = gr.Button("Refresh Images")
|
| 125 |
-
refresh_button.click(get_generated_images, outputs=[view_2d, view_3d])
|
| 126 |
|
| 127 |
app.launch()
|
| 128 |
-
|
|
|
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
def process_step_file(file):
|
| 6 |
+
"""Process the uploaded STEP file and run the APDL script."""
|
| 7 |
+
step_file_path = file.name
|
| 8 |
+
apdl_script = f"""
|
| 9 |
+
/CLEAR
|
| 10 |
+
/CWD, '{os.getcwd()}'
|
| 11 |
+
/PREP7
|
| 12 |
+
/IMPORT, STEP, '{step_file_path}'
|
| 13 |
+
NUMMRG, ALL
|
| 14 |
+
ET, 1, SOLID186
|
| 15 |
+
ESIZE, 10
|
| 16 |
+
AMESH, ALL
|
| 17 |
+
MP, EX, 1, 200E9
|
| 18 |
+
MP, PRXY, 1, 0.3
|
| 19 |
+
D, ALL, UX, 0
|
| 20 |
+
D, ALL, UY, 0
|
| 21 |
+
F, NODE(0, 0, 0), FX, 1000
|
| 22 |
+
FINISH
|
| 23 |
+
/SOLU
|
| 24 |
+
ANTYPE, 0
|
| 25 |
+
SOLVE
|
| 26 |
+
FINISH
|
| 27 |
+
/POST1
|
| 28 |
+
/SHOW, JPEG
|
| 29 |
+
PLNSOL, U, SUM
|
| 30 |
+
FILE, '2D_View', 'JPEG'
|
| 31 |
+
/SHOW, CLOSE
|
| 32 |
+
/VIEW, 1, 1, 1, 1
|
| 33 |
+
PLNSOL, S, EQV
|
| 34 |
+
FILE, '3D_View', 'JPEG'
|
| 35 |
+
/SHOW, CLOSE
|
| 36 |
+
FINISH
|
| 37 |
+
"""
|
| 38 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
script_file = "script.dat"
|
| 40 |
with open(script_file, "w") as f:
|
| 41 |
f.write(apdl_script)
|
| 42 |
|
|
|
|
| 43 |
ansys_command = [
|
| 44 |
+
"C:\\Program Files\\ANSYS Inc\\v211\\ansys\\bin\\winx64\\ansys211.exe",
|
| 45 |
+
"-b", "-i", script_file, "-o", "output.log"
|
|
|
|
|
|
|
| 46 |
]
|
| 47 |
|
| 48 |
try:
|
|
|
|
| 49 |
subprocess.run(ansys_command, check=True)
|
|
|
|
|
|
|
| 50 |
if os.path.exists("2D_View.jpg") and os.path.exists("3D_View.jpg"):
|
| 51 |
+
return "Script executed successfully. Results generated."
|
| 52 |
else:
|
| 53 |
+
return "Execution completed, but results not found."
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return f"Error executing script: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
with gr.Blocks() as app:
|
| 58 |
+
gr.Markdown("# ANSYS STEP File Processing")
|
| 59 |
|
| 60 |
with gr.Row():
|
| 61 |
step_file = gr.File(label="Upload STEP File")
|
| 62 |
+
process_button = gr.Button("Process File")
|
| 63 |
+
output = gr.Text(label="Output Log")
|
| 64 |
+
process_button.click(process_step_file, inputs=step_file, outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
+
gr.Markdown("## Generated Views")
|
| 68 |
+
view_2d = gr.Image(label="2D View")
|
| 69 |
+
view_3d = gr.Image(label="3D View")
|
| 70 |
+
refresh_button = gr.Button("Refresh Results")
|
| 71 |
+
refresh_button.click(lambda: ("2D_View.jpg", "3D_View.jpg"), outputs=[view_2d, view_3d])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
app.launch()
|
|
|