boxespy/boxes/generators/displayshelf.py

204 lines
7.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# Copyright (C) 2013-2016 Florian Festi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from boxes import *
2023-01-09 21:18:31 +01:00
class DisplayShelf(Boxes):
"""Shelf with slanted floors"""
2022-12-07 03:29:59 +01:00
ui_group = "Shelf"
2023-01-09 21:18:31 +01:00
# arguments/properties
num: int
x: float
y: float
h: float
angle: float
thickness: float
radians: float
sl: float
front_wall_height: float
include_back: bool
slope_top: bool
outside: bool
num_of_dividers: int
divider_wall_height: float
2023-01-08 19:41:02 +01:00
def __init__(self) -> None:
Boxes.__init__(self)
self.addSettingsArgs(edges.FingerJointSettings)
2018-04-15 19:41:14 +02:00
self.buildArgParser(x=400, y=100, h=300, outside=True)
self.argparser.add_argument(
2023-01-09 21:18:31 +01:00
"--num", action="store", type=int, default=3,
help="number of shelves")
self.argparser.add_argument(
2023-01-09 21:18:31 +01:00
"--front_wall_height", action="store", type=float, default=20.0,
help="height of front walls")
self.argparser.add_argument(
2023-01-09 21:18:31 +01:00
"--angle", action="store", type=float, default=30.0,
help="angle of floors (negative values for slanting backwards)")
2022-12-07 03:29:59 +01:00
self.argparser.add_argument(
"--include_back", action="store", type=boolarg, default=False,
help="Include panel on the back of the shelf")
self.argparser.add_argument(
"--slope_top", action="store", type=boolarg, default=False,
help="Slope the sides and the top by front wall height")
2023-01-09 21:18:31 +01:00
self.argparser.add_argument(
"--num_of_dividers", action="store", type=int, default=0,
help="number of dividers")
self.argparser.add_argument(
"--divider_wall_height", action="store", type=float, default=20.0,
help="height of divider walls")
2022-12-07 03:29:59 +01:00
def generate_finger_holes(self):
t = self.thickness
2022-12-07 03:29:59 +01:00
a = self.radians
2023-01-09 21:18:31 +01:00
hs = (self.sl + t) * math.sin(a) + math.cos(a) * t
for i in range(self.num):
2023-01-09 21:18:31 +01:00
pos_x = abs(0.5 * t * math.sin(a))
pos_y = hs - math.cos(a) * 0.5 * t + i * (self.h - abs(hs)) / (self.num - 0.5)
if a < 0:
pos_y += -math.sin(a) * self.sl
self.fingerHolesAt(pos_x, pos_y, self.sl, -self.angle)
2023-01-09 21:18:31 +01:00
pos_x += math.cos(-a) * (self.sl + 0.5 * t) + math.sin(a) * 0.5 * t
pos_y += math.sin(-a) * (self.sl + 0.5 * t) + math.cos(a) * 0.5 * t
self.fingerHolesAt(pos_x, pos_y, self.front_wall_height, 90 - self.angle)
2022-12-07 03:29:59 +01:00
def generate_sloped_sides(self, width, height):
2023-01-09 21:18:31 +01:00
top_segment_height = height / self.num
2022-12-07 03:29:59 +01:00
a = self.radians
2023-01-09 21:18:31 +01:00
# Maximum size to cut out
2022-12-07 03:29:59 +01:00
vertical_cut = top_segment_height - self.front_wall_height
hypotenuse = vertical_cut / math.sin(a)
horizontal_cut = math.sqrt((hypotenuse ** 2) - (vertical_cut ** 2))
2023-01-09 21:18:31 +01:00
if horizontal_cut > width:
# Shrink the cut to keep the full height
horizontal_cut = width - 1 # keep a 1mm edge on the top
vertical_cut = horizontal_cut * math.tan(a)
2022-12-07 03:29:59 +01:00
hypotenuse = math.sqrt((horizontal_cut ** 2) + (vertical_cut ** 2))
top = width - horizontal_cut
front = height - vertical_cut
2023-01-09 21:18:31 +01:00
borders = [width, 90, front, 90 - self.angle, hypotenuse, self.angle, top, 90, height, 90]
2022-12-07 03:29:59 +01:00
edges = 'eeeef' if self.include_back else 'e'
self.polygonWall(borders, edge=edges, callback=[self.generate_finger_holes], move="up", label="left side")
self.polygonWall(borders, edge=edges, callback=[self.generate_finger_holes], move="up", label="right side")
def generate_rectangular_sides(self, width, height):
edges = "eeee"
if self.include_back:
edges = "eeef"
self.rectangularWall(width, height, edges, callback=[self.generate_finger_holes], move="up", label="left side")
self.rectangularWall(width, height, edges, callback=[self.generate_finger_holes], move="up", label="right side")
2023-01-09 21:18:31 +01:00
def generate_shelve_finger_holes(self):
if self.num_of_dividers <= 0:
return
trays = self.num_of_dividers + 1
for i in range(1, trays):
self.fingerHolesAt((self.x / trays) * i, 0, self.sl, 90)
def generate_front_lip_finger_holes(self):
if self.num_of_dividers <= 0:
return
trays = self.num_of_dividers + 1
height = self.front_wall_height
if self.divider_wall_height < height:
height = self.divider_wall_height
for i in range(1, trays):
self.fingerHolesAt((self.x / trays) * i, 0, height, 90)
2022-12-07 03:29:59 +01:00
def generate_shelves(self):
if self.front_wall_height:
for i in range(self.num):
2023-01-09 21:18:31 +01:00
self.rectangularWall(
self.x,
self.sl,
"ffef",
callback=[self.generate_shelve_finger_holes],
move="up",
label=f"shelf {i + 1}"
)
self.rectangularWall(
self.x,
self.front_wall_height,
"Ffef",
callback=[self.generate_front_lip_finger_holes],
move="up",
label=f"front lip {i + 1}"
)
2022-12-07 03:29:59 +01:00
else:
for i in range(self.num):
2023-01-09 21:18:31 +01:00
self.rectangularWall(
self.x,
self.sl,
"Efef",
callback=[self.generate_shelve_finger_holes],
move="up",
label=f"shelf {i + 1}"
)
def generate_dividers(self):
edges_ = "feee"
if self.front_wall_height:
edges_ = "ffee"
if self.divider_wall_height > self.front_wall_height:
edges_ = [
"f",
edges.CompoundEdge(self, "fe", [self.front_wall_height, self.divider_wall_height - self.front_wall_height]),
"e",
"e"
]
for i in range(self.num):
for j in range(self.num_of_dividers):
self.rectangularWall(self.sl, self.divider_wall_height, edges_, move="up", label=f"divider {j + 1} for shelf {i + 1}")
def render(self):
# adjust to the variables you want in the local scope
x, y, h = self.x, self.y, self.h
2022-12-07 03:29:59 +01:00
front = self.front_wall_height
thickness = self.thickness
if self.outside:
self.x = x = self.adjustSize(x)
2022-12-07 03:29:59 +01:00
if self.include_back:
self.y = y = self.adjustSize(y, False)
2022-12-07 03:29:59 +01:00
self.radians = a = math.radians(self.angle)
self.sl = (y - (thickness * (math.cos(a) + abs(math.sin(a)))) - max(0, math.sin(a) * front)) / math.cos(a)
# render your parts here
2022-12-07 03:29:59 +01:00
if self.slope_top:
self.generate_sloped_sides(y, h)
else:
2022-12-07 03:29:59 +01:00
self.generate_rectangular_sides(y, h)
self.generate_shelves()
2023-01-09 21:18:31 +01:00
if self.num_of_dividers > 0:
self.generate_dividers()
2022-12-07 03:29:59 +01:00
if self.include_back:
self.rectangularWall(x, h, "eFeF", label="back wall", move="up")