Skip to content

Commit

Permalink
ENH: enable splitting STL into multiple tiles and zip them (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
fgebhart authored Apr 25, 2022
1 parent 9d42e37 commit 214839b
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 77 deletions.
20 changes: 14 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
ABOUT,
BTN_LABEL_CREATE_STL,
BTN_LABEL_DOWNLOAD_STL,
DEFAULT_TILING_FORMAT,
DISK_CLEANING_THRESHOLD,
MAP_CENTER,
MAP_ZOOM,
MAX_ALLOWED_AREA_SIZE,
SQUARED_SIDE_RATIO,
ModelSizeSlider,
SquaredCheckbox,
TilingSelect,
ZOffsetSlider,
ZScaleSlider,
)
Expand Down Expand Up @@ -60,7 +62,7 @@ def _compute_stl(geometry: dict, progress_bar: st.progress) -> None:
geo_hash = get_hash_of_geojson(geometry)
mapa_cache_dir = TMPDIR()
run_cleanup_job(path=mapa_cache_dir, disk_cleaning_threshold=DISK_CLEANING_THRESHOLD)
path = mapa_cache_dir / f"{geo_hash}.stl"
path = mapa_cache_dir / geo_hash
progress_bar.progress(0)
convert_bbox_to_stl(
bbox_geometry=geometry,
Expand All @@ -70,6 +72,7 @@ def _compute_stl(geometry: dict, progress_bar: st.progress) -> None:
cut_to_format_ratio=SQUARED_SIDE_RATIO if ensure_squared else None,
output_file=path,
progress_bar=progress_bar,
split_area_in_tiles=DEFAULT_TILING_FORMAT if tiling_option is None else tiling_option,
)
# it is important to spawn this success message in the sidebar, because state will get lost otherwise
st.sidebar.success("Successfully computed STL file!")
Expand Down Expand Up @@ -98,7 +101,7 @@ def _download_btn(data: str, disabled: bool) -> None:
st.sidebar.download_button(
label=BTN_LABEL_DOWNLOAD_STL,
data=data,
file_name=f'{datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}_mapa-streamlit.stl',
file_name=f'{datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}_mapa-streamlit.zip',
disabled=disabled,
)

Expand Down Expand Up @@ -181,9 +184,9 @@ def _get_active_drawing_hash(state: st.AutoSessionState, drawings: List[str]) ->
unsafe_allow_html=True,
)

stl_path = TMPDIR() / f"{geo_hash}.stl"
if stl_path.is_file():
with open(stl_path, "rb") as fp:
output_file = TMPDIR() / f"{geo_hash}.zip"
if output_file.is_file():
with open(output_file, "rb") as fp:
_download_btn(fp, False)
else:
_download_btn(b"None", True)
Expand All @@ -195,7 +198,7 @@ def _get_active_drawing_hash(state: st.AutoSessionState, drawings: List[str]) ->
st.write(
"""
# Customization
Use below options to customize the output STL file:
Use below options to customize the output:
"""
)
z_offset = st.slider(
Expand Down Expand Up @@ -225,3 +228,8 @@ def _get_active_drawing_hash(state: st.AutoSessionState, drawings: List[str]) ->
label=SquaredCheckbox.label,
help=SquaredCheckbox.help,
)
tiling_option = st.selectbox(
label=TilingSelect.label,
options=TilingSelect.options,
help=TilingSelect.help,
)
12 changes: 12 additions & 0 deletions mapa_streamlit/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from importlib.metadata import version
from typing import Tuple

from mapa_streamlit import __version__

Expand Down Expand Up @@ -34,6 +35,7 @@
DEFAULT_Z_SCALE = 2.0
SQUARED_SIDE_RATIO = 1.0
DEFAULT_MODEL_SIZE = 100
DEFAULT_TILING_FORMAT = "1x1"


class ZOffsetSlider:
Expand Down Expand Up @@ -72,3 +74,13 @@ class SquaredCheckbox:
"rectangle you selected will be cut to achieve this. This option might be helpful, as drawing a perfect "
"square by hand is impossible and because the visual map is projected."
)


class TilingSelect:
label: str = "Split output STL file in multiple tiles?"
options: Tuple[str] = (DEFAULT_TILING_FORMAT, "1x2", "2x1", "2x2", "2x3", "3x2", "3x3")
help: str = (
"Select shape and number of tiles you want the generated output to be spilt into. This is helpful when "
"aiming for a print larger than the printer area. The first number splits the north-south axis and the "
"second number splits the west-east axis."
)
Loading

0 comments on commit 214839b

Please sign in to comment.