Use .saved_context() instead of .ctx.save() and .restore()
This commit is contained in:
parent
1724f67bdf
commit
2ba6fa1bf3
|
@ -45,10 +45,9 @@ class AllEdges(Boxes):
|
|||
|
||||
for c in chars:
|
||||
self.moveTo(0, 15*t)
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.moveTo(x, 0, 180)
|
||||
self.edges[c](x, h=4*t)
|
||||
self.ctx.restore()
|
||||
self.text("%s - %s" % (c, self.edges[c].description), y=5*t)
|
||||
|
||||
self.close()
|
||||
|
|
|
@ -54,7 +54,7 @@ class AngledBox(Boxes):
|
|||
self.moveTo((tx-lx)/2., 0)
|
||||
|
||||
if hole:
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
hr, hh, hside = self.regularPolygon(2*n+2, h=y/2.0-t)
|
||||
dx = side - hside
|
||||
hlx = lx - dx
|
||||
|
@ -63,7 +63,6 @@ class AngledBox(Boxes):
|
|||
for i, l in enumerate(([hlx] + ([hside] * n))* 2):
|
||||
self.edge(l)
|
||||
self.corner(360.0/(2*n + 2))
|
||||
self.ctx.restore()
|
||||
|
||||
for i, l in enumerate(([lx] + ([side] * n))* 2):
|
||||
self.cc(callback, i, 0, edge.startwidth() + self.burn)
|
||||
|
@ -105,7 +104,7 @@ class AngledBox(Boxes):
|
|||
edges.FingerJointSettings(self.thickness, True, angle=360./(2 * (n+1))).edgeObjects(self, chars="gGH")
|
||||
|
||||
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.floor(x, y , n, edge='F', move="right")
|
||||
if self.top == "angled lid":
|
||||
self.floor(x, y, n, edge='e', move="right")
|
||||
|
@ -114,7 +113,6 @@ class AngledBox(Boxes):
|
|||
self.floor(x, y, n, edge='F', move="right", hole=True)
|
||||
if self.top == "angled lid2":
|
||||
self.floor(x, y, n, edge='E', move="right")
|
||||
self.ctx.restore()
|
||||
self.floor(x, y , n, edge='F', move="up only")
|
||||
|
||||
fingers = self.top in ("angled lid2", "angled hole")
|
||||
|
|
|
@ -113,18 +113,16 @@ class CardBox(Boxes):
|
|||
p.char = "A"
|
||||
self.addPart(p)
|
||||
|
||||
self.ctx.save()
|
||||
|
||||
with self.saved_context():
|
||||
# Lid
|
||||
self.rectangularWall(x-t*.2, y, "Feee", move="right")
|
||||
# Bottom
|
||||
self.rectangularWall(x, y, "ffff", callback=[self.divider_bottom],
|
||||
move="right")
|
||||
|
||||
self.ctx.restore()
|
||||
self.rectangularWall(x, y, "EEEE", move="up only")
|
||||
self.ctx.save()
|
||||
|
||||
with self.saved_context():
|
||||
# Back
|
||||
self.rectangularWall(x, h, "FFEF",
|
||||
callback=[self.divider_back_and_front],
|
||||
|
@ -134,7 +132,6 @@ class CardBox(Boxes):
|
|||
callback=[self.divider_back_and_front],
|
||||
move="right")
|
||||
|
||||
self.ctx.restore()
|
||||
self.rectangularWall(x, h, "EEEE", move="up only")
|
||||
|
||||
#lip of the lid
|
||||
|
|
|
@ -108,11 +108,11 @@ class FlexBox2(Boxes):
|
|||
self.open()
|
||||
|
||||
self.moveTo(2 * self.thickness, self.thickness)
|
||||
self.ctx.save()
|
||||
self.surroundingWall(move="right")
|
||||
|
||||
with self.saved_context():
|
||||
self.surroundingWall(move="right")
|
||||
self.rectangularWall(self.y, self.x, edges="FFFF")
|
||||
self.ctx.restore()
|
||||
|
||||
self.surroundingWall(move="up only")
|
||||
|
||||
self.flexBoxSide(self.y, self.h, self.radius, move="right")
|
||||
|
|
|
@ -152,13 +152,12 @@ class FlexBox3(Boxes):
|
|||
space=1., surroundingspaces=1)
|
||||
s.edgeObjects(self, "gGH")
|
||||
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.surroundingWall(move="right")
|
||||
self.rectangularWall(x, z, edges="FFFF", move="right")
|
||||
self.rectangularWall(h, z + 2 * (d + self.thickness), edges="GeGF", move="right")
|
||||
self.lidSide(move="right")
|
||||
self.lidSide(move="mirror right")
|
||||
self.ctx.restore()
|
||||
|
||||
self.surroundingWall(move="up only")
|
||||
|
||||
|
|
|
@ -73,10 +73,9 @@ class JigsawPuzzle(Boxes): # change class name here and below
|
|||
self.corner(parity * 90)
|
||||
# if level == 3 and parity>0: # and random.random() < 100*0.5**(self.depth-2):
|
||||
# self.corner(-360, 0.4*self.size/2**self.depth)
|
||||
# self.ctx.save()
|
||||
# with self.savedcontext():
|
||||
# self.corner(parity*-90)
|
||||
# self.edge(self.size/2**self.depth)
|
||||
# self.ctx.restore()
|
||||
|
||||
def render(self):
|
||||
size = self.size
|
||||
|
|
|
@ -64,6 +64,13 @@ class Lamp(Boxes):
|
|||
def __init__(self):
|
||||
Boxes.__init__(self)
|
||||
self.addSettingsArgs(edges.FingerJointSettings)
|
||||
self.buildArgParser(x=220, y=75, h=70)
|
||||
self.argparser.add_argument(
|
||||
"--radius", action="store", type=float, default="105",
|
||||
help="radius of the lamp")
|
||||
self.argparser.add_argument(
|
||||
"--width", action="store", type=float, default="10",
|
||||
help="width of the ring")
|
||||
|
||||
def side(self, y, h):
|
||||
return
|
||||
|
@ -74,7 +81,7 @@ class Lamp(Boxes):
|
|||
self.edges["f"](h)
|
||||
self.corner(90)
|
||||
|
||||
def render(self, r, w, x, y, h):
|
||||
def render(self):
|
||||
"""
|
||||
r : radius of lamp
|
||||
w : width of surrounding ring
|
||||
|
@ -87,6 +94,9 @@ class Lamp(Boxes):
|
|||
|
||||
# self.edges["f"].settings = (5, 5) # XXX
|
||||
|
||||
x, y, h = self.x, self.y, self.h
|
||||
r, w = self.radius, self.width
|
||||
|
||||
s = RoundedTriangleSettings(self.thickness, angle=72, r_hole=2)
|
||||
self.addPart(RoundedTriangle(self, s))
|
||||
|
||||
|
@ -108,13 +118,13 @@ class Lamp(Boxes):
|
|||
self.surroundingWall(d, d, r, 120, top='h', bottom='h', callback=[
|
||||
None, hole, None, hole], move="up")
|
||||
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.rectangularWall(x, y, edges="fFfF", holesMargin=5, move="right")
|
||||
self.rectangularWall(x, y, edges="fFfF", holesMargin=5, move="right")
|
||||
# sides
|
||||
self.rectangularWall(y, h, "fftf", move="right")
|
||||
self.rectangularWall(y, h, "fftf")
|
||||
self.ctx.restore()
|
||||
|
||||
self.rectangularWall(x, y, edges="fFfF", holesMargin=5,
|
||||
move="up only")
|
||||
|
||||
|
|
|
@ -71,12 +71,10 @@ class MagazinFile(Boxes):
|
|||
|
||||
self.open()
|
||||
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.rectangularWall(x, h, "Ffef", move="up")
|
||||
self.rectangularWall(x, hi, "Ffef", move="up")
|
||||
|
||||
self.rectangularWall(y, x, "ffff")
|
||||
self.ctx.restore()
|
||||
self.rectangularWall(x, y, "ffff")
|
||||
|
||||
self.rectangularWall(x, h, "Ffef", move="right only")
|
||||
self.side(y, h, hi)
|
||||
|
|
|
@ -138,12 +138,11 @@ class OttoBody(Boxes):
|
|||
# bottom
|
||||
self.rectangularWall(x, y, "ffff", callback=[self.bottomCB], move="up")
|
||||
# PCB mounts
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.PCB_Clamp(y-53.5, 4.5, hl, move="right")
|
||||
self.PCB_Clamp(y-50, 4.5, hl, move="right")
|
||||
self.PCB_Clip(3.5, hl, move="right")
|
||||
self.rectangularWall(15, 15, callback=[self.buttonCB])
|
||||
self.ctx.restore()
|
||||
self.PCB_Clamp(y-53.5, 4.5, hl, move="up only")
|
||||
# servo mounts
|
||||
self.moveTo(0, 50)
|
||||
|
|
|
@ -187,7 +187,7 @@ class Planetary2(Boxes):
|
|||
|
||||
# Planets
|
||||
for i in range(numplanets):
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.gears(teeth=self.planetteeth, dimension=self.modulus,
|
||||
angle=pressure_angle,
|
||||
callback=lambda:self.pins(0.25*size2, pinsize, i),
|
||||
|
@ -202,7 +202,6 @@ class Planetary2(Boxes):
|
|||
angle=pressure_angle,
|
||||
callback=lambda:self.pins(0.25*size2, pinsize, i),
|
||||
profile_shift=profile_shift, move="right")
|
||||
self.ctx.restore()
|
||||
|
||||
self.gears(teeth=self.planetteeth, dimension=self.modulus,
|
||||
angle=pressure_angle,
|
||||
|
@ -210,13 +209,3 @@ class Planetary2(Boxes):
|
|||
|
||||
self.text("1:%.1f" % abs(ratio))
|
||||
self.close()
|
||||
|
||||
|
||||
def main():
|
||||
b = Planetary()
|
||||
b.parseArgs()
|
||||
b.render()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
@ -56,7 +56,6 @@ class Pulley(Boxes):
|
|||
if self.move(w, w, move, before=True):
|
||||
return
|
||||
|
||||
self.ctx.save()
|
||||
self.moveTo(w / 2, w / 2)
|
||||
self.cc(callback, None, 0.0, 0.0)
|
||||
|
||||
|
@ -65,7 +64,6 @@ class Pulley(Boxes):
|
|||
|
||||
self.moveTo(diameter / 2 + self.burn, 0, 90)
|
||||
self.corner(360, diameter / 2)
|
||||
self.ctx.restore()
|
||||
self.move(w, w, move)
|
||||
|
||||
def render(self):
|
||||
|
|
|
@ -59,7 +59,7 @@ class RegularBox(Boxes):
|
|||
|
||||
r, sh, side = self.regularPolygon(n, radius=r)
|
||||
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.regularPolygonWall(corners=n, r=r, edges='F', move="right")
|
||||
if self.top == "angled lid":
|
||||
self.regularPolygonWall(corners=n, r=r, edges='e', move="right")
|
||||
|
@ -76,7 +76,6 @@ class RegularBox(Boxes):
|
|||
if self.top == "round lid":
|
||||
self.parts.disc(sh*2, move="right")
|
||||
|
||||
self.ctx.restore()
|
||||
self.regularPolygonWall(corners=n, r=r, edges='F', move="up only")
|
||||
|
||||
side = 2 * math.sin(math.radians(180.0/n)) * r
|
||||
|
|
|
@ -211,7 +211,8 @@ class Rotary(Boxes):
|
|||
60, t + 2)], move="up")
|
||||
self.rectangularWall(hl, hh, edges="hfef", callback=[self.holderBaseCB], move="up")
|
||||
self.rectangularWall(hl, hw, edges="ffff", callback=[lambda: self.hole(hl / 2 - 16 - 20, 25, 5)], move="up")
|
||||
self.ctx.save()
|
||||
|
||||
with self.saved_context():
|
||||
self.rectangularWall(hw, hh, edges="hFeF", callback=[
|
||||
lambda: self.hole(hw / 2, hh - 20, 4)],move="right")
|
||||
self.rectangularWall(hw, hh, edges="hFeF", move="right")
|
||||
|
@ -223,7 +224,6 @@ class Rotary(Boxes):
|
|||
callback=[lambda: self.fingerHolesAt(20 - 0.5 * t, 0, th)])
|
||||
self.rectangularWall(hw + 20, th, edges="fFeF", move="right",
|
||||
callback=[lambda: self.fingerHolesAt(20 - 0.5 * t, 0, th)])
|
||||
self.ctx.restore()
|
||||
|
||||
self.rectangularWall(hw, hh, edges="hFeF", move="up only")
|
||||
outset = OutsetEdge(self, None)
|
||||
|
@ -251,16 +251,16 @@ class Rotary(Boxes):
|
|||
self.link(hl - 40, 25, a, True, move="up")
|
||||
self.link(hl - 40, 25, a, True, move="up")
|
||||
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.rectangularWall(hw - 2 * t - 2, 60, edges="efef", move="right")
|
||||
self.rectangularWall(hw - 4 * t - 4, 60, edges="efef", move="right")
|
||||
# Spindel auxiliaries
|
||||
self.parts.waivyKnob(50, callback=lambda: self.nutHole("M8"), move="right")
|
||||
self.parts.waivyKnob(50, callback=lambda: self.nutHole("M8"), move="right")
|
||||
self.ctx.restore()
|
||||
|
||||
self.rectangularWall(hw - 2 * t - 4, 60, edges="efef", move="up only")
|
||||
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
slot = edges.SlottedEdge(self, [(30 - t) / 2, (30 - t) / 2], slots=15)
|
||||
self.rectangularWall(30, 30, edges=["e", "e", slot, "e"],
|
||||
callback=[lambda: self.hole(7, 23, self.axle / 2)], move="right")
|
||||
|
@ -276,7 +276,6 @@ class Rotary(Boxes):
|
|||
self.rectangularWall(20, 30,
|
||||
callback=[lambda: self.hole(10, 15, 4)], move="right")
|
||||
|
||||
self.ctx.restore()
|
||||
self.rectangularWall(30, 30, move="up only")
|
||||
|
||||
self.h = h = bh + 2 + 1.0 * d # height of outer pieces
|
||||
|
@ -291,10 +290,9 @@ class Rotary(Boxes):
|
|||
move="up")
|
||||
self.rectangularWall(3.6 * d, ow, edges="ffff", move="up")
|
||||
self.rectangularWall(3.6 * d, ow, edges="ffff", move="up")
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.rectangularWall(ow, h, edges="hFFH", move="right")
|
||||
self.rectangularWall(ow, h, edges="hFFH", move="right")
|
||||
self.ctx.restore()
|
||||
self.rectangularWall(ow, h, edges="hFFH", move="up only")
|
||||
|
||||
# Motor block
|
||||
|
@ -302,13 +300,12 @@ class Rotary(Boxes):
|
|||
self.rectangularWall(3.6 * d, h, edges=["h", "f", MotorEdge(self, None),"f"], callback=[self.mainPlate], move="up")
|
||||
self.rectangularWall(3.6 * d, h, edges=["h", "f", MotorEdge(self, None),"f"], callback=[self.frontPlate], move="up")
|
||||
self.rectangularWall(3.6 * d, mw, edges="ffff", move="up")
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.rectangularWall(mw, h, edges="hFeH", move="right")
|
||||
self.rectangularWall(mw, h, edges="hFeH", move="right")
|
||||
|
||||
self.pulley(88, "GT2_2mm", r_axle=a / 2.0, move="right")
|
||||
self.pulley(88, "GT2_2mm", r_axle=a / 2.0, move="right")
|
||||
self.ctx.restore()
|
||||
self.rectangularWall(mw, h, edges="hFeH", move="up only")
|
||||
self.axle = 19
|
||||
|
||||
|
|
|
@ -71,13 +71,13 @@ class RoundedBox(Boxes):
|
|||
|
||||
t = self.thickness
|
||||
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.roundedPlate(x, y, r, wallpieces=self.wallpieces, move="right")
|
||||
self.roundedPlate(x, y, r, wallpieces=self.wallpieces, move="right",
|
||||
callback=[self.hole] if self.top != "closed" else None)
|
||||
if self.top == "lid":
|
||||
self.roundedPlate(x, y, r, "E", wallpieces=self.wallpieces, move="right")
|
||||
self.ctx.restore()
|
||||
|
||||
self.roundedPlate(x, y, r, wallpieces=self.wallpieces, move="up only")
|
||||
|
||||
self.surroundingWall(x, y, r, h, "F", "F", pieces=self.wallpieces)
|
||||
|
|
|
@ -84,12 +84,11 @@ class RoyalGame(Boxes): # Change class name!
|
|||
@holeCol
|
||||
def rosette(self, x, y, s):
|
||||
self.moveTo(x, y, 22.5)
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.moveTo(0.1*s, 0, -30)
|
||||
for i in range(8):
|
||||
self.polyline(0, (60, 0.35*s), 0, 120, 0, (60, 0.35*s), 0,
|
||||
-120, 0, (45, 0.1*s), 0, -120)
|
||||
self.ctx.restore()
|
||||
self.moveTo(0, 0, -22.5)
|
||||
self.moveTo(0.175*s, 0)
|
||||
for i in range(8):
|
||||
|
@ -103,7 +102,7 @@ class RoyalGame(Boxes): # Change class name!
|
|||
posy = y+dy*0.25*s
|
||||
self.rectangularHole(posx, posy, 0.4*s, 0.5*s)
|
||||
self.hole(posx, posy, 0.05*s)
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.moveTo(posx, posy-0.2*s, 60)
|
||||
self.corner(60, 0.4*s)
|
||||
self.corner(120)
|
||||
|
@ -114,7 +113,6 @@ class RoyalGame(Boxes): # Change class name!
|
|||
self.corner(60, 0.5*s)
|
||||
self.corner(120)
|
||||
self.corner(60, 0.5*s)
|
||||
self.ctx.restore()
|
||||
|
||||
for i in range(4):
|
||||
self.rectangularHole(x, y + (i-1.5)*s*0.25, 0.12*s, 0.12*s)
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from boxes import Boxes
|
||||
from boxes import Boxes, restore
|
||||
|
||||
|
||||
class Silverware(Boxes):
|
||||
|
@ -64,9 +64,8 @@ class Silverware(Boxes):
|
|||
},
|
||||
move="up")
|
||||
|
||||
@restore
|
||||
def centerWall(self, x, h):
|
||||
self.ctx.save()
|
||||
|
||||
self.moveTo(self.edges["f"].spacing(), self.edges["f"].spacing())
|
||||
for i in range(2, 5):
|
||||
self.fingerHolesAt(i * x / 6.0, 0, h - 10)
|
||||
|
@ -81,9 +80,6 @@ class Silverware(Boxes):
|
|||
self.corner(90)
|
||||
self.edges["f"](h - 10)
|
||||
self.corner(90)
|
||||
self.ctx.restore()
|
||||
|
||||
self.moveTo(x + 2 * self.edges["f"].spacing())
|
||||
|
||||
##################################################
|
||||
### main
|
||||
|
@ -101,6 +97,7 @@ class Silverware(Boxes):
|
|||
|
||||
self.wall(x, y, h, r)
|
||||
self.centerWall(x, h)
|
||||
self.moveTo(x + 2 * self.edges["f"].spacing())
|
||||
|
||||
l = (y - t) / 2.0
|
||||
|
||||
|
|
|
@ -52,12 +52,11 @@ class TwoPiece(Boxes):
|
|||
|
||||
for i in range(2):
|
||||
d = i * 2 * (t+p)
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.rectangularWall(x+d, h, "fFeF", bedBolts=None, move="right")
|
||||
self.rectangularWall(y+d, h, "ffef", bedBolts=None, move="right")
|
||||
self.rectangularWall(x+d, h, "fFeF", bedBolts=None, move="right")
|
||||
self.rectangularWall(y+d, h, "ffef", bedBolts=None, move="right")
|
||||
self.ctx.restore()
|
||||
self.rectangularWall(y, h, "ffef", bedBolts=None, move="up only")
|
||||
|
||||
self.rectangularWall(x, y, "hhhh", bedBolts=None, move="right")
|
||||
|
|
|
@ -89,8 +89,7 @@ class UnevenHeightBox(Boxes):
|
|||
self.wall(x, h2, h3, [b, "F", "F"], move="right")
|
||||
self.wall(y, h3, h0, [b, "f", "f"], move="right")
|
||||
|
||||
self.ctx.save()
|
||||
|
||||
with self.saved_context():
|
||||
if b != "e":
|
||||
self.rectangularWall(x, y, "ffff", move="up")
|
||||
|
||||
|
@ -99,12 +98,10 @@ class UnevenHeightBox(Boxes):
|
|||
lidheights = [maxh-h for h in heights]
|
||||
h0, h1, h2, h3 = lidheights
|
||||
lidheights += lidheights
|
||||
|
||||
edges = ["E" if (lidheights[i] == 0.0 and lidheights[i+1] == 0.0) else "f" for i in range(4)]
|
||||
self.rectangularWall(x, y, edges, move="up")
|
||||
|
||||
self.ctx.restore()
|
||||
|
||||
if self.lid:
|
||||
self.moveTo(0, maxh+self.edges["F"].spacing()+self.edges[b].spacing()+3*self.spacing, 180)
|
||||
self.wall(y, h0, h3, "Fff", move="right" +
|
||||
(" only" if h0 == h3 == 0.0 else ""))
|
||||
|
|
|
@ -67,7 +67,7 @@ class UniversalBox(_TopEdge, _ChestLid):
|
|||
d2 = d3 = None
|
||||
|
||||
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.rectangularWall(x, h, [b, "F", t1, "F"],
|
||||
bedBolts=[d2], move="up")
|
||||
self.rectangularWall(x, h, [b, "F", t3, "F"],
|
||||
|
@ -85,7 +85,6 @@ class UniversalBox(_TopEdge, _ChestLid):
|
|||
self.set_source_color(Color.BLACK)
|
||||
self.drawAddOnLid(x, y, self.lid)
|
||||
|
||||
self.ctx.restore()
|
||||
self.rectangularWall(x, h, [b, "F", t3, "F"],
|
||||
bedBolts=[d2], move="right only")
|
||||
self.rectangularWall(y, h, [b, "f", t2, "f"],
|
||||
|
|
|
@ -39,11 +39,10 @@ class WineRack(Boxes):
|
|||
help="which of the honey comb walls to add")
|
||||
|
||||
def hexFingerHoles(self, x, y, l, angle=90):
|
||||
self.ctx.save()
|
||||
with self.saved_context():
|
||||
self.moveTo(x, y, angle)
|
||||
self.moveTo(self.delta, 0, 0)
|
||||
self.fingerHolesAt(0, 0, l-2*self.delta, 0)
|
||||
self.ctx.restore()
|
||||
|
||||
def wallCB(self, frontwall=False, backwall=False):
|
||||
r = self.r
|
||||
|
|
Loading…
Reference in New Issue