2d to 3d floor plan converter that gives labels similar to 2d plan with 3d visualization
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")
# Sidebar controls
st.sidebar.header("Room Configuration")
room_width = st.sidebar.number_input("Room Width (meters)", min_value=1.0, max_value=20.0, value=10.0)
room_length = st.sidebar.number_input("Room Length (meters)", min_value=1.0, max_value=20.0, value=12.0)
room_height = st.sidebar.number_input("Room Height (meters)", min_value=2.0, max_value=5.0, value=3.0)
# Add furniture
st.sidebar.header("Add Furniture")
add_furniture = st.sidebar.checkbox("Add Sample Furniture")
def create_room_3d(width, length, height, add_furniture=False):
# Create the basic room structure
x = [0, width, width, 0, 0]
y = [0, 0, length, length, 0]
z = [0, 0, 0, 0, 0]
# Create walls
walls = []
# Floor
walls.append(go.Mesh3d(
x=[0, width, width, 0],
y=[0, 0, length, length],
z=[0, 0, 0, 0],
color='lightgray',
opacity=0.7,
name='Floor'
))
# Walls
walls.append(go.Mesh3d(
x=[0, width, width, 0],
y=[0, 0, 0, 0],
z=[0, 0, height, height],
color='white',
opacity=0.5,
name='Wall 1'
))
walls.append(go.Mesh3d(
x=[width, width, width, width],
y=[0, length, length, 0],
z=[0, 0, height, height],
color='white',
opacity=0.5,
name='Wall 2'
))
# Create the 3D figure
fig = go.Figure(data=walls)
if add_furniture:
# Add a sample bed
bed_width = 2
bed_length = 3
bed_height = 0.5
bed_x = 1
bed_y = 1
fig.add_trace(go.Mesh3d(
x=[bed_x, bed_x + bed_width, bed_x + bed_width, bed_x],
y=[bed_y, bed_y, bed_y + bed_length, bed_y + bed_length],
z=[0, 0, 0, 0],
color='brown',
opacity=0.8,
name='Bed'
))
# Add a sample table
table_width = 1.5
table_length = 1.5
table_height = 0.75
table_x = width - 2
table_y = length - 2
fig.add_trace(go.Mesh3d(
x=[table_x, table_x + table_width, table_x + table_width, table_x],
y=[table_y, table_y, table_y + table_length, table_y + table_length],
z=[0, 0, 0, 0],
color='sandybrown',
opacity=0.8,
name='Table'
))
# Update layout
fig.update_layout(
scene=dict(
xaxis_title='Width (m)',
yaxis_title='Length (m)',
zaxis_title='Height (m)',
aspectmode='data'
),
width=800,
height=600,
showlegend=True
)
return fig
# Create tabs for different views
tab1, tab2 = st.tabs(["3D View", "2D Floor Plan"])
with tab1:
st.header("3D Visualization")
fig_3d = create_room_3d(room_width, room_length, room_height, add_furniture)
st.plotly_chart(fig_3d, use_container_width=True)
with tab2:
st.header("2D Floor Plan")
fig_2d = go.Figure()
# Draw room outline
fig_2d.add_trace(go.Scatter(
x=[0, room_width, room_width, 0, 0],
y=[0, 0, room_length, room_length, 0],
mode='lines',
name='Room Outline',
line=dict(color='black', width=2)
))
if add_furniture:
# Add bed in 2D
bed_x = 1
bed_y = 1
bed_width = 2
bed_length = 3
fig_2d.add_trace(go.Scatter(
x=[bed_x, bed_x + bed_width, bed_x + bed_width, bed_x, bed_x],
y=[bed_y, bed_y, bed_y + bed_length, bed_y + bed_length, bed_y],
mode='lines',
name='Bed',
fill='toself',
line=dict(color='brown')
))
# Add table in 2D
table_x = room_width - 2
table_y = room_length - 2
table_width = 1.5
table_length = 1.5
fig_2d.add_trace(go.Scatter(
x=[table_x, table_x + table_width, table_x + table_width, table_x, table_x],
y=[table_y, table_y, table_y + table_length, table_y + table_length, table_y],
mode='lines',
name='Table',
fill='toself',
line=dict(color='sandybrown')
))
fig_2d.update_layout(
showlegend=True,
yaxis=dict(
scaleanchor="x",
scaleratio=1,
),
width=800,
height=600
)
st.plotly_chart(fig_2d, use_container_width=True)
# Instructions
st.sidebar.markdown("""
## How to use:
1. Adjust room dimensions using the sliders
2. Toggle furniture visibility
3. Switch between 3D and 2D views using the tabs
4. Rotate and zoom the 3D view using your mouse
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?