Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cadquery as cq
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import pyvista as pv
|
| 5 |
+
from reportlab.lib.pagesizes import letter
|
| 6 |
+
from reportlab.pdfgen import canvas
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
# Function for Progressive Die Design
|
| 10 |
+
def generate_die(length, width, thickness):
|
| 11 |
+
try:
|
| 12 |
+
plate = cq.Workplane("XY").box(length, width, thickness)
|
| 13 |
+
punch = cq.Workplane("XY").rect(10, 10).extrude(5).translate((length / 4, width / 4, thickness / 2))
|
| 14 |
+
die = plate.cut(punch)
|
| 15 |
+
filename = "progressive_die.step"
|
| 16 |
+
cq.exporters.export(die, filename)
|
| 17 |
+
return filename
|
| 18 |
+
except Exception as e:
|
| 19 |
+
return f"Error generating die: {str(e)}"
|
| 20 |
+
|
| 21 |
+
# Function to visualize die in 3D
|
| 22 |
+
def visualize_die(length, width, thickness):
|
| 23 |
+
try:
|
| 24 |
+
plate = cq.Workplane("XY").box(length, width, thickness)
|
| 25 |
+
punch = cq.Workplane("XY").rect(10, 10).extrude(5).translate((length / 4, width / 4, thickness / 2))
|
| 26 |
+
die = plate.cut(punch)
|
| 27 |
+
|
| 28 |
+
# Export to STL for visualization
|
| 29 |
+
cq.exporters.exportShape(die.val(), "STL", "progressive_die.stl")
|
| 30 |
+
|
| 31 |
+
# Visualize with PyVista
|
| 32 |
+
mesh = pv.read("progressive_die.stl")
|
| 33 |
+
plotter = pv.Plotter(off_screen=True)
|
| 34 |
+
plotter.add_mesh(mesh, color="blue")
|
| 35 |
+
screenshot = "progressive_die_visualization.png"
|
| 36 |
+
plotter.screenshot(screenshot)
|
| 37 |
+
return screenshot
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"Error visualizing die: {str(e)}"
|
| 40 |
+
|
| 41 |
+
# Function for Stress Analysis
|
| 42 |
+
def stress_analysis(force, die_width, die_height, material_strength):
|
| 43 |
+
try:
|
| 44 |
+
stress = force / (die_width * die_height)
|
| 45 |
+
safety_factor = material_strength / stress
|
| 46 |
+
|
| 47 |
+
fig, ax = plt.subplots()
|
| 48 |
+
ax.bar(["Stress", "Material Strength"], [stress, material_strength])
|
| 49 |
+
ax.set_ylabel("Stress (MPa)")
|
| 50 |
+
ax.set_title("Stress Analysis")
|
| 51 |
+
plt.close(fig)
|
| 52 |
+
|
| 53 |
+
return f"Safety Factor: {round(safety_factor, 2)}", fig
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return f"Error in stress analysis: {str(e)}", None
|
| 56 |
+
|
| 57 |
+
# Function to generate PDF report
|
| 58 |
+
def generate_pdf_report(data, filename="report.pdf"):
|
| 59 |
+
try:
|
| 60 |
+
c = canvas.Canvas(filename, pagesize=letter)
|
| 61 |
+
c.drawString(100, 750, "Simulation Report")
|
| 62 |
+
c.drawString(100, 730, f"Max Stress: {data.get('stress', 'N/A')} MPa")
|
| 63 |
+
c.drawString(100, 710, f"Safety Factor: {data.get('safety_factor', 'N/A')}")
|
| 64 |
+
c.save()
|
| 65 |
+
return filename
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"Error generating report: {str(e)}"
|
| 68 |
+
|
| 69 |
+
# Function for Tool Optimization
|
| 70 |
+
def optimize_tool(speed, feed_rate, depth_of_cut, material):
|
| 71 |
+
try:
|
| 72 |
+
tool_life = 1000 / (speed * feed_rate * depth_of_cut)
|
| 73 |
+
recommended_speed = 0.8 * speed
|
| 74 |
+
recommended_feed_rate = 0.9 * feed_rate
|
| 75 |
+
|
| 76 |
+
return {
|
| 77 |
+
"Estimated Tool Life (hrs)": round(tool_life, 2),
|
| 78 |
+
"Recommended Speed (m/min)": round(recommended_speed, 2),
|
| 79 |
+
"Recommended Feed Rate (mm/rev)": round(recommended_feed_rate, 2)
|
| 80 |
+
}
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return {"Error": str(e)}
|
| 83 |
+
|
| 84 |
+
# Gradio interface functions
|
| 85 |
+
def progressive_die_interface(length, width, thickness):
|
| 86 |
+
filename = generate_die(length, width, thickness)
|
| 87 |
+
visualization = visualize_die(length, width, thickness)
|
| 88 |
+
return filename, visualization
|
| 89 |
+
|
| 90 |
+
def stress_analysis_interface(force, width, height, material_strength):
|
| 91 |
+
safety_factor, fig = stress_analysis(force, width, height, material_strength)
|
| 92 |
+
data = {"stress": force / (width * height), "safety_factor": safety_factor}
|
| 93 |
+
pdf_filename = generate_pdf_report(data)
|
| 94 |
+
return safety_factor, fig, pdf_filename
|
| 95 |
+
|
| 96 |
+
def tool_optimization_interface(speed, feed_rate, depth_of_cut, material):
|
| 97 |
+
return optimize_tool(speed, feed_rate, depth_of_cut, material)
|
| 98 |
+
|
| 99 |
+
# Create the Gradio Interface
|
| 100 |
+
with gr.Blocks() as app:
|
| 101 |
+
gr.Markdown("## Press Tool AI Suite")
|
| 102 |
+
gr.Markdown("Select a tool below to get started:")
|
| 103 |
+
|
| 104 |
+
with gr.Tabs():
|
| 105 |
+
with gr.Tab("Progressive Die Design"):
|
| 106 |
+
gr.Markdown("### Enter Dimensions for Progressive Die")
|
| 107 |
+
length = gr.Number(label="Length (mm)", value=100)
|
| 108 |
+
width = gr.Number(label="Width (mm)", value=50)
|
| 109 |
+
thickness = gr.Number(label="Thickness (mm)", value=10)
|
| 110 |
+
die_output = gr.Textbox(label="Die Output File")
|
| 111 |
+
visualization_output = gr.Image(label="3D Visualization")
|
| 112 |
+
die_button = gr.Button("Generate Die")
|
| 113 |
+
die_button.click(progressive_die_interface, inputs=[length, width, thickness], outputs=[die_output, visualization_output])
|
| 114 |
+
|
| 115 |
+
with gr.Tab("Stress Analysis"):
|
| 116 |
+
gr.Markdown("### Enter Parameters for Stress Analysis")
|
| 117 |
+
force = gr.Number(label="Force (N)", value=10000)
|
| 118 |
+
die_width = gr.Number(label="Width (m)", value=0.05)
|
| 119 |
+
die_height = gr.Number(label="Height (m)", value=0.01)
|
| 120 |
+
material_strength = gr.Number(label="Material Strength (MPa)", value=250)
|
| 121 |
+
safety_factor_output = gr.Textbox(label="Safety Factor")
|
| 122 |
+
stress_chart = gr.Plot()
|
| 123 |
+
pdf_file = gr.File(label="Download Report")
|
| 124 |
+
stress_button = gr.Button("Analyze Stress")
|
| 125 |
+
stress_button.click(stress_analysis_interface, inputs=[force, die_width, die_height, material_strength], outputs=[safety_factor_output, stress_chart, pdf_file])
|
| 126 |
+
|
| 127 |
+
with gr.Tab("Tool Optimization"):
|
| 128 |
+
gr.Markdown("### Enter Machining Parameters for Tool Optimization")
|
| 129 |
+
speed = gr.Number(label="Cutting Speed (m/min)", value=100)
|
| 130 |
+
feed_rate = gr.Number(label="Feed Rate (mm/rev)", value=0.2)
|
| 131 |
+
depth_of_cut = gr.Number(label="Depth of Cut (mm)", value=1.0)
|
| 132 |
+
material = gr.Dropdown(choices=["Steel", "Aluminum", "Titanium"], label="Material", value="Steel")
|
| 133 |
+
optimization_results = gr.JSON(label="Optimization Results")
|
| 134 |
+
optimize_button = gr.Button("Optimize Tool")
|
| 135 |
+
optimize_button.click(tool_optimization_interface, inputs=[speed, feed_rate, depth_of_cut, material], outputs=optimization_results)
|
| 136 |
+
|
| 137 |
+
# Launch the app
|
| 138 |
+
app.launch()
|