boxespy/boxes/generators/flexbox.py

127 lines
3.9 KiB
Python
Raw 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-05-30 18:38:10 +02:00
import math
import boxes
2016-08-17 15:07:41 +02:00
2013-05-30 18:38:10 +02:00
class FlexBox(boxes.Boxes):
"""Box with living hinge and round corners"""
2016-08-17 15:07:41 +02:00
ui_group = "FlexBox"
def __init__(self):
boxes.Boxes.__init__(self)
self.addSettingsArgs(boxes.edges.FingerJointSettings)
self.addSettingsArgs(boxes.edges.FlexSettings)
self.buildArgParser("x", "y", "h", "outside")
self.argparser.add_argument(
2016-08-17 15:07:41 +02:00
"--radius", action="store", type=float, default=15,
help="Radius of the latch in mm")
2018-10-19 21:35:11 +02:00
self.argparser.add_argument(
"--latchsize", action="store", type=float, default=8,
help="size of latch in multiples of thickness")
2016-08-17 15:07:41 +02:00
2018-10-19 21:33:52 +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
self.moveTo(t+r, t)
2016-08-17 15:07:41 +02:00
2013-05-30 18:38:10 +02:00
for i, l in zip(range(2), (x, y)):
self.cc(callback, i)
2016-08-17 15:07:41 +02:00
self.edges["f"](l - 2 * r)
2013-05-30 18:38:10 +02:00
self.corner(90, r)
2016-08-17 15:07:41 +02:00
2013-05-30 18:38:10 +02:00
self.cc(callback, 2)
2016-08-17 15:07:41 +02:00
self.edge(x - 2 * r)
2013-05-30 18:38:10 +02:00
self.corner(90, r)
self.cc(callback, 3)
self.latch(self.latchsize)
2013-05-30 18:38:10 +02:00
self.cc(callback, 4)
2016-08-17 15:07:41 +02:00
self.edges["f"](y - 2 * r - self.latchsize)
2013-05-30 18:38:10 +02:00
self.corner(90, r)
2018-10-19 21:33:52 +02:00
self.move(x+2*t, y+t, move)
2016-07-09 10:31:09 +02:00
2018-10-19 21:33:52 +02:00
def surroundingWall(self, move=None):
x, y, h, r = self.x, self.y, self.h, self.radius
t = self.thickness
2013-05-30 18:38:10 +02:00
c4 = math.pi * r * 0.5
2018-10-19 21:33:52 +02:00
tw = 2*x + 2*y - 8*r + 4*c4
th = h + 2.5*t
if self.move(tw, th, move, True):
return
self.moveTo(0, 0.25*t)
2016-08-17 15:07:41 +02:00
self.edges["F"](y - 2 * r - self.latchsize, False)
2018-10-19 21:33:52 +02:00
if x - 2 * r < t:
self.edges["X"](2 * c4 + x - 2 * r, h + 2 * t)
else:
2018-10-19 21:33:52 +02:00
self.edges["X"](c4, h + 2 * t)
2016-08-17 15:07:41 +02:00
self.edges["F"](x - 2 * r, False)
2018-10-19 21:33:52 +02:00
self.edges["X"](c4, h + 2 * t)
2016-08-17 15:07:41 +02:00
self.edges["F"](y - 2 * r, False)
2018-10-19 21:33:52 +02:00
if x - 2 * r < t:
self.edges["X"](2 * c4 + x - 2 * r, h + 2 * t)
else:
2018-10-19 21:33:52 +02:00
self.edges["X"](c4, h + 2 * t)
2016-08-17 15:07:41 +02:00
self.edge(x - 2 * r)
2018-10-19 21:33:52 +02:00
self.edges["X"](c4, h + 2 * t)
self.latch(self.latchsize, False)
2018-10-19 21:33:52 +02:00
self.edge(h + 2 * t)
self.latch(self.latchsize, False, True)
2013-05-30 18:38:10 +02:00
self.edge(c4)
2016-08-17 15:07:41 +02:00
self.edge(x - 2 * r)
2013-05-30 18:38:10 +02:00
self.edge(c4)
2016-08-17 15:07:41 +02:00
self.edges["F"](y - 2 * r, False)
2013-05-30 18:38:10 +02:00
self.edge(c4)
2016-08-17 15:07:41 +02:00
self.edges["F"](x - 2 * r, False)
2013-05-30 18:38:10 +02:00
self.edge(c4)
2016-08-17 15:07:41 +02:00
self.edges["F"](y - 2 * r - self.latchsize, False)
2013-05-30 18:38:10 +02:00
self.corner(90)
2018-10-19 21:33:52 +02:00
self.edge(h + 2 * t)
2013-05-30 18:38:10 +02:00
self.corner(90)
2018-10-19 21:33:52 +02:00
self.move(tw, th, move)
2013-05-30 18:38:10 +02:00
def render(self):
if self.outside:
self.x = self.adjustSize(self.x)
self.y = self.adjustSize(self.y)
self.h = self.adjustSize(self.h)
x, y, h = self.x, self.y, self.h
2018-10-19 21:35:11 +02:00
self.latchsize *= self.thickness
2016-08-17 15:07:41 +02:00
r = self.radius or min(x, y - self.latchsize) / 2.0
r = min(r, x / 2.0)
self.radius = r = min(r, max(0, (y - self.latchsize) / 2.0))
2018-10-19 21:33:52 +02:00
self.surroundingWall(move="up")
self.flexBoxSide(self.x, self.y, self.radius, move="right")
self.flexBoxSide(self.x, self.y, self.radius, move="mirror")
2013-05-30 18:38:10 +02:00
2016-08-17 15:07:41 +02:00