don770omr commited on
Commit
d457fe5
·
verified ·
1 Parent(s): 71f1454

Upload 2 files

Browse files
Files changed (2) hide show
  1. convertion.py +32 -0
  2. utils.py +118 -0
convertion.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fractions import Fraction
2
+
3
+ # Conversion functions
4
+ def convert_feet_to_inches(value):
5
+ return value * 12 # 1 foot = 12 inches
6
+
7
+ def convert_inches_to_feet(value):
8
+ return value / 12 # 1 inch = 1/12 foot
9
+
10
+ def convert_to_meters(value, unit):
11
+ conversion_factors = {'Feet': 0.3048, 'Inches': 0.0254}
12
+ return value * conversion_factors.get(unit, 1)
13
+
14
+ def convert_meters_to_unit(value, unit):
15
+ conversion_factors = {'Feet': 1/0.3048, 'Inches': 1/0.0254}
16
+ return value * conversion_factors.get(unit, 1)
17
+
18
+ # Function to convert decimal to fractional format
19
+ def decimal_to_fraction(decimal_value):
20
+ # Convert decimal to fraction with a denominator of 16
21
+ fraction = Fraction(decimal_value).limit_denominator(16) # Limit denominator to 16
22
+
23
+ if fraction.denominator == 1:
24
+ return str(fraction.numerator) # Return whole number if the denominator is 1
25
+ else:
26
+ # Handle mixed fractions like 47.75 -> 47 3/4
27
+ whole_number = fraction.numerator // fraction.denominator
28
+ remainder_numerator = abs(fraction.numerator % fraction.denominator)
29
+ if remainder_numerator == 0:
30
+ return str(whole_number)
31
+ else:
32
+ return f"{whole_number} - {remainder_numerator}/{fraction.denominator}"
utils.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from io import BytesIO
2
+ from reportlab.lib.pagesizes import letter
3
+ from reportlab.pdfgen import canvas
4
+ from reportlab.platypus import Table, TableStyle
5
+ from convertion import convert_feet_to_inches,convert_inches_to_feet
6
+ from convertion import convert_meters_to_unit, convert_to_meters
7
+ from io import BytesIO
8
+ from reportlab.lib.pagesizes import letter
9
+ from reportlab.lib import colors
10
+ from reportlab.lib.styles import getSampleStyleSheet
11
+ from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
12
+ from reportlab.pdfgen import canvas
13
+
14
+
15
+ # Common function for shutter and glass calculations
16
+ def calculate_reduced_dimensions(width, height, unit, selection, object_item_value, item_type="Door"):
17
+ # Use the selected unit for the width and height reduction
18
+ if unit == 'Feet':
19
+ reduction_width = convert_inches_to_feet(object_item_value[selection][item_type]['width'])
20
+ reduction_height = convert_inches_to_feet(object_item_value[selection][item_type]['height'])
21
+ else:
22
+ reduction_width = object_item_value[selection][item_type]['width']
23
+ reduction_height = object_item_value[selection][item_type]['height']
24
+
25
+ # Calculate the reduced dimensions based on the width and height
26
+ reduced_width = (width - reduction_width) / 2.0
27
+ reduced_height = height - reduction_height
28
+ return reduced_width, reduced_height
29
+
30
+ # Functions for door and window area/frame calculation
31
+ def calculate_area_and_frame(width, height, unit, item_type="Door"):
32
+ # Convert width and height to meters for area and frame calculation
33
+ width_in_meters = convert_to_meters(width, unit)
34
+ height_in_meters = convert_to_meters(height, unit)
35
+ area = width_in_meters * height_in_meters
36
+ frame_thickness = {'Door': 0.1, 'Window': 0.05}
37
+ frame_area = 2 * (width_in_meters + height_in_meters) * frame_thickness.get(item_type, 0.1)
38
+
39
+ # Convert area to square feet if necessary
40
+ if unit == "Feet":
41
+ area = area * 10.7639 # Convert square meters to square feet
42
+ frame_area = frame_area * 10.7639 # Convert frame area to square feet
43
+ # If the unit is inches, convert to square feet
44
+ elif unit == "Inches":
45
+ area = (area * 0.092903) * 10.7639 # Convert inches squared to square feet
46
+ frame_area = (frame_area * 0.092903) * 10.7639 # Convert frame area to square feet
47
+
48
+ return area, frame_area
49
+
50
+ def generate_pdf(dfs, company_name, address, unit, items):
51
+ buffer = BytesIO()
52
+
53
+ # Create a canvas for the PDF
54
+ c = canvas.Canvas(buffer, pagesize=letter)
55
+ width, height = letter
56
+
57
+ # Company details
58
+ c.setFont("Helvetica", 12)
59
+ c.drawString(30, height - 40, f"Company Name: {company_name}")
60
+ c.drawString(30, height - 60, f"Address: {address}")
61
+
62
+ # Draw a line under the address
63
+ c.setStrokeColor(colors.black)
64
+ c.setLineWidth(1)
65
+ c.line(30, height - 70, width - 30, height - 70) # Draw line under the address
66
+
67
+
68
+ # Title
69
+ c.setFont("Helvetica-Bold", 16)
70
+ c.drawString(30, height - 100, f"Dimensions Calculation Results: for units ({unit})")
71
+
72
+ # Calculate the y-position to avoid overlapping
73
+ y_position = height - 150
74
+
75
+ # Iterate over each item in the items list
76
+ for i, item in enumerate(items):
77
+ # Add a new page if the y-position gets too low
78
+ if y_position < 100:
79
+ c.showPage()
80
+ y_position = height - 40 # Reset the y-position
81
+
82
+ # Display item label (e.g., Item 1, Item 2)
83
+ c.setFont("Helvetica-Bold", 12)
84
+ c.drawString(30, y_position, f"Item {i+1}: {item}")
85
+ y_position -= 130 # Move down after the label
86
+
87
+ # Retrieve the corresponding dataframe for this item
88
+ df = dfs[i]
89
+
90
+ # Create and style the table
91
+ data = [["Dimension", "Outer Frame", "Shutter", "Glass"]] # Table Header
92
+ for index, row in df.iterrows():
93
+ data.append([index] + [str(value) for value in row]) # Use row values directly for each column
94
+
95
+ # Create the table
96
+ table = Table(data, colWidths=[100, 100, 100, 100], rowHeights=30)
97
+ table.setStyle(TableStyle([
98
+ ('TEXTCOLOR', (0, 0), (-1, 0), (0, 0, 0)),
99
+ ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
100
+ ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
101
+ ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
102
+ ('BACKGROUND', (0, 0), (-1, 0), (0.7, 0.7, 0.7)),
103
+ ('GRID', (0, 0), (-1, -1), 0.5, (0, 0, 0))
104
+ ]))
105
+
106
+ # Position the table just below the item label
107
+ table.wrapOn(c, width, height)
108
+ table.drawOn(c, 30, y_position)
109
+
110
+ # Adjust the y-position for the next table
111
+ y_position -= len(data) + 40 # Move down for next table
112
+ c.line(30, y_position , width - 30, y_position) # Draw line under the address
113
+
114
+ # Save the PDF
115
+ c.save()
116
+
117
+ buffer.seek(0)
118
+ return buffer