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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -108
app.py CHANGED
@@ -1,113 +1,91 @@
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:
 
1
+ import FreeCAD, Fem, FemGui
2
+ from FreeCAD import Gui
3
  import os
4
 
5
+ def run_fea(step_file, output_dir):
6
+ """
7
+ Automates FEA analysis in FreeCAD using the FEM Workbench.
8
+
9
+ Args:
10
+ step_file (str): Path to the STEP file.
11
+ output_dir (str): Directory to save output files (images and analysis data).
12
+ """
13
+ # Create a new FreeCAD document
14
+ doc = FreeCAD.newDocument("FEM_Analysis")
15
+
16
+ # Import STEP file
17
+ obj = doc.addObject("Part::Feature", "ImportedPart")
18
+ obj.Shape = FreeCAD.importShape(step_file)
19
+ doc.recompute()
20
+
21
+ # Assign material properties
22
+ mat = doc.addObject("App::MaterialObjectPython", "Material")
23
+ mat.Material = {
24
+ "Name": "Steel",
25
+ "YoungsModulus": 210e9, # Pa (N/m²)
26
+ "PoissonRatio": 0.3,
27
+ }
28
+ doc.addObject("Fem::ConstraintMaterial", "MaterialConstraint").References = [(obj, "Face1")] # Adjust face as needed
29
+ doc.recompute()
30
+
31
+ # Create and configure the mesh
32
+ mesh = doc.addObject("Fem::FemMeshShapeNetgenObject", "FEMMesh")
33
+ mesh.Shape = obj.Shape
34
+ mesh.MaxSize = 5 # Set mesh size (adjust as required)
35
+ doc.recompute()
36
+
37
+ # Apply boundary conditions
38
+ fixed_constraint = doc.addObject("Fem::ConstraintFixed", "FixedConstraint")
39
+ fixed_constraint.References = [(obj, "Face1")] # Adjust face as needed
40
+ doc.recompute()
41
+
42
+ # Apply force
43
+ force_constraint = doc.addObject("Fem::ConstraintForce", "ForceConstraint")
44
+ force_constraint.References = [(obj, "Face2")] # Adjust face as needed
45
+ force_constraint.Force = 1000 # N
46
+ doc.recompute()
47
+
48
+ # Add solver (CalculiX)
49
+ solver = doc.addObject("Fem::SolverCalculixCcxTools", "Solver")
50
+ solver.InputFile = os.path.join(output_dir, "input.inp")
51
+ solver.WorkingDir = output_dir
52
+ doc.recompute()
53
+
54
+ # Write input file and run the solver
55
+ solver.write_input_file()
56
+ solver.run()
57
+
58
+ # Load and process results
59
+ results = solver.load_results()
60
+ results_obj = doc.addObject("Fem::FemResultObject", "Results")
61
+ results_obj.Mesh = results.Mesh
62
+ results_obj.NodeCount = len(results.Mesh.Nodes)
63
+ doc.recompute()
64
+
65
+ # Save 2D and 3D views as images
66
+ two_d_image_path = os.path.join(output_dir, "2D_View.png")
67
+ three_d_image_path = os.path.join(output_dir, "3D_View.png")
68
+
69
+ Gui.ActiveDocument.ActiveView.saveImage(two_d_image_path, 1024, 768, "White")
70
+ Gui.ActiveDocument.ActiveView.viewIsometric()
71
+ Gui.ActiveDocument.ActiveView.saveImage(three_d_image_path, 1024, 768, "White")
72
+
73
+ # Save the FreeCAD project file
74
+ doc.saveAs(os.path.join(output_dir, "FEM_Analysis.FCStd"))
75
+
76
+ print(f"Analysis completed successfully.")
77
+ print(f"2D View saved to: {two_d_image_path}")
78
+ print(f"3D View saved to: {three_d_image_path}")
79
+
80
+ # Example Usage
81
+ if __name__ == "__main__":
82
+ step_file = "path_to_your_step_file.stp" # Replace with your STEP file path
83
+ output_dir = "path_to_output_directory" # Replace with your desired output directory
84
+
85
+ if not os.path.exists(output_dir):
86
+ os.makedirs(output_dir)
87
+
88
+ run_fea(step_file, output_dir)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  # Gradio interface
91
  with gr.Blocks() as app: