From a9b75cc1eb0baae59c74cff1d8d27226e7919c25 Mon Sep 17 00:00:00 2001 From: Mario Voigt Date: Thu, 21 Oct 2021 23:06:47 +0200 Subject: [PATCH] added some features to small things filter; renamed to Filter By Length/Area --- .../filter_by_length_area.inx} | 13 ++-- .../filter_by_length_area.py | 67 +++++++++++++++++++ .../filter_by_length_area/meta.json | 19 ++++++ .../filter_small_things.py | 48 ------------- .../filter_small_things/meta.json | 20 ------ 5 files changed, 95 insertions(+), 72 deletions(-) rename extensions/fablabchemnitz/{filter_small_things/filter_small_things.inx => filter_by_length_area/filter_by_length_area.inx} (55%) create mode 100644 extensions/fablabchemnitz/filter_by_length_area/filter_by_length_area.py create mode 100644 extensions/fablabchemnitz/filter_by_length_area/meta.json delete mode 100644 extensions/fablabchemnitz/filter_small_things/filter_small_things.py delete mode 100644 extensions/fablabchemnitz/filter_small_things/meta.json diff --git a/extensions/fablabchemnitz/filter_small_things/filter_small_things.inx b/extensions/fablabchemnitz/filter_by_length_area/filter_by_length_area.inx similarity index 55% rename from extensions/fablabchemnitz/filter_small_things/filter_small_things.inx rename to extensions/fablabchemnitz/filter_by_length_area/filter_by_length_area.inx index b7e2c130..94c09aa5 100644 --- a/extensions/fablabchemnitz/filter_small_things/filter_small_things.inx +++ b/extensions/fablabchemnitz/filter_by_length_area/filter_by_length_area.inx @@ -1,14 +1,19 @@ - Filter Small Things - fablabchemnitz.de.filter_small_things + Filter By Length/Area + fablabchemnitz.de.filter_by_length_area - 1.0 + + false + 1.000 + false + 10000000.000 + @@ -22,6 +27,6 @@ diff --git a/extensions/fablabchemnitz/filter_by_length_area/filter_by_length_area.py b/extensions/fablabchemnitz/filter_by_length_area/filter_by_length_area.py new file mode 100644 index 00000000..e17e3b3f --- /dev/null +++ b/extensions/fablabchemnitz/filter_by_length_area/filter_by_length_area.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 + +''' +Extension for InkScape 1.0 +Features +- Filter paths which are smaller/bigger than a given length or area + +Author: Mario Voigt / FabLab Chemnitz +Mail: mario.voigt@stadtfabrikanten.org +Date: 03.08.2020 +Last patch: 21.10.2021 +License: GNU GPL v3 +''' + +import inkex +from inkex.bezier import csplength, csparea + +class FilterByLengthArea(inkex.EffectExtension): + + def add_arguments(self, pars): + pars.add_argument('--unit') + pars.add_argument('--min_filter_enable', type=inkex.Boolean, default=True, help='Enable filtering min.') + pars.add_argument('--min_threshold', type=float, default=0.000, help='Remove paths with an threshold smaller than this value') + pars.add_argument('--max_filter_enable', type=inkex.Boolean, default=False, help='Enable filtering max.') + pars.add_argument('--max_threshold', type=float, default=10000000.000, help='Remove paths with an threshold bigger than this value') + pars.add_argument('--measure', default="length") + + def effect(self): + + if len(self.svg.selected) == 0: + inkex.utils.debug("Your selection is empty.") + return + + if self.options.min_filter_enable is False and self.options.max_filter_enable is False: + inkex.utils.debug("You need to enabled at least one filter rule!") + return + + self.options.min_threshold = self.svg.unittouu(str(self.options.min_threshold) + self.svg.unit) + self.options.max_threshold = self.svg.unittouu(str(self.options.max_threshold) + self.svg.unit) + unit_factor = 1.0 / self.svg.uutounit(1.0,self.options.unit) + if self.options.min_threshold == 0 or self.options.max_threshold == 0: + inkex.utils.debug("One or both tresholds are zero. Please adjust.") + return + + for element in self.document.xpath("//svg:path", namespaces=inkex.NSS): + try: + 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 self.options.min_filter_enable is True and area < (self.options.min_threshold * (unit_factor * unit_factor)): + element.delete() + if self.options.max_filter_enable is True and area >= (self.options.max_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 self.options.min_filter_enable is True and stotal < (self.options.min_threshold * unit_factor): + element.delete() + if self.options.max_filter_enable is True and stotal >= (self.options.max_threshold * unit_factor): + element.delete() + + except Exception as e: + pass + +if __name__ == '__main__': + FilterByLengthArea().run() \ No newline at end of file diff --git a/extensions/fablabchemnitz/filter_by_length_area/meta.json b/extensions/fablabchemnitz/filter_by_length_area/meta.json new file mode 100644 index 00000000..23343700 --- /dev/null +++ b/extensions/fablabchemnitz/filter_by_length_area/meta.json @@ -0,0 +1,19 @@ +[ + { + "name": "Filter By Length/Area", + "id": "fablabchemnitz.de.filter_by_length_area", + "path": "filter_by_length_area", + "original_name": "Filter By Length/Area", + "original_id": "com.filter_by_length_area", + "license": "GNU GPL v3", + "license_url": "https://gitea.fablabchemnitz.de/FabLab_Chemnitz/mightyscape-1.X/src/branch/master/LICENSE", + "comment": "", + "source_url": "https://gitea.fablabchemnitz.de/FabLab_Chemnitz/mightyscape-1.X/src/branch/master/extensions/fablabchemnitz/filter_by_length_area", + "fork_url": null, + "documentation_url": "https://stadtfabrikanten.org/pages/viewpage.action?pageId=74645969", + "inkscape_gallery_url": null, + "main_authors": [ + "github.com/vmario89" + ] + } +] \ No newline at end of file diff --git a/extensions/fablabchemnitz/filter_small_things/filter_small_things.py b/extensions/fablabchemnitz/filter_small_things/filter_small_things.py deleted file mode 100644 index 97c3d0b0..00000000 --- a/extensions/fablabchemnitz/filter_small_things/filter_small_things.py +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env python3 - -''' -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 -''' - -import inkex -from inkex.bezier import csplength, csparea - -class FilterSmallThings(inkex.EffectExtension): - - 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") - - def effect(self): - self.options.threshold = self.svg.unittouu(str(self.options.threshold) + self.svg.unit) - unit_factor = 1.0 / self.svg.uutounit(1.0,self.options.unit) - if self.options.threshold == 0: - return - - for element in self.document.xpath("//svg:path", namespaces=inkex.NSS): - try: - 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: - pass - -if __name__ == '__main__': - FilterSmallThings().run() \ No newline at end of file diff --git a/extensions/fablabchemnitz/filter_small_things/meta.json b/extensions/fablabchemnitz/filter_small_things/meta.json deleted file mode 100644 index 8d5b56b5..00000000 --- a/extensions/fablabchemnitz/filter_small_things/meta.json +++ /dev/null @@ -1,20 +0,0 @@ -[ - { - "name": "Filter Small Things", - "id": "fablabchemnitz.de.filter_small_things", - "path": "filter_small_things", - "original_name": "Sieve", - "original_id": "com.fourpeaksstudios.sieve", - "license": "GNU GPL v3", - "license_url": "https://github.com/fourpeaksstudios/sieve/blob/master/LICENSE", - "comment": "", - "source_url": "https://gitea.fablabchemnitz.de/FabLab_Chemnitz/mightyscape-1.X/src/branch/master/extensions/fablabchemnitz/filter_small_things", - "fork_url": "https://github.com/fourpeaksstudios/sieve", - "documentation_url": "https://stadtfabrikanten.org/display/IFM/Filter+Small+Things", - "inkscape_gallery_url": null, - "main_authors": [ - "github.com/fourpeaksstudios", - "github.com/vmario89" - ] - } -] \ No newline at end of file