-
Notifications
You must be signed in to change notification settings - Fork 1
/
draw_bbox.py
executable file
·58 lines (44 loc) · 1.59 KB
/
draw_bbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python2
import sys
sys.path.append('/usr/share/inkscape/extensions')
from collections import defaultdict
from inkex import Effect as InkscapeEffect
from inkex import etree, addNS
from simpletransform import computeBBox, applyTransformToNode
from simplestyle import formatStyle
from simplepath import parsePath, translatePath, formatPath
class DrawBBoxes(InkscapeEffect):
def __init__(self):
InkscapeEffect.__init__(self)
self.filename = sys.argv[-1]
def effect(self):
if len(self.selected) > 0:
bboxes = self.calculate_bboxes(self.selected)
for id, node, bbox in bboxes:
self.draw_bbox(bbox)
def calculate_bboxes(self, nodes):
bboxes = [(id, node, computeBBox([node]))
for id, node in nodes.items()]
return bboxes
def draw_bbox(self, bbox):
(x1, x2, y1, y2) = bbox
width = x2 - x1
height = y2 - y1
self.draw_rect(x1, y1, width, height)
#SVG element generation routine
def draw_rect(self, x, y, width, height):
layer = self.current_layer
style = { 'stroke' : '#ff0000',
'stroke-width' : '1',
'fill' : 'none',
}
attribs = {
'style' : formatStyle(style),
'x' : str(x),
'y' : str(y),
'width' : str(width),
'height' : str(height),
}
rect = etree.SubElement(layer, addNS('rect','svg'), attribs )
effect = DrawBBoxes()
effect.affect()