From 5935a95345d089c8863b8595fda77c97b0f3641d Mon Sep 17 00:00:00 2001 From: Mario Voigt Date: Thu, 4 Nov 2021 00:53:13 +0100 Subject: [PATCH] extend filter by extension --- .../colorize_path_lengths.inx | 2 +- .../colorize_path_lengths/meta.json | 4 +- .../contour_scanner_and_trimmer.py | 1 + .../filter_by_length_area.inx | 16 +++- .../filter_by_length_area.py | 89 +++++++++++++------ .../fablabchemnitz/laser_check/laser_check.py | 7 ++ 6 files changed, 83 insertions(+), 36 deletions(-) diff --git a/extensions/fablabchemnitz/colorize_path_lengths/colorize_path_lengths.inx b/extensions/fablabchemnitz/colorize_path_lengths/colorize_path_lengths.inx index 2e5f98af..ba3f60b7 100644 --- a/extensions/fablabchemnitz/colorize_path_lengths/colorize_path_lengths.inx +++ b/extensions/fablabchemnitz/colorize_path_lengths/colorize_path_lengths.inx @@ -1,6 +1,6 @@ - Colorize Path Lengths + Colorize Path Lengths/Slants fablabchemnitz.de.colorize_path_lengths diff --git a/extensions/fablabchemnitz/colorize_path_lengths/meta.json b/extensions/fablabchemnitz/colorize_path_lengths/meta.json index ca9e6b79..fb09aeb5 100644 --- a/extensions/fablabchemnitz/colorize_path_lengths/meta.json +++ b/extensions/fablabchemnitz/colorize_path_lengths/meta.json @@ -1,6 +1,6 @@ [ { - "name": "Colorize Path Lengths", + "name": "Colorize Path Lengths/Slants", "id": "fablabchemnitz.de.colorize_path_lengths", "path": "colorize_path_lengths", "dependent_extensions": null, @@ -11,7 +11,7 @@ "comment": "ported to Inkscape v1 manually by Mario Voigt", "source_url": "https://gitea.fablabchemnitz.de/FabLab_Chemnitz/mightyscape-1.X/src/branch/master/extensions/fablabchemnitz/colorize_path_lengths", "fork_url": "http://dp48069596.lolipop.jp/sd/scripts/script_inkscape/pathselection.zip", - "documentation_url": "https://stadtfabrikanten.org/display/IFM/Colorize+Path+Lengths", + "documentation_url": "https://stadtfabrikanten.org/pages/viewpage.action?pageId=55019108", "inkscape_gallery_url": null, "main_authors": [ "Sunabe Kazumichi", diff --git a/extensions/fablabchemnitz/contour_scanner_and_trimmer/contour_scanner_and_trimmer.py b/extensions/fablabchemnitz/contour_scanner_and_trimmer/contour_scanner_and_trimmer.py index 67eeac36..3505090c 100644 --- a/extensions/fablabchemnitz/contour_scanner_and_trimmer/contour_scanner_and_trimmer.py +++ b/extensions/fablabchemnitz/contour_scanner_and_trimmer/contour_scanner_and_trimmer.py @@ -19,6 +19,7 @@ Extension for InkScape 1.0+ replacedelement.path = replacedelement.path.to_absolute().to_superpath().to_path() - maybe option: break apart while keeping relative/absolute commands (more complex and not sure if we have a great advantage having this) - note: running this extension might leave some empty parent groups in some circumstances. run the clean groups extension separately to fix that + - sort to groups by path type (open, closed, ...) - important to notice - this algorithm might be really slow. Reduce flattening quality to speed up diff --git a/extensions/fablabchemnitz/filter_by_length_area/filter_by_length_area.inx b/extensions/fablabchemnitz/filter_by_length_area/filter_by_length_area.inx index 246b9037..948a13b7 100644 --- a/extensions/fablabchemnitz/filter_by_length_area/filter_by_length_area.inx +++ b/extensions/fablabchemnitz/filter_by_length_area/filter_by_length_area.inx @@ -2,7 +2,7 @@ Filter By Length/Area fablabchemnitz.de.filter_by_length_area - + false @@ -17,17 +17,25 @@ 10.000 false - 1.000 + 1.000 2 false - 10000000.000 + 10000000.000 10000000 - + 3 + + + false + false + + false + false + all 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 index 2056259e..c0ac6070 100644 --- a/extensions/fablabchemnitz/filter_by_length_area/filter_by_length_area.py +++ b/extensions/fablabchemnitz/filter_by_length_area/filter_by_length_area.py @@ -12,6 +12,7 @@ Last patch: 21.10.2021 License: GNU GPL v3 ''' +import colorsys import inkex from inkex.bezier import csplength, csparea @@ -27,14 +28,18 @@ class FilterByLengthArea(inkex.EffectExtension): pars.add_argument('--min_nodes', type=int, default=0, help='Min. nodes/') pars.add_argument('--max_nodes', type=int, default=10000000, help='Max. nodes/') pars.add_argument('--nodes_interval', type=float, default=10000000.000, help='Interval') + pars.add_argument('--precision', type=int, default=3, help='Precision') pars.add_argument('--measure', default="length") - - def effect(self): - so = self.options - if so.min_filter_enable is False and so.max_filter_enable is False: - inkex.utils.debug("You need to enabled at least one filter rule!") - return + pars.add_argument('--delete', type=inkex.Boolean, default=False) + pars.add_argument('--colorize', type=inkex.Boolean, default=False) + pars.add_argument('--sort_by_asc', type=inkex.Boolean, default=False) + pars.add_argument('--reverse_sort', type=inkex.Boolean, default=False) + def effect(self): + global to_sort, so + to_sort = [] + so = self.options + so.min_threshold = self.svg.unittouu(str(so.min_threshold) + self.svg.unit) so.max_threshold = self.svg.unittouu(str(so.max_threshold) + self.svg.unit) unit_factor = 1.0 / self.svg.uutounit(1.0, so.unit) @@ -47,41 +52,67 @@ class FilterByLengthArea(inkex.EffectExtension): else: elements = self.document.xpath("//svg:path", namespaces=inkex.NSS) + if so.debug is True: + inkex.utils.debug("Collecting elements ...") for element in elements: try: csp = element.path.transform(element.composed_transform()).to_superpath() if so.measure == "area": - area = -csparea(csp) #is returned as negative value. we need to invert with - if so.debug is True: - inkex.utils.debug("id={}, area={:0.3f}{}^2".format(element.get('id'), area, so.unit)) - if so.min_filter_enable is True and area < (so.min_threshold * (unit_factor * unit_factor)): - element.delete() - if so.max_filter_enable is True and area >= (so.max_threshold * (unit_factor * unit_factor)): - element.delete() - + area = round(-csparea(csp), so.precision) #is returned as negative value. we need to invert with + if (so.min_filter_enable is True and area < (so.min_threshold * (unit_factor * unit_factor))) or \ + (so.max_filter_enable is True and area >= (so.max_threshold * (unit_factor * unit_factor))) or \ + (so.min_filter_enable is False and so.max_filter_enable is False): #complete selection + if so.debug is True: + inkex.utils.debug("id={}, area={:0.3f}{}^2".format(element.get('id'), area, so.unit)) + to_sort.append({'element': element, 'value': area}) + elif so.measure == "length": slengths, stotal = csplength(csp) #get segment lengths and total length of path in document's internal unit - if so.debug is True: - inkex.utils.debug("id={}, length={:0.3f}{}".format(element.get('id'), self.svg.uutounit(str(stotal), so.unit), so.unit)) - if so.min_filter_enable is True and stotal < (so.min_threshold * unit_factor): - element.delete() - if self.options.max_filter_enable is True and stotal >= (so.max_threshold * unit_factor): - element.delete() - + stotal = round(stotal, so.precision) + if (so.min_filter_enable is True and stotal < (so.min_threshold * unit_factor)) or \ + (so.max_filter_enable is True and stotal >= (so.max_threshold * unit_factor)) or \ + (so.min_filter_enable is False and so.max_filter_enable is False): #complete selection + if so.debug is True: + inkex.utils.debug("id={}, length={:0.3f}{}".format(element.get('id'), self.svg.uutounit(str(stotal), so.unit), so.unit)) + to_sort.append({'element': element, 'value': stotal}) + elif so.measure == "nodes": slengths, stotal = csplength(csp) #get segment lengths and total length of path in document's internal unit + stotal = round(stotal, so.precision) nodes = len(element.path) - if so.debug is True: - inkex.utils.debug("id={}, length={:0.3f}{}, nodes={}".format(element.get('id'), self.svg.uutounit(str(stotal), so.unit), so.unit, nodes)) - if so.min_filter_enable is True and nodes / stotal < so.min_nodes / self.svg.unittouu(str(so.nodes_interval) + so.unit): - element.delete() - if so.max_filter_enable is True and nodes / stotal < so.max_nodes / self.svg.unittouu(str(so.nodes_interval) + so.unit): - element.delete() - + if (so.min_filter_enable is True and nodes / stotal < so.min_nodes / self.svg.unittouu(str(so.nodes_interval) + so.unit)) or \ + (so.max_filter_enable is True and nodes / stotal < so.max_nodes / self.svg.unittouu(str(so.nodes_interval) + so.unit)) or \ + (so.min_filter_enable is False and so.max_filter_enable is False): #complete selection + if so.debug is True: + inkex.utils.debug("id={}, length={:0.3f}{}, nodes={}".format(element.get('id'), self.svg.uutounit(str(stotal), so.unit), so.unit, nodes)) + to_sort.append({'element': element, 'value': nodes}) + except Exception as e: - #self.msg(e) + #inkex.utils.debug(e) pass + + for i in range(0, len(to_sort)): + element = to_sort[i].get('element') + if so.delete is True: + element.delete() + if so.delete is True: + return #quit here + + if so.sort_by_asc is True: + to_sort.sort(key=lambda x: x.get('value')) #sort by target value + + for i in range(0, len(to_sort)): + element = to_sort[i].get('element') + if so.reverse_sort is True: + idx = len(element.getparent()) + else: + idx = 0 + element.getparent().insert(idx, element) + if so.colorize is True: + color = colorsys.hsv_to_rgb(i / float(len(to_sort)), 1.0, 1.0) + element.style['stroke'] = '#%02x%02x%02x' % tuple(int(x * 255) for x in color) + if __name__ == '__main__': FilterByLengthArea().run() \ No newline at end of file diff --git a/extensions/fablabchemnitz/laser_check/laser_check.py b/extensions/fablabchemnitz/laser_check/laser_check.py index 411a610b..41b87639 100644 --- a/extensions/fablabchemnitz/laser_check/laser_check.py +++ b/extensions/fablabchemnitz/laser_check/laser_check.py @@ -16,6 +16,13 @@ class LaserCheck(inkex.EffectExtension): - number of parts to weed in total; number of parts which are smaller than vector grid - add some inkex.Desc to all elements which were checked and which have some issue. use special syntax to remove old stuff each time the check is applied again - this code is horrible ugly stuff + - output time/cost estimations per stroke color + - add check for stroke colors -> make some useful predefinitions like (for default modes) + - black = cutting + - blue = cutting inside + - green = cutting outside + - pink = Vektorgravur + ... ''' def add_arguments(self, pars):