From 19950150210e5a3d7a76881b96e9b8b49e663309 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Thu, 28 Mar 2019 15:52:37 -0400 Subject: [PATCH] add SVG debugging with LineStrings --- .gitignore | 1 + lib/debug.py | 42 ++++++++++++++++++++++++++++++++++++++++++ lib/svg/__init__.py | 4 ++-- lib/svg/path.py | 9 +++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 08d9f2794..79b25b6e5 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ messages.po .pydevproject .project /debug.log +/debug.svg diff --git a/lib/debug.py b/lib/debug.py index 33d30d4fc..05c367f0c 100644 --- a/lib/debug.py +++ b/lib/debug.py @@ -1,17 +1,27 @@ +import atexit from datetime import datetime import os import socket import sys import time +from inkex import etree +import inkex +from simplestyle import formatStyle + +from svg import line_strings_to_path +from svg.tags import INKSCAPE_GROUPMODE, INKSCAPE_LABEL + class Debug(object): def __init__(self): self.last_log_time = None + self.current_layer = None def enable(self): self.enable_log() self.enable_debugger() + self.enable_svg() def enable_log(self): self.log = self._log @@ -48,6 +58,23 @@ class Debug(object): sys.stderr = stderr + def enable_svg(self): + self.svg = etree.Element("svg", nsmap=inkex.NSS) + atexit.register(self.save_svg) + + def save_svg(self): + tree = etree.ElementTree(self.svg) + with open(os.path.join(os.path.dirname(os.path.dirname(__file__)), "debug.svg"), "w") as debug_svg: + tree.write(debug_svg) + + def add_layer(self, name="Debug"): + layer = etree.Element("g", { + INKSCAPE_GROUPMODE: "layer", + INKSCAPE_LABEL: name + }) + self.svg.append(layer) + self.current_layer = layer + def _noop(self, *args, **kwargs): pass @@ -79,6 +106,21 @@ class Debug(object): return decorated + def log_svg_element(self, element): + if self.current_layer is None: + self.add_layer() + + self.current_layer.append(element) + + def log_line_string(self, line_string, color="#000000"): + """Add a Shapely LineString to the SVG log.""" + self.log_line_strings(self, [line_string]) + + def log_line_strings(self, line_strings, color="#000000"): + path = line_strings_to_path(line_strings) + path.set('style', formatStyle({"stroke": color, "stroke-width": "0.3"})) + self.log_svg_element(path) + debug = Debug() enable = debug.enable diff --git a/lib/svg/__init__.py b/lib/svg/__init__.py index df76c0d25..34cc4b3d7 100644 --- a/lib/svg/__init__.py +++ b/lib/svg/__init__.py @@ -1,4 +1,4 @@ +from .guides import get_guides +from .path import apply_transforms, get_node_transform, get_correction_transform, line_strings_to_csp, point_lists_to_csp, line_strings_to_path from .svg import color_block_to_point_lists, render_stitch_plan from .units import * -from .path import apply_transforms, get_node_transform, get_correction_transform, line_strings_to_csp, point_lists_to_csp -from .guides import get_guides diff --git a/lib/svg/path.py b/lib/svg/path.py index d2b4aee1c..f0f6708bf 100644 --- a/lib/svg/path.py +++ b/lib/svg/path.py @@ -1,3 +1,4 @@ +import cubicsuperpath import inkex import simpletransform @@ -80,3 +81,11 @@ def point_lists_to_csp(point_lists): csp.append(subpath) return csp + + +def line_strings_to_path(line_strings): + csp = line_strings_to_csp(line_strings) + + return inkex.etree.Element("path", { + "d": cubicsuperpath.formatPath(csp) + })