engr-abasit commited on
Commit
62390af
·
verified ·
1 Parent(s): 101053c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -96
app.py CHANGED
@@ -1,114 +1,95 @@
1
  import os
2
  import streamlit as st
 
3
  import matplotlib.pyplot as plt
4
  from fpdf import FPDF
5
- from groq import Groq
6
-
7
- # Set your API key
8
- os.environ["GROQ_API_KEY"] = "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"
9
 
10
  # Initialize Groq client
11
- client = Groq(api_key=os.environ["GROQ_API_KEY"])
12
 
13
- # Function to calculate cutting areas
14
- def calculate_areas(blank_plate, pcs, saw_cut, trim_size):
15
- blank_area = blank_plate['thickness'] * blank_plate['width'] * blank_plate['length']
16
- total_cut_area = sum([pc['width'] * pc['length'] for pc in pcs])
17
-
18
- # Assuming trimming and saw cuts reduce the usable area
19
- trimmed_area = blank_area - (2 * trim_size * (blank_plate['width'] + blank_plate['length']))
20
- usable_area = trimmed_area - (len(pcs) * saw_cut)
21
-
22
- non_used_area = blank_area - total_cut_area
23
- usable_percentage = (usable_area / blank_area) * 100
24
-
25
- return usable_area, total_cut_area, non_used_area, usable_percentage
26
 
27
- # Function to generate a sketch (simplified)
28
- def generate_sketch(blank_plate, pcs):
29
- fig, ax = plt.subplots(figsize=(6, 6))
30
- ax.set_xlim([0, blank_plate['width']])
31
- ax.set_ylim([0, blank_plate['length']])
32
-
33
- # Draw the blank plate
34
- ax.add_patch(plt.Rectangle((0, 0), blank_plate['width'], blank_plate['length'], fill=None, edgecolor='black'))
35
-
36
- # Draw each piece to be cut
37
- for i, pc in enumerate(pcs):
38
- ax.add_patch(plt.Rectangle((pc['x'], pc['y']), pc['width'], pc['length'], fill=True, color="blue", alpha=0.5))
39
-
40
- plt.axis('off')
41
- st.pyplot(fig)
42
 
43
- # Function to generate PDF
44
- def generate_pdf(blank_plate, pcs, areas):
45
- pdf = FPDF()
46
- pdf.add_page()
47
-
48
- pdf.set_font("Arial", size=12)
49
- pdf.cell(200, 10, txt="Cutting Plan Report", ln=True, align='C')
50
-
51
- # Blank Plate details
52
- pdf.ln(10)
53
- pdf.cell(200, 10, txt=f"Blank Plate: {blank_plate['thickness']}mm x {blank_plate['width']}mm x {blank_plate['length']}mm", ln=True)
54
-
55
- # Area details
56
- pdf.ln(5)
57
- pdf.cell(200, 10, txt=f"Usable Area: {areas[0]} mm²", ln=True)
58
- pdf.cell(200, 10, txt=f"Cut Area: {areas[1]} mm²", ln=True)
59
- pdf.cell(200, 10, txt=f"Non-Used Area: {areas[2]} mm²", ln=True)
60
- pdf.cell(200, 10, txt=f"Usable Area Percentage: {areas[3]}%", ln=True)
61
-
62
- # Sketch generation (placeholder)
63
- pdf.ln(10)
64
- pdf.cell(200, 10, txt="Sketch of Cutting Plan:", ln=True)
65
-
66
- # Save the PDF
67
- pdf.output("cutting_plan_report.pdf")
68
- st.success("PDF generated successfully!")
69
 
70
- # Streamlit UI
71
- st.title("Cutting Plan Generator")
 
 
72
 
73
- # Inputs for blank plates
74
- blank_plates = []
75
- num_blank_plates = st.number_input("Number of Blank Plates", min_value=1, value=1)
76
 
77
- for i in range(num_blank_plates):
78
- st.header(f"Blank Plate {i+1}")
79
- thickness = st.number_input(f"Blank Plate {i+1} Thickness (mm)", min_value=1, value=10)
80
- width = st.number_input(f"Blank Plate {i+1} Width (mm)", min_value=1, value=1000)
81
- length = st.number_input(f"Blank Plate {i+1} Length (mm)", min_value=1, value=2000)
82
- num_plates = st.number_input(f"Blank Plate {i+1} Quantity", min_value=1, value=1)
83
-
84
- blank_plates.append({'thickness': thickness, 'width': width, 'length': length, 'num_plates': num_plates})
85
 
86
- # Inputs for pieces to be cut
87
- pcs = []
88
- num_pcs = st.number_input("Number of Pieces to Cut", min_value=1, value=5)
89
 
90
- for i in range(num_pcs):
91
- st.header(f"Piece {i+1}")
92
- pc_width = st.number_input(f"Piece {i+1} Width (mm)", min_value=1, value=200)
93
- pc_length = st.number_input(f"Piece {i+1} Length (mm)", min_value=1, value=500)
94
- pcs.append({'width': pc_width, 'length': pc_length, 'x': 0, 'y': 0})
95
 
96
- # Saw cut and trimming size
97
- saw_cut = st.number_input("Saw Cut Size (mm)", min_value=0, value=5)
98
- trim_size = st.number_input("Trimming Size (mm)", min_value=0, value=10)
 
 
99
 
100
- # Calculate and display the cutting areas for each blank plate
101
- for blank_plate in blank_plates:
102
- areas = calculate_areas(blank_plate, pcs, saw_cut, trim_size)
103
- generate_sketch(blank_plate, pcs)
 
 
 
 
 
 
 
 
104
 
105
- st.write(f"Blank Plate {blank_plates.index(blank_plate)+1}:")
106
- st.write(f"Usable Area: {areas[0]} mm²")
107
- st.write(f"Cut Area: {areas[1]} mm²")
108
- st.write(f"Non-Used Area: {areas[2]} mm²")
109
- st.write(f"Usable Area Percentage: {areas[3]}%")
 
 
 
 
110
 
111
- # Generate PDF
112
- if st.button("Generate PDF"):
113
- generate_pdf(blank_plates[0], pcs, areas)
114
- st.download_button("Download PDF", "cutting_plan_report.pdf")
 
 
 
 
 
1
  import os
2
  import streamlit as st
3
+ from groq import Groq
4
  import matplotlib.pyplot as plt
5
  from fpdf import FPDF
6
+ import io
 
 
 
7
 
8
  # Initialize Groq client
9
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
10
 
11
+ # App title
12
+ st.title("Cutting Plan Generator")
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ # Input for blank plates
15
+ st.header("Blank Plates Details")
16
+ num_blank_types = st.number_input("Number of Blank Plate Types", min_value=1, step=1, key="num_blank_types")
17
+ blank_data = []
18
+ for i in range(num_blank_types):
19
+ with st.expander(f"Blank Plate {i+1}"):
20
+ blank_width = st.number_input(f"Width (mm) for Blank {i+1}", min_value=0.0, step=0.1, key=f"blank_width_{i}")
21
+ blank_length = st.number_input(f"Length (mm) for Blank {i+1}", min_value=0.0, step=0.1, key=f"blank_length_{i}")
22
+ num_blank_plates = st.number_input(f"Number of Plates for Blank {i+1}", min_value=1, step=1, key=f"num_blank_plates_{i}")
23
+ blank_data.append({"width": blank_width, "length": blank_length, "count": num_blank_plates})
 
 
 
 
 
24
 
25
+ # Input for pieces
26
+ st.header("Pieces to be Cut Details")
27
+ num_piece_types = st.number_input("Number of Piece Types", min_value=1, step=1, key="num_piece_types")
28
+ piece_data = []
29
+ for i in range(num_piece_types):
30
+ with st.expander(f"Piece {i+1}"):
31
+ piece_width = st.number_input(f"Width (mm) for Piece {i+1}", min_value=0.0, step=0.1, key=f"piece_width_{i}")
32
+ piece_length = st.number_input(f"Length (mm) for Piece {i+1}", min_value=0.0, step=0.1, key=f"piece_length_{i}")
33
+ num_pieces = st.number_input(f"Number of Pieces for Piece {i+1}", min_value=1, step=1, key=f"num_pieces_{i}")
34
+ piece_data.append({"width": piece_width, "length": piece_length, "count": num_pieces})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ # Saw cut size and trimming size
37
+ st.header("Other Specifications")
38
+ saw_cut_size = st.number_input("Saw Cut Size (mm)", min_value=0.0, step=0.1, key="saw_cut_size")
39
+ trimming_size = st.number_input("Trimming Size (mm)", min_value=0.0, step=0.1, key="trimming_size")
40
 
41
+ # Priority of blank sizes
42
+ priority = st.selectbox("Priority of Blank Sizes", options=["Smallest First", "Largest First"], key="priority")
 
43
 
44
+ # Generate Cutting Plan and Sketch
45
+ if st.button("Generate Cutting Plan"):
46
+ total_cut_area = 0
47
+ blank_areas = []
48
+ for blank in blank_data:
49
+ blank_area = (blank["width"] - 2 * trimming_size) * (blank["length"] - 2 * trimming_size) * blank["count"]
50
+ blank_areas.append(blank_area)
 
51
 
52
+ for piece in piece_data:
53
+ total_cut_area += piece["width"] * piece["length"] * piece["count"]
 
54
 
55
+ total_blank_area = sum(blank_areas)
56
+ unused_area = total_blank_area - total_cut_area
57
+ usable_area_percentage = (total_cut_area / total_blank_area) * 100
 
 
58
 
59
+ st.subheader("Results")
60
+ st.write(f"Total Usable Surface Area: {total_blank_area:.2f} mm²")
61
+ st.write(f"Total Cut Square Area: {total_cut_area:.2f} mm²")
62
+ st.write(f"Total Non-used Square Area: {unused_area:.2f} mm²")
63
+ st.write(f"Percentage of Usable Area: {usable_area_percentage:.2f}%")
64
 
65
+ # Generate sketch
66
+ fig, ax = plt.subplots(figsize=(8, 6))
67
+ for i, blank in enumerate(blank_data):
68
+ ax.add_patch(plt.Rectangle((0, i * (blank["length"] + 10)), blank["width"], blank["length"], edgecolor="blue", facecolor="none", label=f"Blank {i+1}" if i == 0 else ""))
69
+ for j, piece in enumerate(piece_data):
70
+ ax.add_patch(plt.Rectangle((j * (piece["width"] + 5), i * (blank["length"] + 10) + trimming_size), piece["width"], piece["length"], edgecolor="red", facecolor="pink", label=f"Piece {j+1}" if i == 0 else ""))
71
+
72
+ ax.set_xlim(0, max(blank["width"] for blank in blank_data) + 50)
73
+ ax.set_ylim(0, (len(blank_data) * max(blank["length"] for blank in blank_data)) + 100)
74
+ ax.set_title("Cutting Plan Sketch")
75
+ ax.legend(loc="upper right")
76
+ st.pyplot(fig)
77
 
78
+ # Save as PDF
79
+ pdf = FPDF()
80
+ pdf.add_page()
81
+ pdf.set_font("Arial", size=12)
82
+ pdf.cell(200, 10, txt="Cutting Plan Report", ln=True, align="C")
83
+ pdf.cell(200, 10, txt=f"Total Usable Surface Area: {total_blank_area:.2f} mm²", ln=True)
84
+ pdf.cell(200, 10, txt=f"Total Cut Square Area: {total_cut_area:.2f} mm²", ln=True)
85
+ pdf.cell(200, 10, txt=f"Total Non-used Square Area: {unused_area:.2f} mm²", ln=True)
86
+ pdf.cell(200, 10, txt=f"Percentage of Usable Area: {usable_area_percentage:.2f}%", ln=True)
87
 
88
+ pdf_output = io.BytesIO()
89
+ pdf.output(pdf_output)
90
+ st.download_button(
91
+ label="Download Cutting Plan as PDF",
92
+ data=pdf_output.getvalue(),
93
+ file_name="cutting_plan.pdf",
94
+ mime="application/pdf",
95
+ )