summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--lib/debug.py42
-rw-r--r--lib/svg/__init__.py4
-rw-r--r--lib/svg/path.py9
4 files changed, 54 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 08d9f279..79b25b6e 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 33d30d4f..05c367f0 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 df76c0d2..34cc4b3d 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 d2b4aee1..f0f6708b 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)
+ })