jithenderchoudary commited on
Commit
6dc5e25
·
verified ·
1 Parent(s): 339824b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -0
app.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import os
9
+ from ansys.mapdl.core import launch_mapdl # For ANSYS integration
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
+ # Function to visualize die in 3D
24
+ def visualize_die(length, width, thickness):
25
+ try:
26
+ plate = cq.Workplane("XY").box(length, width, thickness)
27
+ punch = cq.Workplane("XY").rect(10, 10).extrude(5).translate((length / 4, width / 4, thickness / 2))
28
+ die = plate.cut(punch)
29
+
30
+ # Export to STL for visualization
31
+ cq.exporters.exportShape(die.val(), "STL", "progressive_die.stl")
32
+
33
+ # Visualize with PyVista
34
+ mesh = pv.read("progressive_die.stl")
35
+ plotter = pv.Plotter(off_screen=True)
36
+ plotter.add_mesh(mesh, color="blue")
37
+ screenshot = "progressive_die_visualization.png"
38
+ plotter.screenshot(screenshot)
39
+ return screenshot
40
+ except Exception as e:
41
+ return f"Error visualizing die: {str(e)}"
42
+
43
+ # Function for Python-based Stress Analysis with a combined graph
44
+ def stress_analysis(force, die_width, die_height, material_strength):
45
+ try:
46
+ # Calculate parameters
47
+ stress = force / (die_width * die_height) # Stress = Force / Area
48
+ safety_factor = material_strength / stress # Safety Factor = Material Strength / Stress
49
+
50
+ # Data for plotting
51
+ x = ["Stress", "Material Strength", "Safety Factor"]
52
+ y_stress = [stress, material_strength, safety_factor]
53
+
54
+ # Create a combined graph
55
+ fig, ax = plt.subplots()
56
+ ax.plot(x, y_stress, marker='o', label="Simulation Parameters")
57
+ ax.axhline(y=1, color='red', linestyle='--', label="Critical Safety Factor (1)")
58
+ ax.set_ylabel("Value")
59
+ ax.set_title("Stress Analysis Parameters")
60
+ ax.legend()
61
+ plt.tight_layout()
62
+ plt.close(fig)
63
+
64
+ return f"Safety Factor: {round(safety_factor, 2)}", fig
65
+ except Exception as e:
66
+ return f"Error in stress analysis: {str(e)}", None
67
+
68
+ # Function to generate PDF report
69
+ def generate_pdf_report(data, filename="report.pdf"):
70
+ try:
71
+ c = canvas.Canvas(filename, pagesize=letter)
72
+ c.drawString(100, 750, "Simulation Report")
73
+ c.drawString(100, 730, f"Max Stress: {data.get('stress', 'N/A')} MPa")
74
+ c.drawString(100, 710, f"Safety Factor: {data.get('safety_factor', 'N/A')}")
75
+ c.save()
76
+ return filename
77
+ except Exception as e:
78
+ return f"Error generating report: {str(e)}"
79
+
80
+ # Gradio interface functions
81
+ def stress_analysis_interface(force, die_width, die_height, material_strength, simulation_tool):
82
+ if simulation_tool == "Python":
83
+ # Python-based stress analysis with combined graph
84
+ safety_factor, fig = stress_analysis(force, die_width, die_height, material_strength)
85
+ data = {"stress": force / (die_width * die_height), "safety_factor": safety_factor}
86
+ pdf_filename = generate_pdf_report(data)
87
+ return safety_factor, fig, pdf_filename
88
+
89
+ else:
90
+ return "Invalid simulation tool selected", None, None
91
+
92
+ # Create Gradio App
93
+ with gr.Blocks() as app:
94
+ gr.Markdown("## Press Tool AI Suite")
95
+ gr.Markdown("Select a tool below to get started:")
96
+
97
+ with gr.Tabs():
98
+ with gr.Tab("Progressive Die Design"):
99
+ gr.Markdown("### Enter Dimensions for Progressive Die")
100
+ length = gr.Number(label="Length (mm)", value=100)
101
+ width = gr.Number(label="Width (mm)", value=50)
102
+ thickness = gr.Number(label="Thickness (mm)", value=10)
103
+ die_output = gr.Textbox(label="Die Output File")
104
+ visualization_output = gr.Image(label="3D Visualization")
105
+ die_button = gr.Button("Generate Die")
106
+ die_button.click(
107
+ lambda l, w, t: (generate_die(l, w, t), visualize_die(l, w, t)),
108
+ inputs=[length, width, thickness],
109
+ outputs=[die_output, visualization_output],
110
+ )
111
+
112
+ with gr.Tab("Stress Analysis"):
113
+ gr.Markdown("### Select Simulation Tool and Enter Parameters for Stress Analysis")
114
+ simulation_tool = gr.Dropdown(
115
+ choices=["Python"],
116
+ label="Simulation Tool",
117
+ value="Python",
118
+ )
119
+ force = gr.Number(label="Force (N)", value=10000)
120
+ die_width = gr.Number(label="Width (m)", value=0.05)
121
+ die_height = gr.Number(label="Height (m)", value=0.01)
122
+ material_strength = gr.Number(label="Material Strength (MPa)", value=250)
123
+ safety_factor_output = gr.Textbox(label="Safety Factor or Simulation Result")
124
+ stress_chart = gr.Plot()
125
+ pdf_file = gr.File(label="Download Report (Python Only)")
126
+ stress_button = gr.Button("Analyze Stress")
127
+ stress_button.click(
128
+ stress_analysis_interface,
129
+ inputs=[force, die_width, die_height, material_strength, simulation_tool],
130
+ outputs=[safety_factor_output, stress_chart, pdf_file],
131
+ )
132
+
133
+ # Launch the app
134
+ app.launch()