From 52dfa0607c69a1ebc2dbb746c678e85b0f1979af Mon Sep 17 00:00:00 2001 From: Mario Voigt Date: Mon, 3 Aug 2020 00:13:43 +0200 Subject: [PATCH] Changed Sieve to Small Things Filter --- extensions/fablabchemnitz_sieve.inx | 23 --------- extensions/fablabchemnitz_sieve.py | 43 ---------------- .../fablabchemnitz_small_things_filter.inx | 27 ++++++++++ .../fablabchemnitz_small_things_filter.py | 51 +++++++++++++++++++ 4 files changed, 78 insertions(+), 66 deletions(-) delete mode 100644 extensions/fablabchemnitz_sieve.inx delete mode 100644 extensions/fablabchemnitz_sieve.py create mode 100644 extensions/fablabchemnitz_small_things_filter.inx create mode 100644 extensions/fablabchemnitz_small_things_filter.py diff --git a/extensions/fablabchemnitz_sieve.inx b/extensions/fablabchemnitz_sieve.inx deleted file mode 100644 index fac2ed7a..00000000 --- a/extensions/fablabchemnitz_sieve.inx +++ /dev/null @@ -1,23 +0,0 @@ - - - <_name>Small Area Filter - fablabchemnitz.de.small_area_filter - Paths with area smaller than the given area threshold will be deleted - - <_item value="mm">mm^2 - <_item value="cm">cm^2 - <_item value="m">m^2 - - 1.0 - - all - - - - - - - - diff --git a/extensions/fablabchemnitz_sieve.py b/extensions/fablabchemnitz_sieve.py deleted file mode 100644 index 6fb47c86..00000000 --- a/extensions/fablabchemnitz_sieve.py +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env python3 - -import inkex -import svgpathtools - -def isclosedac(p): - return abs(p.start-p.end) < 1e-6 - - -class Sieve(inkex.Effect): - def __init__(self): - inkex.Effect.__init__(self) - self.arg_parser.add_argument('--unit') - self.arg_parser.add_argument('--area', type=float, help='Remove paths with an area smaller than this value') - - def effect(self): - namedView = self.document.getroot().find(inkex.addNS('namedview', 'sodipodi')) - doc_units = namedView.get(inkex.addNS('document-units', 'inkscape')) - #inkex.utils.debug("document unit is " + doc_units) - self.options.area = self.svg.unittouu(str(self.options.area) + doc_units) - unit_factor = 1.0 / self.svg.uutounit(1.0,self.options.unit) - #inkex.utils.debug("unit_factor is " + str(unit_factor)) - - if self.options.area == 0: - return - - for path in self.document.xpath("//svg:path", namespaces=inkex.NSS): - try: - parsed_path = svgpathtools.parse_path(path.attrib["d"]) - - if not isclosedac(parsed_path): - continue - - area = parsed_path.area() - #inkex.utils.debug(area) #print calculated area with document units - #inkex.utils.debug(str(self.options.area * (unit_factor * unit_factor))) #print threshold area with selected units - if area < (self.options.area * (unit_factor * unit_factor)): - path.getparent().remove(path) - except: - pass - -if __name__ == '__main__': - Sieve().run() \ No newline at end of file diff --git a/extensions/fablabchemnitz_small_things_filter.inx b/extensions/fablabchemnitz_small_things_filter.inx new file mode 100644 index 00000000..0997475b --- /dev/null +++ b/extensions/fablabchemnitz_small_things_filter.inx @@ -0,0 +1,27 @@ + + + <_name>Filter Small Things + fablabchemnitz.de.small_things_filter + Paths with value smaller than the given threshold will be deleted + + <_item value="mm">mm + <_item value="cm">cm + <_item value="m">m + + 1.0 + + + + + + all + + + + + + + + diff --git a/extensions/fablabchemnitz_small_things_filter.py b/extensions/fablabchemnitz_small_things_filter.py new file mode 100644 index 00000000..52788e6e --- /dev/null +++ b/extensions/fablabchemnitz_small_things_filter.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +import inkex +import svgpathtools + +def isclosedac(p): + return abs(p.start-p.end) < 1e-6 + + +class Sieve(inkex.Effect): + def __init__(self): + inkex.Effect.__init__(self) + self.arg_parser.add_argument('--unit') + self.arg_parser.add_argument('--threshold', type=float, help='Remove paths with an threshold smaller than this value') + self.arg_parser.add_argument('--measure', default="length") + + def effect(self): + namedView = self.document.getroot().find(inkex.addNS('namedview', 'sodipodi')) + doc_units = namedView.get(inkex.addNS('document-units', 'inkscape')) + #inkex.utils.debug("document unit is " + doc_units) + self.options.threshold = self.svg.unittouu(str(self.options.threshold) + doc_units) + unit_factor = 1.0 / self.svg.uutounit(1.0,self.options.unit) + #inkex.utils.debug("unit_factor is " + str(unit_factor)) + + if self.options.threshold == 0: + return + + for path in self.document.xpath("//svg:path", namespaces=inkex.NSS): + try: + parsed_path = svgpathtools.parse_path(path.attrib["d"]) + + #if not isclosedac(parsed_path): + # continue + + if self.options.measure == "area": + calc = parsed_path.area() + #inkex.utils.debug(calc) #print calculated area with document units + #inkex.utils.debug(str(self.options.threshold * (unit_factor * unit_factor))) #print threshold area with selected units + if calc < (self.options.threshold * (unit_factor * unit_factor)): + path.getparent().remove(path) + else: #length + calc = parsed_path.length() + #inkex.utils.debug(calc) #print calculated area with document units + #inkex.utils.debug(str(self.options.threshold * (unit_factor * unit_factor))) #print threshold area with selected units + if calc < (self.options.threshold * unit_factor): + path.getparent().remove(path) + except: + pass + +if __name__ == '__main__': + Sieve().run() \ No newline at end of file