jithenderchoudary commited on
Commit
609c6e8
·
verified ·
1 Parent(s): 283ebf7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -27
app.py CHANGED
@@ -2,25 +2,52 @@ import gradio as gr
2
  import sys
3
  import os
4
 
5
-
6
- # Update with the correct paths for your system
7
- sys.path.append(r"C:\Program Files\FreeCAD 1.0\bin") # FreeCAD bin path
8
- sys.path.append(r"C:\Program Files\FreeCAD 1.0\lib") # FreeCAD lib path
9
 
10
  try:
11
- import FreeCAD
12
- print("FreeCAD module imported successfully.")
 
13
  except ImportError as e:
14
- print(f"Error importing FreeCAD module: {e}")
15
-
16
-
17
- def run_fea(step_file, output_dir):
18
- """Run FEA using FreeCAD."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  doc = FreeCAD.newDocument("FEM_Analysis")
20
 
21
  # Import STEP file
22
  obj = doc.addObject("Part::Feature", "ImportedPart")
23
- obj.Shape = FreeCAD.importShape(step_file)
24
  doc.recompute()
25
 
26
  # Assign material properties
@@ -67,7 +94,7 @@ def run_fea(step_file, output_dir):
67
  results_obj.NodeCount = len(results.Mesh.Nodes)
68
  doc.recompute()
69
 
70
- # Save views as images
71
  two_d_image = os.path.join(output_dir, "2D_View.png")
72
  three_d_image = os.path.join(output_dir, "3D_View.png")
73
 
@@ -75,33 +102,47 @@ def run_fea(step_file, output_dir):
75
  Gui.ActiveDocument.ActiveView.viewIsometric()
76
  Gui.ActiveDocument.ActiveView.saveImage(three_d_image, 1024, 768, "White")
77
 
78
- doc.saveAs(os.path.join(output_dir, "FEM_Analysis.FCStd"))
 
 
 
79
 
80
- return two_d_image, three_d_image
81
 
82
- def fea_workflow(file):
83
- """Gradio wrapper for FEA."""
 
84
  output_dir = "output"
85
  os.makedirs(output_dir, exist_ok=True)
86
- step_file = file.name
87
 
88
  try:
89
- two_d_image, three_d_image = run_fea(step_file, output_dir)
90
- return two_d_image, three_d_image
 
 
 
 
 
91
  except Exception as e:
92
- return f"Error during FEA: {e}", None
93
 
94
  # Gradio Interface
95
  with gr.Blocks() as app:
96
- gr.Markdown("# FreeCAD FEA for 2D and 3D Views")
97
-
98
  with gr.Row():
99
  step_file = gr.File(label="Upload STEP File")
100
- view_2d = gr.Image(label="2D View")
101
- view_3d = gr.Image(label="3D View")
102
 
103
- step_file.upload(fea_workflow, inputs=step_file, outputs=[view_2d, view_3d])
 
104
 
105
- app.launch()
 
 
 
 
106
 
 
107
 
 
 
2
  import sys
3
  import os
4
 
5
+ # Add FreeCAD library paths
6
+ sys.path.append(r"C:\Program Files\FreeCAD 1.0\bin") # Update with your FreeCAD bin path
7
+ sys.path.append(r"C:\Program Files\FreeCAD 1.0\lib") # Update with your FreeCAD lib path
 
8
 
9
  try:
10
+ import FreeCAD, Fem, FemGui
11
+ from FreeCAD import Gui
12
+ print("FreeCAD modules imported successfully.")
13
  except ImportError as e:
14
+ print(f"Error importing FreeCAD modules: {e}")
15
+ sys.exit(1)
16
+
17
+ # Default APDL Script
18
+ apdl_script = """
19
+ /CLEAR
20
+ /PREP7
21
+ MP, EX, 1, 210E3
22
+ MP, PRXY, 1, 0.3
23
+ RECTNG, 0, 100, 0, 50
24
+ ET, 1, PLANE183
25
+ ESIZE, 5
26
+ AMESH, ALL
27
+ D, NODE(0, 0), ALL, 0
28
+ D, NODE(100, 0), UY, 0
29
+ F, NODE(50, 50), FX, 1000
30
+ FINISH
31
+ /SOLU
32
+ ANTYPE, 0
33
+ SOLVE
34
+ FINISH
35
+ /POST1
36
+ PLNSOL, U, SUM
37
+ /IMAGE, SAVE, '2D_View', JPG
38
+ /VIEW, 1, 1, 1, 1
39
+ PLNSOL, S, EQV
40
+ /IMAGE, SAVE, '3D_View', JPG
41
+ FINISH
42
+ """
43
+
44
+ def run_fea_with_apdl(step_file, apdl_script, output_dir):
45
+ """Run FEA using FreeCAD and APDL script."""
46
  doc = FreeCAD.newDocument("FEM_Analysis")
47
 
48
  # Import STEP file
49
  obj = doc.addObject("Part::Feature", "ImportedPart")
50
+ obj.Shape = FreeCAD.importShape(step_file.name)
51
  doc.recompute()
52
 
53
  # Assign material properties
 
94
  results_obj.NodeCount = len(results.Mesh.Nodes)
95
  doc.recompute()
96
 
97
+ # Save 2D/3D views as images
98
  two_d_image = os.path.join(output_dir, "2D_View.png")
99
  three_d_image = os.path.join(output_dir, "3D_View.png")
100
 
 
102
  Gui.ActiveDocument.ActiveView.viewIsometric()
103
  Gui.ActiveDocument.ActiveView.saveImage(three_d_image, 1024, 768, "White")
104
 
105
+ # Save APDL Script to File
106
+ apdl_file = os.path.join(output_dir, "script.dat")
107
+ with open(apdl_file, "w") as f:
108
+ f.write(apdl_script)
109
 
110
+ return two_d_image, three_d_image, apdl_file
111
 
112
+ # Gradio Workflow
113
+ def fea_workflow(file, apdl_script):
114
+ """Gradio workflow for FEA with APDL integration."""
115
  output_dir = "output"
116
  os.makedirs(output_dir, exist_ok=True)
 
117
 
118
  try:
119
+ two_d_image, three_d_image, apdl_file = run_fea_with_apdl(file, apdl_script, output_dir)
120
+ return (
121
+ "Analysis completed successfully!",
122
+ two_d_image,
123
+ three_d_image,
124
+ f"APDL script saved to: {apdl_file}"
125
+ )
126
  except Exception as e:
127
+ return f"Error during FEA: {e}", None, None, None
128
 
129
  # Gradio Interface
130
  with gr.Blocks() as app:
131
+ gr.Markdown("# FreeCAD and APDL-Based FEA Workflow")
132
+
133
  with gr.Row():
134
  step_file = gr.File(label="Upload STEP File")
135
+ apdl_editor = gr.Textbox(value=apdl_script, label="APDL Script", lines=20)
 
136
 
137
+ with gr.Row():
138
+ execute_button = gr.Button("Submit")
139
 
140
+ with gr.Row():
141
+ status = gr.Text(label="Execution Status")
142
+ view_2d = gr.Image(label="2D View", interactive=False)
143
+ view_3d = gr.Image(label="3D View", interactive=False)
144
+ apdl_path = gr.Text(label="APDL Script Path")
145
 
146
+ execute_button.click(fea_workflow, inputs=[step_file, apdl_editor], outputs=[status, view_2d, view_3d, apdl_path])
147
 
148
+ app.launch()