Move spacing out of the Edge margin() and into Boxes.move()

Also move ctx.save() and restoreinto Boxes.move().
Adjust (hopefully) all places using it.
This commit is contained in:
Florian Festi 2016-07-27 22:19:32 +02:00
parent 558b63f20d
commit 4cf33dae50
8 changed files with 38 additions and 50 deletions

View File

@ -691,11 +691,13 @@ class Boxes:
""" """
if not where: if not where:
return False where = ""
terms = where.split() terms = where.split()
dontdraw = before and "only" in terms dontdraw = before and "only" in terms
x += self.spacing
y += self.spacing
moves = { moves = {
"up": (0, y, False), "up": (0, y, False),
"down" : (0, -y, True), "down" : (0, -y, True),
@ -703,6 +705,11 @@ class Boxes:
"right" : (x, 0, False), "right" : (x, 0, False),
"only" : (0, 0, None), "only" : (0, 0, None),
} }
if not before:
# restore position
self.ctx.restore()
for term in terms: for term in terms:
if not term in moves: if not term in moves:
raise ValueError("Unknown direction: '%s'" % term) raise ValueError("Unknown direction: '%s'" % term)
@ -711,6 +718,11 @@ class Boxes:
self.moveTo(x, y) self.moveTo(x, y)
elif (not movebeforeprint and not before) or dontdraw: elif (not movebeforeprint and not before) or dontdraw:
self.moveTo(x, y) self.moveTo(x, y)
if not dontdraw:
if before:
# save position
self.ctx.save()
self.moveTo(self.spacing/2.0, self.spacing/2.0)
return dontdraw return dontdraw
@restore @restore
@ -1002,7 +1014,6 @@ class Boxes:
if self.move(overallwidth, overallheight, move, before=True): if self.move(overallwidth, overallheight, move, before=True):
return return
self.ctx.save()
self.moveTo(self.edges["f"].margin(), self.moveTo(self.edges["f"].margin(),
self.edges["f"].margin()) self.edges["f"].margin())
self.moveTo(r, 0) self.moveTo(r, 0)
@ -1034,7 +1045,6 @@ class Boxes:
r = 0 r = 0
self.hexHolesPlate(x-2*holesMargin, y-2*holesMargin, r, self.hexHolesPlate(x-2*holesMargin, y-2*holesMargin, r,
settings=holesSettings) settings=holesSettings)
self.ctx.restore()
self.ctx.stroke() self.ctx.stroke()
self.move(overallwidth, overallheight, move) self.move(overallwidth, overallheight, move)
@ -1081,7 +1091,6 @@ class Boxes:
if self.move(overallwidth, overallheight, move, before=True): if self.move(overallwidth, overallheight, move, before=True):
return return
self.ctx.save()
self.moveTo(left.spacing(), bottom.margin()) self.moveTo(left.spacing(), bottom.margin())
self.cc(callback, 0, y=bottomwidth+self.burn) self.cc(callback, 0, y=bottomwidth+self.burn)
@ -1115,7 +1124,6 @@ class Boxes:
left(h) left(h)
self.edgeCorner(left, bottom, 90) self.edgeCorner(left, bottom, 90)
self.ctx.restore()
self.ctx.stroke() self.ctx.stroke()
self.move(overallwidth, overallheight, move) self.move(overallwidth, overallheight, move)
@ -1149,7 +1157,6 @@ class Boxes:
if self.move(overallwidth, overallheight, move, before=True): if self.move(overallwidth, overallheight, move, before=True):
return return
self.ctx.save()
self.moveTo(edges[-1].spacing(), edges[0].margin()) self.moveTo(edges[-1].spacing(), edges[0].margin())
for i, l in enumerate((x, y, x, y)): for i, l in enumerate((x, y, x, y)):
self.cc(callback, i, y=edges[i].startwidth()+self.burn) self.cc(callback, i, y=edges[i].startwidth()+self.burn)
@ -1162,7 +1169,7 @@ class Boxes:
self.moveTo(holesMargin+edges[-1].endwidth(), self.moveTo(holesMargin+edges[-1].endwidth(),
holesMargin+edges[0].startwidth()) holesMargin+edges[0].startwidth())
self.hexHolesRectangle(x-2*holesMargin, y-2*holesMargin) self.hexHolesRectangle(x-2*holesMargin, y-2*holesMargin)
self.ctx.restore()
self.ctx.stroke() self.ctx.stroke()
self.move(overallwidth, overallheight, move) self.move(overallwidth, overallheight, move)

View File

@ -184,7 +184,7 @@ class BaseEdge(object):
def margin(self): def margin(self):
"""Space needed right of the starting point""" """Space needed right of the starting point"""
return self.boxes.spacing return 0.0
def spacing(self): def spacing(self):
"""Space the edge needs outside of the inner space of the part""" """Space the edge needs outside of the inner space of the part"""
@ -277,9 +277,9 @@ class GrippingEdge(BaseEdge):
def margin(self): def margin(self):
if self.settings.outset: if self.settings.outset:
return self.settings.depth + self.boxes.spacing return self.settings.depth
else: else:
return self.boxes.spacing return 0.0
def __call__(self, length, **kw): def __call__(self, length, **kw):
if length == 0.0: if length == 0.0:
@ -458,7 +458,7 @@ class FingerJointEdge(BaseEdge):
def margin(self): def margin(self):
""" """ """ """
return self.boxes.spacing + self.boxes.thickness return self.boxes.thickness
class FingerJointEdgeCounterPart(FingerJointEdge): class FingerJointEdgeCounterPart(FingerJointEdge):
"""Finger joint edge - other side""" """Finger joint edge - other side"""
@ -472,7 +472,7 @@ class FingerJointEdgeCounterPart(FingerJointEdge):
def margin(self): def margin(self):
""" """ """ """
return self.boxes.spacing return 0.0
class FingerHoles: class FingerHoles:
"""Hole matching a finger joint edge""" """Hole matching a finger joint edge"""
@ -769,7 +769,7 @@ class HingePin(BaseEdge):
return self.settings.outset * self.boxes.thickness return self.settings.outset * self.boxes.thickness
def margin(self): def margin(self):
return self.thickness + self.boxes.spacing return self.thickness
def A(self, _reversed=False): def A(self, _reversed=False):
t = self.thickness t = self.thickness
@ -963,7 +963,7 @@ class ClickEdge(ClickConnector):
return self.boxes.thickness return self.boxes.thickness
def margin(self): def margin(self):
return self.boxes.spacing return 0.0
def __call__(self, length, **kw): def __call__(self, length, **kw):
t = self.thickness t = self.thickness
@ -1050,7 +1050,7 @@ class DoveTailJoint(BaseEdge):
def margin(self): def margin(self):
""" """ """ """
return self.settings.depth + self.boxes.spacing return self.settings.depth
class DoveTailJointCounterPart(DoveTailJoint): class DoveTailJointCounterPart(DoveTailJoint):
"""Edge for other side of dove joints """ """Edge for other side of dove joints """
@ -1060,7 +1060,7 @@ class DoveTailJointCounterPart(DoveTailJoint):
positive = False positive = False
def margin(self): def margin(self):
return self.boxes.spacing return 0.0
class FlexSettings(Settings): class FlexSettings(Settings):
"""Settings for one directional flex cuts """Settings for one directional flex cuts

View File

@ -516,7 +516,7 @@ class Gears():
return pitch_radius, 2*outer_radius, 2*outer_radius return pitch_radius, 2*outer_radius, 2*outer_radius
def gearCarrier(self, r, spoke_width, positions, mount_radius, mount_hole, circle=True, move=None): def gearCarrier(self, r, spoke_width, positions, mount_radius, mount_hole, circle=True, move=None):
width = (r+spoke_width+self.boxes.spacing)*2 width = (r+spoke_width)*2
if self.boxes.move(width, width, move, before=True): if self.boxes.move(width, width, move, before=True):
return return
try: try:
@ -587,7 +587,6 @@ class Gears():
(pitch_radius, base_radius, addendum, dedendum, (pitch_radius, base_radius, addendum, dedendum,
outer_radius, root_radius, tooth) = gear_calculations(teeth, pitch, angle, clearance, self.options.internal_ring, self.options.profile_shift*0.01) outer_radius, root_radius, tooth) = gear_calculations(teeth, pitch, angle, clearance, self.options.internal_ring, self.options.profile_shift*0.01)
s = self.boxes.spacing
b = self.boxes.burn b = self.boxes.burn
# Add Rack (instead) # Add Rack (instead)
if self.options.drawrack: if self.options.drawrack:
@ -596,26 +595,23 @@ class Gears():
tooth_count = self.options.teeth_length tooth_count = self.options.teeth_length
(points, guide_points) = generate_rack_points(tooth_count, pitch, addendum, angle, (points, guide_points) = generate_rack_points(tooth_count, pitch, addendum, angle,
base_height, tab_width, clearance, pitchcircle) base_height, tab_width, clearance, pitchcircle)
width = tooth_count * pitch + 2*tab_width + 2 * s width = tooth_count * pitch + 2*tab_width
height = base_height+ 2* addendum + 2 * s height = base_height+ 2* addendum
if self.boxes.move(width, height, move, before=True): if self.boxes.move(width, height, move, before=True):
return return
self.boxes.cc(callback, None, s+b, s+b) self.boxes.cc(callback, None, s+b, s+b)
self.boxes.ctx.save() self.boxes.moveTo(width/2.0, base_height+addendum, -180)
self.boxes.moveTo(width/2.0+s, base_height+s+addendum, -180)
self.drawPoints(points) self.drawPoints(points)
self.drawPoints(guide_points, kerfdir=0) self.drawPoints(guide_points, kerfdir=0)
self.boxes.ctx.restore()
self.boxes.move(width, height, move) self.boxes.move(width, height, move)
return return
# Move only # Move only
width = height = 2 * (outer_radius +s) width = height = 2 * outer_radius
if self.options.internal_ring: if self.options.internal_ring:
width = height = width + 2 * self.options.spoke_width width = height = width + 2 * self.options.spoke_width
if self.boxes.move(width, height, move, before=True): if self.boxes.move(width, height, move, before=True):
return return
self.boxes.ctx.save()
# Detect Undercut of teeth # Detect Undercut of teeth
## undercut = int(ceil(undercut_min_teeth( angle ))) ## undercut = int(ceil(undercut_min_teeth( angle )))
@ -698,7 +694,6 @@ class Gears():
self.boxes.text(note, -outer_radius, y) self.boxes.text(note, -outer_radius, y)
y += text_height * 1.2 y += text_height * 1.2
self.boxes.ctx.restore()
self.boxes.move(width, height, move) self.boxes.move(width, height, move)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -45,22 +45,21 @@ class Box(Boxes):
x, y, h, angle = self.x, self.y, self.h, self.angle x, y, h, angle = self.x, self.y, self.h, self.angle
t = self.thickness t = self.thickness
r = self.getR() r = self.getR()
if self.move(x+3*t, 0.5*x+5*t, move, True): if self.move(x+2*t, 0.5*x+3*t, move, True):
return return
self.ctx.save() self.moveTo(t, 0)
self.moveTo(1.5*t, t)
self.edge(x) self.edge(x)
self.corner(90+angle) self.corner(90+angle)
self.edges["a"](3*t) self.edges["a"](3*t)
self.corner(180-2*angle, r) self.corner(180-2*angle, r)
self.edges["a"](3*t) self.edges["a"](3*t)
self.corner(90+angle) self.corner(90+angle)
self.ctx.restore()
self.move(x+3*t, 0.5*x+5*t, move, False) self.move(x+2*t, 0.5*x+3*t, move, False)
def top(self): def top(self):
# XXX move param
x, y, h = self.x, self.y, self.h x, y, h = self.x, self.y, self.h
t = self.thickness t = self.thickness
angle = 30 angle = 30
@ -109,7 +108,6 @@ class Box(Boxes):
d2 = d3 = None d2 = d3 = None
self.moveTo(self.thickness, self.thickness)
self.rectangularWall(y, h, [b, "f", t2, "f"], self.rectangularWall(y, h, [b, "f", t2, "f"],
bedBolts=[d3], move="right") bedBolts=[d3], move="right")
self.rectangularWall(x, h, [b, "F", t1, "F"], self.rectangularWall(x, h, [b, "F", t1, "F"],

View File

@ -42,7 +42,6 @@ class Box(Boxes):
d2 = d3 = None d2 = d3 = None
self.moveTo(t, t)
self.rectangularWall(x, h, "FFeF", bedBolts=d2, move="right") self.rectangularWall(x, h, "FFeF", bedBolts=d2, move="right")
self.rectangularWall(y, h, "Feef", bedBolts=d3, move="up") self.rectangularWall(y, h, "Feef", bedBolts=d3, move="up")
self.rectangularWall(y, h, "Feef", bedBolts=d3) self.rectangularWall(y, h, "Feef", bedBolts=d3)

View File

@ -130,7 +130,7 @@ class FlexBox(Boxes):
self.moveTo(2*self.thickness, self.thickness+2*d) self.moveTo(2*self.thickness, self.thickness+2*d)
self.ctx.save() self.ctx.save()
self.surroundingWall() self.surroundingWall()
self.moveTo(x+y-2*r+self.c4+self.thickness, -2*d-self.thickness) self.moveTo(x+y-2*r+self.c4+2*self.thickness, -2*d-self.thickness)
self.rectangularWall(x, z, edges="FFFF", move="right") self.rectangularWall(x, z, edges="FFFF", move="right")
self.rectangularWall(h, z+2*(d+self.thickness), self.rectangularWall(h, z+2*(d+self.thickness),
edges="GeGF", move="right") edges="GeGF", move="right")

View File

@ -26,23 +26,20 @@ class Parts:
return getattr(self.boxes, name) return getattr(self.boxes, name)
def disc(self, diameter, callback=None, move=""): def disc(self, diameter, callback=None, move=""):
size = diameter+2*self.boxes.spacing size = diameter
r = diameter/2.0 r = diameter/2.0
if self.move(size, size, move, before=True): if self.move(size, size, move, before=True):
return return
self.boxes.ctx.save()
self.moveTo(size/2, size/2) self.moveTo(size/2, size/2)
self.cc(callback, None, 0, 0) self.cc(callback, None, 0, 0)
self.moveTo(r+self.burn,0, 90) self.moveTo(r+self.burn,0, 90)
self.corner(360, r) self.corner(360, r)
self.boxes.ctx.restore()
self.move(size, size, move) self.move(size, size, move)
def waivyKnob(self, diameter, n=20, angle=45, callback=None, move=""): def waivyKnob(self, diameter, n=20, angle=45, callback=None, move=""):
size = diameter+pi*diameter/n+2*self.boxes.spacing size = diameter+pi*diameter/n
if self.move(size, size, move, before=True): if self.move(size, size, move, before=True):
return return
self.boxes.ctx.save()
self.moveTo(size/2, size/2) self.moveTo(size/2, size/2)
self.cc(callback, None, 0, 0) self.cc(callback, None, 0, 0)
self.moveTo(diameter/2, 0, angle) self.moveTo(diameter/2, 0, angle)
@ -51,31 +48,25 @@ class Parts:
for i in range(n//2): for i in range(n//2):
self.boxes.corner(a, r) self.boxes.corner(a, r)
self.boxes.corner(a2, r2) self.boxes.corner(a2, r2)
self.boxes.ctx.restore()
self.move(size, size, move, before=True) self.move(size, size, move, before=True)
def concaveKnob(self, diameter, n=3, rounded=0.2, angle=70, callback=None, move=""): def concaveKnob(self, diameter, n=3, rounded=0.2, angle=70, callback=None, move=""):
size = diameter+2*self.boxes.spacing size = diameter
if self.move(size, size, move, before=True): if self.move(size, size, move, before=True):
return return
self.boxes.ctx.save()
self.rectangularHole(size/2, size/2, size, size) self.rectangularHole(size/2, size/2, size, size)
self.moveTo(size/2, size/2) self.moveTo(size/2, size/2)
self.hole(0,0, 3)
self.hole(0,0, diameter/2)
self.cc(callback, None, 0, 0) self.cc(callback, None, 0, 0)
self.moveTo(diameter/2, 0, 90+angle) self.moveTo(diameter/2, 0, 90+angle)
a, r = arcOnCircle(360./n*(1-rounded), -angle, diameter/2) a, r = arcOnCircle(360./n*(1-rounded), -angle, diameter/2)
print(360./n*(1-rounded), a, r)
if abs(a) < 0.01: # avoid trying to make a straight line as an arc if abs(a) < 0.01: # avoid trying to make a straight line as an arc
a, r = arcOnCircle(360./n*(1-rounded), -angle-0.01, diameter/2) a, r = arcOnCircle(360./n*(1-rounded), -angle-0.01, diameter/2)
print(a, r)
for i in range(n): for i in range(n):
self.boxes.corner(a, r) self.boxes.corner(a, r)
self.corner(angle) self.corner(angle)
self.corner(360./n*rounded, diameter/2) self.corner(360./n*rounded, diameter/2)
self.corner(angle) self.corner(angle)
self.boxes.ctx.restore()
self.move(size, size, move, before=True) self.move(size, size, move, before=True)

View File

@ -117,10 +117,9 @@ class Pulley:
tooth_width_scale = (tooth_width + additional_tooth_width ) / tooth_width tooth_width_scale = (tooth_width + additional_tooth_width ) / tooth_width
tooth_depth_scale = ((tooth_depth + additional_tooth_depth ) / tooth_depth) tooth_depth_scale = ((tooth_depth + additional_tooth_depth ) / tooth_depth)
total_width = pulley_OD + 2 * self.boxes.spacing total_width = pulley_OD
if self.boxes.move(total_width, total_width, move, before=True): if self.boxes.move(total_width, total_width, move, before=True):
return return
self.boxes.ctx.save()
self.boxes.moveTo(total_width/2, total_width/2) self.boxes.moveTo(total_width/2, total_width/2)
self.boxes.cc(callback, None, 0.0, 0.0) self.boxes.cc(callback, None, 0.0, 0.0)
if r_axle: if r_axle:
@ -133,5 +132,4 @@ class Pulley:
points.extend((vtransl(pt, m) for pt in self.teeth[profile][1:-1])) points.extend((vtransl(pt, m) for pt in self.teeth[profile][1:-1]))
self.drawPoints(points) self.drawPoints(points)
self.boxes.ctx.restore()
self.boxes.move(total_width, total_width, move) self.boxes.move(total_width, total_width, move)