Added bbox adjust extension

This commit is contained in:
leyghisbb 2021-04-21 10:48:01 +02:00
parent 0d36adb024
commit e475d8b3b0
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<menu-tip>Widen the document to send all lines properly to Epilog Dashboard (adds 1.0 doc units of extra offset)</menu-tip>
<object-type>all</object-type>
<effects-menu>
<submenu name="FabLab Chemnitz">
<submenu name="Various">
</submenu>
</effects-menu>
</effect>
<script>
<command reldir="inx" interpreter="python">epilog_bbox_adjust.py</command>
</script>
</inkscape-extension>

View File

@ -0,0 +1,35 @@
#!/usr/bin/env python3
'''
This tool is a helper to adjust the document border including an offset value, which is added.
Sending vector data to Epilog Dashboard often results in trimmed paths. This leads to wrong geometry where the laser misses to cut them.
So we add a default (small) amount of 0.05 doc units to expand the document's canvas
'''
import inkex
from inkex import Transform
class BBoxAdjust(inkex.EffectExtension):
def effect(self):
offset = 1.0 #in documents' units
# 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()
for element in self.svg.root.getchildren():
if isinstance (element, inkex.ShapeElement):
bbox += element.bounding_box()
# adjust the viewBox to the bbox size and add the desired offset
self.document.getroot().attrib['viewBox'] = f'{-offset} {-offset} {bbox.width + offset * 2} {bbox.height + offset * 2}'
self.document.getroot().attrib['width'] = f'{bbox.width + offset * 2}' + self.svg.unit
self.document.getroot().attrib['height'] = f'{bbox.height + offset * 2}' + self.svg.unit
# 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():
if isinstance (element, inkex.ShapeElement):
element.transform = Transform(mat) * element.transform
if __name__ == '__main__':
BBoxAdjust().run()