2020-08-03 00:13:43 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-04-21 10:53:20 +02:00
|
|
|
'''
|
|
|
|
Extension for InkScape 1.0
|
|
|
|
Features
|
|
|
|
- Filter paths which are smaller than a given length or area
|
|
|
|
|
|
|
|
Author: Mario Voigt / FabLab Chemnitz
|
|
|
|
Mail: mario.voigt@stadtfabrikanten.org
|
|
|
|
Date: 03.08.2020
|
|
|
|
Last patch: 20.04.2021
|
|
|
|
License: GNU GPL v3
|
|
|
|
'''
|
|
|
|
|
2020-08-03 00:13:43 +02:00
|
|
|
import inkex
|
2021-04-19 22:23:44 +02:00
|
|
|
from inkex.bezier import csplength, csparea
|
2020-08-03 00:13:43 +02:00
|
|
|
|
2021-06-02 23:30:37 +02:00
|
|
|
class FilterSmallThings(inkex.EffectExtension):
|
2021-04-15 17:03:47 +02:00
|
|
|
|
|
|
|
def add_arguments(self, pars):
|
|
|
|
pars.add_argument('--unit')
|
|
|
|
pars.add_argument('--threshold', type=float, help='Remove paths with an threshold smaller than this value')
|
|
|
|
pars.add_argument('--measure', default="length")
|
2020-08-03 00:13:43 +02:00
|
|
|
|
|
|
|
def effect(self):
|
2020-08-04 12:13:04 +02:00
|
|
|
self.options.threshold = self.svg.unittouu(str(self.options.threshold) + self.svg.unit)
|
2020-08-03 00:13:43 +02:00
|
|
|
unit_factor = 1.0 / self.svg.uutounit(1.0,self.options.unit)
|
|
|
|
if self.options.threshold == 0:
|
|
|
|
return
|
|
|
|
|
2021-04-19 22:23:44 +02:00
|
|
|
for element in self.document.xpath("//svg:path", namespaces=inkex.NSS):
|
2020-08-03 00:13:43 +02:00
|
|
|
try:
|
2021-04-19 22:23:44 +02:00
|
|
|
csp = element.path.transform(element.composed_transform()).to_superpath()
|
|
|
|
|
|
|
|
if self.options.measure == "area":
|
|
|
|
area = -csparea(csp) #is returned as negative value. we need to invert with -
|
|
|
|
if area < (self.options.threshold * (unit_factor * unit_factor)):
|
|
|
|
element.delete()
|
|
|
|
|
|
|
|
elif self.options.measure == "length":
|
|
|
|
slengths, stotal = csplength(csp) #get segment lengths and total length of path in document's internal unit
|
|
|
|
if stotal < (self.options.threshold * unit_factor):
|
|
|
|
element.delete()
|
|
|
|
except Exception as e:
|
2020-08-03 00:13:43 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-06-02 23:30:37 +02:00
|
|
|
FilterSmallThings().run()
|