Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ansys.mapdl.core import launch_mapdl
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Function to generate progressive die design using ANSYS MAPDL
|
| 6 |
+
def generate_progressive_die_ANSYS(length, width, thickness, num_stations, punch_diameter, punch_depth, die_shape):
|
| 7 |
+
try:
|
| 8 |
+
if length <= 0 or width <= 0 or thickness <= 0 or num_stations <= 0:
|
| 9 |
+
return "Dimensions and number of stations must be greater than zero."
|
| 10 |
+
|
| 11 |
+
# Launch ANSYS MAPDL session in non-interactive mode
|
| 12 |
+
mapdl = launch_mapdl(interactive=False)
|
| 13 |
+
mapdl.clear() # Clear any previous settings
|
| 14 |
+
|
| 15 |
+
# Preprocessing
|
| 16 |
+
mapdl.prep7()
|
| 17 |
+
|
| 18 |
+
# Die shape logic
|
| 19 |
+
if die_shape == "Rectangle":
|
| 20 |
+
mapdl.block(0, length, 0, width, 0, thickness) # Create a rectangular block (base die)
|
| 21 |
+
elif die_shape == "Circle":
|
| 22 |
+
mapdl.cylind(0, 0, width / 2, 0, thickness) # Create a cylindrical block (base die)
|
| 23 |
+
|
| 24 |
+
# Create punch shapes for each station
|
| 25 |
+
punch_spacing = length / (num_stations + 1) # Space punches evenly along length
|
| 26 |
+
|
| 27 |
+
for station in range(1, num_stations + 1):
|
| 28 |
+
punch_center_x = station * punch_spacing
|
| 29 |
+
punch_center_y = width / 2 # Center the punch along the width
|
| 30 |
+
|
| 31 |
+
# Create cylindrical punches (holes in the die)
|
| 32 |
+
mapdl.cylind(punch_center_x, punch_center_y, punch_diameter / 2, 0, punch_depth)
|
| 33 |
+
|
| 34 |
+
# Save the geometry as a STEP file
|
| 35 |
+
filename = f"progressive_die_{die_shape}_{length}x{width}x{thickness}_{num_stations}_stations.step"
|
| 36 |
+
mapdl.save_as(filename)
|
| 37 |
+
mapdl.exit() # Close the session
|
| 38 |
+
|
| 39 |
+
# Return success message with station info
|
| 40 |
+
return f"Progressive Die design saved successfully as {filename}. The die has {num_stations} punch stations."
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Error generating progressive die design: {str(e)}"
|
| 44 |
+
|
| 45 |
+
# Gradio interface function
|
| 46 |
+
def progressive_die_interface(length, width, thickness, num_stations, punch_diameter, punch_depth, die_shape):
|
| 47 |
+
return generate_progressive_die_ANSYS(length, width, thickness, num_stations, punch_diameter, punch_depth, die_shape)
|
| 48 |
+
|
| 49 |
+
# Create the Gradio Interface
|
| 50 |
+
with gr.Blocks() as app:
|
| 51 |
+
gr.Markdown("## Progressive Die Design with ANSYS MAPDL")
|
| 52 |
+
gr.Markdown("### Enter the parameters for the die design")
|
| 53 |
+
|
| 54 |
+
with gr.Tabs():
|
| 55 |
+
# Tab for Progressive Die Design
|
| 56 |
+
with gr.Tab("Progressive Die Design"):
|
| 57 |
+
gr.Markdown("### Enter Dimensions and Punch Parameters")
|
| 58 |
+
length = gr.Number(label="Die Length (mm)", value=500)
|
| 59 |
+
width = gr.Number(label="Die Width (mm)", value=200)
|
| 60 |
+
thickness = gr.Number(label="Die Thickness (mm)", value=50)
|
| 61 |
+
num_stations = gr.Number(label="Number of Stations", value=5)
|
| 62 |
+
punch_diameter = gr.Number(label="Punch Diameter (mm)", value=20)
|
| 63 |
+
punch_depth = gr.Number(label="Punch Depth (mm)", value=10)
|
| 64 |
+
die_shape = gr.Dropdown(label="Die Shape", choices=["Rectangle", "Circle"], value="Rectangle")
|
| 65 |
+
die_output = gr.Textbox(label="Output", interactive=False)
|
| 66 |
+
die_button = gr.Button("Generate Die")
|
| 67 |
+
die_button.click(progressive_die_interface, inputs=[length, width, thickness, num_stations, punch_diameter, punch_depth, die_shape], outputs=die_output)
|
| 68 |
+
|
| 69 |
+
# Launch the app
|
| 70 |
+
app.launch()
|
| 71 |
+
|
| 72 |
+
|