This repository has been archived on 2023-03-25. You can view files and clone it, but cannot push or open issues or pull requests.
mightyscape-1.1-deprecated/extensions/fablabchemnitz/lasercut_sequence/lasercut_sequence.py

42 lines
1.4 KiB
Python
Raw Normal View History

2020-07-30 01:16:18 +02:00
#!/usr/bin/env python3
import inkex
import measure
from inkex.paths import Path
from inkex import paths
def getArea(path):
2020-08-30 11:23:15 +02:00
return abs(measure.csparea(paths.CubicSuperPath(path + "z")))
2020-07-30 01:16:18 +02:00
class LaserSort(inkex.Effect):
2020-08-30 11:23:15 +02:00
def __init__(self):
inkex.Effect.__init__(self)
def effect(self):
elements = self.document.xpath('//svg:path',namespaces=inkex.NSS)
for el in elements:
oldpathstring = el.attrib['d']
nodes = Path(oldpathstring).to_arrays()
currentSection = []
sections = [currentSection]
for node in nodes:
command = node.pop(0)
currentSection.append(command + ' ' + ' '.join(list(map(lambda c: ','.join(map(str, c)), node))))
if command.lower() == 'z':
currentSection = []
sections.append(currentSection)
sections = list(map(lambda n: ' '.join(n), filter(lambda n: len(n) > 0, sections)))
2020-07-30 01:16:18 +02:00
2020-08-30 11:23:15 +02:00
if (sections[-1][-2].lower() != 'z'):
nonClosedSection = ' ' + sections.pop()
else:
nonClosedSection = ''
sections = filter(lambda s: s[0].lower() != 'z', sections)
sections = sorted(sections, key=getArea)
newpathstring = "z ".join(sections) + nonClosedSection
el.set('d', newpathstring)
if __name__ == '__main__':
LaserSort().run()