karthikmn's picture
Update app.py
71602b2 verified
from ansys.mapdl.core import launch_mapdl
import gradio as gr
# Function to generate die design using ANSYS MAPDL
def generate_die_ANSYS(length, width, thickness, die_shape):
try:
if length <= 0 or width <= 0 or thickness <= 0:
return "Dimensions must be greater than zero."
# Launch ANSYS MAPDL session (without interactive argument)
mapdl = launch_mapdl() # Start MAPDL session
mapdl.clear() # Clear any previous settings
# Preprocessing
mapdl.prep7()
# Die shape logic
if die_shape == "Rectangle":
mapdl.block(0, length, 0, width, 0, thickness) # Create a rectangular block
elif die_shape == "Circle":
mapdl.cylind(0, 0, width / 2, 0, thickness) # Create a cylindrical block
# Save the geometry as a STEP file
filename = f"generated_die_{die_shape}_{length}x{width}x{thickness}.step"
mapdl.save_as(filename) # Save the design as STEP file
mapdl.exit() # Exit the MAPDL session
# Return success message with the file path
return f"Die design saved successfully as {filename}. You can download the file."
except Exception as e:
return f"Error generating die design: {str(e)}"
# Gradio interface functions for Progressive Die Design
def progressive_die_interface(length, width, thickness, die_shape):
return generate_die_ANSYS(length, width, thickness, die_shape)
# Create the Gradio Interface
with gr.Blocks() as app:
gr.Markdown("## Progressive Die Design Tool with ANSYS")
gr.Markdown("Select the dimensions and shape for the progressive die below:")
# Tab for Progressive Die Design
with gr.Tab("Progressive Die Design"):
gr.Markdown("### Enter Dimensions and Shape for Progressive Die")
length = gr.Number(label="Length (mm)", value=100)
width = gr.Number(label="Width (mm)", value=50)
thickness = gr.Number(label="Thickness (mm)", value=10)
die_shape = gr.Dropdown(label="Die Shape", choices=["Rectangle", "Circle"], value="Rectangle")
die_output = gr.Textbox(label="Output", interactive=False)
die_button = gr.Button("Generate Die")
die_button.click(progressive_die_interface, inputs=[length, width, thickness, die_shape], outputs=die_output)
# Launch the app
app.launch()