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 | |
| ! 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 | |
| ! Post-processing for 2D view | |
| /POST1 ! Enter post-processor | |
| PLNSOL, U, SUM ! Plot nodal displacement (2D view) | |
| /IMAGE, SAVE, '2D_View', JPG ! Save 2D view as an image | |
| ! Post-processing for 3D view | |
| /VIEW, 1, 1, 1, 1 ! Set 3D view (isometric) | |
| /DV3D ! Enable dynamic 3D view | |
| PLNSOL, S, EQV ! Plot equivalent stress (3D view) | |
| /IMAGE, SAVE, '3D_View', JPG ! Save 3D view as an image | |
| ! Exit ANSYS | |
| FINISH | |
| """ | |
| def upload_step_file(file): | |
| """Function to handle STEP file upload.""" | |
| return f"STEP file '{file.name}' uploaded successfully." | |
| def modify_apdl_script(modified_script): | |
| """Function to modify and save the APDL script.""" | |
| global apdl_script | |
| apdl_script = modified_script | |
| return "APDL script updated successfully." | |
| def execute_script(): | |
| """Function to execute the APDL script using ANSYS.""" | |
| global apdl_script | |
| # Save the APDL script to a file | |
| script_file = "script.dat" | |
| with open(script_file, "w") as f: | |
| f.write(apdl_script) | |
| # Define ANSYS execution command | |
| #ansys_command = [ | |
| # "ansys", # Replace with the full path to your ANSYS executable | |
| # "-b", # Batch mode | |
| # "-i", script_file, # Input script | |
| # "-o", "output.log" # Output log | |
| ansys_command = [ | |
| "C:\\Program Files\\ANSYS Inc\\v211\\ansys.exe", # Replace with actual path | |
| "-b", | |
| "-i", script_file, | |
| "-o", "output.log" | |
| ] | |
| ] | |
| try: | |
| # Run the command | |
| subprocess.run(ansys_command, check=True) | |
| return "APDL script executed. Check generated views (2D_View.jpg, 3D_View.jpg)." | |
| except subprocess.CalledProcessError as e: | |
| return f"Error executing APDL script: {e}" | |
| def get_generated_images(): | |
| """Return the generated 2D and 3D view images if they exist.""" | |
| if os.path.exists("2D_View.jpg") and os.path.exists("3D_View.jpg"): | |
| return "2D_View.jpg", "3D_View.jpg" | |
| else: | |
| return None, None | |
| # 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") | |
| execute_button.click(execute_script, outputs=execution_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() | |