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-0.92-deprecated/extensions/fablabchemnitz_purge_short_lines.py

74 lines
2.4 KiB
Python
Raw Normal View History

2019-11-14 20:05:10 +01:00
#!/usr/bin/env python
import sys
sys.path.append('/usr/share/inkscape/extensions')
# Purge short lines
# This script is designed to be used to clean up/simplify 2D vector exports from
# SketchUp. It ignores everything but paths between exactly 2 points.
import inkex
import math
from simplestyle import *
debug = False
class EnerothPurgeShortEdges(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.OptionParser.add_option('-w', '--length', action = 'store',
type = 'float', dest = 'length', default = 10.0)
def effect(self):
length = self.options.length
svg = self.document.getroot()
if len(self.selected)==0:
self.iterate_node(self.document.getroot())
else:
for id, node in self.selected.iteritems():
self.iterate_node(node)
def iterate_node(self, node):
self.do_node(node)
for child in node:
self.iterate_node(child)
def do_node(self, node):
if node.attrib.has_key('d'):
points = []
instruction = None
prev_coords = [0,0]
words = node.get('d').split(' ')
for i, word in enumerate(words):
if len(word) == 1:
instruction = word
# inkex.debug(word)
else:
# Sometimes there is the case that "coords" returns only an array like [-1.29] (only one coordinate) instead of XY coordinates. Reason is the type "map"
coords = map(lambda c: float(c), word.split(','))
# inkex.debug(coords)
if instruction.lower() == instruction:
# inkex.debug("coords len=" + str(len(coords)) + "prev_coords len=" + str(len(prev_coords)))
coords[0] += prev_coords[0]
coords[1] += prev_coords[1]
# if len(coords) == 2:
# coords[1] += prev_coords[1]
prev_coords = coords
# Assume all coordinates are points of straight lines (instructor being M, m, L or l)
# inkex.debug("X=" + str(coords[0]) + "Y=" + str(coords[1]))
points.append(coords)
# inkex.debug("pointsCount=" + str(len(points)))
if len(points) == 2:
length = math.sqrt((points[0][0]-points[1][0])**2 + (points[0][1]-points[1][1])**2)
# inkex.debug("length=" + str(length))
if debug:
inkex.debug(length)
if length < self.options.length:
# inkex.debug("delete")
node.getparent().remove(node)
EnerothPurgeShortEdges().affect()