Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,11 +11,18 @@ import os
|
|
| 11 |
# Function for Progressive Die Design
|
| 12 |
def generate_die(length, width, thickness):
|
| 13 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
plate = cq.Workplane("XY").box(length, width, thickness)
|
| 15 |
punch = cq.Workplane("XY").rect(10, 10).extrude(5).translate((length / 4, width / 4, thickness / 2))
|
| 16 |
die = plate.cut(punch)
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
cq.exporters.export(die, filename)
|
|
|
|
| 19 |
return filename
|
| 20 |
except Exception as e:
|
| 21 |
return f"Error generating die: {str(e)}"
|
|
@@ -24,25 +31,32 @@ def generate_die(length, width, thickness):
|
|
| 24 |
# Function to visualize die in 3D
|
| 25 |
def visualize_die(length, width, thickness):
|
| 26 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
plate = cq.Workplane("XY").box(length, width, thickness)
|
| 28 |
punch = cq.Workplane("XY").rect(10, 10).extrude(5).translate((length / 4, width / 4, thickness / 2))
|
| 29 |
die = plate.cut(punch)
|
| 30 |
|
| 31 |
-
# Export
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
|
|
|
| 36 |
plotter = pv.Plotter(off_screen=True)
|
| 37 |
plotter.add_mesh(mesh, color="blue")
|
| 38 |
-
screenshot = "progressive_die_visualization.png"
|
| 39 |
plotter.screenshot(screenshot)
|
|
|
|
| 40 |
return screenshot
|
| 41 |
except Exception as e:
|
| 42 |
return f"Error visualizing die: {str(e)}"
|
| 43 |
|
| 44 |
|
| 45 |
-
# Stress Analysis
|
| 46 |
def stress_analysis(force, die_width, die_height, material_strength, temperature_change=50, alpha=1e-5, elastic_modulus=200000, fatigue_strength=150):
|
| 47 |
try:
|
| 48 |
# Mechanical stress
|
|
@@ -119,7 +133,7 @@ with gr.Blocks() as app:
|
|
| 119 |
length = gr.Number(label="Length (mm)", value=100)
|
| 120 |
width = gr.Number(label="Width (mm)", value=50)
|
| 121 |
thickness = gr.Number(label="Thickness (mm)", value=10)
|
| 122 |
-
die_output = gr.Textbox(label="
|
| 123 |
visualization_output = gr.Image(label="3D Visualization")
|
| 124 |
die_button = gr.Button("Generate Die")
|
| 125 |
die_button.click(
|
|
|
|
| 11 |
# Function for Progressive Die Design
|
| 12 |
def generate_die(length, width, thickness):
|
| 13 |
try:
|
| 14 |
+
output_dir = "outputs"
|
| 15 |
+
os.makedirs(output_dir, exist_ok=True) # Ensure output directory exists
|
| 16 |
+
|
| 17 |
+
# Create die geometry
|
| 18 |
plate = cq.Workplane("XY").box(length, width, thickness)
|
| 19 |
punch = cq.Workplane("XY").rect(10, 10).extrude(5).translate((length / 4, width / 4, thickness / 2))
|
| 20 |
die = plate.cut(punch)
|
| 21 |
+
|
| 22 |
+
# Export STEP file
|
| 23 |
+
filename = os.path.join(output_dir, "progressive_die.step")
|
| 24 |
cq.exporters.export(die, filename)
|
| 25 |
+
|
| 26 |
return filename
|
| 27 |
except Exception as e:
|
| 28 |
return f"Error generating die: {str(e)}"
|
|
|
|
| 31 |
# Function to visualize die in 3D
|
| 32 |
def visualize_die(length, width, thickness):
|
| 33 |
try:
|
| 34 |
+
output_dir = "outputs"
|
| 35 |
+
os.makedirs(output_dir, exist_ok=True) # Ensure output directory exists
|
| 36 |
+
|
| 37 |
+
# Create die geometry
|
| 38 |
plate = cq.Workplane("XY").box(length, width, thickness)
|
| 39 |
punch = cq.Workplane("XY").rect(10, 10).extrude(5).translate((length / 4, width / 4, thickness / 2))
|
| 40 |
die = plate.cut(punch)
|
| 41 |
|
| 42 |
+
# Export STL file for visualization
|
| 43 |
+
stl_file = os.path.join(output_dir, "progressive_die.stl")
|
| 44 |
+
cq.exporters.exportShape(die.val(), "STL", stl_file)
|
| 45 |
|
| 46 |
+
# Generate 3D visualization
|
| 47 |
+
pv.global_theme.off_screen = True # Ensure off-screen rendering
|
| 48 |
+
mesh = pv.read(stl_file)
|
| 49 |
plotter = pv.Plotter(off_screen=True)
|
| 50 |
plotter.add_mesh(mesh, color="blue")
|
| 51 |
+
screenshot = os.path.join(output_dir, "progressive_die_visualization.png")
|
| 52 |
plotter.screenshot(screenshot)
|
| 53 |
+
|
| 54 |
return screenshot
|
| 55 |
except Exception as e:
|
| 56 |
return f"Error visualizing die: {str(e)}"
|
| 57 |
|
| 58 |
|
| 59 |
+
# Function for Stress Analysis (including thermal stress and fatigue strength)
|
| 60 |
def stress_analysis(force, die_width, die_height, material_strength, temperature_change=50, alpha=1e-5, elastic_modulus=200000, fatigue_strength=150):
|
| 61 |
try:
|
| 62 |
# Mechanical stress
|
|
|
|
| 133 |
length = gr.Number(label="Length (mm)", value=100)
|
| 134 |
width = gr.Number(label="Width (mm)", value=50)
|
| 135 |
thickness = gr.Number(label="Thickness (mm)", value=10)
|
| 136 |
+
die_output = gr.Textbox(label="STEP File Location")
|
| 137 |
visualization_output = gr.Image(label="3D Visualization")
|
| 138 |
die_button = gr.Button("Generate Die")
|
| 139 |
die_button.click(
|