Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import subprocess | |
| import os | |
| # APDL Script for ANSYS | |
| apdl_script = """/CLEAR ! Clear any existing database | |
| /PREP7 ! Enter preprocessor | |
| /CWD, '{cwd}' ! Set the working directory | |
| ! Define material properties | |
| MP, EX, 1, 210E3 ! Young's Modulus (e.g., Steel) | |
| MP, PRXY, 1, 0.3 ! Poisson's Ratio | |
| ! Define geometry (e.g., a rectangular plate) | |
| RECTNG, 0, 100, 0, 50 ! Rectangle from (0,0) to (100,50) | |
| ! Define element type and mesh | |
| ET, 1, PLANE183 ! 2D structural element | |
| ESIZE, 5 ! Set element size | |
| AMESH, ALL ! Mesh the area | |
| ! Apply boundary conditions | |
| D, NODE(0, 0), ALL, 0 ! Fix the bottom-left corner | |
| D, NODE(100, 0), UY, 0 ! Restrict vertical movement at bottom-right | |
| ! Apply loads | |
| F, NODE(50, 50), FX, 1000 ! Apply a horizontal load at the top-middle node | |
| ! Solve | |
| FINISH ! Exit preprocessor | |
| /SOLU ! Enter solution processor | |
| ANTYPE, 0 ! Static analysis | |
| SOLVE ! Solve the system | |
| FINISH ! Exit solution processor | |
| /POST1 ! Enter post-processor | |
| /SHOW, JPEG ! Set output format to JPEG | |
| /DEVICE, HARD, JPEG ! Configure device for image output | |
| PLNSOL, U, SUM ! Plot nodal displacement (2D view) | |
| FILE, '2D_View', 'JPEG' ! Save 2D view as an image | |
| /SHOW, CLOSE ! Close the graphical window | |
| /VIEW, 1, 1, 1, 1 ! Set 3D view (isometric) | |
| /SHOW, JPEG ! Set output format to JPEG | |
| /DEVICE, HARD, JPEG ! Configure device for image output | |
| PLNSOL, S, EQV ! Plot equivalent stress (3D view) | |
| FILE, '3D_View', 'JPEG' ! Save 3D view as an image | |
| /SHOW, CLOSE ! Close the graphical window | |
| FINISH | |
| """ | |
| def upload_step_file(file): | |
| """Handle STEP file upload.""" | |
| return f"STEP file '{file.name}' uploaded successfully." | |
| def modify_apdl_script(modified_script): | |
| """Modify and save the APDL script.""" | |
| global apdl_script | |
| apdl_script = modified_script | |
| return "APDL script updated successfully." | |
| def execute_script(): | |
| """Execute the APDL script using ANSYS.""" | |
| global apdl_script | |
| # Set the working directory | |
| cwd = os.getcwd() | |
| apdl_script_with_cwd = apdl_script.format(cwd=cwd) | |
| # Save the APDL script to a file | |
| script_file = "script.dat" | |
| with open(script_file, "w") as f: | |
| f.write(apdl_script_with_cwd) | |
| # Define ANSYS execution command | |
| ansys_command = [ | |
| "C:\\Program Files\\ANSYS Inc\\v211\\ansys\\bin\\winx64\\ansys.exe", # Replace with your actual path | |
| "-b", | |
| "-i", script_file, | |
| "-o", "output.log" | |
| ] | |
| try: | |
| # Run the ANSYS command | |
| subprocess.run(ansys_command, check=True) | |
| # Check for image outputs | |
| two_d_image = os.path.join(cwd, "2D_View.jpg") | |
| three_d_image = os.path.join(cwd, "3D_View.jpg") | |
| if os.path.exists(two_d_image) and os.path.exists(three_d_image): | |
| return "APDL script executed successfully. 2D and 3D views generated." | |
| else: | |
| with open("output.log", "r") as log_file: | |
| log_contents = log_file.read() | |
| return f"APDL script executed, but images not found. Check log:\n{log_contents}" | |
| except subprocess.CalledProcessError as e: | |
| return f"Error executing APDL script: {str(e)}. Check output.log for details." | |
| except FileNotFoundError: | |
| return "ANSYS executable not found. Verify the path in ansys_command." | |
| def get_generated_images(): | |
| """Return the generated 2D and 3D view images if they exist.""" | |
| two_d_image = os.path.join(os.getcwd(), "2D_View.jpg") | |
| three_d_image = os.path.join(os.getcwd(), "3D_View.jpg") | |
| if os.path.exists(two_d_image) and os.path.exists(three_d_image): | |
| return two_d_image, three_d_image | |
| else: | |
| return None, None | |
| def read_output_log(): | |
| """Read and return the contents of output.log.""" | |
| if os.path.exists("output.log"): | |
| with open("output.log", "r") as f: | |
| return f.read() | |
| else: | |
| return "No output log found." | |
| # Gradio interface | |
| with gr.Blocks() as app: | |
| gr.Markdown("# ANSYS APDL Script for 2D and 3D Views") | |
| with gr.Row(): | |
| step_file = gr.File(label="Upload STEP File") | |
| step_output = gr.Text(label="Upload Status") | |
| step_file.upload(upload_step_file, inputs=step_file, outputs=step_output) | |
| with gr.Row(): | |
| apdl_view = gr.Textbox(apdl_script, lines=20, label="APDL Script") | |
| modify_button = gr.Button("Save Changes") | |
| modify_button.click(modify_apdl_script, inputs=apdl_view, outputs=step_output) | |
| with gr.Row(): | |
| execute_button = gr.Button("Run Script") | |
| execution_output = gr.Text(label="Execution Status") | |
| log_output = gr.Textbox(label="Output Log", lines=10, interactive=False) | |
| execute_button.click(execute_script, outputs=[execution_output]) | |
| execute_button.click(read_output_log, outputs=log_output) | |
| with gr.Row(): | |
| gr.Markdown("## Generated Images") | |
| view_2d = gr.Image(label="2D View", interactive=False) | |
| view_3d = gr.Image(label="3D View", interactive=False) | |
| refresh_button = gr.Button("Refresh Images") | |
| refresh_button.click(get_generated_images, outputs=[view_2d, view_3d]) | |
| app.launch() | |