jithenderchoudary commited on
Commit
623109e
·
verified ·
1 Parent(s): c65ef55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -120
app.py CHANGED
@@ -1,121 +1,2 @@
1
- import FreeCAD, Fem, FemGui
2
- from FreeCAD import Gui
3
- import sys
4
- import os
5
 
6
- # Add FreeCAD library paths
7
- sys.path.append(r"C:\Program Files\FreeCAD 1.0\bin") # Update with your FreeCAD bin path
8
- sys.path.append(r"C:\Program Files\FreeCAD 1.0\lib") # Update with your FreeCAD lib path
9
-
10
- try:
11
- import FreeCAD, Fem, FemGui
12
- from FreeCAD import Gui
13
- print("FreeCAD modules imported successfully.")
14
- except ImportError as e:
15
- print(f"Error importing FreeCAD modules: {e}")
16
- sys.exit(1)
17
-
18
- def run_fea(step_file, output_dir):
19
- """Run FEA using FreeCAD."""
20
- doc = FreeCAD.newDocument("FEM_Analysis")
21
-
22
- # Import STEP file
23
- obj = doc.addObject("Part::Feature", "ImportedPart")
24
- obj.Shape = FreeCAD.importShape(step_file)
25
- doc.recompute()
26
-
27
- # Assign material properties
28
- mat = doc.addObject("App::MaterialObjectPython", "Material")
29
- mat.Material = {
30
- "Name": "Steel",
31
- "YoungsModulus": 210e9, # Pa
32
- "PoissonRatio": 0.3,
33
- }
34
- doc.addObject("Fem::ConstraintMaterial", "MaterialConstraint").References = [(obj, "Face1")]
35
- doc.recompute()
36
-
37
- # Create a mesh
38
- mesh = doc.addObject("Fem::FemMeshShapeNetgenObject", "FEMMesh")
39
- mesh.Shape = obj.Shape
40
- mesh.MaxSize = 5
41
- doc.recompute()
42
-
43
- # Apply boundary conditions
44
- fixed_constraint = doc.addObject("Fem::ConstraintFixed", "FixedConstraint")
45
- fixed_constraint.References = [(obj, "Face1")]
46
- doc.recompute()
47
-
48
- # Apply a force
49
- force_constraint = doc.addObject("Fem::ConstraintForce", "ForceConstraint")
50
- force_constraint.References = [(obj, "Face2")]
51
- force_constraint.Force = 1000 # N
52
- doc.recompute()
53
-
54
- # Add solver
55
- solver = doc.addObject("Fem::SolverCalculixCcxTools", "Solver")
56
- solver.InputFile = os.path.join(output_dir, "input.inp")
57
- solver.WorkingDir = output_dir
58
- doc.recompute()
59
-
60
- # Solve
61
- solver.write_input_file()
62
- solver.run()
63
-
64
- # Save results
65
- results = solver.load_results()
66
- results_obj = doc.addObject("Fem::FemResultObject", "Results")
67
- results_obj.Mesh = results.Mesh
68
- results_obj.NodeCount = len(results.Mesh.Nodes)
69
- doc.recompute()
70
-
71
- # Save views as images
72
- Gui.ActiveDocument.ActiveView.saveImage(os.path.join(output_dir, "2D_View.png"), 1024, 768, "White")
73
- Gui.ActiveDocument.ActiveView.viewIsometric()
74
- Gui.ActiveDocument.ActiveView.saveImage(os.path.join(output_dir, "3D_View.png"), 1024, 768, "White")
75
-
76
- doc.saveAs(os.path.join(output_dir, "FEM_Analysis.FCStd"))
77
-
78
- print(f"Analysis completed successfully.")
79
- print(f"2D View saved to {output_dir}/2D_View.png")
80
- print(f"3D View saved to {output_dir}/3D_View.png")
81
-
82
- # Example usage
83
- if __name__ == "__main__":
84
- step_file = "path_to_your_step_file.stp" # Replace with your STEP file
85
- output_dir = "path_to_output_directory" # Replace with your output directory
86
-
87
- if not os.path.exists(output_dir):
88
- os.makedirs(output_dir)
89
-
90
- run_fea(step_file, output_dir)
91
-
92
-
93
- # Gradio interface
94
- with gr.Blocks() as app:
95
- gr.Markdown("# ANSYS APDL Script for 2D and 3D Views")
96
-
97
- with gr.Row():
98
- step_file = gr.File(label="Upload STEP File")
99
- step_output = gr.Text(label="Upload Status")
100
- step_file.upload(upload_step_file, inputs=step_file, outputs=step_output)
101
-
102
- with gr.Row():
103
- apdl_view = gr.Textbox(apdl_script, lines=20, label="APDL Script")
104
- modify_button = gr.Button("Save Changes")
105
- modify_button.click(modify_apdl_script, inputs=apdl_view, outputs=step_output)
106
-
107
- with gr.Row():
108
- execute_button = gr.Button("Run Script")
109
- execution_output = gr.Text(label="Execution Status")
110
- log_output = gr.Textbox(label="Output Log", lines=10, interactive=False)
111
- execute_button.click(execute_script, outputs=[execution_output])
112
- execute_button.click(read_output_log, outputs=log_output)
113
-
114
- with gr.Row():
115
- gr.Markdown("## Generated Images")
116
- view_2d = gr.Image(label="2D View", interactive=False)
117
- view_3d = gr.Image(label="3D View", interactive=False)
118
- refresh_button = gr.Button("Refresh Images")
119
- refresh_button.click(get_generated_images, outputs=[view_2d, view_3d])
120
-
121
- app.launch()
 
1
+ python fea_app.py
 
 
 
2