ease up and more stable reload drawing

This commit is contained in:
Mario Voigt 2021-11-29 12:03:36 +01:00
parent d75ec118c2
commit cdc3408a4b

View File

@ -5,28 +5,45 @@ from lxml import etree
class Reload(inkex.EffectExtension): class Reload(inkex.EffectExtension):
'''
This extension gets the current doc path and reads it as doc
Then we clear the recent document from all items except basic stuff like root (svg:svg), defs and namedview
finally we overwrite all attributes from svg:svg, defs and namedview to the recent
'''
def effect(self): def effect(self):
currentDoc = self.document_path() currentDoc = self.document_path()
if currentDoc == "": if currentDoc == "":
self.msg("Your document is not saved as a permanent file yet. Cannot reload.") self.msg("Your document is not saved as a permanent file yet. Cannot reload.")
exit(1) exit(1)
originalRoot = self.document.getroot()
originalNamedview = self.svg.namedview
originalDefs = originalRoot.find("{http://www.w3.org/2000/svg}defs")
originalRoot.clear() #drop all children and attributes from root
stream = open(self.document_path(), 'r') stream = open(self.document_path(), 'r')
p = etree.XMLParser(huge_tree=True) p = etree.XMLParser(huge_tree=True)
doc = etree.parse(stream, parser=etree.XMLParser(huge_tree=True)) doc = etree.parse(stream, parser=etree.XMLParser(huge_tree=True))
stream.close() stream.close()
root = self.document.getroot()
kept = [] #required. if we delete them directly without adding new defs or namedview, inkscape will crash copyRoot = doc.getroot()
for node in self.document.xpath('//*', namespaces=inkex.NSS): copyNamedview = copyRoot.find(inkex.addNS('namedview', 'sodipodi'))
if node.TAG not in ('svg', 'defs', 'namedview'): copyDefs = copyRoot.find("{http://www.w3.org/2000/svg}defs")
node.delete() for child in copyRoot.getchildren():
elif node.TAG in ('defs', 'namedview'): #except 'svg' originalRoot.append(child)
kept.append(node)
children = doc.getroot().getchildren() #update all attributes in originalSVG
for child in children: for copyAttrib in copyRoot.attrib:
root.append(child) originalRoot.attrib[copyAttrib] = copyRoot.attrib[copyAttrib]
for k in kept:
k.delete() #update all attributes in originalNamedview
for copyAttrib in copyNamedview.attrib:
originalNamedview.attrib[copyAttrib] = copyNamedview.attrib[copyAttrib]
#update all attributes in originalDefs
for copyAttrib in copyDefs.attrib:
originalDefs.attrib[copyAttrib] = copyDefs.attrib[copyAttrib]
if __name__ == '__main__': if __name__ == '__main__':
Reload().run() Reload().run()