From 82216b184c669d6dea26672e5c0771146e62ca39 Mon Sep 17 00:00:00 2001 From: Kaalleen Date: Sat, 29 Jan 2022 09:53:50 +0100 Subject: [PATCH] remove some pattern and marker mixups and some style issues --- lib/elements/auto_fill.py | 29 +++++++------- lib/elements/utils.py | 4 +- lib/extensions/base.py | 6 +-- lib/extensions/params.py | 2 +- lib/marker.py | 41 +++++++++++++++++++- lib/patterns.py | 50 +++---------------------- lib/stitches/ConnectAndSamplePattern.py | 6 +-- lib/stitches/LineStringSampling.py | 2 +- lib/stitches/PointTransfer.py | 4 +- lib/stitches/StitchPattern.py | 3 +- lib/stitches/fill.py | 2 +- 11 files changed, 72 insertions(+), 77 deletions(-) diff --git a/lib/elements/auto_fill.py b/lib/elements/auto_fill.py index 3f75180b6..614e68876 100644 --- a/lib/elements/auto_fill.py +++ b/lib/elements/auto_fill.py @@ -3,27 +3,26 @@ # Copyright (c) 2010 Authors # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. +import logging import math +import re import sys import traceback -import re -import logging -import inkex +import inkex from shapely import geometry as shgeo from shapely.validation import explain_validity + from ..i18n import _ +from ..marker import get_marker_elements from ..stitch_plan import StitchGroup -from ..stitches import auto_fill, fill -from ..stitches import StitchPattern -from ..utils import cache, version -from .element import param -from .element import EmbroideryElement -from ..patterns import get_patterns -from .validation import ValidationWarning -from ..utils import Point as InkstitchPoint +from ..stitches import StitchPattern, auto_fill, fill from ..svg import PIXELS_PER_MM from ..svg.tags import INKSCAPE_LABEL +from ..utils import Point as InkstitchPoint +from ..utils import cache, version +from .element import EmbroideryElement, param +from .validation import ValidationWarning class SmallShapeWarning(ValidationWarning): @@ -393,7 +392,8 @@ class AutoFill(EmbroideryElement): else: return None - def to_stitch_groups(self, last_patch): + def to_stitch_groups(self, last_patch): # noqa: C901 + # TODO: split this up do_legacy_fill() etc. stitch_groups = [] starting_point = self.get_starting_point(last_patch) @@ -458,9 +458,8 @@ class AutoFill(EmbroideryElement): stitches=path) stitch_groups.append(stitch_group) elif self.fill_method == 2: # Guided Auto Fill - lines = get_patterns( - self.node, "#inkstitch-guide-line-marker", False, True) - lines = lines['stroke_patterns'] + lines = get_marker_elements(self.node, "guide-line", False, True) + lines = lines['stroke'] if not lines or lines[0].is_empty: inkex.errormsg( _("No line marked as guide line found within the same group as patch")) diff --git a/lib/elements/utils.py b/lib/elements/utils.py index 9fec8b635..9b9b8f143 100644 --- a/lib/elements/utils.py +++ b/lib/elements/utils.py @@ -4,7 +4,7 @@ # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. from ..commands import is_command -from ..patterns import is_pattern +from ..marker import has_marker from ..svg.tags import (EMBROIDERABLE_TAGS, SVG_IMAGE_TAG, SVG_PATH_TAG, SVG_POLYLINE_TAG, SVG_TEXT_TAG) from .auto_fill import AutoFill @@ -29,7 +29,7 @@ def node_to_elements(node): # noqa: C901 elif node.tag == SVG_PATH_TAG and not node.get('d', ''): return [EmptyDObject(node)] - elif is_pattern(node): + elif has_marker(node, 'pattern'): return [PatternObject(node)] elif node.tag in EMBROIDERABLE_TAGS: diff --git a/lib/extensions/base.py b/lib/extensions/base.py index 563854580..cf846324e 100644 --- a/lib/extensions/base.py +++ b/lib/extensions/base.py @@ -16,7 +16,7 @@ from ..commands import is_command, layer_commands from ..elements import EmbroideryElement, nodes_to_elements from ..elements.clone import is_clone from ..i18n import _ -from ..patterns import is_pattern +from ..marker import has_marker from ..svg import generate_unique_id from ..svg.tags import (CONNECTOR_TYPE, EMBROIDERABLE_TAGS, INKSCAPE_GROUPMODE, NOT_EMBROIDERABLE_TAGS, SVG_DEFS_TAG, SVG_GROUP_TAG) @@ -161,10 +161,10 @@ class InkstitchExtension(inkex.Effect): if selected: if node.tag == SVG_GROUP_TAG: pass - elif (node.tag in EMBROIDERABLE_TAGS or is_clone(node)) and not is_pattern(node): + elif (node.tag in EMBROIDERABLE_TAGS or is_clone(node)) and not has_marker(node, 'pattern'): nodes.append(node) # add images, text and patterns for the troubleshoot extension - elif troubleshoot and (node.tag in NOT_EMBROIDERABLE_TAGS or is_pattern(node)): + elif troubleshoot and (node.tag in NOT_EMBROIDERABLE_TAGS or has_marker(node, 'pattern')): nodes.append(node) return nodes diff --git a/lib/extensions/params.py b/lib/extensions/params.py index 30f6ba1dd..559636257 100644 --- a/lib/extensions/params.py +++ b/lib/extensions/params.py @@ -296,7 +296,7 @@ class ParamsTab(ScrolledPanel): widgets[3].Show(True) choice["last_initialized_choice"] = current_selection - def __do_layout(self, only_settings_grid=False): + def __do_layout(self, only_settings_grid=False): # noqa: C901 # just to add space around the settings box = wx.BoxSizer(wx.VERTICAL) diff --git a/lib/marker.py b/lib/marker.py index 1d9145e79..3c145145c 100644 --- a/lib/marker.py +++ b/lib/marker.py @@ -7,10 +7,12 @@ from copy import deepcopy from os import path import inkex +from shapely import geometry as shgeo +from .svg.tags import EMBROIDERABLE_TAGS from .utils import cache, get_bundled_dir -MARKER = ['pattern'] +MARKER = ['pattern', 'guide-line'] def ensure_marker(svg, marker): @@ -35,3 +37,40 @@ def set_marker(node, position, marker): style = [i for i in style if not i.startswith('marker-%s' % position)] style.append('marker-%s:url(#inkstitch-%s-marker)' % (position, marker)) node.set('style', ";".join(style)) + + +def get_marker_elements(node, marker, get_fills=True, get_strokes=True): + from .elements import EmbroideryElement + from .elements.stroke import Stroke + + fills = [] + strokes = [] + xpath = "./parent::svg:g/*[contains(@style, 'marker-start:url(#inkstitch-%s-marker)')]" % marker + markers = node.xpath(xpath, namespaces=inkex.NSS) + for marker in markers: + if marker.tag not in EMBROIDERABLE_TAGS: + continue + + element = EmbroideryElement(marker) + fill = element.get_style('fill') + stroke = element.get_style('stroke') + + if get_fills and fill is not None: + fill = Stroke(marker).paths + linear_rings = [shgeo.LinearRing(path) for path in fill] + for ring in linear_rings: + fills.append(shgeo.Polygon(ring)) + + if get_strokes and stroke is not None: + stroke = Stroke(marker).paths + line_strings = [shgeo.LineString(path) for path in stroke] + strokes.append(shgeo.MultiLineString(line_strings)) + + return {'fill': fills, 'stroke': strokes} + + +def has_marker(node, marker): + if node.tag not in EMBROIDERABLE_TAGS: + return False + style = node.get('style') or '' + return "marker-start:url(#inkstitch-%s-marker)" % marker in style diff --git a/lib/patterns.py b/lib/patterns.py index 7ec4d082a..1650523c2 100644 --- a/lib/patterns.py +++ b/lib/patterns.py @@ -3,25 +3,17 @@ # Copyright (c) 2010 Authors # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. -import inkex from shapely import geometry as shgeo +from .marker import get_marker_elements from .stitch_plan import Stitch -from .svg.tags import EMBROIDERABLE_TAGS from .utils import Point -def is_pattern(node): - if node.tag not in EMBROIDERABLE_TAGS: - return False - style = node.get('style') or '' - return "marker-start:url(#inkstitch-pattern-marker)" in style - - def apply_patterns(patches, node): - patterns = get_patterns(node, "#inkstitch-pattern-marker") - _apply_fill_patterns(patterns['fill_patterns'], patches) - _apply_stroke_patterns(patterns['stroke_patterns'], patches) + patterns = get_marker_elements(node, "pattern") + _apply_fill_patterns(patterns['fill'], patches) + _apply_stroke_patterns(patterns['stroke'], patches) def _apply_stroke_patterns(patterns, patches): @@ -32,8 +24,7 @@ def _apply_stroke_patterns(patterns, patches): patch_points.append(stitch) if i == len(patch.stitches) - 1: continue - intersection_points = _get_pattern_points( - stitch, patch.stitches[i+1], pattern) + intersection_points = _get_pattern_points(stitch, patch.stitches[i+1], pattern) for point in intersection_points: patch_points.append(Stitch(point, tags=('pattern_point',))) patch.stitches = patch_points @@ -65,37 +56,6 @@ def _apply_fill_patterns(patterns, patches): patch.stitches = patch_points -def get_patterns(node, marker_id, get_fills=True, get_strokes=True): - from .elements import EmbroideryElement - from .elements.auto_fill import auto_fill - from .elements.stroke import Stroke - - fills = [] - strokes = [] - xpath = "./parent::svg:g/*[contains(@style, 'marker-start:url("+marker_id+")')]" - patterns = node.xpath(xpath, namespaces=inkex.NSS) - for pattern in patterns: - if pattern.tag not in EMBROIDERABLE_TAGS: - continue - - element = EmbroideryElement(pattern) - fill = element.get_style('fill') - stroke = element.get_style('stroke') - - if fill is not None: - fill_pattern = Stroke(pattern).paths - linear_rings = [shgeo.LinearRing(path) for path in fill_pattern] - for ring in linear_rings: - fills.append(shgeo.Polygon(ring)) - - if get_strokes and stroke is not None: - stroke_pattern = Stroke(pattern).paths - line_strings = [shgeo.LineString(path) for path in stroke_pattern] - strokes.append(shgeo.MultiLineString(line_strings)) - - return {'fill_patterns': fills, 'stroke_patterns': strokes} - - def _get_pattern_points(first, second, pattern): points = [] intersection = shgeo.LineString([first, second]).intersection(pattern) diff --git a/lib/stitches/ConnectAndSamplePattern.py b/lib/stitches/ConnectAndSamplePattern.py index 33a1ba6df..1cf2b2a12 100644 --- a/lib/stitches/ConnectAndSamplePattern.py +++ b/lib/stitches/ConnectAndSamplePattern.py @@ -7,7 +7,6 @@ import trimesh import numpy as np from scipy import spatial import math -from shapely.geometry import asLineString from anytree import PreOrderIter from ..stitches import LineStringSampling from ..stitches import PointTransfer @@ -52,7 +51,7 @@ def cut(line, distance): return LineString([(cp.x, cp.y)] + coords[i:] + coords[:i]) -def connect_raster_tree_nearest_neighbor( +def connect_raster_tree_nearest_neighbor( # noqa: C901 tree, used_offset, stitch_distance, close_point, offset_by_half): """ Takes the offsetted curves organized as tree, connects and samples them. @@ -458,8 +457,7 @@ def calculate_replacing_middle_point(line_segment, abs_offset, max_stitch_distan return line_segment.coords[1] -def connect_raster_tree_from_inner_to_outer( - tree, used_offset, stitch_distance, close_point, offset_by_half): +def connect_raster_tree_from_inner_to_outer(tree, used_offset, stitch_distance, close_point, offset_by_half): # noqa: C901 """ Takes the offsetted curves organized as tree, connects and samples them. Strategy: A connection from parent to child is made as fast as possible to diff --git a/lib/stitches/LineStringSampling.py b/lib/stitches/LineStringSampling.py index 43f650e6f..71660e2d1 100644 --- a/lib/stitches/LineStringSampling.py +++ b/lib/stitches/LineStringSampling.py @@ -70,7 +70,7 @@ def calculate_line_angles(line): return Angles -def raster_line_string_with_priority_points(line, start_distance, end_distance, maxstitch_distance, +def raster_line_string_with_priority_points(line, start_distance, end_distance, maxstitch_distance, # noqa: C901 must_use_points_deque, abs_offset, offset_by_half, replace_forbidden_points): """ Rasters a line between start_distance and end_distance. diff --git a/lib/stitches/PointTransfer.py b/lib/stitches/PointTransfer.py index b6e4e026e..93fe02c5f 100644 --- a/lib/stitches/PointTransfer.py +++ b/lib/stitches/PointTransfer.py @@ -36,7 +36,7 @@ def calc_transferred_point(bisectorline, child): return point, priority -def transfer_points_to_surrounding(treenode, used_offset, offset_by_half, to_transfer_points, to_transfer_points_origin=[], +def transfer_points_to_surrounding(treenode, used_offset, offset_by_half, to_transfer_points, to_transfer_points_origin=[], # noqa: C901 overnext_neighbor=False, transfer_forbidden_points=False, transfer_to_parent=True, transfer_to_sibling=True, transfer_to_child=True): """ @@ -305,7 +305,7 @@ def calc_transferred_point_graph(bisectorline, edge_geometry): return point, priority -def transfer_points_to_surrounding_graph(fill_stitch_graph, current_edge, used_offset, offset_by_half, to_transfer_points, +def transfer_points_to_surrounding_graph(fill_stitch_graph, current_edge, used_offset, offset_by_half, to_transfer_points, # noqa: C901 overnext_neighbor=False, transfer_forbidden_points=False, transfer_to_previous=True, transfer_to_next=True): """ Takes the current graph edge and its rastered points (to_transfer_points) and transfers these points to its previous and next edges (if selected) diff --git a/lib/stitches/StitchPattern.py b/lib/stitches/StitchPattern.py index 62ef2b0fe..4a38c0bc7 100644 --- a/lib/stitches/StitchPattern.py +++ b/lib/stitches/StitchPattern.py @@ -159,8 +159,7 @@ def check_and_prepare_tree_for_valid_spiral(root): return True -def offset_poly( - poly, offset, join_style, stitch_distance, offset_by_half, strategy, starting_point): +def offset_poly(poly, offset, join_style, stitch_distance, offset_by_half, strategy, starting_point): # noqa: C901 """ Takes a polygon (which can have holes) as input and creates offsetted versions until the polygon is filled with these smaller offsets. diff --git a/lib/stitches/fill.py b/lib/stitches/fill.py index 5afcb2281..ceac56d96 100644 --- a/lib/stitches/fill.py +++ b/lib/stitches/fill.py @@ -145,7 +145,7 @@ def repair_non_simple_lines(line): return repaired -def intersect_region_with_grating_line(shape, line, row_spacing, end_row_spacing=None, flip=False): +def intersect_region_with_grating_line(shape, line, row_spacing, end_row_spacing=None, flip=False): # noqa: C901 row_spacing = abs(row_spacing) (minx, miny, maxx, maxy) = shape.bounds