|
|
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 |
|
|
from ansys.mapdl.core import launch_mapdl |
|
|
|
|
|
|
|
|
|
|
|
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)}" |
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
cq.exporters.exportShape(die.val(), "STL", "progressive_die.stl") |
|
|
|
|
|
|
|
|
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)}" |
|
|
|
|
|
|
|
|
|
|
|
def stress_analysis(force, die_width, die_height, material_strength): |
|
|
try: |
|
|
stress = force / (die_width * die_height) |
|
|
safety_factor = material_strength / stress |
|
|
|
|
|
fig, ax = plt.subplots() |
|
|
ax.bar(["Stress", "Material Strength"], [stress, material_strength]) |
|
|
ax.set_ylabel("Stress (MPa)") |
|
|
ax.set_title("Stress Analysis") |
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
def run_ansys_simulation(force, die_width, die_height, material_strength): |
|
|
try: |
|
|
|
|
|
mapdl = launch_mapdl() |
|
|
mapdl.prep7() |
|
|
|
|
|
|
|
|
mapdl.rectng(0, die_width, 0, die_height) |
|
|
mapdl.mp('EX', 1, material_strength) |
|
|
mapdl.et(1, 'PLANE183') |
|
|
|
|
|
|
|
|
mapdl.nsel('S', 'LOC', 'X', 0) |
|
|
mapdl.d('ALL', 'UX', 0) |
|
|
mapdl.f('ALL', 'FY', -force) |
|
|
|
|
|
|
|
|
mapdl.run('/SOLU') |
|
|
mapdl.solve() |
|
|
mapdl.finish() |
|
|
|
|
|
|
|
|
mapdl.post1() |
|
|
stress = mapdl.get_value('NODE', 1, 'S', 'EQV') |
|
|
mapdl.exit() |
|
|
|
|
|
return f"Max Stress: {stress:.2f} MPa" |
|
|
except Exception as e: |
|
|
return f"Error running ANSYS simulation: {str(e)}" |
|
|
|
|
|
|
|
|
|
|
|
def solidworks_stress_analysis(force, die_width, die_height, material_strength): |
|
|
|
|
|
try: |
|
|
output_file = "/path/to/solidworks/output.txt" |
|
|
if os.path.exists(output_file): |
|
|
with open(output_file, "r") as file: |
|
|
result = file.read() |
|
|
return result.strip() |
|
|
else: |
|
|
return "SolidWorks simulation output not found." |
|
|
except Exception as e: |
|
|
return f"Error running SolidWorks simulation: {str(e)}" |
|
|
|
|
|
|
|
|
|
|
|
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)}" |
|
|
|
|
|
|
|
|
|
|
|
def optimize_tool(speed, feed_rate, depth_of_cut, material): |
|
|
try: |
|
|
tool_life = 1000 / (speed * feed_rate * depth_of_cut) |
|
|
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)} |
|
|
|
|
|
|
|
|
|
|
|
def stress_analysis_interface(force, die_width, die_height, material_strength, simulation_tool): |
|
|
if simulation_tool == "Python": |
|
|
|
|
|
safety_factor, fig = stress_analysis(force, die_width, die_height, material_strength) |
|
|
data = {"stress": force / (die_width * die_height), "safety_factor": safety_factor} |
|
|
pdf_filename = generate_pdf_report(data) |
|
|
return safety_factor, fig, pdf_filename |
|
|
|
|
|
elif simulation_tool == "ANSYS": |
|
|
|
|
|
result = run_ansys_simulation(force, die_width, die_height, material_strength) |
|
|
return result, None, None |
|
|
|
|
|
elif simulation_tool == "SolidWorks": |
|
|
|
|
|
result = solidworks_stress_analysis(force, die_width, die_height, material_strength) |
|
|
return result, None, None |
|
|
|
|
|
else: |
|
|
return "Invalid simulation tool selected", None, None |
|
|
|
|
|
|
|
|
|
|
|
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") |
|
|
simulation_tool = gr.Dropdown( |
|
|
choices=["Python", "ANSYS", "SolidWorks"], |
|
|
label="Simulation Tool", |
|
|
value="Python", |
|
|
) |
|
|
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) |
|
|
safety_factor_output = gr.Textbox(label="Safety Factor or Simulation Result") |
|
|
stress_chart = gr.Plot() |
|
|
pdf_file = gr.File(label="Download Report (Python Only)") |
|
|
stress_button = gr.Button("Analyze Stress") |
|
|
stress_button.click( |
|
|
stress_analysis_interface, |
|
|
inputs=[force, die_width, die_height, material_strength, simulation_tool], |
|
|
outputs=[safety_factor_output, stress_chart, pdf_file], |
|
|
) |
|
|
|
|
|
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( |
|
|
optimize_tool, |
|
|
inputs=[speed, feed_rate, depth_of_cut, material], |
|
|
outputs=optimization_results, |
|
|
) |
|
|
|
|
|
|
|
|
app.launch() |
|
|
|