jithenderchoudary commited on
Commit
c65ef55
·
verified ·
1 Parent(s): 479542b

Update app.py

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