Last active
December 16, 2015 19:59
-
-
Save larscwallin/5489140 to your computer and use it in GitHub Desktop.
larscwallin.inx.extractelements version 0.1 extracts all selected SVG Elements to file and/or displayed as a string in the Inkscape GUI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <inkscape-extension> | |
| <_name>Extract Elements</_name> | |
| <id>com.larscwallin.inx.extractelements</id> | |
| <dependency type="executable" location="extensions">larscwallin.inx.extractelements.py</dependency> | |
| <dependency type="executable" location="extensions">inkex.py</dependency> | |
| <param name="where" type="string" _gui-text="Output path"></param> | |
| <param name="encode" type="boolean" _gui-text="Do you wish to Base64 encode the SVG?">false</param> | |
| <param name="viewresult" type="boolean" _gui-text="Do you wish to view the resulting?">true</param> | |
| <effect> | |
| <object-type>all</object-type> | |
| <effects-menu> | |
| <submenu _name="Export"/> | |
| </effects-menu> | |
| </effect> | |
| <script> | |
| <command reldir="extensions" interpreter="python">larscwallin.inx.extractelements.py</command> | |
| </script> | |
| </inkscape-extension> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| #inkex.errormsg(_("This will be written to Python stderr")) | |
| # These two lines are only needed if you don't put the script directly into | |
| # the installation directory | |
| import sys | |
| import base64 | |
| import string | |
| import webbrowser | |
| import threading | |
| import os.path | |
| sys.path.append('/usr/share/inkscape/extensions') | |
| import SvgDocument | |
| import inkex | |
| from simplestyle import * | |
| # The simplestyle module provides functions for style parsing. | |
| class ElementsToSVG(inkex.Effect): | |
| exportTemplate = """<?xml version="1.0" standalone="no"?> | |
| <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" | |
| "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> | |
| <svg xmlns="http://www.w3.org/2000/svg" version="1.1"> | |
| {{placeholder}} | |
| </svg>""" | |
| def __init__(self): | |
| """ | |
| Constructor. | |
| Defines the "--what" option of a script. | |
| """ | |
| # Call the base class constructor. | |
| inkex.Effect.__init__(self) | |
| self.OptionParser.add_option('-w', '--where', action = 'store', | |
| type = 'string', dest = 'where', default = '', | |
| help = '') | |
| self.OptionParser.add_option('--encode', action = 'store', | |
| type = 'inkbool', dest = 'encode', default = False, | |
| help = 'Do you want to Base64 encode the result?') | |
| self.OptionParser.add_option('--viewresult', action = 'store', | |
| type = 'inkbool', dest = 'viewresult', default = False, | |
| help = 'Do you want to view the resulting document?') | |
| def effect(self): | |
| """ | |
| Effect behaviour. | |
| """ | |
| # Get script's "--what" option value. | |
| self.where = self.options.where | |
| self.base64Encode = self.options.encode | |
| self.viewResult = self.options.encode | |
| self.getselected() | |
| if(self.selected.__len__() > 0): | |
| selected = [] | |
| # Iterate through all selected elements | |
| for element in self.selected.values(): | |
| currentNodeLabel = str(element.get(inkex.addNS('label', 'inkscape'),'')) | |
| currentNodeId = element.get('id') | |
| currentNodeSource = inkex.etree.tostring(element) | |
| if(currentNodeSource!=''): | |
| # Wrap the node in an SVG doc | |
| tplResult = string.replace(self.exportTemplate,'{{placeholder}}',currentNodeSource) | |
| # If the result of the operation is valid, add the SVG source to the selected array | |
| if(tplResult): | |
| selected.append([currentNodeId,currentNodeLabel,tplResult]) | |
| for node in selected: | |
| #'data:image/svg+xml;base64,'+ | |
| content = node[2] | |
| if(content!=''): | |
| if(self.base64Encode): | |
| content = (base64.b64encode(content)) | |
| if(self.where!=''): | |
| # The easiest way to name rendered elements is by using their id since we can trust that this is always unique. | |
| filename = os.path.join(self.where, (node[1]+node[0]+'.svg')) | |
| success = self.saveToFile(content,filename) | |
| if(success and self.viewResult): | |
| self.viewOutput(filename) | |
| else: | |
| inkex.debug('Unable to write to file "' + filename + '"') | |
| else: | |
| if(self.viewResult): | |
| if(self.base64Encode): | |
| inkex.debug('data:image/svg+xml;base64,'+content) | |
| #self.viewOutput('data:image/svg+xml;base64,'+content) | |
| else: | |
| inkex.debug(content) | |
| #self.viewOutput('data:image/svg+xml,'+content) | |
| else: | |
| if(self.base64Encode): | |
| inkex.debug('data:image/svg+xml;base64,'+content) | |
| else: | |
| inkex.debug(content) | |
| else: | |
| inkex.debug('No SVG source available for element ' + node[0]) | |
| def saveToFile(self,content,filename): | |
| FILE = open(filename,'w') | |
| if(FILE): | |
| FILE.write(content) | |
| FILE.close() | |
| return True | |
| else: | |
| return False | |
| def viewOutput(self,url): | |
| runner = BrowserRunner() | |
| runner.url = url | |
| runner.start() | |
| class BrowserRunner(threading.Thread): | |
| url = '' | |
| def __init__(self): | |
| threading.Thread.__init__ (self) | |
| def run(self): | |
| webbrowser.open('file://' + self.url) | |
| # Create effect instance and apply it. | |
| effect = ElementsToSVG() | |
| effect.affect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment