File size: 2,974 Bytes
0f7737e
 
 
8bcce63
0f7737e
531e195
 
6126a18
531e195
 
 
6126a18
531e195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6126a18
531e195
 
8bcce63
531e195
 
8bcce63
 
 
 
 
 
 
 
531e195
8bcce63
531e195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
import trimesh

# Function for Stress Analysis
def stress_analysis(force, die_width, die_height, material_strength):
    try:
        # Calculate parameters
        stress = force / (die_width * die_height)  # Stress = Force / Area
        safety_factor = material_strength / stress  # Safety Factor = Material Strength / Stress

        # Data for plotting
        x = ["Stress", "Material Strength", "Safety Factor"]
        y_stress = [stress, material_strength, safety_factor]

        # Create a combined graph
        fig, ax = plt.subplots()
        ax.plot(x, y_stress, marker='o', label="Simulation Parameters")
        ax.axhline(y=1, color='red', linestyle='--', label="Critical Safety Factor (1)")
        ax.set_ylabel("Value")
        ax.set_title("Stress Analysis Parameters")
        ax.legend()

        # Save the figure to a file
        fig_path = "/tmp/stress_analysis_graph.png"
        fig.savefig(fig_path)

        # Close the figure after saving it
        plt.close(fig)

        return fig_path  # Return the image path
    except Exception as e:
        return f"Error in stress analysis: {str(e)}"

# Function for 3D Visualization with Trimesh
def visualize_die(length, width, thickness):
    try:
        # Create a basic die shape using Trimesh
        box = trimesh.creation.box(extents=(length, width, thickness))
        
        # Save the 3D model as a file
        stl_path = "/tmp/progressive_die.stl"
        box.export(stl_path)

        # Convert the STL file to a PNG image
        screenshot = "/tmp/progressive_die_visualization.png"
        box.show()

        return screenshot  # Return the screenshot path
    except Exception as e:
        return f"Error visualizing die: {str(e)}"

# Combined Interface Function
def interface(force, die_width, die_height, material_strength, length, width, thickness):
    # Stress Analysis
    stress_graph_path = stress_analysis(force, die_width, die_height, material_strength)
    
    # 3D Visualization
    die_visualization_path = visualize_die(length, width, thickness)
    
    return stress_graph_path, die_visualization_path

# Gradio Interface
inputs = [
    gr.Slider(minimum=100, maximum=500, value=200, label="Material Yield Strength (MPa)"),
    gr.Slider(minimum=100, maximum=500, value=200, label="Die Width (mm)"),
    gr.Slider(minimum=100, maximum=500, value=200, label="Die Height (mm)"),
    gr.Slider(minimum=5000, maximum=20000, value=12000, label="Force (N)"),
    gr.Slider(minimum=100, maximum=500, value=200, label="Length (mm)"),
    gr.Slider(minimum=100, maximum=500, value=200, label="Width (mm)"),
    gr.Slider(minimum=10, maximum=50, value=10, label="Thickness (mm)"),
]

outputs = [
    gr.Image(type="filepath", label="Stress Analysis Graph"),
    gr.Image(type="filepath", label="3D Visualization"),
]

app = gr.Interface(fn=interface, inputs=inputs, outputs=outputs)
app.launch()