some ehnancements in 'export selection as'

This commit is contained in:
Mario Voigt 2021-05-20 20:36:17 +02:00
parent 2d0448e913
commit 2f75f50105
2 changed files with 34 additions and 14 deletions

View File

@ -6,10 +6,12 @@
<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="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" gui-description="Do not use dxf12_outlines.py! This will try to create R12 DXF files, which will fail!">/usr/share/inkscape/extensions/dxf_outlines.py</param>
<param name="export_svg" type="bool" gui-text="Export as SVG">true</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="export_pdf" type="bool" gui-text="Export as PDF 1.5">false</param>
<param name="newwindow" type="bool" gui-text="Open file in new Inkscape instance">false</param> <param name="newwindow" type="bool" gui-text="Open file in new Inkscape instance">false</param>
<label>Note: If svg/dxf/pdf already existed before, they might get deleted or overwritten unwanted. Please take care!</label>
<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>
<effects-menu> <effects-menu>

View File

@ -30,6 +30,7 @@ class ExportObject(inkex.EffectExtension):
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_svg", type=inkex.Boolean, default=False, help="Create a svg file")
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("--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")
@ -50,6 +51,18 @@ class ExportObject(inkex.EffectExtension):
warnings.simplefilter("default", ResourceWarning) warnings.simplefilter("default", ResourceWarning)
def effect(self): def effect(self):
svg_export = self.options.export_svg
if self.options.export_svg is False and \
self.options.export_dxf is False and \
self.options.export_pdf is False and \
self.options.newwindow is False:
inkex.utils.debug("You must select at least one option to continue!")
return
else:
self.options.export_svg = True #required for all other options!
if not self.svg.selected: if not self.svg.selected:
inkex.errormsg("Selection is empty. Please select some objects first!") inkex.errormsg("Selection is empty. Please select some objects first!")
return return
@ -82,7 +95,7 @@ class ExportObject(inkex.EffectExtension):
bbox += inkex.BoundingBox((x1, x2), (y1, y2)) bbox += inkex.BoundingBox((x1, x2), (y1, y2))
template = self.create_document() template = self.create_document()
filename = None svg_filename = None
group = etree.SubElement(template, '{http://www.w3.org/2000/svg}g') group = etree.SubElement(template, '{http://www.w3.org/2000/svg}g')
group.attrib['id'] = GROUP_ID group.attrib['id'] = GROUP_ID
@ -98,12 +111,13 @@ class ExportObject(inkex.EffectExtension):
template.attrib['width'] = f'{bbox.width + offset * 2}' + self.svg.unit template.attrib['width'] = f'{bbox.width + offset * 2}' + self.svg.unit
template.attrib['height'] = f'{bbox.height + offset * 2}' + self.svg.unit template.attrib['height'] = f'{bbox.height + offset * 2}' + self.svg.unit
if filename is None: if svg_filename is None:
filename = elem.attrib.get('id', None) filename_base = elem.attrib.get('id', None).replace(os.sep, '_')
if filename: if filename_base:
filename = filename.replace(os.sep, '_') + '.svg' svg_filename = filename_base + '.svg'
if not filename: #should never be the case. Inkscape might crash if the id attribute is empty or not existent due to invalid SVG if not filename_base: #should never be the case. Inkscape might crash if the id attribute is empty or not existent due to invalid SVG
filename = self.svg.get_unique_id("selection") + '.svg' filename_base = self.svg.get_unique_id("selection")
svg_filename = filename_base + '.svg'
template.append(group) template.append(group)
@ -114,23 +128,24 @@ class ExportObject(inkex.EffectExtension):
if child.tag == '{http://www.w3.org/2000/svg}metadata': if child.tag == '{http://www.w3.org/2000/svg}metadata':
template.remove(child) template.remove(child)
self.save_document(template, export_dir / filename) if self.options.export_svg is True:
self.save_document(template, export_dir / svg_filename)
if self.options.opendir is True: if self.options.opendir is True:
self.openExplorer(export_dir) self.openExplorer(export_dir)
if self.options.newwindow is True: if self.options.newwindow is True:
#inkscape(os.path.join(export_dir, filename)) #blocking #inkscape(os.path.join(export_dir, svg_filename)) #blocking cmd
self.spawnIndependentInkscape(os.path.join(export_dir, filename)) #non-blocking self.spawnIndependentInkscape(os.path.join(export_dir, svg_filename)) #non-blocking
if self.options.export_dxf is True: if self.options.export_dxf is True:
#ensure that python command is available #we pass 25.4/96 which stands for unit mm. See inkex.units.UNITS and dxf_outlines.inx #ensure that python command is available #we pass 25.4/96 which stands for unit mm. See inkex.units.UNITS and dxf_outlines.inx
cmd = [ cmd = [
sys.executable, #the path of the python interpreter which is used for this script sys.executable, #the path of the python interpreter which is used for this script
self.options.dxf_exporter_path, self.options.dxf_exporter_path,
'--output=' + os.path.join(export_dir, filename + '.dxf'), '--output=' + os.path.join(export_dir, filename_base + '.dxf'),
r'--units=25.4/96', r'--units=25.4/96',
os.path.join(export_dir, filename) os.path.join(export_dir, svg_filename)
] ]
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()
@ -138,11 +153,14 @@ class ExportObject(inkex.EffectExtension):
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: 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'))) cli_output = inkscape(os.path.join(export_dir, svg_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_base + '.pdf')))
if len(cli_output) > 0: 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("Inkscape returned the following output when trying to run the file export; the file export may still have worked:")
self.msg(cli_output) self.msg(cli_output)
if svg_export is False:
os.remove(os.path.join(export_dir, svg_filename)) #remove SVG if not enabled to export. Might delete existing SVG accidently!
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():