from fastapi import FastAPI, File, UploadFile import shutil import os from utils.cad_utils import convert_cad_to_step from utils.ansys_utils import run_simulation from utils.gcode_utils import generate_gcode app = FastAPI() # Define a temporary directory to save files TEMP_DIR = "/tmp/cad_files" os.makedirs(TEMP_DIR, exist_ok=True) @app.post("/automate_workflow/") async def automate_workflow(cad_file: UploadFile = File(...)): """ Automates the workflow: 1. Convert CAD file to STEP format. 2. Run simulation in ANSYS. 3. Generate G-Code for CNC. """ # Save uploaded CAD file to temporary directory file_location = os.path.join(TEMP_DIR, cad_file.filename) with open(file_location, "wb") as f: shutil.copyfileobj(cad_file.file, f) # Step 1: Convert CAD to STEP format try: step_file = convert_cad_to_step(file_location) except Exception as e: return {"error": f"Error converting CAD to STEP: {str(e)}"} # Step 2: Run simulation in ANSYS try: simulation_results = run_simulation(step_file) except Exception as e: return {"error": f"Error running ANSYS simulation: {str(e)}"} # Step 3: Generate G-Code for CNC try: gcode_file = generate_gcode(step_file) except Exception as e: return {"error": f"Error generating G-Code: {str(e)}"} return { "simulation_results": simulation_results, "gcode_file": gcode_file }