Add holesMargin param to rectangularWall

Allow callables and (callable, edgewidth) tuple as edge style
This commit is contained in:
Florian Festi 2013-05-14 17:52:33 +02:00
parent 35bb326533
commit cf19c86c9c
1 changed files with 47 additions and 4 deletions

View File

@ -2,10 +2,20 @@
import cairo import cairo
import math import math
from functools import wraps
def dist(dx, dy): def dist(dx, dy):
return (dx*dx+dy*dy)**0.5 return (dx*dx+dy*dy)**0.5
def restore(func):
@wraps(func)
def f(self, *args, **kw):
self.ctx.save()
func(self, *args, **kw)
self.ctx.restore()
return f
class Boxes: class Boxes:
def __init__(self, width=300, height=200, thickness=3.0): def __init__(self, width=300, height=200, thickness=3.0):
@ -51,6 +61,7 @@ class Boxes:
raise raise
self.ctx.restore() self.ctx.restore()
############################################################ ############################################################
### Turtle graphics commands ### Turtle graphics commands
############################################################ ############################################################
@ -361,11 +372,16 @@ class Boxes:
### parts ### parts
################################################## ##################################################
def roundedPlate(self, x, y, r, callback=None): def roundedPlate(self, x, y, r, callback=None,
holesMargin=None, holesSettings=None):
"""fits surroundingWall """fits surroundingWall
first edge is split to have a joint in the middle of the side first edge is split to have a joint in the middle of the side
callback is called at the beginning of the straight edges callback is called at the beginning of the straight edges
0, 1 for the two part of the first edge, 2, 3, 4 for the others""" 0, 1 for the two part of the first edge, 2, 3, 4 for the others
set holesMargin to get hex holes.
"""
self.ctx.save() self.ctx.save()
self.moveTo(r, 0) self.moveTo(r, 0)
self.cc(callback, 0) self.cc(callback, 0)
@ -377,9 +393,25 @@ class Boxes:
self.cc(callback, i+2) self.cc(callback, i+2)
self.fingerJoint(l-2*r) self.fingerJoint(l-2*r)
self.corner(90, r) self.corner(90, r)
self.ctx.restore()
self.ctx.save()
if holesMargin is not None:
self.moveTo(holesMargin, holesMargin)
if r > holesMargin:
r -= holesMargin
else:
r = 0
self.hexHolesPlate(x-2*holesMargin, y-2*holesMargin, r,
settings=holesSettings)
self.ctx.restore() self.ctx.restore()
def _edge(self, l, style): def _edge(self, l, style):
if type(style) is tuple:
style = style[0]
if callable(style):
return style(l)
if style in 'eE': if style in 'eE':
self.edge(l) self.edge(l)
elif style == 'h': elif style == 'h':
@ -393,6 +425,8 @@ class Boxes:
def _edgewidth(self, style): def _edgewidth(self, style):
"""return how far a given edge type needs to be set out""" """return how far a given edge type needs to be set out"""
if type(style) is tuple:
return style[1]
if style == 'h': if style == 'h':
return (self.fingerHoleEdgeWidth+1) * self.thickness return (self.fingerHoleEdgeWidth+1) * self.thickness
elif style in 'FE': elif style in 'FE':
@ -408,7 +442,7 @@ class Boxes:
0 for right half of first x side; 0 for right half of first x side;
1 and 3 for y sides; 1 and 3 for y sides;
2 for second x side 2 for second x side
4 for second half of thefirst x side 4 for second half of the first x side
""" """
c4 = (r+self.burn)*math.pi*0.5 # circumference of quarter circle c4 = (r+self.burn)*math.pi*0.5 # circumference of quarter circle
topwidth = self._edgewidth(top) topwidth = self._edgewidth(top)
@ -442,7 +476,11 @@ class Boxes:
self.edge(bottomwidth) self.edge(bottomwidth)
self.corner(90) self.corner(90)
def rectangularWall(self, x, y, edges="eeee"): @restore
def rectangularWall(self, x, y, edges="eeee",
holesMargin=None, holesSettings=None):
if len(edges) != 4:
raise ValueError, "four edges required"
edges += edges # append for wrapping around edges += edges # append for wrapping around
for i, l in enumerate((x, y, x, y)): for i, l in enumerate((x, y, x, y)):
self._edge(self._edgewidth(edges[i-1]), 'e') self._edge(self._edgewidth(edges[i-1]), 'e')
@ -450,6 +488,11 @@ class Boxes:
self._edge(self._edgewidth(edges[i+1]), 'e') self._edge(self._edgewidth(edges[i+1]), 'e')
self.corner(90) self.corner(90)
if holesMargin is not None:
self.moveTo(holesMargin+self._edgewidth(edges[-1]),
holesMargin+self._edgewidth(edges[0]))
self.hexHolesRectangle(x-2*holesMargin, y-2*holesMargin)
################################################## ##################################################
### main ### main
################################################## ##################################################