File size: 4,602 Bytes
d3dade0
2964bff
 
 
f47e6ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3dade0
f47e6ef
 
 
 
 
 
 
 
 
 
 
 
e80a404
 
 
6a1d50d
 
 
 
 
e80a404
6a1d50d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2be39ae
e80a404
 
 
 
 
 
f47e6ef
ab9eaf0
 
 
 
 
 
 
 
f47e6ef
 
 
 
 
 
 
e80a404
f47e6ef
 
 
 
e80a404
f47e6ef
 
 
 
ab9eaf0
 
 
f47e6ef
e80a404
 
 
 
 
 
2964bff
f47e6ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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 = [
        "C:\\Program Files\\ANSYS Inc\\v211\\ansys.exe",  # Replace with actual path
        "-b",
        "-i", script_file,
        "-o", "output.log"
    ]

    try:
        # Run the ANSYS command
        subprocess.run(ansys_command, check=True)
        
        # Check if 2D and 3D images exist
        if os.path.exists("2D_View.jpg") and os.path.exists("3D_View.jpg"):
            return "APDL script executed successfully. 2D and 3D views generated."
        else:
            return "APDL script executed, but 2D/3D images are missing. Check output.log for details."
    
    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."""
    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

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()