Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,91 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 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:
|