From 4cf33dae501c914a4a02b574a418d3f6a38b17a2 Mon Sep 17 00:00:00 2001 From: Florian Festi Date: Wed, 27 Jul 2016 22:19:32 +0200 Subject: [PATCH] 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. --- boxes/__init__.py | 21 ++++++++++++++------- boxes/edges.py | 18 +++++++++--------- boxes/gears.py | 15 +++++---------- boxes/generators/box2.py | 10 ++++------ boxes/generators/box3.py | 1 - boxes/generators/flexbox3.py | 2 +- boxes/parts.py | 17 ++++------------- boxes/pulley.py | 4 +--- 8 files changed, 38 insertions(+), 50 deletions(-) diff --git a/boxes/__init__.py b/boxes/__init__.py index 15c9f2d..c9e3648 100755 --- a/boxes/__init__.py +++ b/boxes/__init__.py @@ -691,11 +691,13 @@ class Boxes: """ if not where: - return False + where = "" terms = where.split() dontdraw = before and "only" in terms + x += self.spacing + y += self.spacing moves = { "up": (0, y, False), "down" : (0, -y, True), @@ -703,6 +705,11 @@ class Boxes: "right" : (x, 0, False), "only" : (0, 0, None), } + + if not before: + # restore position + self.ctx.restore() + for term in terms: if not term in moves: raise ValueError("Unknown direction: '%s'" % term) @@ -711,6 +718,11 @@ class Boxes: self.moveTo(x, y) elif (not movebeforeprint and not before) or dontdraw: 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 @restore @@ -1002,7 +1014,6 @@ class Boxes: if self.move(overallwidth, overallheight, move, before=True): return - self.ctx.save() self.moveTo(self.edges["f"].margin(), self.edges["f"].margin()) self.moveTo(r, 0) @@ -1034,7 +1045,6 @@ class Boxes: r = 0 self.hexHolesPlate(x-2*holesMargin, y-2*holesMargin, r, settings=holesSettings) - self.ctx.restore() self.ctx.stroke() self.move(overallwidth, overallheight, move) @@ -1081,7 +1091,6 @@ class Boxes: if self.move(overallwidth, overallheight, move, before=True): return - self.ctx.save() self.moveTo(left.spacing(), bottom.margin()) self.cc(callback, 0, y=bottomwidth+self.burn) @@ -1115,7 +1124,6 @@ class Boxes: left(h) self.edgeCorner(left, bottom, 90) - self.ctx.restore() self.ctx.stroke() self.move(overallwidth, overallheight, move) @@ -1149,7 +1157,6 @@ class Boxes: if self.move(overallwidth, overallheight, move, before=True): return - self.ctx.save() self.moveTo(edges[-1].spacing(), edges[0].margin()) for i, l in enumerate((x, y, x, y)): self.cc(callback, i, y=edges[i].startwidth()+self.burn) @@ -1162,7 +1169,7 @@ class Boxes: self.moveTo(holesMargin+edges[-1].endwidth(), holesMargin+edges[0].startwidth()) self.hexHolesRectangle(x-2*holesMargin, y-2*holesMargin) - self.ctx.restore() + self.ctx.stroke() self.move(overallwidth, overallheight, move) diff --git a/boxes/edges.py b/boxes/edges.py index 5497b82..4c233e9 100644 --- a/boxes/edges.py +++ b/boxes/edges.py @@ -184,7 +184,7 @@ class BaseEdge(object): def margin(self): """Space needed right of the starting point""" - return self.boxes.spacing + return 0.0 def spacing(self): """Space the edge needs outside of the inner space of the part""" @@ -277,9 +277,9 @@ class GrippingEdge(BaseEdge): def margin(self): if self.settings.outset: - return self.settings.depth + self.boxes.spacing + return self.settings.depth else: - return self.boxes.spacing + return 0.0 def __call__(self, length, **kw): if length == 0.0: @@ -458,7 +458,7 @@ class FingerJointEdge(BaseEdge): def margin(self): """ """ - return self.boxes.spacing + self.boxes.thickness + return self.boxes.thickness class FingerJointEdgeCounterPart(FingerJointEdge): """Finger joint edge - other side""" @@ -472,7 +472,7 @@ class FingerJointEdgeCounterPart(FingerJointEdge): def margin(self): """ """ - return self.boxes.spacing + return 0.0 class FingerHoles: """Hole matching a finger joint edge""" @@ -769,7 +769,7 @@ class HingePin(BaseEdge): return self.settings.outset * self.boxes.thickness def margin(self): - return self.thickness + self.boxes.spacing + return self.thickness def A(self, _reversed=False): t = self.thickness @@ -963,7 +963,7 @@ class ClickEdge(ClickConnector): return self.boxes.thickness def margin(self): - return self.boxes.spacing + return 0.0 def __call__(self, length, **kw): t = self.thickness @@ -1050,7 +1050,7 @@ class DoveTailJoint(BaseEdge): def margin(self): """ """ - return self.settings.depth + self.boxes.spacing + return self.settings.depth class DoveTailJointCounterPart(DoveTailJoint): """Edge for other side of dove joints """ @@ -1060,7 +1060,7 @@ class DoveTailJointCounterPart(DoveTailJoint): positive = False def margin(self): - return self.boxes.spacing + return 0.0 class FlexSettings(Settings): """Settings for one directional flex cuts diff --git a/boxes/gears.py b/boxes/gears.py index e7880e7..9ae94eb 100644 --- a/boxes/gears.py +++ b/boxes/gears.py @@ -516,7 +516,7 @@ class Gears(): return pitch_radius, 2*outer_radius, 2*outer_radius 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): return try: @@ -587,7 +587,6 @@ class Gears(): (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) - s = self.boxes.spacing b = self.boxes.burn # Add Rack (instead) if self.options.drawrack: @@ -596,26 +595,23 @@ class Gears(): tooth_count = self.options.teeth_length (points, guide_points) = generate_rack_points(tooth_count, pitch, addendum, angle, base_height, tab_width, clearance, pitchcircle) - width = tooth_count * pitch + 2*tab_width + 2 * s - height = base_height+ 2* addendum + 2 * s + width = tooth_count * pitch + 2*tab_width + height = base_height+ 2* addendum if self.boxes.move(width, height, move, before=True): return self.boxes.cc(callback, None, s+b, s+b) - self.boxes.ctx.save() - self.boxes.moveTo(width/2.0+s, base_height+s+addendum, -180) + self.boxes.moveTo(width/2.0, base_height+addendum, -180) self.drawPoints(points) self.drawPoints(guide_points, kerfdir=0) - self.boxes.ctx.restore() self.boxes.move(width, height, move) return # Move only - width = height = 2 * (outer_radius +s) + width = height = 2 * outer_radius if self.options.internal_ring: width = height = width + 2 * self.options.spoke_width if self.boxes.move(width, height, move, before=True): return - self.boxes.ctx.save() # Detect Undercut of teeth ## undercut = int(ceil(undercut_min_teeth( angle ))) @@ -698,7 +694,6 @@ class Gears(): self.boxes.text(note, -outer_radius, y) y += text_height * 1.2 - self.boxes.ctx.restore() self.boxes.move(width, height, move) if __name__ == '__main__': diff --git a/boxes/generators/box2.py b/boxes/generators/box2.py index c7b2ede..3623eeb 100755 --- a/boxes/generators/box2.py +++ b/boxes/generators/box2.py @@ -45,22 +45,21 @@ class Box(Boxes): x, y, h, angle = self.x, self.y, self.h, self.angle t = self.thickness 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 - self.ctx.save() - self.moveTo(1.5*t, t) + self.moveTo(t, 0) self.edge(x) self.corner(90+angle) self.edges["a"](3*t) self.corner(180-2*angle, r) self.edges["a"](3*t) 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): + # XXX move param x, y, h = self.x, self.y, self.h t = self.thickness angle = 30 @@ -109,7 +108,6 @@ class Box(Boxes): d2 = d3 = None - self.moveTo(self.thickness, self.thickness) self.rectangularWall(y, h, [b, "f", t2, "f"], bedBolts=[d3], move="right") self.rectangularWall(x, h, [b, "F", t1, "F"], diff --git a/boxes/generators/box3.py b/boxes/generators/box3.py index 1a68303..32699ea 100755 --- a/boxes/generators/box3.py +++ b/boxes/generators/box3.py @@ -42,7 +42,6 @@ class Box(Boxes): d2 = d3 = None - self.moveTo(t, t) 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) diff --git a/boxes/generators/flexbox3.py b/boxes/generators/flexbox3.py index a4c9941..20594aa 100755 --- a/boxes/generators/flexbox3.py +++ b/boxes/generators/flexbox3.py @@ -130,7 +130,7 @@ class FlexBox(Boxes): self.moveTo(2*self.thickness, self.thickness+2*d) self.ctx.save() 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(h, z+2*(d+self.thickness), edges="GeGF", move="right") diff --git a/boxes/parts.py b/boxes/parts.py index e307eae..aa6586f 100644 --- a/boxes/parts.py +++ b/boxes/parts.py @@ -26,23 +26,20 @@ class Parts: return getattr(self.boxes, name) def disc(self, diameter, callback=None, move=""): - size = diameter+2*self.boxes.spacing + size = diameter r = diameter/2.0 if self.move(size, size, move, before=True): return - self.boxes.ctx.save() self.moveTo(size/2, size/2) self.cc(callback, None, 0, 0) self.moveTo(r+self.burn,0, 90) self.corner(360, r) - self.boxes.ctx.restore() self.move(size, size, 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): return - self.boxes.ctx.save() self.moveTo(size/2, size/2) self.cc(callback, None, 0, 0) self.moveTo(diameter/2, 0, angle) @@ -51,31 +48,25 @@ class Parts: for i in range(n//2): self.boxes.corner(a, r) self.boxes.corner(a2, r2) - self.boxes.ctx.restore() self.move(size, size, move, before=True) 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): return - self.boxes.ctx.save() self.rectangularHole(size/2, size/2, size, size) self.moveTo(size/2, size/2) - self.hole(0,0, 3) - self.hole(0,0, diameter/2) self.cc(callback, None, 0, 0) self.moveTo(diameter/2, 0, 90+angle) 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 a, r = arcOnCircle(360./n*(1-rounded), -angle-0.01, diameter/2) - print(a, r) for i in range(n): self.boxes.corner(a, r) self.corner(angle) self.corner(360./n*rounded, diameter/2) self.corner(angle) - self.boxes.ctx.restore() + self.move(size, size, move, before=True) diff --git a/boxes/pulley.py b/boxes/pulley.py index 67fb90b..e5a2d8e 100644 --- a/boxes/pulley.py +++ b/boxes/pulley.py @@ -117,10 +117,9 @@ class Pulley: tooth_width_scale = (tooth_width + additional_tooth_width ) / tooth_width 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): return - self.boxes.ctx.save() self.boxes.moveTo(total_width/2, total_width/2) self.boxes.cc(callback, None, 0.0, 0.0) if r_axle: @@ -133,5 +132,4 @@ class Pulley: points.extend((vtransl(pt, m) for pt in self.teeth[profile][1:-1])) self.drawPoints(points) - self.boxes.ctx.restore() self.boxes.move(total_width, total_width, move)