small generic fixes

This commit is contained in:
Mario Voigt 2021-04-13 20:39:22 +02:00
parent c5dfaf6127
commit 885b91c33d
4 changed files with 48 additions and 10 deletions

View File

@ -121,10 +121,11 @@ class DXFDWGImport(inkex.EffectExtension):
self.arg_parser.add_argument("--XLINE", type=inkex.Boolean, default=True) self.arg_parser.add_argument("--XLINE", type=inkex.Boolean, default=True)
def openExplorer(self, temp_output_dir): def openExplorer(self, temp_output_dir):
DETACHED_PROCESS = 0x00000008
if os.name == 'nt': if os.name == 'nt':
subprocess.Popen(["explorer",temp_output_dir], close_fds=True) subprocess.Popen(["explorer", temp_output_dir], close_fds=True, creationflags=DETACHED_PROCESS).wait()
else: else:
subprocess.Popen(["xdg-open",temp_output_dir], close_fds=True) subprocess.Popen(["xdg-open", temp_output_dir], close_fds=True, start_new_session=True).wait()
def effect(self): def effect(self):
#get input file and copy it to some new temporary directory #get input file and copy it to some new temporary directory

View File

@ -1,9 +1,14 @@
<?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</_name> <_name>Export selection as SVG/DXF</_name>
<id>fablabchemnitz.de.export_selection_as_svg</id> <id>fablabchemnitz.de.export_selection_as_svg_dxf</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="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="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="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 SVG file.</menu-tip>

View File

@ -1,12 +1,17 @@
# -*- coding: utf-8 -*- #!/usr/bin/env python3
from copy import deepcopy from copy import deepcopy
from pathlib import Path from pathlib import Path
import logging import logging
import math import math
import os import os
import subprocess
from subprocess import Popen, PIPE
import inkex import inkex
import inkex.command import inkex.command
from inkex.command import inkscape, inkscape_command
from lxml import etree from lxml import etree
from scour.scour import scourString from scour.scour import scourString
@ -21,15 +26,29 @@ 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("--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("--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("--newwindow", type=inkex.Boolean, default=False, help="Open file in new Inkscape window")
def openExplorer(self, dir):
DETACHED_PROCESS = 0x00000008
if os.name == 'nt':
Popen(["explorer", dir], close_fds=True, creationflags=DETACHED_PROCESS).wait()
else:
Popen(["xdg-open", dir], close_fds=True, start_new_session=True).wait()
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!")
return return
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)
bbox = inkex.BoundingBox() bbox = inkex.BoundingBox()
for elem in self.svg.selected.values(): for elem in self.svg.selected.values():
transform = inkex.Transform() transform = inkex.Transform()
parent = elem.getparent() parent = elem.getparent()
@ -61,8 +80,8 @@ class ExportObject(inkex.EffectExtension):
width = math.ceil(bbox.width) width = math.ceil(bbox.width)
height = math.ceil(bbox.height) height = math.ceil(bbox.height)
template.attrib['viewBox'] = f'0 0 {width} {height}' template.attrib['viewBox'] = f'0 0 {width} {height}'
template.attrib['width'] = f'{width}' template.attrib['width'] = f'{width}' + self.svg.unit
template.attrib['height'] = f'{height}' template.attrib['height'] = f'{height}' + self.svg.unit
if filename is None: if filename is None:
filename = elem.attrib.get('id', None) filename = elem.attrib.get('id', None)
@ -74,7 +93,7 @@ class ExportObject(inkex.EffectExtension):
template.append(group) template.append(group)
if not self.options.wrap_transform: if not self.options.wrap_transform:
self.load(inkex.command.inkscape_command(template.tostring(), select=GROUP_ID, verbs=['SelectionUnGroup'])) self.load(inkscape_command(template.tostring(), select=GROUP_ID, verbs=['SelectionUnGroup']))
template = self.svg template = self.svg
for child in template.getchildren(): for child in template.getchildren():
if child.tag == '{http://www.w3.org/2000/svg}metadata': if child.tag == '{http://www.w3.org/2000/svg}metadata':
@ -82,6 +101,19 @@ class ExportObject(inkex.EffectExtension):
self.save_document(template, export_dir / filename) self.save_document(template, export_dir / filename)
if self.options.opendir is True:
self.openExplorer(export_dir)
if self.options.newwindow is True:
inkscape(os.path.join(export_dir, filename))
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
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)]
proc = Popen(cmd, shell=False, stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
#inkex.utils.debug("%d %s %s" % (proc.returncode, stdout, stderr))
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

@ -454,7 +454,7 @@ else: #check if we have access to "wine"
"Please open that with CutStudio manually. \n\n" + \ "Please open that with CutStudio manually. \n\n" + \
"Tip: On Linux, you can use 'wine' to install CutStudio 3.10. Then, the file will be directly opened with CutStudio. \n" + \ "Tip: On Linux, you can use 'wine' to install CutStudio 3.10. Then, the file will be directly opened with CutStudio. \n" + \
" Diagnostic information: \n" + str(exc)) " Diagnostic information: \n" + str(exc))
#os.popen("/usr/bin/xdg-open " + filename) #os.popen("/usr/bin/xdg-open " + filename) #os.popen is deprecated. Use subprocess.Popen
#Popen(["inkscape", filename+".filtered.svg"], stderr=DEVNULL) #Popen(["inkscape", filename+".filtered.svg"], stderr=DEVNULL)
#Popen(["inkscape", filename+".cutstudio.eps"]) #Popen(["inkscape", filename+".cutstudio.eps"])
#os.unlink(filename+".filtered.svg") #os.unlink(filename+".filtered.svg")