DieDesignAI / app.py
jithenderchoudary's picture
Update app.py
95975bd verified
import cadquery as cq
import numpy as np
import matplotlib.pyplot as plt
import pyvista as pv
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
import gradio as gr
import os
# Function for Progressive Die Design
def generate_die(length, width, thickness):
try:
output_dir = "outputs"
os.makedirs(output_dir, exist_ok=True) # Ensure output directory exists
# Create die geometry
plate = cq.Workplane("XY").box(length, width, thickness)
punch = cq.Workplane("XY").rect(10, 10).extrude(5).translate((length / 4, width / 4, thickness / 2))
die = plate.cut(punch)
# Export STEP file
filename = os.path.join(output_dir, "progressive_die.step")
cq.exporters.export(die, filename)
return filename
except Exception as e:
return f"Error generating die: {str(e)}"
def visualize_die(length, width, thickness):
try:
output_dir = "outputs"
os.makedirs(output_dir, exist_ok=True) # Ensure output directory exists
# Create die geometry
plate = cq.Workplane("XY").box(length, width, thickness)
punch = cq.Workplane("XY").rect(10, 10).extrude(5).translate((length / 4, width / 4, thickness / 2))
die = plate.cut(punch)
# Export STL file for visualization
stl_file = os.path.join(output_dir, "progressive_die.stl")
cq.exporters.exportShape(die.val(), "STL", stl_file)
# Generate 3D visualization screenshot
pv.global_theme.off_screen = True # Ensure off-screen rendering
mesh = pv.read(stl_file)
plotter = pv.Plotter(off_screen=True)
plotter.add_mesh(mesh, color="blue")
screenshot = os.path.join(output_dir, "progressive_die_visualization.png")
plotter.screenshot(screenshot)
# Ensure the screenshot file exists
if not os.path.exists(screenshot):
raise FileNotFoundError("Screenshot file was not generated.")
return screenshot
except Exception as e:
return f"Error visualizing die: {str(e)}"
# Function for Stress Analysis (including thermal stress and fatigue strength)
def stress_analysis(force, die_width, die_height, material_strength, temperature_change=50, alpha=1e-5, elastic_modulus=200000, fatigue_strength=150):
try:
# Mechanical stress
stress = force / (die_width * die_height) # Stress = Force / Area
safety_factor = material_strength / stress # Safety Factor = Material Strength / Stress
# Thermal stress
thermal_stress = elastic_modulus * alpha * temperature_change
# Fatigue strength
fatigue_stress = fatigue_strength
# Generate data for plotting
x = np.linspace(1, 100, 100) # Operational range (e.g., % load)
stress_curve = stress * x / 100 # Simulated stress
material_strength_curve = np.full_like(x, material_strength) # Constant material strength
safety_factor_curve = material_strength_curve / stress_curve # Varying safety factor
thermal_stress_curve = np.full_like(x, thermal_stress) # Constant thermal stress
fatigue_strength_curve = np.full_like(x, fatigue_stress) # Constant fatigue strength
# Create combined graph
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, stress_curve, label="Stress (σ)", color="blue")
ax.plot(x, material_strength_curve, label="Material Strength (σ_y)", color="green")
ax.plot(x, safety_factor_curve, label="Safety Factor (SF)", color="orange")
ax.plot(x, thermal_stress_curve, label="Thermal Stress (σ_thermal)", color="purple")
ax.plot(x, fatigue_strength_curve, label="Fatigue Strength (σ_fatigue)", color="brown")
ax.axhline(1, color="red", linestyle="--", label="Critical Safety Threshold (SF=1)")
ax.set_title("Combined Stress Analysis Parameters")
ax.set_xlabel("Operational Range (%)")
ax.set_ylabel("Parameter Value (MPa or Unitless)")
ax.legend()
ax.grid()
plt.tight_layout()
plt.close(fig)
return f"Safety Factor: {round(safety_factor, 2)}", fig
except Exception as e:
return f"Error in stress analysis: {str(e)}", None
# Tool Optimization Function
def optimize_tool(speed, feed_rate, depth_of_cut, material):
"""
Optimizes machining parameters for maximum tool life.
"""
try:
# Simple formula for tool life estimation
tool_life = 1000 / (speed * feed_rate * depth_of_cut)
# Recommend adjustments for better tool life
recommended_speed = 0.8 * speed
recommended_feed_rate = 0.9 * feed_rate
return {
"Estimated Tool Life (hrs)": round(tool_life, 2),
"Recommended Speed (m/min)": round(recommended_speed, 2),
"Recommended Feed Rate (mm/rev)": round(recommended_feed_rate, 2),
}
except Exception as e:
return {"Error": str(e)}
# Create Gradio App
with gr.Blocks() as app:
gr.Markdown("## Press Tool AI Suite")
gr.Markdown("Select a tool below to get started:")
with gr.Tabs():
with gr.Tab("Progressive Die Design"):
gr.Markdown("### Enter Dimensions 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_output = gr.Textbox(label="STEP File Location")
visualization_output = gr.Image(label="3D Visualization")
die_button = gr.Button("Generate Die")
die_button.click(
lambda l, w, t: (generate_die(l, w, t), visualize_die(l, w, t)),
inputs=[length, width, thickness],
outputs=[die_output, visualization_output],
)
with gr.Tab("Stress Analysis"):
gr.Markdown("### Select Simulation Tool and Enter Parameters for Stress Analysis")
force = gr.Number(label="Force (N)", value=10000)
die_width = gr.Number(label="Width (m)", value=0.05)
die_height = gr.Number(label="Height (m)", value=0.01)
material_strength = gr.Number(label="Material Strength (MPa)", value=250)
temperature_change = gr.Number(label="Temperature Change (°C)", value=50)
alpha = gr.Number(label="Thermal Expansion Coefficient (1/°C)", value=1e-5)
elastic_modulus = gr.Number(label="Elastic Modulus (MPa)", value=200000)
fatigue_strength = gr.Number(label="Fatigue Strength (MPa)", value=150)
safety_factor_output = gr.Textbox(label="Safety Factor")
stress_chart = gr.Plot()
stress_button = gr.Button("Analyze Stress")
stress_button.click(
lambda f, dw, dh, ms, tc, a, em, fs: stress_analysis(f, dw, dh, ms, tc, a, em, fs),
inputs=[force, die_width, die_height, material_strength, temperature_change, alpha, elastic_modulus, fatigue_strength],
outputs=[safety_factor_output, stress_chart],
)
with gr.Tab("Tool Optimization"):
gr.Markdown("### Enter Machining Parameters for Tool Optimization")
speed = gr.Number(label="Cutting Speed (m/min)", value=100)
feed_rate = gr.Number(label="Feed Rate (mm/rev)", value=0.2)
depth_of_cut = gr.Number(label="Depth of Cut (mm)", value=1.0)
material = gr.Dropdown(choices=["Steel", "Aluminum", "Titanium"], label="Material", value="Steel")
optimization_results = gr.JSON(label="Optimization Results")
optimize_button = gr.Button("Optimize Tool")
optimize_button.click(
lambda s, fr, dc, m: optimize_tool(s, fr, dc, m),
inputs=[speed, feed_rate, depth_of_cut, material],
outputs=optimization_results,
)
# Launch the app
app.launch()