different large refactorings (subdirectores, removed obsolete stuff) and
bug fixes
This commit is contained in:
57
extensions/fablabchemnitz/inkcut/fablabchemnitz_inkcut.py
Normal file
57
extensions/fablabchemnitz/inkcut/fablabchemnitz_inkcut.py
Normal file
@ -0,0 +1,57 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Inkcut, Plot HPGL directly from Inkscape.
|
||||
inkcut.py
|
||||
|
||||
Copyright 2018 The Inkcut Team
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
MA 02110-1301, USA.
|
||||
"""
|
||||
import os
|
||||
import inkex
|
||||
from lxml import etree
|
||||
from subprocess import Popen, PIPE
|
||||
from shutil import copy2
|
||||
from distutils.spawn import find_executable
|
||||
|
||||
def contains_text(nodes):
|
||||
for node in nodes:
|
||||
tag = node.tag[node.tag.rfind("}")+1:]
|
||||
if tag == 'text':
|
||||
return True
|
||||
return False
|
||||
|
||||
def convert_objects_to_paths(file, document):
|
||||
tempfile = os.path.splitext(file)[0] + "-prepare.svg"
|
||||
# tempfile is needed here only because we want to force the extension to be .svg
|
||||
# so that we can open and close it silently
|
||||
copy2(file, tempfile)
|
||||
|
||||
command = "inkscape " + tempfile + ' --actions="EditSelectAllInAllLayers;EditUnlinkClone;ObjectToPath;FileSave;FileQuit"'
|
||||
|
||||
if find_executable('xvfb-run'):
|
||||
command = 'xvfb-run -a ' + command
|
||||
|
||||
p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
|
||||
(out, err) = p.communicate()
|
||||
|
||||
if p.returncode != 0:
|
||||
inkex.errormsg("Failed to convert objects to paths. Continued without converting.")
|
||||
inkex.errormsg(out)
|
||||
inkex.errormsg(err)
|
||||
return document.getroot()
|
||||
else:
|
||||
return etree.parse(tempfile).getroot()
|
@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
|
||||
<name>Inkcut - Cut selection</name>
|
||||
<id>fablabchemnitz.de.inkcut_cut</id>
|
||||
<effect>
|
||||
<object-type>all</object-type>
|
||||
<effects-menu>
|
||||
<submenu name="FabLab Chemnitz">
|
||||
<submenu name="Import/Export/Transfer"/>
|
||||
</submenu>
|
||||
</effects-menu>
|
||||
</effect>
|
||||
<script>
|
||||
<command location="inx" interpreter="python">fablabchemnitz_inkcut_cut.py</command>
|
||||
</script>
|
||||
</inkscape-extension>
|
@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Inkcut, Plot HPGL directly from Inkscape.
|
||||
extension.py
|
||||
|
||||
Copyright 2010-2018 The Inkcut Team
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
MA 02110-1301, USA.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
|
||||
import inkex
|
||||
from lxml import etree
|
||||
import subprocess
|
||||
from fablabchemnitz_inkcut import contains_text, convert_objects_to_paths
|
||||
|
||||
DEBUG = False
|
||||
|
||||
try:
|
||||
from subprocess import DEVNULL # py3k
|
||||
except ImportError:
|
||||
import os
|
||||
DEVNULL = open(os.devnull, 'wb')
|
||||
|
||||
class InkscapeInkcutPlugin(inkex.Effect):
|
||||
def effect(self):
|
||||
""" Like cut but requires no selection and does no validation for
|
||||
text nodes.
|
||||
"""
|
||||
|
||||
nodes = self.svg.selected
|
||||
if not len(nodes):
|
||||
inkex.errormsg("There were no paths were selected.")
|
||||
return
|
||||
|
||||
document = self.document
|
||||
if contains_text(self.svg.selected.values()):
|
||||
document = convert_objects_to_paths(self.args[-1], self.document)
|
||||
|
||||
#: If running from source
|
||||
if DEBUG:
|
||||
python = '~/inkcut/venv/bin/python'
|
||||
inkcut = '~/inkcut/main.py'
|
||||
cmd = [python, inkcut]
|
||||
else:
|
||||
cmd = ['inkcut']
|
||||
|
||||
cmd += ['open', '-',
|
||||
'--nodes']+[str(k) for k in nodes.keys()]
|
||||
p = subprocess.Popen(cmd,
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=DEVNULL,
|
||||
stderr=subprocess.STDOUT,
|
||||
close_fds=sys.platform != "win32")
|
||||
p.stdin.write(etree.tostring(document))
|
||||
p.stdin.close()
|
||||
|
||||
# Set the returncode to avoid this warning when popen is garbage collected:
|
||||
# "ResourceWarning: subprocess XXX is still running".
|
||||
# See https://bugs.python.org/issue38890 and
|
||||
# https://bugs.python.org/issue26741.
|
||||
p.returncode = 0
|
||||
|
||||
if __name__ == '__main__':
|
||||
InkscapeInkcutPlugin().run()
|
@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
|
||||
<name>Inkcut - Open current document</name>
|
||||
<id>fablabchemnitz.de.inkcut_open</id>
|
||||
<effect>
|
||||
<object-type>all</object-type>
|
||||
<effects-menu>
|
||||
<submenu name="FabLab Chemnitz">
|
||||
<submenu name="Import/Export/Transfer"/>
|
||||
</submenu>
|
||||
</effects-menu>
|
||||
</effect>
|
||||
<script>
|
||||
<command location="inx" interpreter="python">fablabchemnitz_inkcut_open.py</command>
|
||||
</script>
|
||||
</inkscape-extension>
|
@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
Inkcut, Plot HPGL directly from Inkscape.
|
||||
extension.py
|
||||
|
||||
Copyright 2010-2018 The Inkcut Team
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
MA 02110-1301, USA.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import inkex
|
||||
import importlib
|
||||
import subprocess
|
||||
from lxml import etree
|
||||
from fablabchemnitz_inkcut import convert_objects_to_paths
|
||||
|
||||
DEBUG = False
|
||||
try:
|
||||
from subprocess import DEVNULL # py3k
|
||||
except ImportError:
|
||||
import os
|
||||
DEVNULL = open(os.devnull, 'wb')
|
||||
|
||||
|
||||
class InkscapeInkcutPlugin(inkex.Effect):
|
||||
def effect(self):
|
||||
""" Like cut but requires no selection and does no validation for
|
||||
text nodes.
|
||||
"""
|
||||
#: If running from source
|
||||
if DEBUG:
|
||||
python = '~/inkcut/venv/bin/python'
|
||||
inkcut = '~/inkcut/main.py'
|
||||
cmd = [python, inkcut]
|
||||
else:
|
||||
cmd = ['inkcut']
|
||||
|
||||
document = convert_objects_to_paths(self.options.input_file, self.document)
|
||||
|
||||
cmd += ['open', '-']
|
||||
p = subprocess.Popen(cmd,
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=DEVNULL,
|
||||
stderr=subprocess.STDOUT,
|
||||
close_fds=sys.platform != "win32")
|
||||
p.stdin.write(etree.tostring(document))
|
||||
p.stdin.close()
|
||||
# Set the returncode to avoid this warning when popen is garbage collected:
|
||||
# "ResourceWarning: subprocess XXX is still running".
|
||||
# See https://bugs.python.org/issue38890 and
|
||||
# https://bugs.python.org/issue26741.
|
||||
p.returncode = 0
|
||||
|
||||
if __name__ == '__main__':
|
||||
InkscapeInkcutPlugin().run()
|
Reference in New Issue
Block a user