diff --git a/extensions/fablabchemnitz/bounding_box/bounding_box.inx b/extensions/fablabchemnitz/bounding_box/bounding_box.inx index 38ecee40..5cbe9501 100644 --- a/extensions/fablabchemnitz/bounding_box/bounding_box.inx +++ b/extensions/fablabchemnitz/bounding_box/bounding_box.inx @@ -3,10 +3,20 @@ Bounding Box fablabchemnitz.de.bounding_box + + + + + + + + 0.000 - true 0.000 + + true false + true all diff --git a/extensions/fablabchemnitz/bounding_box/bounding_box.py b/extensions/fablabchemnitz/bounding_box/bounding_box.py index e41bcaff..7dafd727 100644 --- a/extensions/fablabchemnitz/bounding_box/bounding_box.py +++ b/extensions/fablabchemnitz/bounding_box/bounding_box.py @@ -8,19 +8,22 @@ class BoundingBox(inkex.EffectExtension): def add_arguments(self, pars): pars.add_argument('--offset', type=float, default=0.0, help='Offset from object (all directions)') + pars.add_argument('--unit', default="mm") pars.add_argument('--box', type=inkex.Boolean, default=0.0, help='Draw boxes') pars.add_argument('--corner_radius', type=float, default=0.0, help='Corner radius') 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') def drawBBox(self, bbox): + so = self.options + offset = self.svg.unittouu(str(so.offset) + so.unit) 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), + 'style' : str(inkex.Style({'stroke':'#ff0000','stroke-width':str(self.svg.unittouu("1px")),'fill':'none'})), + 'x' : str(bbox.left - offset), + 'y' : str(bbox.top - offset), + 'width' : str(bbox.width + 2 * offset), + 'height': str(bbox.height + 2 * offset), 'ry' : str(self.options.corner_radius), 'rx' : str(self.options.corner_radius) } @@ -28,23 +31,42 @@ class BoundingBox(inkex.EffectExtension): if self.options.circle: attribs = { - 'style': str(inkex.Style({'stroke':'#ff0000','stroke-width' : '1','fill':'none'})), + 'style': str(inkex.Style({'stroke':'#ff0000','stroke-width':str(self.svg.unittouu("1px")),'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), + #'r' : str(bbox.width / 2 + offset), + 'r' : str(math.sqrt((bbox.width + 2 * offset)* (bbox.width + 2 * 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) + def effect(self): + + scale_factor = self.svg.unittouu("1px") + if len(self.svg.selected) > 0: if self.options.split is False: for element in self.svg.selected.values(): - self.drawBBox(element.bounding_box()) - else: + if isinstance (element, inkex.ShapeElement) and element.tag != inkex.addNS('use','svg') and element.get('inkscape:groupmode') != 'layer': #bbox fails for svg:use elements and layers: + if isinstance (element, inkex.Rectangle) or \ + isinstance (element, inkex.Circle) or \ + isinstance (element, inkex.Ellipse): + self.drawBBox(element.bounding_box() * scale_factor) + else: + self.drawBBox(element.bounding_box()) + else: #combined bbox #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 - + #self.drawBBox(self.svg.selection.bounding_box()) #works for InkScape 1.1dev (9b1fc87, 2020-08-27)) @ Windows + bbox = inkex.BoundingBox() + for element in self.svg.selected.values(): + if isinstance (element, inkex.ShapeElement) and element.tag != inkex.addNS('use','svg') and element.get('inkscape:groupmode') != 'layer': #bbox fails for svg:use elements and layers: + if isinstance (element, inkex.Rectangle) or \ + isinstance (element, inkex.Circle) or \ + isinstance (element, inkex.Ellipse): + bbox += element.bounding_box() * scale_factor + else: + bbox += element.bounding_box() + self.drawBBox(bbox) else: inkex.errormsg('Please select some objects first.') return