jithenderchoudary commited on
Commit
b697dc3
·
verified ·
1 Parent(s): 5293b90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -114
app.py CHANGED
@@ -1,140 +1,92 @@
1
  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
- /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
12
- ! Define geometry (e.g., a rectangular plate)
13
- RECTNG, 0, 100, 0, 50 ! Rectangle from (0,0) to (100,50)
14
- ! Define element type and mesh
15
- ET, 1, PLANE183 ! 2D structural element
16
- ESIZE, 5 ! Set element size
17
- AMESH, ALL ! Mesh the area
18
- ! Apply boundary conditions
19
- D, NODE(0, 0), ALL, 0 ! Fix the bottom-left corner
20
- D, NODE(100, 0), UY, 0 ! Restrict vertical movement at bottom-right
21
- ! Apply loads
22
- F, NODE(50, 50), FX, 1000 ! Apply a horizontal load at the top-middle node
23
- ! Solve
24
- FINISH ! Exit preprocessor
25
- /SOLU ! Enter solution processor
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"
 
 
 
 
 
 
 
 
 
 
 
 
73
  ]
74
 
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
-
104
- def read_output_log():
105
- """Read and return the contents of output.log."""
106
- if os.path.exists("output.log"):
107
- with open("output.log", "r") as f:
108
- return f.read()
109
- else:
110
- return "No output log found."
111
 
112
  # Gradio interface
113
  with gr.Blocks() as app:
114
- gr.Markdown("# ANSYS APDL Script for 2D and 3D Views")
115
 
116
  with gr.Row():
117
  step_file = gr.File(label="Upload STEP File")
118
- step_output = gr.Text(label="Upload Status")
119
- step_file.upload(upload_step_file, inputs=step_file, outputs=step_output)
 
120
 
121
  with gr.Row():
122
- apdl_view = gr.Textbox(apdl_script, lines=20, label="APDL Script")
123
- modify_button = gr.Button("Save Changes")
124
- modify_button.click(modify_apdl_script, inputs=apdl_view, outputs=step_output)
 
 
125
 
126
  with gr.Row():
127
- execute_button = gr.Button("Run Script")
128
- execution_output = gr.Text(label="Execution Status")
129
- log_output = gr.Textbox(label="Output Log", lines=10, interactive=False)
130
- execute_button.click(execute_script, outputs=[execution_output])
131
- execute_button.click(read_output_log, outputs=log_output)
132
-
133
- with gr.Row():
134
- gr.Markdown("## Generated Images")
135
- view_2d = gr.Image(label="2D View", interactive=False)
136
- view_3d = gr.Image(label="3D View", interactive=False)
137
- refresh_button = gr.Button("Refresh Images")
138
- refresh_button.click(get_generated_images, outputs=[view_2d, view_3d])
139
 
140
  app.launch()
 
 
1
  import gradio as gr
 
2
  import os
3
+ import subprocess
4
 
5
+ # Default script template for FreeCAD
6
+ freecad_script = """import FreeCAD
7
+ import Part
8
+
9
+ # Open STEP file
10
+ doc = FreeCAD.newDocument("MyDocument")
11
+ shape = Part.Shape()
12
+ shape.read("{step_file}")
13
+ part = doc.addObject("Part::Feature", "ImportedPart")
14
+ part.Shape = shape
15
+
16
+ # Save as a new STEP file
17
+ doc.recompute()
18
+ doc.saveAs("{output_file}")
19
+ print(f"STEP file processed and saved to {output_file}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  """
21
 
22
  def upload_step_file(file):
23
  """Handle STEP file upload."""
24
+ file_path = os.path.join(os.getcwd(), file.name)
25
+ with open(file_path, "wb") as f:
26
+ f.write(file.read())
27
+ return f"STEP file '{file.name}' uploaded successfully.", file_path
28
+
29
+ def modify_script(script_text):
30
+ """Modify and save the FreeCAD script."""
31
+ global freecad_script
32
+ freecad_script = script_text
33
+ script_file = "modified_script.py"
 
 
 
 
 
 
 
 
34
  with open(script_file, "w") as f:
35
+ f.write(freecad_script)
36
+ return "FreeCAD script updated successfully.", script_file
37
+
38
+ def execute_freecad_script(step_file):
39
+ """Execute the FreeCAD script."""
40
+ global freecad_script
41
+ script_path = "temp_script.py"
42
+ output_file = "output_processed.step"
43
+
44
+ # Fill the script with file paths
45
+ script_with_paths = freecad_script.format(
46
+ step_file=step_file, output_file=output_file
47
+ )
48
+ with open(script_path, "w") as f:
49
+ f.write(script_with_paths)
50
+
51
+ # Command to run FreeCAD
52
+ freecad_command = [
53
+ "C:\\Program Files\\FreeCAD 1.0\\bin\\FreeCADCmd.exe",
54
+ script_path,
55
  ]
56
 
57
  try:
58
+ subprocess.run(freecad_command, check=True)
59
+ if os.path.exists(output_file):
60
+ return f"Script executed successfully. Processed STEP file saved at {output_file}.", output_file
 
 
 
 
 
 
61
  else:
62
+ return "Script executed, but no output file found.", None
 
 
 
63
  except subprocess.CalledProcessError as e:
64
+ return f"Error executing FreeCAD script: {e}", None
65
  except FileNotFoundError:
66
+ return "FreeCAD executable not found. Verify the path in freecad_command.", None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  # Gradio interface
69
  with gr.Blocks() as app:
70
+ gr.Markdown("# FreeCAD Script Processor")
71
 
72
  with gr.Row():
73
  step_file = gr.File(label="Upload STEP File")
74
+ upload_status = gr.Text(label="Upload Status")
75
+ step_path = gr.Text(label="Uploaded File Path (Internal Use)", visible=False)
76
+ step_file.upload(upload_step_file, inputs=step_file, outputs=[upload_status, step_path])
77
 
78
  with gr.Row():
79
+ script_view = gr.Textbox(freecad_script, lines=20, label="FreeCAD Script")
80
+ save_button = gr.Button("Save Script")
81
+ script_status = gr.Text(label="Script Status")
82
+ modified_script = gr.File(label="Download Modified Script")
83
+ save_button.click(modify_script, inputs=script_view, outputs=[script_status, modified_script])
84
 
85
  with gr.Row():
86
+ execute_button = gr.Button("Run FreeCAD Script")
87
+ execution_status = gr.Text(label="Execution Status")
88
+ output_file = gr.File(label="Download Processed STEP File")
89
+ execute_button.click(execute_freecad_script, inputs=step_path, outputs=[execution_status, output_file])
 
 
 
 
 
 
 
 
90
 
91
  app.launch()
92
+