added some features to small things filter; renamed to Filter By

Length/Area
This commit is contained in:
Mario Voigt 2021-10-21 23:06:47 +02:00
parent dc2dc15aee
commit a9b75cc1eb
5 changed files with 95 additions and 72 deletions

View File

@ -1,14 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>Filter Small Things</name>
<id>fablabchemnitz.de.filter_small_things</id>
<name>Filter By Length/Area</name>
<id>fablabchemnitz.de.filter_by_length_area</id>
<label>Paths with value smaller than the given threshold will be deleted</label>
<param name="unit" gui-text="Unit" type="optiongroup" appearance="combo">
<option value="mm">mm</option>
<option value="cm">cm</option>
<option value="m">m</option>
</param>
<param name="threshold" type="float" min="0.0" max="10000000.0" gui-text="Threshold">1.0</param>
<label appearance="heder">Threshold</label>
<param name="min_filter_enable" type="bool" gui-text="Enable filtering min.">false</param>
<param name="min_threshold" type="float" min="0.000" precision="3" max="10000000.000" gui-text="Min.">1.000</param>
<param name="max_filter_enable" type="bool" gui-text="Enable filtering max.">false</param>
<param name="max_threshold" type="float" min="0.000" precision="3" max="10000000.000" gui-text="Max.">10000000.000</param>
<label appearance="heder">Equation</label>
<param name="measure" type="optiongroup" appearance="combo" gui-text="Measure by">
<option value="length">Length (Unit)</option>
<option value="area">Area (Unit^2)</option>
@ -22,6 +27,6 @@
</effects-menu>
</effect>
<script>
<command location="inx" interpreter="python">filter_small_things.py</command>
<command location="inx" interpreter="python">filter_by_length_area.py</command>
</script>
</inkscape-extension>

View File

@ -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()

View File

@ -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"
]
}
]

View File

@ -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()

View File

@ -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"
]
}
]