apis / app.py
karthikmn's picture
Update app.py
acfa89d verified
import gradio as gr
from ansys.mapdl.core import launch_mapdl
# Mock CAD generation
def generate_cad_model(dimensions):
"""
Generates a mock CAD model. Use a placeholder for environments without FreeCAD.
"""
file_path = "/mnt/data/example_model.step"
with open(file_path, 'w') as f:
f.write(f"Example CAD model with dimensions: {dimensions}")
return file_path
# ANSYS Simulation Function
def run_simulation(cad_file):
"""
Runs a simulation using ANSYS MAPDL with the provided CAD file.
"""
try:
mapdl = launch_mapdl()
mapdl.prep7()
mapdl.et(1, "solid185")
mapdl.mp("EX", 1, 210e3)
mapdl.mp("NUXY", 1, 0.3)
mapdl.mp("DENS", 1, 7.85e-9)
mapdl.n(1, 0, 0, 0)
mapdl.n(2, 100, 0, 0)
mapdl.n(3, 0, 50, 0)
mapdl.e(1, 2, 3)
mapdl.finish()
mapdl.solve()
results = mapdl.post_processing.nodal_solution
mapdl.exit()
return f"Simulation completed successfully. Results: {results}"
except Exception as e:
return f"Error running simulation: {e}"
# Gradio Interface
def automate_workflow(dimensions, material, tolerances):
# Step 1: Generate CAD model
cad_file = generate_cad_model(dimensions)
# Step 2: Run simulation
simulation_results = run_simulation(cad_file)
return simulation_results, cad_file
interface = gr.Interface(
fn=automate_workflow,
inputs=[
gr.Textbox(label="Dimensions (e.g., 100,50,30 mm)"),
gr.Textbox(label="Material (e.g., Steel)"),
gr.Textbox(label="Tolerances (e.g., ±0.05 mm)")
],
outputs=["text", "file"],
title="ANSYS Simulation Workflow"
)
if __name__ == "__main__":
interface.launch()