DieDesignAI / app.py
jithenderchoudary's picture
Update app.py
dd03019 verified
raw
history blame
6.83 kB
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:
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)
filename = "progressive_die.step"
cq.exporters.export(die, filename)
return filename
except Exception as e:
return f"Error generating die: {str(e)}"
# Function to visualize die in 3D
def visualize_die(length, width, thickness):
try:
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 to STL for visualization
cq.exporters.exportShape(die.val(), "STL", "progressive_die.stl")
# Visualize with PyVista
mesh = pv.read("progressive_die.stl")
plotter = pv.Plotter(off_screen=True)
plotter.add_mesh(mesh, color="blue")
screenshot = "progressive_die_visualization.png"
plotter.screenshot(screenshot)
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
# Function to generate PDF report
def generate_pdf_report(data, filename="report.pdf"):
try:
c = canvas.Canvas(filename, pagesize=letter)
c.drawString(100, 750, "Simulation Report")
c.drawString(100, 730, f"Max Stress: {data.get('stress', 'N/A')} MPa")
c.drawString(100, 710, f"Safety Factor: {data.get('safety_factor', 'N/A')}")
c.save()
return filename
except Exception as e:
return f"Error generating report: {str(e)}"
# Gradio interface functions
def stress_analysis_interface(force, die_width, die_height, material_strength, temperature_change, alpha, elastic_modulus, fatigue_strength):
safety_factor, fig = stress_analysis(force, die_width, die_height, material_strength, temperature_change, alpha, elastic_modulus, fatigue_strength)
data = {"stress": force / (die_width * die_height), "safety_factor": safety_factor}
pdf_filename = generate_pdf_report(data)
return safety_factor, fig, pdf_filename
# 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="Die Output File")
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()
pdf_file = gr.File(label="Download Report")
stress_button = gr.Button("Analyze Stress")
stress_button.click(
stress_analysis_interface,
inputs=[force, die_width, die_height, material_strength, temperature_change, alpha, elastic_modulus, fatigue_strength],
outputs=[safety_factor_output, stress_chart, pdf_file],
)
# Launch the app
app.launch()