Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import sys | |
| import os | |
| # Add FreeCAD library paths | |
| sys.path.append(r"C:\Program Files\FreeCAD 1.0\bin") # Update with your FreeCAD bin path | |
| sys.path.append(r"C:\Program Files\FreeCAD 1.0\lib") # Update with your FreeCAD lib path | |
| try: | |
| import FreeCAD, Fem, FemGui | |
| from FreeCAD import Gui | |
| print("FreeCAD modules imported successfully.") | |
| except ImportError as e: | |
| print(f"Error importing FreeCAD modules: {e}") | |
| sys.exit(1) | |
| def run_fea(step_file, output_dir): | |
| """Run FEA using FreeCAD.""" | |
| doc = FreeCAD.newDocument("FEM_Analysis") | |
| # Import STEP file | |
| obj = doc.addObject("Part::Feature", "ImportedPart") | |
| obj.Shape = FreeCAD.importShape(step_file) | |
| doc.recompute() | |
| # Assign material properties | |
| mat = doc.addObject("App::MaterialObjectPython", "Material") | |
| mat.Material = { | |
| "Name": "Steel", | |
| "YoungsModulus": 210e9, # Pa | |
| "PoissonRatio": 0.3, | |
| } | |
| doc.addObject("Fem::ConstraintMaterial", "MaterialConstraint").References = [(obj, "Face1")] | |
| doc.recompute() | |
| # Create a mesh | |
| mesh = doc.addObject("Fem::FemMeshShapeNetgenObject", "FEMMesh") | |
| mesh.Shape = obj.Shape | |
| mesh.MaxSize = 5 | |
| doc.recompute() | |
| # Apply boundary conditions | |
| fixed_constraint = doc.addObject("Fem::ConstraintFixed", "FixedConstraint") | |
| fixed_constraint.References = [(obj, "Face1")] | |
| doc.recompute() | |
| # Apply a force | |
| force_constraint = doc.addObject("Fem::ConstraintForce", "ForceConstraint") | |
| force_constraint.References = [(obj, "Face2")] | |
| force_constraint.Force = 1000 # N | |
| doc.recompute() | |
| # Add solver | |
| solver = doc.addObject("Fem::SolverCalculixCcxTools", "Solver") | |
| solver.InputFile = os.path.join(output_dir, "input.inp") | |
| solver.WorkingDir = output_dir | |
| doc.recompute() | |
| # Solve | |
| solver.write_input_file() | |
| solver.run() | |
| # Save results | |
| results = solver.load_results() | |
| results_obj = doc.addObject("Fem::FemResultObject", "Results") | |
| results_obj.Mesh = results.Mesh | |
| results_obj.NodeCount = len(results.Mesh.Nodes) | |
| doc.recompute() | |
| # Save views as images | |
| two_d_image = os.path.join(output_dir, "2D_View.png") | |
| three_d_image = os.path.join(output_dir, "3D_View.png") | |
| Gui.ActiveDocument.ActiveView.saveImage(two_d_image, 1024, 768, "White") | |
| Gui.ActiveDocument.ActiveView.viewIsometric() | |
| Gui.ActiveDocument.ActiveView.saveImage(three_d_image, 1024, 768, "White") | |
| doc.saveAs(os.path.join(output_dir, "FEM_Analysis.FCStd")) | |
| return two_d_image, three_d_image | |
| def fea_workflow(file): | |
| """Gradio wrapper for FEA.""" | |
| output_dir = "output" | |
| os.makedirs(output_dir, exist_ok=True) | |
| step_file = file.name | |
| try: | |
| two_d_image, three_d_image = run_fea(step_file, output_dir) | |
| return two_d_image, three_d_image | |
| except Exception as e: | |
| return f"Error during FEA: {e}", None | |
| # Gradio Interface | |
| with gr.Blocks() as app: | |
| gr.Markdown("# FreeCAD FEA for 2D and 3D Views") | |
| with gr.Row(): | |
| step_file = gr.File(label="Upload STEP File") | |
| view_2d = gr.Image(label="2D View") | |
| view_3d = gr.Image(label="3D View") | |
| step_file.upload(fea_workflow, inputs=step_file, outputs=[view_2d, view_3d]) | |
| app.launch() | |