Changed Sieve to Small Things Filter

This commit is contained in:
Mario Voigt 2020-08-03 00:13:43 +02:00
parent 81aa68198d
commit 52dfa0607c
4 changed files with 78 additions and 66 deletions

View File

@ -1,23 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<_name>Small Area Filter</_name>
<id>fablabchemnitz.de.small_area_filter</id>
<param name="use" type="description" xml:space="preserve">Paths with area smaller than the given area threshold will be deleted</param>
<param name="unit" _gui-text="Unit" type="enum">
<_item value="mm">mm^2</_item>
<_item value="cm">cm^2</_item>
<_item value="m">m^2</_item>
</param>
<param name="area" type="float" min="0.0" max="10000000.0" _gui-text="Area (Unit^2):">1.0</param>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu _name="FabLab Chemnitz">
<submenu _name="Nesting/Cut Optimization" />
</submenu>
</effects-menu>
</effect>
<script>
<command reldir="extensions" interpreter="python">fablabchemnitz_sieve.py</command>
</script>
</inkscape-extension>

View File

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

View File

@ -0,0 +1,27 @@
<?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.small_things_filter</id>
<param name="use" type="description" xml:space="preserve">Paths with value smaller than the given threshold will be deleted</param>
<param name="unit" _gui-text="Unit" type="enum">
<_item value="mm">mm</_item>
<_item value="cm">cm</_item>
<_item value="m">m</_item>
</param>
<param name="threshold" type="float" min="0.0" max="10000000.0" _gui-text="Threshold">1.0</param>
<param name="measure" type="optiongroup" appearance="minimal" _gui-text="Measure by">
<option value="length">Length (Unit)</option>
<option value="area">Area (Unit^2)</option>
</param>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu _name="FabLab Chemnitz">
<submenu _name="Nesting/Cut Optimization" />
</submenu>
</effects-menu>
</effect>
<script>
<command reldir="extensions" interpreter="python">fablabchemnitz_small_things_filter.py</command>
</script>
</inkscape-extension>

View File

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