fixes in bbox extension

This commit is contained in:
Mario Voigt 2021-11-05 02:08:20 +01:00
parent c4b01c7f94
commit 9216775939
2 changed files with 45 additions and 13 deletions

View File

@ -3,10 +3,20 @@
<name>Bounding Box</name>
<id>fablabchemnitz.de.bounding_box</id>
<label>Draws bounding boxes around selected objects, useful for debugging. Author: Pawel Mosakowski. Modded by Mario Voigt.</label>
<param name="unit" gui-text="Unit" type="optiongroup" appearance="combo" gui-description="The unit applies to interval and thresholds">
<option value="mm">mm</option>
<option value="cm">cm</option>
<option value="px">px</option>
<option value="pt">pt</option>
<option value="pc">pc</option>
<option value="in">in</option>
</param>
<param name="offset" min="-10000.000" max="10000.000" precision="3" type="float" gui-text="Offset from object (all directions)">0.000</param>
<param name="box" type="bool" gui-text="Draw boxes">true</param>
<param name="corner_radius" type="float" min="0.000" precision="3" max="10000.000" gui-text="Corner radius" gui-description="Only applies for box type">0.000</param>
<separator/>
<param name="box" type="bool" gui-text="Draw boxes">true</param>
<param name="circle" type="bool" gui-text="Draw circles">false</param>
<separator/>
<param name="split" type="bool" gui-text="Handle selection as group">true</param>
<effect>
<object-type>all</object-type>

View File

@ -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