|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Cookie Cutter Sweeper Inkscape Extension |
| 4 | +# |
| 5 | +# Copyright (c) 2015 Christian Walther |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in |
| 15 | +# all copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +# THE SOFTWARE. |
| 24 | + |
| 25 | +import sys |
| 26 | +import os |
| 27 | +import subprocess |
| 28 | +import tempfile |
| 29 | +import shutil |
| 30 | +import platform |
| 31 | +import optparse |
| 32 | + |
| 33 | +if __name__ == '__main__': |
| 34 | + if sys.platform.startswith('darwin'): |
| 35 | + bindir = 'mac' |
| 36 | + elif sys.platform.startswith('win32'): |
| 37 | + bindir = 'windows' |
| 38 | + elif sys.platform.startswith('linux') and platform.machine() == 'x86_64': |
| 39 | + bindir = 'linux-x86_64' |
| 40 | + elif sys.platform.startswith('linux') and platform.machine() == 'i686': |
| 41 | + bindir = 'linux-i386' |
| 42 | + else: |
| 43 | + sys.stderr.write('Your platform "' + sys.platform + ' ' + platform.machine() + '" is not currently supported by the Cookie Cutter Sweeper integration. Try building Cookie Cutter Sweeper from source (https://github.com/cwalther/cookie-cutter-sweeper) and adjusting the Inkscape extension in ' + os.path.join(os.getcwd(), __file__) + '.') |
| 44 | + sys.exit(1) |
| 45 | + |
| 46 | + optionparser = optparse.OptionParser(usage='usage: %prog [options] SVGfile') |
| 47 | + optionparser.add_option('--id', action='append', type='string', dest='ids', default=[], help='id attribute of selected object, ignored') |
| 48 | + optionparser.add_option('-o', '--outputfile', action='store', type='string', dest='outputfile', default='~/cookie.stl', help='STL output file') |
| 49 | + options, arguments = optionparser.parse_args() |
| 50 | + |
| 51 | + tempdir = tempfile.mkdtemp() |
| 52 | + process = subprocess.Popen(['inkscape', '--export-png=' + os.path.join(tempdir, 'cookie.png'), '--export-area-drawing', '--export-dpi=254', '--export-background=#000000', '--export-background-opacity=0', arguments[-1]], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 53 | + output = process.communicate()[0] |
| 54 | + if process.returncode != 0: |
| 55 | + sys.stderr.write('Calling inkscape failed:\n') |
| 56 | + sys.stderr.write(output) |
| 57 | + shutil.rmtree(tempdir, ignore_errors=True) |
| 58 | + sys.exit(process.returncode) |
| 59 | + |
| 60 | + process = subprocess.Popen([os.path.join(os.getcwd(), bindir, 'sweep'), '--flip-x', os.path.join(os.getcwd(), 'section.png'), os.path.join(tempdir, 'cookie.png'), os.path.expanduser(options.outputfile)], stderr=subprocess.PIPE) |
| 61 | + output = process.communicate()[1] |
| 62 | + if process.returncode != 0: |
| 63 | + sys.stderr.write('Calling sweep failed:\n') |
| 64 | + sys.stderr.write(output) |
| 65 | + shutil.rmtree(tempdir, ignore_errors=True) |
| 66 | + sys.exit(process.returncode) |
| 67 | + |
| 68 | + shutil.rmtree(tempdir, ignore_errors=True) |
0 commit comments