added bbox option to export selection extension

This commit is contained in:
Mario Voigt 2021-04-19 23:36:44 +02:00
parent 67c7cea393
commit 0d36adb024
3 changed files with 21 additions and 12 deletions

View File

@ -1,2 +0,0 @@
eclipse.preferences.version=1
encoding//extensions/fablabchemnitz/create_links.py=utf-8

View File

@ -1,17 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension"> <inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<_name>Export selection as SVG/DXF</_name> <_name>Export Selection As ...</_name>
<id>fablabchemnitz.de.export_selection_as_svg_dxf</id> <id>fablabchemnitz.de.export_selection_as</id>
<param name="wrap_transform" type="boolean" _gui-text="Wrap final document in transform">false</param> <param name="wrap_transform" type="boolean" _gui-text="Wrap final document in transform">false</param>
<param name="border_offset" type="float" min="0.000" max="9999.000" precision="3" gui-text="Add border offset around selection">1.000</param>
<param name="export_dir" type="path" mode="folder" gui-text="Location to save exported documents">./inkscape_export/</param> <param name="export_dir" type="path" mode="folder" gui-text="Location to save exported documents">./inkscape_export/</param>
<param name="opendir" type="bool" gui-text="Open containing output directory after export">false</param> <param name="opendir" type="bool" gui-text="Open containing output directory after export">false</param>
<param name="dxf_exporter_path" type="path" mode="file" filetypes="py" gui-text="Location of dxf_outlines.py">/usr/share/inkscape/extensions/dxf_outlines.py</param> <param name="dxf_exporter_path" type="path" mode="file" filetypes="py" gui-text="Location of dxf_outlines.py">/usr/share/inkscape/extensions/dxf_outlines.py</param>
<param name="export_dxf" type="bool" gui-text="Export as DXF R14 file (mm units)">false</param> <param name="export_dxf" type="bool" gui-text="Export as DXF R14 file (mm units)">false</param>
<param name="export_pdf" type="bool" gui-text="Export as PDF 1.5">false</param>
<param name="newwindow" type="bool" gui-text="Open file in new blocking Inkscape window">false</param> <param name="newwindow" type="bool" gui-text="Open file in new blocking Inkscape window">false</param>
<effect needs-document="true" needs-live-preview="false"> <effect needs-document="true" needs-live-preview="false">
<object-type>all</object-type> <object-type>all</object-type>
<menu-tip>Export selection to separate SVG file.</menu-tip> <menu-tip>Export selection to separate files.</menu-tip>
<effects-menu> <effects-menu>
<submenu name="FabLab Chemnitz"> <submenu name="FabLab Chemnitz">
<submenu name="Import/Export/Transfer"/> <submenu name="Import/Export/Transfer"/>

View File

@ -26,10 +26,12 @@ class ExportObject(inkex.EffectExtension):
def add_arguments(self, pars): def add_arguments(self, pars):
pars.add_argument("--wrap_transform", type=inkex.Boolean, default=False, help="Wrap final document in transform") pars.add_argument("--wrap_transform", type=inkex.Boolean, default=False, help="Wrap final document in transform")
pars.add_argument("--border_offset", type=float, default=1.000, help="Add border offset around selection")
pars.add_argument("--export_dir", default="~/inkscape_export/", help="Location to save exported documents") pars.add_argument("--export_dir", default="~/inkscape_export/", help="Location to save exported documents")
pars.add_argument("--opendir", type=inkex.Boolean, default=False, help="Open containing output directory after export") pars.add_argument("--opendir", type=inkex.Boolean, default=False, help="Open containing output directory after export")
pars.add_argument("--dxf_exporter_path", default="/usr/share/inkscape/extensions/dxf_outlines.py", help="Location of dxf_outlines.py") pars.add_argument("--dxf_exporter_path", default="/usr/share/inkscape/extensions/dxf_outlines.py", help="Location of dxf_outlines.py")
pars.add_argument("--export_dxf", type=inkex.Boolean, default=False, help="Create a dxf file") pars.add_argument("--export_dxf", type=inkex.Boolean, default=False, help="Create a dxf file")
pars.add_argument("--export_pdf", type=inkex.Boolean, default=False, help="Create a pdf file")
pars.add_argument("--newwindow", type=inkex.Boolean, default=False, help="Open file in new Inkscape window") pars.add_argument("--newwindow", type=inkex.Boolean, default=False, help="Open file in new Inkscape window")
@ -48,6 +50,8 @@ class ExportObject(inkex.EffectExtension):
export_dir = Path(self.absolute_href(self.options.export_dir)) export_dir = Path(self.absolute_href(self.options.export_dir))
os.makedirs(export_dir, exist_ok=True) os.makedirs(export_dir, exist_ok=True)
offset = self.options.border_offset
bbox = inkex.BoundingBox() bbox = inkex.BoundingBox()
for elem in self.svg.selected.values(): for elem in self.svg.selected.values():
@ -73,21 +77,20 @@ class ExportObject(inkex.EffectExtension):
group.attrib['transform'] = str(inkex.Transform(((1, 0, -bbox.left), (0, 1, -bbox.top)))) group.attrib['transform'] = str(inkex.Transform(((1, 0, -bbox.left), (0, 1, -bbox.top))))
for elem in self.svg.selected.values(): for elem in self.svg.selected.values():
elem_copy = deepcopy(elem) elem_copy = deepcopy(elem)
elem_copy.attrib['transform'] = str(elem.composed_transform()) elem_copy.attrib['transform'] = str(elem.composed_transform())
group.append(elem_copy) group.append(elem_copy)
template.attrib['viewBox'] = f'0 0 {bbox.width} {bbox.height}' template.attrib['viewBox'] = f'{-offset} {-offset} {bbox.width + offset * 2} {bbox.height + offset * 2}'
template.attrib['width'] = f'{bbox.width}' + self.svg.unit template.attrib['width'] = f'{bbox.width + offset * 2}' + self.svg.unit
template.attrib['height'] = f'{bbox.height}' + self.svg.unit template.attrib['height'] = f'{bbox.height + offset * 2}' + self.svg.unit
if filename is None: if filename is None:
filename = elem.attrib.get('id', None) filename = elem.attrib.get('id', None)
if filename: if filename:
filename = filename.replace(os.sep, '_') + '.svg' filename = filename.replace(os.sep, '_') + '.svg'
if not filename: if not filename: #should never be the case. Inkscape might crash if the id attribute is empty or not existent due to invalid SVG
filename = 'element.svg' filename = self.svg.get_unique_id("selection") + '.svg'
template.append(group) template.append(group)
@ -112,7 +115,13 @@ class ExportObject(inkex.EffectExtension):
proc = Popen(cmd, shell=False, stdout=PIPE, stderr=PIPE) proc = Popen(cmd, shell=False, stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate() stdout, stderr = proc.communicate()
#inkex.utils.debug("%d %s %s" % (proc.returncode, stdout, stderr)) #inkex.utils.debug("%d %s %s" % (proc.returncode, stdout, stderr))
if self.options.export_pdf is True:
cli_output = inkscape(os.path.join(export_dir, filename), actions='export-pdf-version:1.5;export-text-to-path;export-filename:{file_name};export-do;FileClose'.format(file_name=os.path.join(export_dir, filename + '.pdf')))
if len(cli_output) > 0:
self.msg("Inkscape returned the following output when trying to run the file export; the file export may still have worked:")
self.msg(cli_output)
def create_document(self): def create_document(self):
document = self.svg.copy() document = self.svg.copy()
for child in document.getchildren(): for child in document.getchildren():