small enhancements in export selection

This commit is contained in:
Mario Voigt 2021-05-27 13:38:38 +02:00
parent 5194ee3181
commit 487a9b4382
3 changed files with 56 additions and 27 deletions

View File

@ -2,16 +2,49 @@
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension"> <inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>Export Selection As ...</name> <name>Export Selection As ...</name>
<id>fablabchemnitz.de.export_selection_as</id> <id>fablabchemnitz.de.export_selection_as</id>
<param name="wrap_transform" type="bool" gui-text="Wrap final document in transform">false</param> <param name="tab" type="notebook">
<param name="border_offset" type="float" min="0.000" max="9999.000" precision="3" gui-text="Add border offset around selection">1.000</param> <page name="tab_settings" gui-text="Settings">
<param name="export_dir" type="path" mode="folder" gui-text="Location to save exported documents">./inkscape_export/</param> <param name="wrap_transform" type="bool" gui-text="Wrap final document in transform">false</param>
<param name="opendir" type="bool" gui-text="Open containing output directory after export">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="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_dir" type="path" mode="folder" gui-text="Location to save exported documents">./inkscape_export/</param>
<param name="export_svg" type="bool" gui-text="Export as SVG">true</param> <param name="opendir" type="bool" gui-text="Open containing output directory after export">false</param>
<param name="export_dxf" type="bool" gui-text="Export as DXF R14 file (mm units)">false</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_pdf" type="bool" gui-text="Export as PDF 1.5">false</param> <param name="export_svg" type="bool" gui-text="Export as SVG">true</param>
<param name="newwindow" type="bool" gui-text="Open file in new Inkscape instance">false</param> <param name="export_dxf" type="bool" gui-text="Export as DXF R14 file (mm units)">false</param>
<label>Note: If svg/dxf/pdf already existed before, they might get deleted or overwritten unwanted. Please take care!</label> <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>
<label>Note: If svg/dxf/pdf already existed before, they might get accidently deleted or overwritten. Please take care!</label>
</page>
<page name="tab_about" gui-text="About">
<label appearance="header">Export Selection As ...</label>
<label>Extension to export the current selection into different formats like SVG, DXF or PDF.</label>
<label>2021 / written by Mario Voigt (Stadtfabrikanten e.V. / FabLab Chemnitz)</label>
<spacer/>
<label appearance="header">Online Documentation</label>
<label appearance="url">https://y.stadtfabrikanten.org/exportselectionas</label>
<spacer/>
<label appearance="header">Contributing</label>
<label appearance="url">https://gitea.fablabchemnitz.de/MarioVoigt/mightyscape-1.X</label>
<label appearance="url">mailto:mario.voigt@stadtfabrikanten.org</label>
<spacer/>
<label appearance="header">Based on</label>
<label appearance="url">https://github.com/mireq/inkscape-export-selection-as-svg</label>
<spacer/>
<label appearance="header">MightyScape Extension Collection</label>
<label>This piece of software is part of the MightyScape for Inkscape Extension Collection and is licensed under GNU GPL v3</label>
<label appearance="url">https://y.stadtfabrikanten.org/mightyscape-overview</label>
</page>
<page name="tab_donate" gui-text="Donate">
<label appearance="header">Coffee + Pizza</label>
<label>We are the Stadtfabrikanten, running the FabLab Chemnitz since 2016. A FabLab is an open workshop that gives people access to machines and digital tools like 3D printers, laser cutters and CNC milling machines.</label>
<spacer/>
<label>You like our work and want to support us? You can donate to our non-profit organization by different ways:</label>
<label appearance="url">https://y.stadtfabrikanten.org/donate</label>
<spacer/>
<label>Thanks for using our extension and helping us!</label>
<image>../000_about_fablabchemnitz.svg</image>
</page>
</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>
<effects-menu> <effects-menu>

View File

@ -12,14 +12,11 @@ import warnings
import inkex import inkex
import inkex.command import inkex.command
from inkex.command import inkscape, inkscape_command from inkex.command import inkscape, inkscape_command
import tempfile
from lxml import etree from lxml import etree
from scour.scour import scourString from scour.scour import scourString
'''
ToDo: work with temporary SVG file in temp dir instead handling deletion stuff at the end / valide that the file exists when spawning new instance
'''
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
DETACHED_PROCESS = 0x00000008 DETACHED_PROCESS = 0x00000008
@ -28,6 +25,7 @@ GROUP_ID = 'export_selection_transform'
class ExportObject(inkex.EffectExtension): class ExportObject(inkex.EffectExtension):
def add_arguments(self, pars): def add_arguments(self, pars):
pars.add_argument("--tab")
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("--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")
@ -45,6 +43,9 @@ class ExportObject(inkex.EffectExtension):
Popen(["xdg-open", dir], close_fds=True, start_new_session=True).wait() Popen(["xdg-open", dir], close_fds=True, start_new_session=True).wait()
def spawnIndependentInkscape(self, file): #function to spawn non-blocking inkscape instance. the inkscape command is available because it is added to ENVIRONMENT when Inkscape main instance is started def spawnIndependentInkscape(self, file): #function to spawn non-blocking inkscape instance. the inkscape command is available because it is added to ENVIRONMENT when Inkscape main instance is started
if not os.path.exists(file):
inkex.utils.debug("Error. {} does not exist!".format(file))
exit(1)
warnings.simplefilter('ignore', ResourceWarning) #suppress "enable tracemalloc to get the object allocation traceback" warnings.simplefilter('ignore', ResourceWarning) #suppress "enable tracemalloc to get the object allocation traceback"
if os.name == 'nt': if os.name == 'nt':
Popen(["inkscape", file], close_fds=True, creationflags=DETACHED_PROCESS) Popen(["inkscape", file], close_fds=True, creationflags=DETACHED_PROCESS)
@ -62,9 +63,7 @@ class ExportObject(inkex.EffectExtension):
self.options.newwindow is False: self.options.newwindow is False:
inkex.utils.debug("You must select at least one option to continue!") inkex.utils.debug("You must select at least one option to continue!")
return 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
@ -130,6 +129,8 @@ 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, os.path.join(tempfile.gettempdir(), svg_filename)) # save one into temp dir to access for dxf/pdf/new window instance
if self.options.export_svg is True: if self.options.export_svg is True:
self.save_document(template, export_dir / svg_filename) self.save_document(template, export_dir / svg_filename)
@ -138,7 +139,7 @@ class ExportObject(inkex.EffectExtension):
if self.options.newwindow is True: if self.options.newwindow is True:
#inkscape(os.path.join(export_dir, svg_filename)) #blocking cmd #inkscape(os.path.join(export_dir, svg_filename)) #blocking cmd
self.spawnIndependentInkscape(os.path.join(export_dir, svg_filename)) #non-blocking self.spawnIndependentInkscape(os.path.join(tempfile.gettempdir(), 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
@ -147,7 +148,7 @@ class ExportObject(inkex.EffectExtension):
self.options.dxf_exporter_path, self.options.dxf_exporter_path,
'--output=' + os.path.join(export_dir, filename_base + '.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, svg_filename) os.path.join(tempfile.gettempdir(), 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()
@ -155,14 +156,11 @@ 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, 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'))) cli_output = inkscape(os.path.join(tempfile.gettempdir(), 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 and self.options.newwindow is False: #we need the SVG file in case we open in new window because we spawn a new instance
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():

View File

@ -170,13 +170,11 @@ class InputSTL(inkex.EffectExtension):
'--rotate', str(args.rz), '--rotate', str(args.rz),
'--first-layer-height', '0.1mm', '--first-layer-height', '0.1mm',
'--export-svg', '-o', svgfile, args.inputfile] '--export-svg', '-o', svgfile, args.inputfile]
def scale_points(pts, scale): def scale_points(pts, scale):
""" str='276.422496,309.4 260.209984,309.4 260.209984,209.03 276.422496,209.03' """ """ str='276.422496,309.4 260.209984,309.4 260.209984,209.03 276.422496,209.03' """
return re.sub('\d*\.\d*', lambda x: str(float(x.group(0))*scale), pts) return re.sub('\d*\.\d*', lambda x: str(float(x.group(0))*scale), pts)
## CAUTION: keep svg_pathstats() in sync with inkscape-centerlinetrace ## CAUTION: keep svg_pathstats() in sync with inkscape-centerlinetrace
def svg_pathstats(path_d): def svg_pathstats(path_d):
""" calculate statistics from an svg path: """ calculate statistics from an svg path: