Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,2 +1,107 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
| 1 |
+
import gradio as gr
|
| 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 |
+
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
|
| 27 |
+
mat = doc.addObject("App::MaterialObjectPython", "Material")
|
| 28 |
+
mat.Material = {
|
| 29 |
+
"Name": "Steel",
|
| 30 |
+
"YoungsModulus": 210e9, # Pa
|
| 31 |
+
"PoissonRatio": 0.3,
|
| 32 |
+
}
|
| 33 |
+
doc.addObject("Fem::ConstraintMaterial", "MaterialConstraint").References = [(obj, "Face1")]
|
| 34 |
+
doc.recompute()
|
| 35 |
+
|
| 36 |
+
# Create a mesh
|
| 37 |
+
mesh = doc.addObject("Fem::FemMeshShapeNetgenObject", "FEMMesh")
|
| 38 |
+
mesh.Shape = obj.Shape
|
| 39 |
+
mesh.MaxSize = 5
|
| 40 |
+
doc.recompute()
|
| 41 |
+
|
| 42 |
+
# Apply boundary conditions
|
| 43 |
+
fixed_constraint = doc.addObject("Fem::ConstraintFixed", "FixedConstraint")
|
| 44 |
+
fixed_constraint.References = [(obj, "Face1")]
|
| 45 |
+
doc.recompute()
|
| 46 |
+
|
| 47 |
+
# Apply a force
|
| 48 |
+
force_constraint = doc.addObject("Fem::ConstraintForce", "ForceConstraint")
|
| 49 |
+
force_constraint.References = [(obj, "Face2")]
|
| 50 |
+
force_constraint.Force = 1000 # N
|
| 51 |
+
doc.recompute()
|
| 52 |
+
|
| 53 |
+
# Add solver
|
| 54 |
+
solver = doc.addObject("Fem::SolverCalculixCcxTools", "Solver")
|
| 55 |
+
solver.InputFile = os.path.join(output_dir, "input.inp")
|
| 56 |
+
solver.WorkingDir = output_dir
|
| 57 |
+
doc.recompute()
|
| 58 |
+
|
| 59 |
+
# Solve
|
| 60 |
+
solver.write_input_file()
|
| 61 |
+
solver.run()
|
| 62 |
+
|
| 63 |
+
# Save results
|
| 64 |
+
results = solver.load_results()
|
| 65 |
+
results_obj = doc.addObject("Fem::FemResultObject", "Results")
|
| 66 |
+
results_obj.Mesh = results.Mesh
|
| 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 |
+
|
| 74 |
+
Gui.ActiveDocument.ActiveView.saveImage(two_d_image, 1024, 768, "White")
|
| 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 |
|