Detect size of drawing and set svg viewport accordingly

Remove width and height parameter from Boxes.open()
Code for detecting the size ignores text. Until this is fixed you need
to surround text by lines of some sort.
This commit is contained in:
Florian Festi 2016-06-07 20:20:35 +02:00
parent 3361f2ae13
commit 0ba0c19af2
19 changed files with 29 additions and 35 deletions

View File

@ -21,6 +21,7 @@ from argparse import ArgumentParser
import re import re
from functools import wraps from functools import wraps
from boxes import edges from boxes import edges
from boxes import svgutil
### Helpers ### Helpers
@ -173,22 +174,18 @@ class Boxes:
"--burn", action="store", type=float, default=0.05, "--burn", action="store", type=float, default=0.05,
help="burn correction in mm") help="burn correction in mm")
def open(self, width, height): def open(self):
""" """
Prepare for rendering Prepare for rendering
Call this function from your .render() method Call this function from your .render() method
:param width: width of canvas in mm
:param height: height of canvas in mm
""" """
self.spacing = 2*self.burn + 0.5 * self.thickness self.spacing = 2*self.burn + 0.5 * self.thickness
self.fingerHoleEdgeWidth = 1.0 # multitudes of self.thickness self.fingerHoleEdgeWidth = 1.0 # multitudes of self.thickness
self.bedBoltSettings = (3, 5.5, 2, 20, 15) #d, d_nut, h_nut, l, l1 self.bedBoltSettings = (3, 5.5, 2, 20, 15) #d, d_nut, h_nut, l, l1
self.hexHolesSettings = (5, 3, 'circle') # r, dist, style self.hexHolesSettings = (5, 3, 'circle') # r, dist, style
self._init_surface(width, height) self._init_surface(1000, 1000)
self._buildObjects() self._buildObjects()
def buildArgParser(self, *l): def buildArgParser(self, *l):
@ -329,7 +326,7 @@ class Boxes:
"""Implement this method in your sub class. """Implement this method in your sub class.
You will typically need to call .parseArgs() before calling this one""" You will typically need to call .parseArgs() before calling this one"""
self.open(100, 100) self.open()
# Change settings and creat new Edges and part classes here # Change settings and creat new Edges and part classes here
raise NotImplemented raise NotImplemented
self.close() self.close()
@ -385,6 +382,11 @@ class Boxes:
self.surface.flush() self.surface.flush()
self.surface.finish() self.surface.finish()
svg = svgutil.SVGFile(self.output)
svg.getEnvelope()
svg.rewriteViewPort()
return
f = open(self.output, "r+") f = open(self.output, "r+")
s = f.read(1024) s = f.read(1024)
pos = s.find('pt"') pos = s.find('pt"')
@ -1111,7 +1113,7 @@ class DemoBox(Boxes):
def render(self): def render(self):
""" """ """ """
x, y, h, t = self.x, self.y, self.h, self.thickness x, y, h, t = self.x, self.y, self.h, self.thickness
self.open(2*x+10*self.thickness, y+2*h+20*self.thickness) self.open()
self.ctx.save() self.ctx.save()
self.moveTo(t, t) self.moveTo(t, t)

View File

@ -32,8 +32,8 @@ class Box(Boxes):
# adjust to the variables you want in the local scope # adjust to the variables you want in the local scope
x, y, h = self.x, self.y, self.h x, y, h = self.x, self.y, self.h
t = self.thickness t = self.thickness
# Calculate canvas size and pass as width and height # Initialize canvas
self.open(width=x+y+40, height=y+2*h+50) self.open()
# Change settings of default edges if needed. E.g.: # Change settings of default edges if needed. E.g.:
self.edges["f"].settings.setValues(self.thickness, space=3, finger=3, self.edges["f"].settings.setValues(self.thickness, space=3, finger=3,

View File

@ -29,7 +29,7 @@ class Box(Boxes):
def render(self): def render(self):
x, y, h = self.x, self.y, self.h x, y, h = self.x, self.y, self.h
t = self.thickness t = self.thickness
self.open(width=max(x+y,2*x)+10*t, height=y+2*h+12*t) self.open()
d2 = [edges.Bolts(2)] d2 = [edges.Bolts(2)]
d3 = [edges.Bolts(3)] d3 = [edges.Bolts(3)]

View File

@ -32,7 +32,7 @@ class Box(Boxes):
def render(self): def render(self):
x, y, h = self.x, self.y, self.h x, y, h = self.x, self.y, self.h
self.open(width=x+y+40, height=y+2*h+50) self.open()
b = self.edges.get(self.bottom_edge, self.edges["F"]) b = self.edges.get(self.bottom_edge, self.edges["F"])
t = self.edges.get(self.top_edge, self.edges["e"]) t = self.edges.get(self.top_edge, self.edges["e"])

View File

@ -30,7 +30,7 @@ class Box(Boxes):
x, y, h = self.x, self.y, self.h x, y, h = self.x, self.y, self.h
t = self.thickness t = self.thickness
self.open(width=x+y+10*t, height=y+h+10*t) self.open()
d2 = [edges.Bolts(2)] d2 = [edges.Bolts(2)]
d3 = [edges.Bolts(3)] d3 = [edges.Bolts(3)]

View File

@ -22,7 +22,7 @@ class Castle(Boxes):
Boxes.__init__(self) Boxes.__init__(self)
def render(self, t_x=70, t_h=250, w1_x=300, w1_h=120, w2_x=100, w2_h=120): 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) self.open()
s = edges.FingerJointSettings(self.thickness, relative=False, s = edges.FingerJointSettings(self.thickness, relative=False,
space = 10, finger=10, height=10, space = 10, finger=10, height=10,
width=self.thickness) width=self.thickness)

View File

@ -53,7 +53,7 @@ in 0.5mm steps, 3 holes each size"""
x, y, h = self.x, self.y, self.h x, y, h = self.x, self.y, self.h
t = self.thickness t = self.thickness
self.open(width=x+y+40, height=3*y+2*h+16*t) self.open()
self.edges["f"].settings.setValues(self.thickness, space=3, finger=3, self.edges["f"].settings.setValues(self.thickness, space=3, finger=3,
surroundingspaces=1) surroundingspaces=1)

View File

@ -76,10 +76,7 @@ class FlexBox(boxes.Boxes):
self.latchsize = 8 * self.thickness self.latchsize = 8 * self.thickness
c4 = math.pi * r * 0.5 c4 = math.pi * r * 0.5
width = 2*x + 2*y - 8*r + 4*c4 + 4*self.thickness self.open()
height = y + h + 8*self.thickness
self.open(width, height)
self.moveTo(self.thickness, self.thickness) self.moveTo(self.thickness, self.thickness)
self.surroundingWall() self.surroundingWall()

View File

@ -71,10 +71,7 @@ class FlexBox(Boxes):
self.c4 = c4 = math.pi * self.radius * 0.5 self.c4 = c4 = math.pi * self.radius * 0.5
self.latchsize = 8*self.thickness self.latchsize = 8*self.thickness
width = 2*self.x + self.y - 3*self.radius + 2*c4 + 7*self.thickness + self.latchsize # lock self.open()
height = self.y + self.h + 8*self.thickness
self.open(width, height)
self.fingerJointSettings = (4, 4) self.fingerJointSettings = (4, 4)

View File

@ -109,7 +109,7 @@ class FlexBox(Boxes):
width = 2*x + y - 2*r + c4 + 14*thickness + 3*h # lock width = 2*x + y - 2*r + c4 + 14*thickness + 3*h # lock
height = y + z + 8*thickness height = y + z + 8*thickness
self.open(width, height) self.open()
self.edges["f"].settings.setValues( self.edges["f"].settings.setValues(
self.thickness, finger=2, space=2, surroundingspaces=1) self.thickness, finger=2, space=2, surroundingspaces=1)

View File

@ -33,7 +33,7 @@ class FlexTest(Boxes):
def render(self): def render(self):
x, y = self.x, self.y x, y = self.x, self.y
self.open(x+60, y+20) self.open()
self.edges["X"].settings.setValues( self.edges["X"].settings.setValues(
self.thickness, relative=True, self.thickness, relative=True,

View File

@ -31,7 +31,7 @@ class Folder(Boxes):
def render(self): def render(self):
x, y, r, h = self.x, self.y, self.r, self.h x, y, r, h = self.x, self.y, self.r, self.h
c2 = math.pi * h c2 = math.pi * h
self.open(width=2*x+3*h+20, height=y+20) self.open()
self.moveTo(r+self.thickness, self.thickness) self.moveTo(r+self.thickness, self.thickness)
self.edge(x-r) self.edge(x-r)
self.edges["X"](c2, y) self.edges["X"](c2, y)

View File

@ -77,7 +77,7 @@ class Lamp(Boxes):
h : height box h : height box
""" """
self.open(width=1000, height=1000) self.open()
#self.edges["f"].settings = (5, 5) # XXX #self.edges["f"].settings = (5, 5) # XXX

View File

@ -64,7 +64,7 @@ class Box(Boxes):
self.hi = hi = self.hi or (h / 2.0) self.hi = hi = self.hi or (h / 2.0)
t = self.thickness t = self.thickness
self.open(width=x+y+8*t, height=x+h+hi+4*t) self.open()
self.ctx.save() self.ctx.save()
self.rectangularWall(x, h, "Ffef", move="up") self.rectangularWall(x, h, "Ffef", move="up")

View File

@ -121,7 +121,7 @@ class Printer(Boxes):
self.move(overallwidth, overallheight, move) self.move(overallwidth, overallheight, move)
def render(self): def render(self):
self.open(650, 600) self.open()
self.edges["f"].settings.setValues(self.thickness, surroundingspaces=0) self.edges["f"].settings.setValues(self.thickness, surroundingspaces=0)
self.ctx.save() self.ctx.save()
for i in range(3): for i in range(3):

View File

@ -70,7 +70,7 @@ using flex for rounded corners"""
def render(self): def render(self):
x, y, h, r = 250, 250/1.618, 120, 30 x, y, h, r = 250, 250/1.618, 120, 30
self.open(750, 450) self.open()
t = self.thickness t = self.thickness
b = self.burn b = self.burn

View File

@ -28,8 +28,7 @@ class TrayInsert(Boxes):
h = self.h h = self.h
t = self.thickness t = self.thickness
self.open(width=max(x, y) + 4*t, self.open()
height=(len(self.sx)+len(self.sy)-2)*(h+t)+2*t)
self.moveTo(t, t) self.moveTo(t, t)
# Inner walls # Inner walls

View File

@ -105,8 +105,7 @@ class Layout(Boxes):
for f in line: for f in line:
hasfloor |= f hasfloor |= f
self.open(max((lx+1)*sum(self.y), (ly+1)*sum(self.x))+lx*ly*4*t, self.open()
2*self.h + hasfloor*sum(self.y) + 12*t)
self.edges["s"] = boxes.edges.Slot(self, self.hi/2.0) self.edges["s"] = boxes.edges.Slot(self, self.hi/2.0)
self.edges["C"] = boxes.edges.CrossingFingerHoleEdge(self, self.hi) self.edges["C"] = boxes.edges.CrossingFingerHoleEdge(self, self.hi)

View File

@ -78,7 +78,7 @@ class TypeTray(Boxes):
hi = self.hi = self.hi or h hi = self.hi = self.hi or h
t = self.thickness t = self.thickness
self.open(width=2*max(x,y)+10*t, height=(len(self.sx)+len(self.sy))*(h+2*t)+4*t) self.open()
self.moveTo(t, t) self.moveTo(t, t)
# outer walls # outer walls