Bbox enhancements for adjuster extension

This commit is contained in:
Mario Voigt 2021-05-23 16:38:46 +02:00
parent c6000b4ada
commit 34c26e41ae
2 changed files with 40 additions and 9 deletions

View File

@ -2,14 +2,24 @@
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>Epilog Dashboard BBox Adjust</name>
<id>fablabchemnitz.de.epilog_bbox_adjust</id>
<effect needs-document="true" needs-live-preview="false">
<param name="offset" type="float" min="0.0" max="1000.0" precision="3" gui-text="XY Offset (mm) from top left corner">1.0</param>
<param name="use_machine_size" type="bool" gui-text="Use machine size (else use symmetric border)">false</param>
<param name="machine_size" gui-text="Machine/Size" type="optiongroup" appearance="combo">
<option value="406x305">406 x 305 mm (Zing 16)</option>
<option value="610x305">610 x 305 mm (Zing 24 / Fusion Edge 12)</option>
<option value="812x508">812 x 508 mm (Fusion Pro 32 / Fusion M2 32)</option>
<option value="1016x711">1016 x 711 (Fusion M2 40)</option>
<option value="1219x914">1219 x 914 mm (Fusion Pro 48)</option>
</param>
<label>Widen the document to send all lines properly to Epilog Dashboard</label>
<effect needs-document="true" needs-live-preview="true">
<object-type>all</object-type>
<effects-menu>
<submenu name="FabLab Chemnitz">
<submenu name="Various"/>
</submenu>
</effects-menu>
<menu-tip>Widen the document to send all lines properly to Epilog Dashboard (adds 1.0 doc units of extra offset)</menu-tip>
<menu-tip>Widen the document to send all lines properly to Epilog Dashboard</menu-tip>
</effect>
<script>
<command location="inx" interpreter="python">epilog_bbox_adjust.py</command>

View File

@ -10,17 +10,29 @@ So we add a default (small) amount of 1.0 doc units to expand the document's can
Author: Mario Voigt / FabLab Chemnitz
Mail: mario.voigt@stadtfabrikanten.org
Date: 21.04.2021
Last patch: 21.04.2021
Last patch: 23.05.2021
License: GNU GPL v3
#known bugs:
- viewbox/width/height do not correctly apply if documents only contain an object (not a path). After converting it to path it works. Seems to be a bbox problem
- note from 07.05.2021: seems if the following order is viewBox/width/height, or width/height/viewBox, the units are not respected. So me mess around a little bit
'''
import math
import inkex
from inkex import Transform
class BBoxAdjust(inkex.EffectExtension):
def add_arguments(self, pars):
pars.add_argument("--offset", type=float, default="1.0", help="XY Offset (mm) from top left corner")
pars.add_argument("--use_machine_size", type=inkex.Boolean, default=False, help="Use machine size")
pars.add_argument("--machine_size", default="812x508", help="Machine/Size")
def effect(self):
offset = 1.0 #in documents' units
offset = self.options.offset
#units = self.svg.unit
units = "mm" #force millimeters
# create a new bounding box and get the bbox size of all elements of the document (we cannot use the page's bbox)
bbox = inkex.BoundingBox()
@ -28,12 +40,21 @@ class BBoxAdjust(inkex.EffectExtension):
if isinstance (element, inkex.ShapeElement):
bbox += element.bounding_box()
# adjust the viewBox to the bbox size and add the desired offset
# note from 07.05.2021: seems if the following order is viewBox/width/height, or width/height/viewBox, the units are not respected. So me mess around a little bit
self.document.getroot().attrib['width'] = f'{bbox.width + offset * 2}' + self.svg.unit
self.document.getroot().attrib['viewBox'] = f'{-offset} {-offset} {bbox.width + offset * 2} {bbox.height + offset * 2}'
self.document.getroot().attrib['height'] = f'{bbox.height + offset * 2}' + self.svg.unit
if abs(bbox.width) == math.inf or abs(bbox.height) == math.inf:
inkex.utils.debug("Error calculating bounding box!")
return
# adjust the viewBox to the bbox size and add the desired offset
if self.options.use_machine_size is True:
machineHeight = float(self.options.machine_size.split('x')[0])
machineWidth = float(self.options.machine_size.split('x')[1])
self.document.getroot().attrib['width'] = f'{machineHeight}' + units
self.document.getroot().attrib['viewBox'] = f'{-offset} {-offset} {machineHeight} {machineWidth}'
self.document.getroot().attrib['height'] = f'{machineWidth}' + units
else:
self.document.getroot().attrib['width'] = f'{bbox.width + offset * 2}' + units
self.document.getroot().attrib['viewBox'] = f'{-offset} {-offset} {bbox.width + offset * 2} {bbox.height + offset * 2}'
self.document.getroot().attrib['height'] = f'{bbox.height + offset * 2}' + units
# translate all elements to fit the adjusted viewBox
mat = Transform("translate(%f, %f)" % (-bbox.left,-bbox.top)).matrix
for element in self.svg.root.getchildren():