2d to 3d floor plan conversion with 3d view of plan layout
To upload files, please first save the app
import streamlit as st
import numpy as np
import plotly.graph_objects as go
st.title("2D to 3D Floor Plan Converter")
# Sample floor plan data
room_coordinates = {
"Living Room": [(0, 0), (5, 0), (5, 4), (0, 4)],
"Kitchen": [(5, 0), (8, 0), (8, 3), (5, 3)],
"Bedroom": [(0, 4), (4, 4), (4, 7), (0, 7)],
"Bathroom": [(4, 4), (6, 4), (6, 6), (4, 6)]
}
# Room heights
room_heights = {
"Living Room": 3.0,
"Kitchen": 3.0,
"Bedroom": 3.0,
"Bathroom": 3.0
}
def create_3d_floor_plan():
fig = go.Figure()
colors = {
"Living Room": "lightblue",
"Kitchen": "lightgreen",
"Bedroom": "pink",
"Bathroom": "lavender"
}
# Create walls for each room
for room_name, coords in room_coordinates.items():
height = room_heights[room_name]
color = colors[room_name]
# Floor
x = [coord[0] for coord in coords]
y = [coord[1] for coord in coords]
z = [0] * len(coords)
fig.add_trace(go.Mesh3d(
x=x + x,
y=y + y,
z=z + [height] * len(coords),
i=[0, 0, 0],
j=[1, 2, 3],
k=[2, 3, 1],
color=color,
opacity=0.7,
name=room_name
))
# Walls
for i in range(len(coords)):
x_wall = [coords[i][0], coords[(i+1)%len(coords)][0]]
y_wall = [coords[i][1], coords[(i+1)%len(coords)][1]]
z_wall = [0, 0]
fig.add_trace(go.Mesh3d(
x=x_wall + x_wall,
y=y_wall + y_wall,
z=z_wall + [height, height],
i=[0],
j=[1],
k=[2],
color='lightgrey',
opacity=0.5
))
fig.update_layout(
scene=dict(
aspectmode='data',
camera=dict(
up=dict(x=0, y=0, z=1),
center=dict(x=0, y=0, z=0),
eye=dict(x=1.5, y=1.5, z=1.5)
)
),
showlegend=True,
title="3D Floor Plan"
)
return fig
# Add height adjustment sliders
st.sidebar.header("Room Height Adjustments")
for room in room_heights.keys():
room_heights[room] = st.sidebar.slider(
f"{room} Height (meters)",
min_value=2.0,
max_value=4.0,
value=3.0,
step=0.1
)
# Display the 3D floor plan
st.plotly_chart(create_3d_floor_plan(), use_container_width=True)
# Add explanation
st.markdown("""
### How it works
This app converts a 2D floor plan into a 3D visualization where you can:
- Adjust room heights using the sliders in the sidebar
- Rotate, zoom, and pan the 3D view
- See different rooms highlighted in different colors
- View walls and floor structure
### Controls
- **Rotate**: Click and drag
- **Zoom**: Scroll or pinch
- **Pan**: Right-click and drag
- **Reset View**: Double click
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?