summaryrefslogtreecommitdiff
path: root/lib/stitches
diff options
context:
space:
mode:
authorKaalleen <36401965+kaalleen@users.noreply.github.com>2025-01-24 18:16:08 +0100
committerGitHub <noreply@github.com>2025-01-24 18:16:08 +0100
commit82c6af07a530e43ecb1804066a8cfff34c077a8f (patch)
tree193a47b7d553fee8224c87adf6a06d788a83dbc5 /lib/stitches
parent28dd69065816fb9695c05ee6ddaff7714387358d (diff)
Auto-run: try harder to avoid networkx issues (#3457)
* auto-run: try harder to avoid networkx issues * fix auto-run transforms
Diffstat (limited to 'lib/stitches')
-rw-r--r--lib/stitches/auto_run.py3
-rw-r--r--lib/stitches/utils/autoroute.py39
2 files changed, 37 insertions, 5 deletions
diff --git a/lib/stitches/auto_run.py b/lib/stitches/auto_run.py
index 28da5482..760d0393 100644
--- a/lib/stitches/auto_run.py
+++ b/lib/stitches/auto_run.py
@@ -121,6 +121,7 @@ class LineSegments:
def autorun(elements, preserve_order=False, break_up=None, starting_point=None, ending_point=None, trim=False):
graph = build_graph(elements, preserve_order, break_up)
+
graph = add_jumps(graph, elements, preserve_order)
starting_point, ending_point = get_starting_and_ending_nodes(
@@ -132,7 +133,7 @@ def autorun(elements, preserve_order=False, break_up=None, starting_point=None,
new_elements, trims, original_parents = path_to_elements(graph, path, trim)
if preserve_order:
- preserve_original_groups(new_elements, original_parents)
+ preserve_original_groups(new_elements, original_parents, transform=False)
else:
parent = elements[0].node.getparent()
insert_index = parent.index(elements[0].node)
diff --git a/lib/stitches/utils/autoroute.py b/lib/stitches/utils/autoroute.py
index a83e8002..b10ae21c 100644
--- a/lib/stitches/utils/autoroute.py
+++ b/lib/stitches/utils/autoroute.py
@@ -30,7 +30,7 @@ def find_path(graph, starting_node, ending_node):
#
# Visiting the edges again on the way back allows us to set up
# "underpathing".
- path = nx.shortest_path(graph, starting_node, ending_node)
+ path = _get_shortest_path(graph, starting_node, ending_node)
check_stop_flag()
@@ -77,6 +77,17 @@ def find_path(graph, starting_node, ending_node):
return final_path
+def _get_shortest_path(graph, starting_node, ending_node):
+ try:
+ path = nx.shortest_path(graph, starting_node, ending_node)
+ except nx.exception.NetworkXNoPath:
+ # They did something strange, let's add a jump from start to end
+ # so at least we don't have to end with an error
+ _insert_jump(graph, graph.nodes[starting_node]['point'], graph.nodes[ending_node]['point'])
+ path = nx.shortest_path(graph, starting_node, ending_node)
+ return path
+
+
def add_jumps(graph, elements, preserve_order):
"""Add jump stitches between elements as necessary.
@@ -94,9 +105,18 @@ def _add_ordered_jumps(graph, elements):
# For each sequential pair of elements, find the shortest possible jump
# stitch between them and add it. The directions of these new edges
# will enforce stitching the elements in order.
+ last_element = None
for element1, element2 in zip(elements[:-1], elements[1:]):
+ # Try to fix missing links
+ if last_element is not None:
+ success = _insert_smallest_jump(graph, last_element, element2)
+ if success:
+ last_element = None
+
check_stop_flag()
- _insert_smallest_jump(graph, element1, element2)
+ success = _insert_smallest_jump(graph, element1, element2)
+ if not success:
+ last_element = element1
# add jumps between subpath too, we do not care about directions here
for element in elements:
@@ -130,6 +150,11 @@ def _insert_smallest_jump(graph, element1, element2):
if potential_edges:
edge = min(potential_edges, key=lambda p1_p2: p1_p2[0].distance(p1_p2[1]))
graph.add_edge(str(edge[0]), str(edge[1]), jump=True)
+ return True
+ else:
+ # return False if we haven't successfully inserted a jump
+ # so we can try again later
+ return False
def _insert_jump(graph, node1, node2):
@@ -195,6 +220,9 @@ def find_node(graph, point, extreme_function, constrain_to_satin=False, satin=No
else:
nodes = graph.nodes()
+ if not nodes:
+ return list(graph.nodes)[0]
+
if point is None:
return extreme_function(nodes, key=lambda node: graph.nodes[node]['point'].x)
else:
@@ -241,7 +269,7 @@ def create_new_group(parent, insert_index, label, correction_transform=True):
return group
-def preserve_original_groups(elements, original_parent_nodes):
+def preserve_original_groups(elements, original_parent_nodes, transform=True):
"""Ensure that all elements are contained in the original SVG group elements.
When preserve_order is True, no elements will be reordered in the XML tree.
@@ -251,9 +279,12 @@ def preserve_original_groups(elements, original_parent_nodes):
"""
for element, parent in zip(elements, original_parent_nodes):
+ transform = ''
+ if transform:
+ transform = get_correction_transform(parent, child=True)
if parent is not None:
parent.append(element.node)
- element.node.set('transform', get_correction_transform(parent, child=True))
+ element.node.set('transform', transform)
def add_elements_to_group(elements, group):