Moved edges out into their own submodule
This commit is contained in:
parent
9513d2520f
commit
be6b02fe6f
4
box.py
4
box.py
|
@ -30,8 +30,8 @@ class Box(Boxes):
|
|||
self.edges["f"].settings.setValues(self.thickness, space=3, finger=3,
|
||||
surroundingspaces=1)
|
||||
|
||||
d2 = [Bolts(2)]
|
||||
d3 = [Bolts(3)]
|
||||
d2 = [edges.Bolts(2)]
|
||||
d3 = [edges.Bolts(3)]
|
||||
|
||||
d2 = d3 = None
|
||||
|
||||
|
|
4
box2.py
4
box2.py
|
@ -31,8 +31,8 @@ class Box(Boxes):
|
|||
self.edges["f"].settings.setValues(self.thickness, space=3, finger=3,
|
||||
surroundingspaces=1)
|
||||
|
||||
d2 = [Bolts(2)]
|
||||
d3 = [Bolts(3)]
|
||||
d2 = [edges.Bolts(2)]
|
||||
d3 = [edges.Bolts(3)]
|
||||
|
||||
d2 = d3 = None
|
||||
|
||||
|
|
4
box3.py
4
box3.py
|
@ -31,8 +31,8 @@ class Box(Boxes):
|
|||
self.edges["f"].settings.setValues(self.thickness, space=3, finger=3,
|
||||
surroundingspaces=1)
|
||||
|
||||
d2 = [Bolts(2)]
|
||||
d3 = [Bolts(3)]
|
||||
d2 = [edges.Bolts(2)]
|
||||
d3 = [edges.Bolts(3)]
|
||||
|
||||
d2 = d3 = None
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ import argparse
|
|||
from argparse import ArgumentParser
|
||||
import re
|
||||
from functools import wraps
|
||||
from boxes import edges
|
||||
|
||||
def dist(dx, dy):
|
||||
return (dx*dx+dy*dy)**0.5
|
||||
|
@ -34,430 +35,11 @@ def restore(func):
|
|||
self.ctx.move_to(*pt)
|
||||
return f
|
||||
|
||||
class BoltPolicy:
|
||||
"""Abstract class
|
||||
Distributes (bed) bolts on a number of segments
|
||||
(fingers of a finger joint)
|
||||
"""
|
||||
def drawbolt(self, pos):
|
||||
"""Add a bolt to this segment?"""
|
||||
return False
|
||||
|
||||
def numFingers(self, numfingers):
|
||||
"""returns next smaller, possible number of fingers"""
|
||||
return numFingers
|
||||
|
||||
def _even(self, numFingers):
|
||||
return (numFingers//2) * 2
|
||||
def _odd(self, numFingers):
|
||||
if numFingers % 2:
|
||||
return numFingers
|
||||
else:
|
||||
return numFingers - 1
|
||||
|
||||
class Bolts(BoltPolicy):
|
||||
"""Distribute a fixed number of bolts evenly"""
|
||||
def __init__(self, bolts=1):
|
||||
self.bolts = bolts
|
||||
def numFingers(self, numFingers):
|
||||
if self.bolts % 2:
|
||||
self.fingers = self._even(numFingers)
|
||||
else:
|
||||
self.fingers = numFingers
|
||||
return self.fingers
|
||||
|
||||
def drawBolt(self, pos):
|
||||
if pos > self.fingers//2:
|
||||
pos = self.fingers - pos
|
||||
if pos==0:
|
||||
return False
|
||||
if pos == self.fingers//2 and not (self.bolts % 2):
|
||||
return False
|
||||
result = (math.floor((float(pos)*(self.bolts+1)/self.fingers)-0.01) !=
|
||||
math.floor((float(pos+1)*(self.bolts+1)/self.fingers)-0.01))
|
||||
#print pos, result, ((float(pos)*(self.bolts+1)/self.fingers)-0.01), ((float(pos+1)*(self.bolts+1)/self.fingers)-0.01)
|
||||
return result
|
||||
|
||||
#############################################################################
|
||||
### Settings
|
||||
#############################################################################
|
||||
|
||||
class Settings:
|
||||
absolute_params = { }
|
||||
relative_params = { }
|
||||
|
||||
def __init__(self, thickness, relative=True, **kw):
|
||||
self.values = self.absolute_params.copy()
|
||||
|
||||
factor = 1.0
|
||||
if relative:
|
||||
factor = thickness
|
||||
for name, value in self.relative_params.items():
|
||||
self.values[name] = value * factor
|
||||
self.setValues(thickness, relative, **kw)
|
||||
|
||||
def setValues(self, thickness, relative=True, **kw):
|
||||
factor = 1.0
|
||||
if relative:
|
||||
factor = thickness
|
||||
for name, value in kw.items():
|
||||
if name in self.absolute_params:
|
||||
self.values[name] = value
|
||||
elif name in self.relative_params:
|
||||
self.values[name] = value * factor
|
||||
else:
|
||||
raise ValueError("Unknown parameter for %s: %s" % (
|
||||
self.__class__.__name__, name))
|
||||
|
||||
def __getattr__(self, name):
|
||||
return self.values[name]
|
||||
|
||||
#############################################################################
|
||||
### Edges
|
||||
#############################################################################
|
||||
|
||||
|
||||
class Edge:
|
||||
char = 'e'
|
||||
|
||||
def __init__(self, boxes, settings):
|
||||
self.boxes = boxes
|
||||
self.ctx = boxes.ctx
|
||||
self.settings = settings
|
||||
|
||||
def __getattr__(self, name):
|
||||
"""Hack for using unalter code form Boxes class"""
|
||||
return getattr(self.boxes, name)
|
||||
|
||||
def __call__(self, length, **kw):
|
||||
self.ctx.move_to(0,0)
|
||||
self.ctx.line_to(length, 0)
|
||||
self.ctx.translate(*self.ctx.get_current_point())
|
||||
|
||||
def width(self):
|
||||
return 0.0
|
||||
|
||||
def margin(self):
|
||||
return self.boxes.spacing
|
||||
|
||||
def spacing(self):
|
||||
return self.width() + self.margin()
|
||||
|
||||
def startAngle(self):
|
||||
return 0.0
|
||||
|
||||
def endAngle(self):
|
||||
return 0.0
|
||||
|
||||
class OutSetEdge(Edge):
|
||||
char = 'E'
|
||||
|
||||
def width(self):
|
||||
return self.boxes.thickness
|
||||
|
||||
class CompoundEdge(Edge):
|
||||
def __init__(self, boxes, types, lengths):
|
||||
Edge.__init__(self, boxes, None)
|
||||
self.types = [self.edges.get(edge, edge) for edge in types]
|
||||
self.lengths = lengths
|
||||
self.length = sum(lengths)
|
||||
|
||||
def width(self):
|
||||
return self.types[0].width()
|
||||
|
||||
def margin(self):
|
||||
return max((e.margin() for e in self.types))
|
||||
|
||||
def __call__(self, length, **kw):
|
||||
if length and abs(length - self.length) > 1E-5:
|
||||
raise ValueError("Wrong length for CompoundEdge")
|
||||
for e, l in zip(self.types, self.lengths):
|
||||
# XXX different margins???
|
||||
e(l)
|
||||
|
||||
class Slot(Edge):
|
||||
def __init__(self, boxes, depth):
|
||||
Edge.__init__(self, boxes, None)
|
||||
self.depth = depth
|
||||
|
||||
def __call__(self, length, **kw):
|
||||
if self.depth:
|
||||
self.boxes.corner(90)
|
||||
self.boxes.edge(self.depth)
|
||||
self.boxes.corner(-90)
|
||||
self.boxes.edge(length)
|
||||
self.boxes.corner(-90)
|
||||
self.boxes.edge(self.depth)
|
||||
self.boxes.corner(90)
|
||||
else:
|
||||
self.boxes.edge(self.length)
|
||||
|
||||
class SlottedEdge(Edge):
|
||||
|
||||
def __init__(self, boxes, sections, edge="e", slots=0):
|
||||
Edge.__init__(self, boxes, None)
|
||||
self.edge = self.edges.get(edge, edge)
|
||||
self.sections = sections
|
||||
self.slots = slots
|
||||
|
||||
def width(self):
|
||||
return self.edge.width()
|
||||
|
||||
def margin(self):
|
||||
return self.edge.margin()
|
||||
|
||||
def __call__(self, length, **kw):
|
||||
for l in self.sections[:-1]:
|
||||
self.edge(l)
|
||||
if self.slots:
|
||||
Slot(self.boxes, self.slots)(self.thickness)
|
||||
else:
|
||||
self.edge(self.thickness)
|
||||
self.edge(self.sections[-1])
|
||||
|
||||
class FingerJointSettings(Settings):
|
||||
absolute_params = {
|
||||
"surroundingspaces" : 2,
|
||||
}
|
||||
|
||||
relative_params = {
|
||||
"space" : 1.0,
|
||||
"finger" : 1.0,
|
||||
"height" : 1.0,
|
||||
"width" : 1.0,
|
||||
}
|
||||
|
||||
class FingerJointEdge(Edge):
|
||||
char = 'f'
|
||||
positive = True
|
||||
|
||||
def __call__(self, length,
|
||||
bedBolts=None, bedBoltSettings=None, **kw):
|
||||
positive = self.positive
|
||||
space, finger = self.settings.space, self.settings.finger
|
||||
|
||||
fingers = int((length-(self.settings.surroundingspaces-1)*space) //
|
||||
(space+finger))
|
||||
|
||||
if bedBolts:
|
||||
fingers = bedBolts.numFingers(fingers)
|
||||
leftover = length - fingers*(space+finger) + space
|
||||
|
||||
s, f, thickness = space, finger, self.thickness
|
||||
d, d_nut, h_nut, l, l1 = bedBoltSettings or self.boxes.bedBoltSettings
|
||||
p = 1 if positive else -1
|
||||
|
||||
if fingers <= 0:
|
||||
fingers = 0
|
||||
leftover = length
|
||||
|
||||
self.edge(leftover/2.0)
|
||||
for i in range(fingers):
|
||||
if i !=0:
|
||||
if not positive and bedBolts and bedBolts.drawBolt(i):
|
||||
self.hole(0.5*space,
|
||||
0.5*self.thickness, 0.5*d)
|
||||
if positive and bedBolts and bedBolts.drawBolt(i):
|
||||
self.bedBoltHole(s, bedBoltSettings)
|
||||
else:
|
||||
self.edge(s)
|
||||
self.corner(-90*p)
|
||||
self.edge(self.settings.height)
|
||||
self.corner(90*p)
|
||||
self.edge(f)
|
||||
self.corner(90*p)
|
||||
self.edge(self.settings.height)
|
||||
self.corner(-90*p)
|
||||
self.edge(leftover/2.0)
|
||||
|
||||
def margin(self):
|
||||
return self.boxes.spacing + self.boxes.thickness
|
||||
|
||||
class FingerJointEdgeCounterPart(FingerJointEdge):
|
||||
char = 'F'
|
||||
positive = False
|
||||
|
||||
def width(self):
|
||||
return self.boxes.thickness
|
||||
|
||||
def margin(self):
|
||||
return self.boxes.spacing
|
||||
|
||||
class FingerHoleEdge(Edge):
|
||||
char = 'h'
|
||||
|
||||
def __call__(self, length, dist=None,
|
||||
bedBolts=None, bedBoltSettings=None, **kw):
|
||||
if dist is None:
|
||||
dist = self.fingerHoleEdgeWidth * self.thickness
|
||||
self.ctx.save()
|
||||
self.moveTo(0, dist+self.thickness/2)
|
||||
self.fingerHoles(length, bedBolts, bedBoltSettings)
|
||||
self.ctx.restore()
|
||||
# XXX continue path
|
||||
self.ctx.move_to(0, 0)
|
||||
self.ctx.line_to(length, 0)
|
||||
self.ctx.translate(*self.ctx.get_current_point())
|
||||
|
||||
def width(self):
|
||||
return (self.fingerHoleEdgeWidth+1) * self.thickness
|
||||
|
||||
class CrossingFingerHoleEdge(Edge):
|
||||
def __init__(self, boxes, height, **kw):
|
||||
Edge.__init__(self, boxes, None, **kw)
|
||||
self.height = height
|
||||
|
||||
def __call__(self, length, **kw):
|
||||
self.fingerHolesAt(length/2.0, 0, self.height)
|
||||
Edge.__call__(self, length)
|
||||
|
||||
|
||||
class DoveTailSettings(Settings):
|
||||
absolute_params = {
|
||||
"angle" : 50,
|
||||
}
|
||||
relative_params = {
|
||||
"size" : 3,
|
||||
"depth" : 1.5,
|
||||
"radius" : 0.2,
|
||||
}
|
||||
|
||||
class DoveTailJoint(Edge):
|
||||
char = 'd'
|
||||
positive = True
|
||||
|
||||
def __call__(self, length, **kw):
|
||||
s = self.settings
|
||||
radius = max(s.radius, self.boxes.burn) # no smaller than burn
|
||||
positive = self.positive
|
||||
a = s.angle + 90
|
||||
alpha = 0.5*math.pi - math.pi*s.angle/180.0
|
||||
|
||||
l1 = radius/math.tan(alpha/2.0)
|
||||
diffx = 0.5*s.depth/math.tan(alpha)
|
||||
l2 = 0.5*s.depth / math.sin(alpha)
|
||||
|
||||
sections = int((length) // (s.size*2))
|
||||
leftover = length - sections*s.size*2
|
||||
|
||||
p = 1 if positive else -1
|
||||
|
||||
self.edge((s.size+leftover)/2.0+diffx-l1)
|
||||
for i in range(sections):
|
||||
self.corner(-1*p*a, radius)
|
||||
self.edge(2*(l2-l1))
|
||||
self.corner(p*a, radius)
|
||||
self.edge(2*(diffx-l1)+s.size)
|
||||
self.corner(p*a, radius)
|
||||
self.edge(2*(l2-l1))
|
||||
self.corner(-1*p*a, radius)
|
||||
if i<sections-1: # all but the last
|
||||
self.edge(2*(diffx-l1)+s.size)
|
||||
self.edge((s.size+leftover)/2.0+diffx-l1)
|
||||
self.ctx.translate(*self.ctx.get_current_point())
|
||||
|
||||
def margin(self):
|
||||
return self.settings.depth + self.boxes.spacing
|
||||
|
||||
class DoveTailJointCounterPart(DoveTailJoint):
|
||||
char = 'D'
|
||||
|
||||
positive = False
|
||||
|
||||
def width(self):
|
||||
return self.settings.depth
|
||||
|
||||
def margin(self):
|
||||
return self.boxes.spacing
|
||||
|
||||
class FlexSettings(Settings):
|
||||
relative_params = {
|
||||
"distance" : 0.5,
|
||||
"connection" : 1.0,
|
||||
"width" : 5.0,
|
||||
}
|
||||
absolute_params = {
|
||||
"stretch" : 1.0,
|
||||
}
|
||||
|
||||
class FlexEdge(Edge):
|
||||
char = 'X'
|
||||
|
||||
def __call__(self, x, h, **kw):
|
||||
dist = self.settings.distance
|
||||
connection = self.settings.connection
|
||||
width = self.settings.width
|
||||
|
||||
burn = self.boxes.burn
|
||||
h += 2*burn
|
||||
lines = int(x // dist)
|
||||
leftover = x - lines * dist
|
||||
sections = int((h-connection) // width)
|
||||
sheight = ((h-connection) / sections)-connection
|
||||
|
||||
for i in range(lines):
|
||||
pos = i*dist + leftover/2
|
||||
if i % 2:
|
||||
self.ctx.move_to(pos, 0)
|
||||
self.ctx.line_to(pos, connection+sheight)
|
||||
for j in range((sections-1)//2):
|
||||
self.ctx.move_to(pos, (2*j+1)* sheight+ (2*j+2)*connection)
|
||||
self.ctx.line_to(pos, (2*j+3)* (sheight+ connection))
|
||||
if not sections % 2:
|
||||
self.ctx.move_to(pos, h - sheight- connection)
|
||||
self.ctx.line_to(pos, h)
|
||||
else:
|
||||
if sections % 2:
|
||||
self.ctx.move_to(pos, h)
|
||||
self.ctx.line_to(pos, h-connection-sheight)
|
||||
for j in range((sections-1)//2):
|
||||
self.ctx.move_to(
|
||||
pos, h-((2*j+1)* sheight+ (2*j+2)*connection))
|
||||
self.ctx.line_to(
|
||||
pos, h-(2*j+3)* (sheight+ connection))
|
||||
|
||||
else:
|
||||
for j in range(sections//2):
|
||||
self.ctx.move_to(pos,
|
||||
h-connection-2*j*(sheight+connection))
|
||||
self.ctx.line_to(pos, h-2*(j+1)*(sheight+connection))
|
||||
|
||||
self.ctx.move_to(0, 0)
|
||||
self.ctx.line_to(x, 0)
|
||||
self.ctx.translate(*self.ctx.get_current_point())
|
||||
|
||||
#############################################################################
|
||||
### Building blocks
|
||||
#############################################################################
|
||||
|
||||
class FingerHoles:
|
||||
def __init__(self, boxes, settings):
|
||||
self.boxes = boxes
|
||||
self.ctx = boxes.ctx
|
||||
self.settings = settings
|
||||
|
||||
def __call__(self, length, bedBolts=None, bedBoltSettings=None):
|
||||
s, f = self.settings.space, self.settings.finger
|
||||
fingers = int((length-(self.settings.surroundingspaces-1)*s) //
|
||||
(s+f))
|
||||
if bedBolts:
|
||||
fingers = bedBolts.numFingers(fingers)
|
||||
d, d_nut, h_nut, l, l1 = bedBoltSettings or self.boxes.bedBoltSettings
|
||||
leftover = length - fingers*(s+f) - f
|
||||
b = self.boxes.burn
|
||||
if self.boxes.debug:
|
||||
self.ctx.rectangle(0, -self.settings.width/2+b,
|
||||
length, self.settings.width - 2*b)
|
||||
for i in range(fingers):
|
||||
pos = leftover/2.0+i*(s+f)
|
||||
if bedBolts and bedBolts.drawBolt(i):
|
||||
self.boxes.hole(pos+0.5*s, 0, d*0.5)
|
||||
self.ctx.rectangle(pos+s+b, -self.settings.width/2+b,
|
||||
f-2*b, self.settings.width - 2*b)
|
||||
|
||||
self.ctx.move_to(0, length)
|
||||
self.ctx.translate(*self.ctx.get_current_point())
|
||||
|
||||
class NutHole:
|
||||
sizes = {
|
||||
"M1.6" : (3.2, 1.3),
|
||||
|
@ -579,26 +161,26 @@ class Boxes:
|
|||
name = name[0].lower() + name[1:]
|
||||
#if not hasattr(self, name):
|
||||
setattr(self, name, part)
|
||||
if isinstance(part, Edge):
|
||||
if isinstance(part, edges.Edge):
|
||||
self.edges[part.char] = part
|
||||
|
||||
def _buildObjects(self):
|
||||
self.edges = {}
|
||||
self.addPart(Edge(self, None))
|
||||
self.addPart(OutSetEdge(self, None))
|
||||
self.addPart(edges.Edge(self, None))
|
||||
self.addPart(edges.OutSetEdge(self, None))
|
||||
|
||||
# Share settings object
|
||||
s = FingerJointSettings(self.thickness)
|
||||
self.addPart(FingerJointEdge(self, s))
|
||||
self.addPart(FingerJointEdgeCounterPart(self, s))
|
||||
self.addPart(FingerHoleEdge(self, s))
|
||||
self.addPart(FingerHoles(self, s))
|
||||
s = edges.FingerJointSettings(self.thickness)
|
||||
self.addPart(edges.FingerJointEdge(self, s))
|
||||
self.addPart(edges.FingerJointEdgeCounterPart(self, s))
|
||||
self.addPart(edges.FingerHoleEdge(self, s))
|
||||
self.addPart(edges.FingerHoles(self, s))
|
||||
|
||||
s = DoveTailSettings(self.thickness)
|
||||
self.addPart(DoveTailJoint(self, s))
|
||||
self.addPart(DoveTailJointCounterPart(self, s))
|
||||
s = FlexSettings(self.thickness)
|
||||
self.addPart(FlexEdge(self, s))
|
||||
s = edges.DoveTailSettings(self.thickness)
|
||||
self.addPart(edges.DoveTailJoint(self, s))
|
||||
self.addPart(edges.DoveTailJointCounterPart(self, s))
|
||||
s = edges.FlexSettings(self.thickness)
|
||||
self.addPart(edges.FlexEdge(self, s))
|
||||
|
||||
self.addPart(NutHole(self, None))
|
||||
|
||||
|
|
|
@ -0,0 +1,437 @@
|
|||
#!/usr/bin/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/>.
|
||||
|
||||
import math
|
||||
|
||||
class BoltPolicy:
|
||||
"""Abstract class
|
||||
Distributes (bed) bolts on a number of segments
|
||||
(fingers of a finger joint)
|
||||
"""
|
||||
def drawbolt(self, pos):
|
||||
"""Add a bolt to this segment?"""
|
||||
return False
|
||||
|
||||
def numFingers(self, numfingers):
|
||||
"""returns next smaller, possible number of fingers"""
|
||||
return numFingers
|
||||
|
||||
def _even(self, numFingers):
|
||||
return (numFingers//2) * 2
|
||||
def _odd(self, numFingers):
|
||||
if numFingers % 2:
|
||||
return numFingers
|
||||
else:
|
||||
return numFingers - 1
|
||||
|
||||
class Bolts(BoltPolicy):
|
||||
"""Distribute a fixed number of bolts evenly"""
|
||||
def __init__(self, bolts=1):
|
||||
self.bolts = bolts
|
||||
def numFingers(self, numFingers):
|
||||
if self.bolts % 2:
|
||||
self.fingers = self._even(numFingers)
|
||||
else:
|
||||
self.fingers = numFingers
|
||||
return self.fingers
|
||||
|
||||
def drawBolt(self, pos):
|
||||
if pos > self.fingers//2:
|
||||
pos = self.fingers - pos
|
||||
if pos==0:
|
||||
return False
|
||||
if pos == self.fingers//2 and not (self.bolts % 2):
|
||||
return False
|
||||
result = (math.floor((float(pos)*(self.bolts+1)/self.fingers)-0.01) !=
|
||||
math.floor((float(pos+1)*(self.bolts+1)/self.fingers)-0.01))
|
||||
#print pos, result, ((float(pos)*(self.bolts+1)/self.fingers)-0.01), ((float(pos+1)*(self.bolts+1)/self.fingers)-0.01)
|
||||
return result
|
||||
|
||||
#############################################################################
|
||||
### Settings
|
||||
#############################################################################
|
||||
|
||||
class Settings:
|
||||
absolute_params = { }
|
||||
relative_params = { }
|
||||
|
||||
def __init__(self, thickness, relative=True, **kw):
|
||||
self.values = self.absolute_params.copy()
|
||||
|
||||
factor = 1.0
|
||||
if relative:
|
||||
factor = thickness
|
||||
for name, value in self.relative_params.items():
|
||||
self.values[name] = value * factor
|
||||
self.setValues(thickness, relative, **kw)
|
||||
|
||||
def setValues(self, thickness, relative=True, **kw):
|
||||
factor = 1.0
|
||||
if relative:
|
||||
factor = thickness
|
||||
for name, value in kw.items():
|
||||
if name in self.absolute_params:
|
||||
self.values[name] = value
|
||||
elif name in self.relative_params:
|
||||
self.values[name] = value * factor
|
||||
else:
|
||||
raise ValueError("Unknown parameter for %s: %s" % (
|
||||
self.__class__.__name__, name))
|
||||
|
||||
def __getattr__(self, name):
|
||||
return self.values[name]
|
||||
|
||||
#############################################################################
|
||||
### Edges
|
||||
#############################################################################
|
||||
|
||||
|
||||
class Edge:
|
||||
char = 'e'
|
||||
|
||||
def __init__(self, boxes, settings):
|
||||
self.boxes = boxes
|
||||
self.ctx = boxes.ctx
|
||||
self.settings = settings
|
||||
|
||||
def __getattr__(self, name):
|
||||
"""Hack for using unalter code form Boxes class"""
|
||||
return getattr(self.boxes, name)
|
||||
|
||||
def __call__(self, length, **kw):
|
||||
self.ctx.move_to(0,0)
|
||||
self.ctx.line_to(length, 0)
|
||||
self.ctx.translate(*self.ctx.get_current_point())
|
||||
|
||||
def width(self):
|
||||
return 0.0
|
||||
|
||||
def margin(self):
|
||||
return self.boxes.spacing
|
||||
|
||||
def spacing(self):
|
||||
return self.width() + self.margin()
|
||||
|
||||
def startAngle(self):
|
||||
return 0.0
|
||||
|
||||
def endAngle(self):
|
||||
return 0.0
|
||||
|
||||
class OutSetEdge(Edge):
|
||||
char = 'E'
|
||||
|
||||
def width(self):
|
||||
return self.boxes.thickness
|
||||
|
||||
class CompoundEdge(Edge):
|
||||
def __init__(self, boxes, types, lengths):
|
||||
Edge.__init__(self, boxes, None)
|
||||
self.types = [self.edges.get(edge, edge) for edge in types]
|
||||
self.lengths = lengths
|
||||
self.length = sum(lengths)
|
||||
|
||||
def width(self):
|
||||
return self.types[0].width()
|
||||
|
||||
def margin(self):
|
||||
return max((e.margin() for e in self.types))
|
||||
|
||||
def __call__(self, length, **kw):
|
||||
if length and abs(length - self.length) > 1E-5:
|
||||
raise ValueError("Wrong length for CompoundEdge")
|
||||
for e, l in zip(self.types, self.lengths):
|
||||
# XXX different margins???
|
||||
e(l)
|
||||
|
||||
class Slot(Edge):
|
||||
def __init__(self, boxes, depth):
|
||||
Edge.__init__(self, boxes, None)
|
||||
self.depth = depth
|
||||
|
||||
def __call__(self, length, **kw):
|
||||
if self.depth:
|
||||
self.boxes.corner(90)
|
||||
self.boxes.edge(self.depth)
|
||||
self.boxes.corner(-90)
|
||||
self.boxes.edge(length)
|
||||
self.boxes.corner(-90)
|
||||
self.boxes.edge(self.depth)
|
||||
self.boxes.corner(90)
|
||||
else:
|
||||
self.boxes.edge(self.length)
|
||||
|
||||
class SlottedEdge(Edge):
|
||||
|
||||
def __init__(self, boxes, sections, edge="e", slots=0):
|
||||
Edge.__init__(self, boxes, None)
|
||||
self.edge = self.edges.get(edge, edge)
|
||||
self.sections = sections
|
||||
self.slots = slots
|
||||
|
||||
def width(self):
|
||||
return self.edge.width()
|
||||
|
||||
def margin(self):
|
||||
return self.edge.margin()
|
||||
|
||||
def __call__(self, length, **kw):
|
||||
for l in self.sections[:-1]:
|
||||
self.edge(l)
|
||||
if self.slots:
|
||||
Slot(self.boxes, self.slots)(self.thickness)
|
||||
else:
|
||||
self.edge(self.thickness)
|
||||
self.edge(self.sections[-1])
|
||||
|
||||
class FingerJointSettings(Settings):
|
||||
absolute_params = {
|
||||
"surroundingspaces" : 2,
|
||||
}
|
||||
|
||||
relative_params = {
|
||||
"space" : 1.0,
|
||||
"finger" : 1.0,
|
||||
"height" : 1.0,
|
||||
"width" : 1.0,
|
||||
}
|
||||
|
||||
class FingerJointEdge(Edge):
|
||||
char = 'f'
|
||||
positive = True
|
||||
|
||||
def __call__(self, length,
|
||||
bedBolts=None, bedBoltSettings=None, **kw):
|
||||
positive = self.positive
|
||||
space, finger = self.settings.space, self.settings.finger
|
||||
|
||||
fingers = int((length-(self.settings.surroundingspaces-1)*space) //
|
||||
(space+finger))
|
||||
|
||||
if bedBolts:
|
||||
fingers = bedBolts.numFingers(fingers)
|
||||
leftover = length - fingers*(space+finger) + space
|
||||
|
||||
s, f, thickness = space, finger, self.thickness
|
||||
d, d_nut, h_nut, l, l1 = bedBoltSettings or self.boxes.bedBoltSettings
|
||||
p = 1 if positive else -1
|
||||
|
||||
if fingers <= 0:
|
||||
fingers = 0
|
||||
leftover = length
|
||||
|
||||
self.edge(leftover/2.0)
|
||||
for i in range(fingers):
|
||||
if i !=0:
|
||||
if not positive and bedBolts and bedBolts.drawBolt(i):
|
||||
self.hole(0.5*space,
|
||||
0.5*self.thickness, 0.5*d)
|
||||
if positive and bedBolts and bedBolts.drawBolt(i):
|
||||
self.bedBoltHole(s, bedBoltSettings)
|
||||
else:
|
||||
self.edge(s)
|
||||
self.corner(-90*p)
|
||||
self.edge(self.settings.height)
|
||||
self.corner(90*p)
|
||||
self.edge(f)
|
||||
self.corner(90*p)
|
||||
self.edge(self.settings.height)
|
||||
self.corner(-90*p)
|
||||
self.edge(leftover/2.0)
|
||||
|
||||
def margin(self):
|
||||
return self.boxes.spacing + self.boxes.thickness
|
||||
|
||||
class FingerJointEdgeCounterPart(FingerJointEdge):
|
||||
char = 'F'
|
||||
positive = False
|
||||
|
||||
def width(self):
|
||||
return self.boxes.thickness
|
||||
|
||||
def margin(self):
|
||||
return self.boxes.spacing
|
||||
|
||||
class FingerHoleEdge(Edge):
|
||||
char = 'h'
|
||||
|
||||
def __call__(self, length, dist=None,
|
||||
bedBolts=None, bedBoltSettings=None, **kw):
|
||||
if dist is None:
|
||||
dist = self.fingerHoleEdgeWidth * self.thickness
|
||||
self.ctx.save()
|
||||
self.moveTo(0, dist+self.thickness/2)
|
||||
self.fingerHoles(length, bedBolts, bedBoltSettings)
|
||||
self.ctx.restore()
|
||||
# XXX continue path
|
||||
self.ctx.move_to(0, 0)
|
||||
self.ctx.line_to(length, 0)
|
||||
self.ctx.translate(*self.ctx.get_current_point())
|
||||
|
||||
def width(self):
|
||||
return (self.fingerHoleEdgeWidth+1) * self.thickness
|
||||
|
||||
class FingerHoles:
|
||||
def __init__(self, boxes, settings):
|
||||
self.boxes = boxes
|
||||
self.ctx = boxes.ctx
|
||||
self.settings = settings
|
||||
|
||||
def __call__(self, length, bedBolts=None, bedBoltSettings=None):
|
||||
s, f = self.settings.space, self.settings.finger
|
||||
fingers = int((length-(self.settings.surroundingspaces-1)*s) //
|
||||
(s+f))
|
||||
if bedBolts:
|
||||
fingers = bedBolts.numFingers(fingers)
|
||||
d, d_nut, h_nut, l, l1 = bedBoltSettings or self.boxes.bedBoltSettings
|
||||
leftover = length - fingers*(s+f) - f
|
||||
b = self.boxes.burn
|
||||
if self.boxes.debug:
|
||||
self.ctx.rectangle(0, -self.settings.width/2+b,
|
||||
length, self.settings.width - 2*b)
|
||||
for i in range(fingers):
|
||||
pos = leftover/2.0+i*(s+f)
|
||||
if bedBolts and bedBolts.drawBolt(i):
|
||||
self.boxes.hole(pos+0.5*s, 0, d*0.5)
|
||||
self.ctx.rectangle(pos+s+b, -self.settings.width/2+b,
|
||||
f-2*b, self.settings.width - 2*b)
|
||||
|
||||
self.ctx.move_to(0, length)
|
||||
self.ctx.translate(*self.ctx.get_current_point())
|
||||
|
||||
class CrossingFingerHoleEdge(Edge):
|
||||
def __init__(self, boxes, height, **kw):
|
||||
Edge.__init__(self, boxes, None, **kw)
|
||||
self.height = height
|
||||
|
||||
def __call__(self, length, **kw):
|
||||
self.fingerHolesAt(length/2.0, 0, self.height)
|
||||
Edge.__call__(self, length)
|
||||
|
||||
|
||||
class DoveTailSettings(Settings):
|
||||
absolute_params = {
|
||||
"angle" : 50,
|
||||
}
|
||||
relative_params = {
|
||||
"size" : 3,
|
||||
"depth" : 1.5,
|
||||
"radius" : 0.2,
|
||||
}
|
||||
|
||||
class DoveTailJoint(Edge):
|
||||
char = 'd'
|
||||
positive = True
|
||||
|
||||
def __call__(self, length, **kw):
|
||||
s = self.settings
|
||||
radius = max(s.radius, self.boxes.burn) # no smaller than burn
|
||||
positive = self.positive
|
||||
a = s.angle + 90
|
||||
alpha = 0.5*math.pi - math.pi*s.angle/180.0
|
||||
|
||||
l1 = radius/math.tan(alpha/2.0)
|
||||
diffx = 0.5*s.depth/math.tan(alpha)
|
||||
l2 = 0.5*s.depth / math.sin(alpha)
|
||||
|
||||
sections = int((length) // (s.size*2))
|
||||
leftover = length - sections*s.size*2
|
||||
|
||||
p = 1 if positive else -1
|
||||
|
||||
self.edge((s.size+leftover)/2.0+diffx-l1)
|
||||
for i in range(sections):
|
||||
self.corner(-1*p*a, radius)
|
||||
self.edge(2*(l2-l1))
|
||||
self.corner(p*a, radius)
|
||||
self.edge(2*(diffx-l1)+s.size)
|
||||
self.corner(p*a, radius)
|
||||
self.edge(2*(l2-l1))
|
||||
self.corner(-1*p*a, radius)
|
||||
if i<sections-1: # all but the last
|
||||
self.edge(2*(diffx-l1)+s.size)
|
||||
self.edge((s.size+leftover)/2.0+diffx-l1)
|
||||
self.ctx.translate(*self.ctx.get_current_point())
|
||||
|
||||
def margin(self):
|
||||
return self.settings.depth + self.boxes.spacing
|
||||
|
||||
class DoveTailJointCounterPart(DoveTailJoint):
|
||||
char = 'D'
|
||||
|
||||
positive = False
|
||||
|
||||
def width(self):
|
||||
return self.settings.depth
|
||||
|
||||
def margin(self):
|
||||
return self.boxes.spacing
|
||||
|
||||
class FlexSettings(Settings):
|
||||
relative_params = {
|
||||
"distance" : 0.5,
|
||||
"connection" : 1.0,
|
||||
"width" : 5.0,
|
||||
}
|
||||
absolute_params = {
|
||||
"stretch" : 1.0,
|
||||
}
|
||||
|
||||
class FlexEdge(Edge):
|
||||
char = 'X'
|
||||
|
||||
def __call__(self, x, h, **kw):
|
||||
dist = self.settings.distance
|
||||
connection = self.settings.connection
|
||||
width = self.settings.width
|
||||
|
||||
burn = self.boxes.burn
|
||||
h += 2*burn
|
||||
lines = int(x // dist)
|
||||
leftover = x - lines * dist
|
||||
sections = int((h-connection) // width)
|
||||
sheight = ((h-connection) / sections)-connection
|
||||
|
||||
for i in range(lines):
|
||||
pos = i*dist + leftover/2
|
||||
if i % 2:
|
||||
self.ctx.move_to(pos, 0)
|
||||
self.ctx.line_to(pos, connection+sheight)
|
||||
for j in range((sections-1)//2):
|
||||
self.ctx.move_to(pos, (2*j+1)* sheight+ (2*j+2)*connection)
|
||||
self.ctx.line_to(pos, (2*j+3)* (sheight+ connection))
|
||||
if not sections % 2:
|
||||
self.ctx.move_to(pos, h - sheight- connection)
|
||||
self.ctx.line_to(pos, h)
|
||||
else:
|
||||
if sections % 2:
|
||||
self.ctx.move_to(pos, h)
|
||||
self.ctx.line_to(pos, h-connection-sheight)
|
||||
for j in range((sections-1)//2):
|
||||
self.ctx.move_to(
|
||||
pos, h-((2*j+1)* sheight+ (2*j+2)*connection))
|
||||
self.ctx.line_to(
|
||||
pos, h-(2*j+3)* (sheight+ connection))
|
||||
|
||||
else:
|
||||
for j in range(sections//2):
|
||||
self.ctx.move_to(pos,
|
||||
h-connection-2*j*(sheight+connection))
|
||||
self.ctx.line_to(pos, h-2*(j+1)*(sheight+connection))
|
||||
|
||||
self.ctx.move_to(0, 0)
|
||||
self.ctx.line_to(x, 0)
|
||||
self.ctx.translate(*self.ctx.get_current_point())
|
10
castle.py
10
castle.py
|
@ -23,13 +23,13 @@ class Castle(Boxes):
|
|||
|
||||
def render(self, t_x=70, t_h=250, w1_x=300, w1_h=120, w2_x=100, w2_h=120):
|
||||
self.open(800, 600)
|
||||
s = FingerJointSettings(self.thickness, relative=False,
|
||||
space = 10, finger=10, height=10,
|
||||
width=self.thickness)
|
||||
p = FingerJointEdge(self, s)
|
||||
s = edges.FingerJointSettings(self.thickness, relative=False,
|
||||
space = 10, finger=10, height=10,
|
||||
width=self.thickness)
|
||||
p = edges.FingerJointEdge(self, s)
|
||||
p.char = "p"
|
||||
self.addPart(p)
|
||||
P = FingerJointEdgeCounterPart(self, s)
|
||||
P = edges.FingerJointEdgeCounterPart(self, s)
|
||||
P.char = "P"
|
||||
self.addPart(P)
|
||||
|
||||
|
|
|
@ -114,11 +114,11 @@ class FlexBox(Boxes):
|
|||
self.edges["f"].settings.setValues(
|
||||
self.thickness, finger=2, space=2, surroundingspaces=1)
|
||||
|
||||
s = FingerJointSettings(self.thickness, surroundingspaces=1)
|
||||
g = FingerJointEdge(self, s)
|
||||
s = edges.FingerJointSettings(self.thickness, surroundingspaces=1)
|
||||
g = edges.FingerJointEdge(self, s)
|
||||
g.char = "g"
|
||||
self.addPart(g)
|
||||
G = FingerJointEdgeCounterPart(self, s)
|
||||
G = edges.FingerJointEdgeCounterPart(self, s)
|
||||
G.char = "G"
|
||||
self.addPart(G)
|
||||
|
||||
|
|
4
lamp.py
4
lamp.py
|
@ -24,14 +24,14 @@ D=23cm, d=21cm
|
|||
d = 8" D = 9"
|
||||
"""
|
||||
|
||||
class RoundedTriangleSettings(Settings):
|
||||
class RoundedTriangleSettings(edges.Settings):
|
||||
absolute_params = {
|
||||
"angle" : 60,
|
||||
"radius" : 30,
|
||||
"r_hole" : None,
|
||||
}
|
||||
|
||||
class RoundedTriangle(Edge):
|
||||
class RoundedTriangle(edges.Edge):
|
||||
char = "t"
|
||||
def __call__(self, length, **kw):
|
||||
angle = self.settings.angle
|
||||
|
|
|
@ -34,12 +34,12 @@ class TrayInsert(Boxes):
|
|||
|
||||
# Inner walls
|
||||
for i in range(len(self.sx)-1):
|
||||
e = [SlottedEdge(self, self.sy, slots=0.5*h), "e", "e", "e"]
|
||||
e = [edges.SlottedEdge(self, self.sy, slots=0.5*h), "e", "e", "e"]
|
||||
self.rectangularWall(y, h, e,
|
||||
move="up")
|
||||
for i in range(len(self.sy)-1):
|
||||
e = ["e", "e",
|
||||
SlottedEdge(self, self.sx[::-1], "e", slots=0.5*h), "e"]
|
||||
edges.SlottedEdge(self, self.sx[::-1], "e", slots=0.5*h), "e"]
|
||||
self.rectangularWall(x, h, e,
|
||||
move="up")
|
||||
self.close()
|
||||
|
|
|
@ -99,8 +99,8 @@ class Layout(Boxes):
|
|||
|
||||
self.hi = hi = self.hi or self.h
|
||||
|
||||
self.edges["s"] = Slot(self, self.hi/2.0)
|
||||
self.edges["C"] = CrossingFingerHoleEdge(self, self.hi)
|
||||
self.edges["s"] = boxes.edges.Slot(self, self.hi/2.0)
|
||||
self.edges["C"] = boxes.edges.CrossingFingerHoleEdge(self, self.hi)
|
||||
|
||||
lx = len(self.x)
|
||||
ly = len(self.y)
|
||||
|
@ -135,7 +135,7 @@ class Layout(Boxes):
|
|||
lengths.pop()
|
||||
edges.pop()
|
||||
self.rectangularWall(sum(lengths), h, [
|
||||
CompoundEdge(self, edges, lengths),
|
||||
boxes.edges.CompoundEdge(self, edges, lengths),
|
||||
"f" if self.vWalls(end, y) else "e",
|
||||
"e",
|
||||
"f" if self.vWalls(start, y) else "e"],
|
||||
|
@ -178,9 +178,9 @@ class Layout(Boxes):
|
|||
"C" : "e"}[e] for e in reversed(edges)]
|
||||
edges = ["e" if e == "s" else e for e in edges]
|
||||
self.rectangularWall(sum(lengths), h, [
|
||||
CompoundEdge(self, edges, lengths),
|
||||
boxes.edges.CompoundEdge(self, edges, lengths),
|
||||
"eFf"[self.hWalls(x, end)],
|
||||
CompoundEdge(self, upper, list(reversed(lengths))),
|
||||
boxes.edges.CompoundEdge(self, upper, list(reversed(lengths))),
|
||||
"eFf"[self.hWalls(x, start)] ],
|
||||
move="right")
|
||||
start = end
|
||||
|
|
|
@ -94,12 +94,12 @@ class TypeTray(Boxes):
|
|||
move="right")
|
||||
# Inner walls
|
||||
for i in range(len(self.sx)-1):
|
||||
e = [SlottedEdge(self, self.sy, "f", slots=0.5*hi), "f", "e", "f"]
|
||||
e = [edges.SlottedEdge(self, self.sy, "f", slots=0.5*hi), "f", "e", "f"]
|
||||
self.rectangularWall(y, hi, e,
|
||||
move="up")
|
||||
for i in range(len(self.sy)-1):
|
||||
e = [SlottedEdge(self, self.sx, "f"), "f",
|
||||
SlottedEdge(self, self.sx[::-1], "e", slots=0.5*hi), "f"]
|
||||
e = [edges.SlottedEdge(self, self.sx, "f"), "f",
|
||||
edges.SlottedEdge(self, self.sx[::-1], "e", slots=0.5*hi), "f"]
|
||||
self.rectangularWall(x, hi, e,
|
||||
move="up")
|
||||
self.close()
|
||||
|
|
Loading…
Reference in New Issue