2020-07-30 01:16:18 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import inkex
|
2020-08-09 02:31:14 +02:00
|
|
|
import math
|
2020-07-30 01:16:18 +02:00
|
|
|
from lxml import etree
|
|
|
|
|
2021-04-04 01:51:59 +02:00
|
|
|
class DrawBBoxes(inkex.EffectExtension):
|
2021-04-15 17:03:47 +02:00
|
|
|
|
|
|
|
def add_arguments(self, pars):
|
|
|
|
pars.add_argument('--offset', type=float, default=0.0, help='Offset from object (all directions)')
|
|
|
|
pars.add_argument('--box', type=inkex.Boolean, default=0.0, help='Draw boxes')
|
|
|
|
pars.add_argument('--circle', type=inkex.Boolean, default=0.0, help='Draw circles')
|
|
|
|
pars.add_argument('--split', type = inkex.Boolean, default = True, help = 'Handle selection as group')
|
2020-08-15 16:31:38 +02:00
|
|
|
|
2020-08-30 12:17:19 +02:00
|
|
|
def drawBBox(self, bbox):
|
2020-08-15 16:31:38 +02:00
|
|
|
if self.options.box:
|
|
|
|
attribs = {
|
|
|
|
'style' : str(inkex.Style({'stroke':'#ff0000','stroke-width' : '1','fill':'none'})),
|
|
|
|
'x' : str(bbox.left - self.options.offset),
|
|
|
|
'y' : str(bbox.top - self.options.offset),
|
|
|
|
'width' : str(bbox.width + 2 * self.options.offset),
|
|
|
|
'height': str(bbox.height + 2 * self.options.offset),
|
|
|
|
}
|
|
|
|
etree.SubElement(self.svg.get_current_layer(), inkex.addNS('rect','svg'), attribs)
|
|
|
|
|
|
|
|
if self.options.circle:
|
|
|
|
attribs = {
|
|
|
|
'style': str(inkex.Style({'stroke':'#ff0000','stroke-width' : '1','fill':'none'})),
|
|
|
|
'cx' : str(bbox.center_x),
|
|
|
|
'cy' : str(bbox.center_y),
|
|
|
|
#'r' : str(bbox.width / 2 + self.options.offset),
|
|
|
|
'r' : str(math.sqrt((bbox.width + 2 * self.options.offset)* (bbox.width + 2 * self.options.offset) + (bbox.height + 2 * self.options.offset) * (bbox.height + 2 * self.options.offset)) / 2),
|
|
|
|
}
|
|
|
|
etree.SubElement(self.svg.get_current_layer(), inkex.addNS('circle','svg'), attribs)
|
|
|
|
|
2020-07-30 01:16:18 +02:00
|
|
|
def effect(self):
|
|
|
|
if len(self.svg.selected) > 0:
|
2020-08-15 16:31:38 +02:00
|
|
|
if self.options.split is False:
|
2021-04-21 15:09:36 +02:00
|
|
|
for element in self.svg.selected.values():
|
|
|
|
self.drawBBox(element.bounding_box())
|
2020-08-15 16:31:38 +02:00
|
|
|
else:
|
2021-02-22 21:49:08 +01:00
|
|
|
#self.drawBBox(self.svg.get_selected_bbox()) #works for InkScape (1:1.0+devel+202008292235+eff2292935) @ Linux and for Windows (but with deprecation)
|
|
|
|
self.drawBBox(self.svg.selection.bounding_box()) #works for InkScape 1.1dev (9b1fc87, 2020-08-27)) @ Windows
|
2020-08-15 16:31:38 +02:00
|
|
|
|
2021-04-02 10:18:33 +02:00
|
|
|
else:
|
|
|
|
inkex.errormsg('Please select some objects first.')
|
|
|
|
return
|
|
|
|
|
2020-08-31 21:25:41 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
DrawBBoxes().run()
|