boxespy/boxes/generators/flexbox3.py

168 lines
5.2 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
2014-03-16 18:26:12 +01:00
# Copyright (C) 2013-2014 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/>.
2013-09-01 01:59:24 +02:00
from boxes import *
2016-08-17 15:07:41 +02:00
2016-07-31 14:15:06 +02:00
class FlexBox3(Boxes):
"""Box with living hinge"""
2016-08-17 15:07:41 +02:00
ui_group = "FlexBox"
2023-01-08 19:41:02 +01:00
def __init__(self) -> None:
Boxes.__init__(self)
2017-02-16 18:50:47 +01:00
self.addSettingsArgs(edges.FingerJointSettings, surroundingspaces=1)
self.addSettingsArgs(edges.FlexSettings)
self.buildArgParser("x", "y", "outside")
self.argparser.add_argument(
2016-08-17 15:07:41 +02:00
"--z", action="store", type=float, default=100.0,
help="height of the box")
self.argparser.add_argument(
2016-08-17 15:07:41 +02:00
"--h", action="store", type=float, default=10.0,
help="height of the lid")
self.argparser.add_argument(
2016-08-17 15:07:41 +02:00
"--radius", action="store", type=float, default=10.0,
help="radius of the lids living hinge")
self.argparser.add_argument(
2016-08-17 15:07:41 +02:00
"--c", action="store", type=float, default=1.0,
dest="d", help="clearance of the lid")
2013-09-01 01:59:24 +02:00
def flexBoxSide(self, x, y, r, callback=None, move=None):
t = self.thickness
if self.move(x+2*t, y+t, move, True):
return
2013-09-01 01:59:24 +02:00
self.moveTo(t, t)
2013-09-01 01:59:24 +02:00
self.cc(callback, 0)
2016-04-02 20:29:09 +02:00
self.edges["f"](x)
2013-09-01 01:59:24 +02:00
self.corner(90, 0)
self.cc(callback, 1)
2016-08-17 15:07:41 +02:00
self.edges["f"](y - r)
2013-09-01 01:59:24 +02:00
self.corner(90, r)
self.cc(callback, 2)
2016-08-17 15:07:41 +02:00
self.edge(x - r)
2013-09-01 01:59:24 +02:00
self.corner(90, 0)
self.cc(callback, 3)
2016-04-02 20:29:09 +02:00
self.edges["f"](y)
2013-09-01 01:59:24 +02:00
self.corner(90)
self.move(x+2*t, y+t, move)
def surroundingWall(self, move=None):
x, y, z, r, d = self.x, self.y, self.z, self.radius, self.d
t = self.thickness
2019-08-08 22:53:08 +02:00
tw = x + y - 2*r + self.c4 + 2*t + t
th = z + 4*t + 2*d
if self.move(tw, th, move, True):
return
self.moveTo(t, d + t)
2016-08-17 15:07:41 +02:00
self.edges["F"](y - r, False)
self.edges["X"](self.c4, z + 2 * t)
2013-09-01 01:59:24 +02:00
self.corner(-90)
self.edge(d)
self.corner(90)
2019-08-08 22:53:08 +02:00
self.edges["f"](x - r + t)
2013-09-01 01:59:24 +02:00
self.corner(90)
self.edges["f"](z + 2 * t + 2 * d)
2013-09-01 01:59:24 +02:00
self.corner(90)
2019-08-08 22:53:08 +02:00
self.edges["f"](x - r + t)
2013-09-01 01:59:24 +02:00
self.corner(90)
self.edge(d)
self.corner(-90)
self.edge(self.c4)
2016-08-17 15:07:41 +02:00
self.edges["F"](y - r)
2013-09-01 01:59:24 +02:00
self.corner(90)
self.edge(t)
2013-09-01 01:59:24 +02:00
self.edges["f"](z)
self.edge(t)
2013-09-01 01:59:24 +02:00
self.corner(90)
self.move(tw, th, move)
def lidSide(self, move=None):
x, y, z, r, d, h = self.x, self.y, self.z, self.radius, self.d, self.h
2013-09-01 01:59:24 +02:00
t = self.thickness
2016-08-17 15:07:41 +02:00
r2 = r + t if r + t <= h + t else h + t
if r < h:
r2 = r + t
base_l = x + 2 * t
if self.move(h+t, base_l+t, move, True):
return
self.edge(h + self.thickness - r2)
self.corner(90, r2)
2019-08-08 22:53:08 +02:00
self.edge(r - r2 + 1 * t)
else:
a = math.acos((r-h)/(r+t))
ang = math.degrees(a)
2019-08-08 22:53:08 +02:00
base_l = x + (r+t) * math.sin(a) - r + t
if self.move(h+t, base_l+t, move, True):
return
self.corner(90-ang)
self.corner(ang, r+t)
2019-08-08 22:53:08 +02:00
self.edges["F"](x - r + t)
self.edgeCorner("F", "f")
self.edges["g"](h)
self.edgeCorner("f", "e")
self.edge(base_l)
self.corner(90)
2013-09-01 01:59:24 +02:00
self.move(h+t, base_l+t, move)
2013-09-01 01:59:24 +02:00
def render(self):
if self.outside:
self.x = self.adjustSize(self.x)
self.y = self.adjustSize(self.y)
self.z = self.adjustSize(self.z)
x, y, z, d, h = self.x, self.y, self.z, self.d, self.h
2016-08-17 15:07:41 +02:00
r = self.radius = self.radius or min(x, y) / 2.0
thickness = self.thickness
self.c4 = c4 = math.pi * r * 0.5 * 0.95
2016-08-17 15:07:41 +02:00
self.latchsize = 8 * thickness
2016-08-17 15:07:41 +02:00
width = 2 * x + y - 2 * r + c4 + 14 * thickness + 3 * h # lock
height = y + z + 8 * thickness
2017-02-16 18:50:47 +01:00
s = edges.FingerJointSettings(self.thickness, finger=1.,
space=1., surroundingspaces=1)
s.edgeObjects(self, "gGH")
2013-09-01 01:59:24 +02:00
with self.saved_context():
self.surroundingWall(move="right")
self.rectangularWall(x, z, edges="FFFF", move="right")
self.rectangularWall(h, z + 2 * (d + self.thickness), edges="GeGF", move="right")
self.lidSide(move="right")
self.lidSide(move="mirror right")
self.surroundingWall(move="up only")
self.flexBoxSide(x, y, r, move="right")
self.flexBoxSide(x, y, r, move="mirror right")
2013-09-01 01:59:24 +02:00
self.rectangularWall(z, y, edges="fFeF")
2016-08-17 15:07:41 +02:00