fix dxf export and non-blocking spawn export selection extension

This commit is contained in:
leyghisbb 2021-05-07 22:24:50 +02:00
parent 782afcf3f6
commit e1e91ef731

View File

@ -5,9 +5,10 @@ from pathlib import Path
import logging import logging
import math import math
import os import os
import sys
import subprocess import subprocess
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
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
@ -18,7 +19,7 @@ from scour.scour import scourString
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
DETACHED_PROCESS = 0x00000008
GROUP_ID = 'export_selection_transform' GROUP_ID = 'export_selection_transform'
@ -36,17 +37,32 @@ class ExportObject(inkex.EffectExtension):
def openExplorer(self, dir): def openExplorer(self, dir):
DETACHED_PROCESS = 0x00000008
if os.name == 'nt': if os.name == 'nt':
Popen(["explorer", dir], close_fds=True, creationflags=DETACHED_PROCESS).wait() Popen(["explorer", dir], close_fds=True, creationflags=DETACHED_PROCESS).wait()
else: else:
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
if os.name == 'nt':
warnings.simplefilter('ignore', ResourceWarning) #suppress "enable tracemalloc to get the object allocation traceback"
Popen(["inkscape", file], close_fds=True, creationflags=DETACHED_PROCESS)
warnings.simplefilter("default", ResourceWarning)
else:
pid = os.fork()
if pid == 0: # new process
os.system("nohup inkscape " + file + " &")
exit()
def effect(self): def effect(self):
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
#preflight check for DXF input dir
if not os.path.exists(self.options.dxf_exporter_path):
inkex.utils.debug("Location of dxf_outlines.py does not exist. Please select a proper file and try again.")
exit(1)
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)
@ -108,14 +124,22 @@ class ExportObject(inkex.EffectExtension):
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)) #inkscape(os.path.join(export_dir, filename)) #blocking
self.spawnIndependentInkscape(os.path.join(export_dir, filename)) #non-blocking
if self.options.export_dxf is True: if self.options.export_dxf is True:
#ensure that python3 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 = ['python3', self.options.dxf_exporter_path, '--output=' + os.path.join(export_dir, filename + '.dxf'), r'--units=25.4/96', os.path.join(export_dir, filename)] cmd = [
sys.executable, #the path of the python interpreter which is used for this script
self.options.dxf_exporter_path,
'--output=' + os.path.join(export_dir, filename + '.dxf'),
r'--units=25.4/96',
os.path.join(export_dir, 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()
#inkex.utils.debug("%d %s %s" % (proc.returncode, stdout, stderr)) if proc.returncode != 0:
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, 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')))