jithenderchoudary commited on
Commit
bd7585f
·
verified ·
1 Parent(s): b475668

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -104
app.py CHANGED
@@ -2,127 +2,72 @@ import gradio as gr
2
  import subprocess
3
  import os
4
 
5
- # APDL Script for ANSYS
6
- apdl_script = """/CLEAR ! Clear any existing database
7
- /PREP7 ! Enter preprocessor
8
- ! Define material properties
9
- MP, EX, 1, 210E3 ! Young's Modulus (e.g., Steel)
10
- MP, PRXY, 1, 0.3 ! Poisson's Ratio
11
- ! Define geometry (e.g., a rectangular plate)
12
- RECTNG, 0, 100, 0, 50 ! Rectangle from (0,0) to (100,50)
13
- ! Define element type and mesh
14
- ET, 1, PLANE183 ! 2D structural element
15
- ESIZE, 5 ! Set element size
16
- AMESH, ALL ! Mesh the area
17
- ! Apply boundary conditions
18
- D, NODE(0, 0), ALL, 0 ! Fix the bottom-left corner
19
- D, NODE(100, 0), UY, 0 ! Restrict vertical movement at bottom-right
20
- ! Apply loads
21
- F, NODE(50, 50), FX, 1000 ! Apply a horizontal load at the top-middle node
22
- ! Solve
23
- FINISH ! Exit preprocessor
24
- /SOLU ! Enter solution processor
25
- ANTYPE, 0 ! Static analysis
26
- SOLVE ! Solve the system
27
- FINISH ! Exit solution processor
28
- ! Post-processing for 2D view
29
- /POST1 ! Enter post-processor
30
- PLNSOL, U, SUM ! Plot nodal displacement (2D view)
31
- /IMAGE, SAVE, '2D_View', JPG ! Save 2D view as an image
32
- ! Post-processing for 3D view
33
- /VIEW, 1, 1, 1, 1 ! Set 3D view (isometric)
34
- /DV3D ! Enable dynamic 3D view
35
- PLNSOL, S, EQV ! Plot equivalent stress (3D view)
36
- /IMAGE, SAVE, '3D_View', JPG ! Save 3D view as an image
37
- ! Exit ANSYS
38
- FINISH
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", # Replace with actual path
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 "APDL script executed successfully. 2D and 3D views generated."
75
  else:
76
- return "APDL script executed, but 2D/3D images are missing. Check output.log for details."
77
-
78
- except subprocess.CalledProcessError as e:
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 APDL Script for 2D and 3D Views")
102
 
103
  with gr.Row():
104
  step_file = gr.File(label="Upload STEP File")
105
- step_output = gr.Text(label="Upload Status")
106
- step_file.upload(upload_step_file, inputs=step_file, outputs=step_output)
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
- execute_button = gr.Button("Run Script")
115
- execution_output = gr.Text(label="Execution Status")
116
- log_output = gr.Textbox(label="Output Log", lines=10, interactive=False)
117
- execute_button.click(execute_script, outputs=[execution_output])
118
- execute_button.click(read_output_log, outputs=log_output)
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()