jithenderchoudary commited on
Commit
6f734cd
·
verified ·
1 Parent(s): e488a20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -22
app.py CHANGED
@@ -5,6 +5,7 @@ import os
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
@@ -25,41 +26,47 @@ FINISH ! Exit preprocessor
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"
@@ -68,23 +75,29 @@ def execute_script():
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
 
 
5
  # APDL Script for ANSYS
6
  apdl_script = """/CLEAR ! Clear any existing database
7
  /PREP7 ! Enter preprocessor
8
+ /CWD, '{cwd}' ! Set the working directory
9
  ! Define material properties
10
  MP, EX, 1, 210E3 ! Young's Modulus (e.g., Steel)
11
  MP, PRXY, 1, 0.3 ! Poisson's Ratio
 
26
  ANTYPE, 0 ! Static analysis
27
  SOLVE ! Solve the system
28
  FINISH ! Exit solution processor
 
29
  /POST1 ! Enter post-processor
30
+ /SHOW, JPEG ! Set output format to JPEG
31
+ /DEVICE, HARD, JPEG ! Configure device for image output
32
+ PLNSOL, U, SUM ! Plot nodal displacement (2D view)
33
+ FILE, '2D_View', 'JPEG' ! Save 2D view as an image
34
+ /SHOW, CLOSE ! Close the graphical window
35
+ /VIEW, 1, 1, 1, 1 ! Set 3D view (isometric)
36
+ /SHOW, JPEG ! Set output format to JPEG
37
+ /DEVICE, HARD, JPEG ! Configure device for image output
38
+ PLNSOL, S, EQV ! Plot equivalent stress (3D view)
39
+ FILE, '3D_View', 'JPEG' ! Save 3D view as an image
40
+ /SHOW, CLOSE ! Close the graphical window
41
  FINISH
42
  """
43
 
44
  def upload_step_file(file):
45
+ """Handle STEP file upload."""
46
  return f"STEP file '{file.name}' uploaded successfully."
47
 
48
  def modify_apdl_script(modified_script):
49
+ """Modify and save the APDL script."""
50
  global apdl_script
51
  apdl_script = modified_script
52
  return "APDL script updated successfully."
53
 
54
  def execute_script():
55
+ """Execute the APDL script using ANSYS."""
56
  global apdl_script
57
 
58
+ # Set the working directory
59
+ cwd = os.getcwd()
60
+ apdl_script_with_cwd = apdl_script.format(cwd=cwd)
61
+
62
  # Save the APDL script to a file
63
  script_file = "script.dat"
64
  with open(script_file, "w") as f:
65
+ f.write(apdl_script_with_cwd)
66
 
67
  # Define ANSYS execution command
68
  ansys_command = [
69
+ "C:\\Program Files\\ANSYS Inc\\v211\\ansys\\bin\\winx64\\ansys.exe", # Replace with your actual path
70
  "-b",
71
  "-i", script_file,
72
  "-o", "output.log"
 
75
  try:
76
  # Run the ANSYS command
77
  subprocess.run(ansys_command, check=True)
78
+
79
+ # Check for image outputs
80
+ two_d_image = os.path.join(cwd, "2D_View.jpg")
81
+ three_d_image = os.path.join(cwd, "3D_View.jpg")
82
+
83
+ if os.path.exists(two_d_image) and os.path.exists(three_d_image):
84
  return "APDL script executed successfully. 2D and 3D views generated."
85
  else:
86
+ with open("output.log", "r") as log_file:
87
+ log_contents = log_file.read()
88
+ return f"APDL script executed, but images not found. Check log:\n{log_contents}"
89
+
90
  except subprocess.CalledProcessError as e:
91
  return f"Error executing APDL script: {str(e)}. Check output.log for details."
92
  except FileNotFoundError:
93
  return "ANSYS executable not found. Verify the path in ansys_command."
94
 
 
95
  def get_generated_images():
96
  """Return the generated 2D and 3D view images if they exist."""
97
+ two_d_image = os.path.join(os.getcwd(), "2D_View.jpg")
98
+ three_d_image = os.path.join(os.getcwd(), "3D_View.jpg")
99
+ if os.path.exists(two_d_image) and os.path.exists(three_d_image):
100
+ return two_d_image, three_d_image
101
  else:
102
  return None, None
103