jithenderchoudary commited on
Commit
47ff52d
·
verified ·
1 Parent(s): 89c646b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -81
app.py CHANGED
@@ -1,104 +1,140 @@
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
- # Open STEP file
9
- doc = FreeCAD.newDocument("MyDocument")
10
- shape = Part.Shape()
11
- shape.read("{step_file}")
12
- part = doc.addObject("Part::Feature", "ImportedPart")
13
- part.Shape = shape
14
- # Save as a new STEP file
15
- doc.recompute()
16
- doc.saveAs("{output_file}")
17
- print(f"STEP file processed and saved to {output_file}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  """
19
 
20
- # Function to handle STEP file upload
21
  def upload_step_file(file):
22
- try:
23
- file_path = os.path.join(os.getcwd(), file.name)
24
- with open(file_path, "wb") as f:
25
- f.write(file.read())
26
- return f"STEP file '{file.name}' uploaded successfully.", file_path
27
- except Exception as e:
28
- return f"Error uploading STEP file: {e}", None
29
-
30
- # Function to modify and save FreeCAD script
31
- def modify_script(script_text):
32
- try:
33
- global freecad_script
34
- freecad_script = script_text
35
- script_file = "modified_script.py"
36
- with open(script_file, "w") as f:
37
- f.write(freecad_script)
38
- return "FreeCAD script updated successfully.", script_file
39
- except Exception as e:
40
- return f"Error modifying script: {e}", None
41
-
42
- # Function to execute the FreeCAD script
43
- def execute_freecad_script(step_file):
44
- global freecad_script
45
- script_path = "temp_script.py"
46
- output_file = "output_processed.step"
 
 
 
 
47
 
48
  try:
49
- # Fill in file paths in the script
50
- script_with_paths = freecad_script.format(
51
- step_file=step_file, output_file=output_file
52
- )
53
- with open(script_path, "w") as f:
54
- f.write(script_with_paths)
55
-
56
- # Find FreeCAD executable path
57
- freecad_path = os.environ.get(
58
- "FREECAD_EXEC", "C:\\Program Files\\FreeCAD 1.0\\bin\\FreeCADCmd.exe"
59
- )
60
-
61
- # Run FreeCAD
62
- subprocess.run([freecad_path, script_path], check=True)
63
-
64
- # Check if output file was created
65
- if os.path.exists(output_file):
66
- return f"Script executed successfully. Processed STEP file saved at {output_file}.", output_file
67
  else:
68
- return "Script executed, but no output file found.", None
 
 
 
69
  except subprocess.CalledProcessError as e:
70
- return f"Error executing FreeCAD script: {e}", None
71
  except FileNotFoundError:
72
- return "FreeCAD executable not found. Verify the installation path.", None
73
- finally:
74
- # Clean up temporary script
75
- if os.path.exists(script_path):
76
- os.remove(script_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  # Gradio interface
79
  with gr.Blocks() as app:
80
- gr.Markdown("# FreeCAD Script Processor")
81
 
82
- # File upload
83
  with gr.Row():
84
  step_file = gr.File(label="Upload STEP File")
85
- upload_status = gr.Text(label="Upload Status")
86
- step_path = gr.Text(label="Uploaded File Path (Internal Use)", visible=False)
87
- step_file.upload(upload_step_file, inputs=step_file, outputs=[upload_status, step_path])
 
 
 
 
88
 
89
- # Script modification
90
  with gr.Row():
91
- script_view = gr.Textbox(freecad_script, lines=20, label="FreeCAD Script")
92
- save_button = gr.Button("Save Script")
93
- script_status = gr.Text(label="Script Status")
94
- modified_script = gr.File(label="Download Modified Script")
95
- save_button.click(modify_script, inputs=script_view, outputs=[script_status, modified_script])
96
 
97
- # Script execution
98
  with gr.Row():
99
- execute_button = gr.Button("Run FreeCAD Script")
100
- execution_status = gr.Text(label="Execution Status")
101
- output_file = gr.File(label="Download Processed STEP File")
102
- execute_button.click(execute_freecad_script, inputs=step_path, outputs=[execution_status, output_file])
 
103
 
104
  app.launch()
 
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()