UsmanGoraya commited on
Commit
72bf935
·
verified ·
1 Parent(s): 0d44332

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -43
app.py CHANGED
@@ -7,6 +7,7 @@ from fpdf import FPDF
7
  import matplotlib.patches as patches
8
  import tempfile
9
  import math
 
10
 
11
  # Constants for estimation
12
  BRICK_VOLUME_CFT = (9 / 12) * (4.5 / 12) * (3 / 12) # Brick Volume in Cubic Feet
@@ -26,10 +27,11 @@ uploaded_file = st.file_uploader("Upload your DXF file", type=["dxf"])
26
  @st.cache_data
27
  def extract_geometry(file_bytes):
28
  try:
29
- doc = ezdxf.read(io.BytesIO(file_bytes))
 
30
  except Exception as e:
31
  st.error(f"Error reading DXF file: {e}")
32
- return [], 0.75, [], [], []
33
 
34
  msp = doc.modelspace()
35
  rooms = []
@@ -37,6 +39,7 @@ def extract_geometry(file_bytes):
37
  doors = []
38
  windows = []
39
  wall_thickness = DEFAULT_WALL_THICKNESS
 
40
 
41
  # Detecting walls (parallel lines that represent wall edges)
42
  wall_lines = []
@@ -45,7 +48,6 @@ def extract_geometry(file_bytes):
45
  wall_lines.append(entity)
46
 
47
  # Check for parallel lines representing a wall (detect double lines)
48
- wall_pairs = []
49
  for i, line1 in enumerate(wall_lines):
50
  for line2 in wall_lines[i + 1:]:
51
  if are_lines_parallel(line1, line2):
@@ -182,44 +184,43 @@ def draw_plan_image(room_shapes):
182
  rect = patches.Rectangle((x0, y0), width, height, linewidth=1, edgecolor='black', facecolor='none')
183
  ax.add_patch(rect)
184
  ax.text(x0 + width / 2, y0 - 2, f"{round(width / 12, 1)} ft", ha='center', fontsize=8)
185
- ax.text(x1 + 2, y0 + height / 2, f"{round(height / 12, 1)} ft", va='center', fontsize=8, rotation=90)
186
- ax.set_aspect('equal')
187
- ax.axis('off')
188
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
189
- fig.savefig(temp_file.name, bbox_inches='tight')
190
- plt.close(fig)
191
- return temp_file.name
192
-
193
-
194
- # Streamlit logic to display results
195
- if uploaded_file:
196
- file_bytes = io.BytesIO(uploaded_file.read())
197
- rooms, wall_thickness, doors, windows, room_shapes, wall_pairs = extract_geometry(file_bytes)
198
-
199
- st.success(f"✔️ Parsed {len(rooms)} rooms, {len(doors)} doors, and {len(windows)} windows.")
200
- st.write(f"📏 Wall Thickness: {wall_thickness} ft")
201
-
202
- # Estimate the materials
203
- bricks, sand, cement, calc_details = estimate(rooms, wall_thickness, doors, windows)
204
-
205
- # Display Room Dimensions
206
- st.subheader("📊 Room Dimensions")
207
- df_rooms = pd.DataFrame(rooms, columns=["Length (ft)", "Width (ft)", "Height (ft)"])
208
- st.dataframe(df_rooms)
209
-
210
- # Display Estimation Results
211
- st.subheader("🧱 Estimation Result")
212
- st.write(f"Total Bricks Required: {bricks}")
213
- st.write(f"Total Sand Volume: {sand} cft")
214
- st.write(f"Total Cement Bags: {cement}")
215
-
216
- # Generate and provide download link for the PDF
217
- st.subheader("📄 Export")
218
- pdf_file = generate_pdf(
219
- {"Bricks Required": bricks, "Sand Volume (cft)": sand, "Cement Bags": cement},
220
- calc_details,
221
- room_shapes
222
- )
223
- with open(pdf_file, "rb") as f:
224
- st.download_button("Download Estimation PDF", f, file_name="estimation_report.pdf")
225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  import matplotlib.patches as patches
8
  import tempfile
9
  import math
10
+ from io import BytesIO
11
 
12
  # Constants for estimation
13
  BRICK_VOLUME_CFT = (9 / 12) * (4.5 / 12) * (3 / 12) # Brick Volume in Cubic Feet
 
27
  @st.cache_data
28
  def extract_geometry(file_bytes):
29
  try:
30
+ # Use BytesIO to read the uploaded file bytes properly
31
+ doc = ezdxf.read(file_bytes)
32
  except Exception as e:
33
  st.error(f"Error reading DXF file: {e}")
34
+ return [], 0.75, [], [], [], []
35
 
36
  msp = doc.modelspace()
37
  rooms = []
 
39
  doors = []
40
  windows = []
41
  wall_thickness = DEFAULT_WALL_THICKNESS
42
+ wall_pairs = []
43
 
44
  # Detecting walls (parallel lines that represent wall edges)
45
  wall_lines = []
 
48
  wall_lines.append(entity)
49
 
50
  # Check for parallel lines representing a wall (detect double lines)
 
51
  for i, line1 in enumerate(wall_lines):
52
  for line2 in wall_lines[i + 1:]:
53
  if are_lines_parallel(line1, line2):
 
184
  rect = patches.Rectangle((x0, y0), width, height, linewidth=1, edgecolor='black', facecolor='none')
185
  ax.add_patch(rect)
186
  ax.text(x0 + width / 2, y0 - 2, f"{round(width / 12, 1)} ft", ha='center', fontsize=8)
187
+ ax.text(x1 + 2, y0 + height / 2, f"{round(height / 12, 1)} ft", va='center', fontsize=8)
188
+
189
+ ax.set_aspect('equal', 'box')
190
+ ax.set_title("2D Room Layout")
191
+ ax.set_xlabel("X (ft)")
192
+ ax.set_ylabel("Y (ft)")
193
+ plt.axis('off')
194
+
195
+ img_path = "/tmp/room_layout.png"
196
+ plt.savefig(img_path)
197
+ plt.close()
198
+ return img_path
199
+
200
+
201
+ # Main function to handle Streamlit interface
202
+ if uploaded_file is not None:
203
+ file_bytes = uploaded_file.read()
204
+
205
+ try:
206
+ rooms, wall_thickness, doors, windows, room_shapes, wall_pairs = extract_geometry(file_bytes)
207
+ st.write(f"Found {len(rooms)} rooms, {len(doors)} doors, {len(windows)} windows.")
208
+
209
+ if rooms:
210
+ number_of_bricks, sand_volume, cement_bags, calc_details = estimate(rooms, wall_thickness, doors, windows)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
 
212
+ st.write(f"Estimated Bricks Required: {number_of_bricks}")
213
+ st.write(f"Estimated Cement Bags: {cement_bags}")
214
+ st.write(f"Estimated Sand Volume (cft): {sand_volume}")
215
+
216
+ pdf_file = generate_pdf({
217
+ "Bricks Required": number_of_bricks,
218
+ "Cement Bags": cement_bags,
219
+ "Sand Volume (cft)": sand_volume
220
+ }, calc_details, room_shapes)
221
+
222
+ with open(pdf_file, "rb") as f:
223
+ st.download_button("Download Estimation PDF", f, file_name="estimation_report.pdf")
224
+
225
+ except Exception as e:
226
+ st.error(f"Error: {str(e)}")