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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import cadquery as cq
|
| 5 |
+
import pyvista as pv
|
| 6 |
+
from ansys.mapdl.core import launch_mapdl
|
| 7 |
+
from reportlab.lib.pagesizes import letter
|
| 8 |
+
from reportlab.pdfgen import canvas
|
| 9 |
+
import os
|
| 10 |
+
|
| 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 |
+
filename = "progressive_die.step"
|
| 18 |
+
cq.exporters.export(die, filename)
|
| 19 |
+
return filename
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"Error generating die: {str(e)}"
|
| 22 |
+
|
| 23 |
+
|
| 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 to STL for visualization
|
| 32 |
+
cq.exporters.exportShape(die.val(), "STL", "progressive_die.stl")
|
| 33 |
+
|
| 34 |
+
# Visualize with PyVista
|
| 35 |
+
mesh = pv.read("progressive_die.stl")
|
| 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 |
+
# Function to generate and display combined graph for multiple parameters
|
| 46 |
+
def generate_combined_graph(force, die_width, die_height, material_strength, punch_size):
|
| 47 |
+
try:
|
| 48 |
+
# Stress = Force / Area (Die Width * Die Height)
|
| 49 |
+
stress = force / (die_width * die_height)
|
| 50 |
+
safety_factor = material_strength / stress # Safety Factor = Material Strength / Stress
|
| 51 |
+
|
| 52 |
+
# Tool life calculation (simplified estimation)
|
| 53 |
+
tool_life = 1000 / (force * punch_size)
|
| 54 |
+
|
| 55 |
+
# Data for plotting (Multiple parameters)
|
| 56 |
+
x = np.array([force, die_width, die_height, punch_size])
|
| 57 |
+
y_stress = np.array([stress, safety_factor, tool_life])
|
| 58 |
+
|
| 59 |
+
# Multiple parameters (force vs material strength, safety factor vs stress, etc.)
|
| 60 |
+
plt.figure(figsize=(10, 6))
|
| 61 |
+
|
| 62 |
+
# Plot multiple curves
|
| 63 |
+
plt.plot(x, y_stress, label="Stress vs Force and Tool Life")
|
| 64 |
+
plt.plot(x, np.array([safety_factor] * len(x)), label="Safety Factor vs Stress", linestyle='--')
|
| 65 |
+
|
| 66 |
+
# Labels and Titles
|
| 67 |
+
plt.xlabel("Force, Die Width, Die Height, Punch Size")
|
| 68 |
+
plt.ylabel("Values")
|
| 69 |
+
plt.title("Combined Simulation Parameters for Die Design and Stress Analysis")
|
| 70 |
+
|
| 71 |
+
# Adding legend
|
| 72 |
+
plt.legend()
|
| 73 |
+
|
| 74 |
+
# Display the graph
|
| 75 |
+
plt.tight_layout()
|
| 76 |
+
plt.close()
|
| 77 |
+
|
| 78 |
+
return "Graph generated successfully."
|
| 79 |
+
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return f"Error in generating graph: {str(e)}"
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# Tool Optimization Function
|
| 85 |
+
def optimize_tool(speed, feed_rate, depth_of_cut, material):
|
| 86 |
+
try:
|
| 87 |
+
tool_life = 1000 / (speed * feed_rate * depth_of_cut)
|
| 88 |
+
recommended_speed = 0.8 * speed
|
| 89 |
+
recommended_feed_rate = 0.9 * feed_rate
|
| 90 |
+
|
| 91 |
+
return {
|
| 92 |
+
"Estimated Tool Life (hrs)": round(tool_life, 2),
|
| 93 |
+
"Recommended Speed (m/min)": round(recommended_speed, 2),
|
| 94 |
+
"Recommended Feed Rate (mm/rev)": round(recommended_feed_rate, 2)
|
| 95 |
+
}
|
| 96 |
+
except Exception as e:
|
| 97 |
+
return {"Error": str(e)}
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
# Gradio interface for Progressive Die Design and Stress Analysis
|
| 101 |
+
def stress_analysis_interface(force, die_width, die_height, material_strength, simulation_tool):
|
| 102 |
+
if simulation_tool == "Python":
|
| 103 |
+
# Generate and plot combined graph
|
| 104 |
+
result = generate_combined_graph(force, die_width, die_height, material_strength, punch_size=10)
|
| 105 |
+
return result # Show graph or return result for Gradio output
|
| 106 |
+
|
| 107 |
+
elif simulation_tool == "ANSYS":
|
| 108 |
+
# Run ANSYS-based simulation
|
| 109 |
+
result = run_ansys_simulation(force, die_width, die_height, material_strength)
|
| 110 |
+
return result
|
| 111 |
+
|
| 112 |
+
elif simulation_tool == "SolidWorks":
|
| 113 |
+
# Run SolidWorks-based simulation
|
| 114 |
+
result = solidworks_stress_analysis(force, die_width, die_height, material_strength)
|
| 115 |
+
return result
|
| 116 |
+
|
| 117 |
+
else:
|
| 118 |
+
return "Invalid simulation tool selected", None
|
| 119 |
+
|
| 120 |
+
# Gradio Interface for Press Tool AI Suite
|
| 121 |
+
with gr.Blocks() as app:
|
| 122 |
+
gr.Markdown("## Press Tool AI Suite")
|
| 123 |
+
|
| 124 |
+
with gr.Tabs():
|
| 125 |
+
with gr.Tab("Progressive Die Design"):
|
| 126 |
+
# Inputs for Progressive Die Design
|
| 127 |
+
length = gr.Number(label="Length (mm)", value=100)
|
| 128 |
+
width = gr.Number(label="Width (mm)", value=50)
|
| 129 |
+
thickness = gr.Number(label="Thickness (mm)", value=10)
|
| 130 |
+
die_output = gr.Textbox(label="Die Output File")
|
| 131 |
+
visualization_output = gr.Image(label="3D Visualization")
|
| 132 |
+
die_button = gr.Button("Generate Die")
|
| 133 |
+
die_button.click(
|
| 134 |
+
lambda l, w, t: (generate_die(l, w, t), visualize_die(l, w, t)),
|
| 135 |
+
inputs=[length, width, thickness],
|
| 136 |
+
outputs=[die_output, visualization_output],
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
with gr.Tab("Stress Analysis"):
|
| 140 |
+
# Inputs for Stress Analysis
|
| 141 |
+
force = gr.Number(label="Force (N)", value=10000)
|
| 142 |
+
die_width = gr.Number(label="Width (m)", value=0.05)
|
| 143 |
+
die_height = gr.Number(label="Height (m)", value=0.01)
|
| 144 |
+
material_strength = gr.Number(label="Material Strength (MPa)", value=250)
|
| 145 |
+
simulation_tool = gr.Dropdown(choices=["Python", "ANSYS", "SolidWorks"], label="Simulation Tool", value="Python")
|
| 146 |
+
|
| 147 |
+
stress_output = gr.Textbox(label="Simulation Result")
|
| 148 |
+
stress_chart = gr.Plot() # This will display the combined graph
|
| 149 |
+
stress_button = gr.Button("Analyze Stress and Visualize")
|
| 150 |
+
stress_button.click(
|
| 151 |
+
stress_analysis_interface,
|
| 152 |
+
inputs=[force, die_width, die_height, material_strength, simulation_tool],
|
| 153 |
+
outputs=[stress_output, stress_chart],
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
with gr.Tab("Tool Optimization"):
|
| 157 |
+
# Inputs for Tool Optimization
|
| 158 |
+
speed = gr.Number(label="Cutting Speed (m/min)", value=100)
|
| 159 |
+
feed_rate = gr.Number(label="Feed Rate (mm/rev)", value=0.2)
|
| 160 |
+
depth_of_cut = gr.Number(label="Depth of Cut (mm)", value=1.0)
|
| 161 |
+
material = gr.Dropdown(choices=["Steel", "Aluminum", "Titanium"], label="Material", value="Steel")
|
| 162 |
+
optimization_results = gr.JSON(label="Optimization Results")
|
| 163 |
+
optimize_button = gr.Button("Optimize Tool")
|
| 164 |
+
optimize_button.click(
|
| 165 |
+
optimize_tool,
|
| 166 |
+
inputs=[speed, feed_rate, depth_of_cut, material],
|
| 167 |
+
outputs=optimization_results,
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
# Launch the app
|
| 171 |
+
app.launch()
|