File size: 5,243 Bytes
4c12729
b697dc3
47ff52d
4c12729
47ff52d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
609c6e8
 
5293b90
47ff52d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b697dc3
89c646b
47ff52d
 
 
 
 
 
 
 
 
5293b90
47ff52d
 
 
 
5293b90
47ff52d
5293b90
47ff52d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5293b90
 
4c12729
47ff52d
5293b90
4c12729
 
47ff52d
 
 
 
 
 
 
4c12729
609c6e8
47ff52d
 
 
 
 
4c12729
609c6e8
47ff52d
 
 
 
 
c65ef55
609c6e8
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
129
130
131
132
133
134
135
136
137
138
139
140
141
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()