chiark / gitweb /
Some minor drawing cleanup (I know I know. Still unused.)
authordaid <daid303@gmail.com>
Mon, 7 Oct 2013 06:15:57 +0000 (08:15 +0200)
committerdaid <daid303@gmail.com>
Mon, 7 Oct 2013 06:15:57 +0000 (08:15 +0200)
Cura/util/drawingLoader/drawing.py
Cura/util/drawingLoader/dxf.py
Cura/util/drawingLoader/svg.py

index d9822f2909e0c801ddabdee094b106680d2cdf3b..0a20693c8bb4c39b7bc5b12147f67d71ddbc45cc 100644 (file)
@@ -4,6 +4,55 @@ __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AG
 import math
 import numpy
 
+class Drawing(object):
+       def __init__(self):
+               self.paths = []
+
+       def addPath(self, x, y, matrix=numpy.matrix(numpy.identity(3, numpy.float64))):
+               p = Path(x, y, matrix)
+               self.paths.append(p)
+               return p
+
+       def saveAsHtml(self, filename):
+               f = open(filename, "w")
+
+               posMax = complex(-1000, -1000)
+               posMin = complex( 1000,  1000)
+               for path in self.paths:
+                       points = path.getPoints()
+                       for p in points:
+                               if p.real > posMax.real:
+                                       posMax = complex(p.real, posMax.imag)
+                               if p.imag > posMax.imag:
+                                       posMax = complex(posMax.real, p.imag)
+                               if p.real < posMin.real:
+                                       posMin = complex(p.real, posMin.imag)
+                               if p.imag < posMin.imag:
+                                       posMin = complex(posMin.real, p.imag)
+
+               f.write("<!DOCTYPE html><html><body>\n")
+               f.write("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" style='width:%dpx;height:%dpx'>\n" % ((posMax - posMin).real, (posMax - posMin).imag))
+               f.write("<g fill-rule='evenodd' style=\"fill: gray; stroke:black;stroke-width:2\">\n")
+               f.write("<path d=\"")
+               for path in self.paths:
+                       points = path.getPoints()
+                       f.write("M %f %f " % (points[0].real - posMin.real, points[0].imag - posMin.imag))
+                       for point in points[1:]:
+                               f.write("L %f %f " % (point.real - posMin.real, point.imag - posMin.imag))
+               f.write("\"/>")
+               f.write("</g>\n")
+
+               f.write("<g style=\"fill: none; stroke:red;stroke-width:1\">\n")
+               f.write("<path d=\"")
+               for path in self.paths:
+                       f.write(path.getSVGPath())
+               f.write("\"/>")
+               f.write("</g>\n")
+
+               f.write("</svg>\n")
+               f.write("</body></html>")
+               f.close()
+
 class Path(object):
        LINE = 0
        ARC = 1
@@ -142,43 +191,3 @@ class Path(object):
        def _m(self, p):
                tmp = numpy.matrix([p.real, p.imag, 1], numpy.float64) * self._matrix
                return complex(tmp[0,0], tmp[0,1])
-
-def saveAsHtml(paths, filename):
-       f = open(filename, "w")
-
-       posMax = complex(-1000, -1000)
-       posMin = complex( 1000,  1000)
-       for path in paths.paths:
-               points = path.getPoints()
-               for p in points:
-                       if p.real > posMax.real:
-                               posMax = complex(p.real, posMax.imag)
-                       if p.imag > posMax.imag:
-                               posMax = complex(posMax.real, p.imag)
-                       if p.real < posMin.real:
-                               posMin = complex(p.real, posMin.imag)
-                       if p.imag < posMin.imag:
-                               posMin = complex(posMin.real, p.imag)
-
-       f.write("<!DOCTYPE html><html><body>\n")
-       f.write("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" style='width:%dpx;height:%dpx'>\n" % ((posMax - posMin).real, (posMax - posMin).imag))
-       f.write("<g fill-rule='evenodd' style=\"fill: gray; stroke:black;stroke-width:2\">\n")
-       f.write("<path d=\"")
-       for path in paths.paths:
-               points = path.getPoints()
-               f.write("M %f %f " % (points[0].real - posMin.real, points[0].imag - posMin.imag))
-               for point in points[1:]:
-                       f.write("L %f %f " % (point.real - posMin.real, point.imag - posMin.imag))
-       f.write("\"/>")
-       f.write("</g>\n")
-
-       f.write("<g style=\"fill: none; stroke:red;stroke-width:1\">\n")
-       f.write("<path d=\"")
-       for path in paths.paths:
-               f.write(path.getSVGPath())
-       f.write("\"/>")
-       f.write("</g>\n")
-
-       f.write("</svg>\n")
-       f.write("</body></html>")
-       f.close()
index 3915572b3c1483031227da42bcfadcf2ed19c9dc..cce9dc9deb9023f98fe10f2b826c7a69e2e504af 100644 (file)
@@ -9,9 +9,9 @@ from xml.etree import ElementTree
 
 from Cura.util.drawingLoader import drawing
 
-class DXF(object):
+class DXF(drawing.Drawing):
        def __init__(self, filename):
-               self.paths = []
+               super(DXF, self).__init__()
                self._lastLine = None
                self._polyLine = None
 
@@ -48,17 +48,15 @@ class DXF(object):
                        if self._lastLine is not None and self._lastLinePoint == complex(float(obj[10]), float(obj[20])):
                                self._lastLine.addLineTo(float(obj[11]), float(obj[21]))
                        else:
-                               p = drawing.Path(float(obj[10]), float(obj[20]))
+                               p = self.addPath(float(obj[10]), float(obj[20]))
                                p.addLineTo(float(obj[11]), float(obj[21]))
-                               self.paths.append(p)
                                self._lastLine = p
                        self._lastLinePoint = complex(float(obj[11]), float(obj[21]))
                elif type == 'POLYLINE':
                        self._polyLine = None
                elif type == 'VERTEX':
                        if self._polyLine is None:
-                               self._polyLine = drawing.Path(float(obj[10]), float(obj[20]))
-                               self.paths.append(self._polyLine)
+                               self._polyLine = self.addPath(float(obj[10]), float(obj[20]))
                        else:
                                self._polyLine.addLineTo(float(obj[10]), float(obj[20]))
                else:
@@ -69,5 +67,5 @@ if __name__ == '__main__':
                print 'File: %s' % (sys.argv[n])
                dxf = DXF(sys.argv[n])
 
-       drawing.saveAsHtml(dxf, "test_export.html")
+       dxf.saveAsHtml("test_export.html")
 
index e57b35333ace4ea8664f921c016124ed2bca4bff..adf6218c54458e0dd5a1fb95c5b5e8215fdb9dcb 100644 (file)
@@ -52,8 +52,9 @@ def toFloat(f):
        f = re.search('^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?', f).group(0)
        return float(f)
 
-class SVG(object):
+class SVG(drawing.Drawing):
        def __init__(self, filename):
+               super(SVG, self).__init__()
                self.tagProcess = {}
                self.tagProcess['rect'] = self._processRectTag
                self.tagProcess['line'] = self._processLineTag
@@ -90,7 +91,6 @@ class SVG(object):
                #From w3c testsuite
                self.tagProcess['SVGTestCase'] = None
 
-               self.paths = []
                f = open(filename, "r")
                self._xml = ElementTree.parse(f)
                self._recursiveCount = 0
@@ -137,43 +137,38 @@ class SVG(object):
                y1 = toFloat(tag.get('y1', '0'))
                x2 = toFloat(tag.get('x2', '0'))
                y2 = toFloat(tag.get('y2', '0'))
-               p = drawing.Path(x1, y1, matrix)
+               p = self.addPath(x1, y1, matrix)
                p.addLineTo(x2, y2)
-               self.paths.append(p)
 
        def _processPolylineTag(self, tag, matrix):
                values = map(toFloat, re.split('[, \t]+', tag.get('points', '').strip()))
-               p = drawing.Path(values[0], values[1], matrix)
+               p = self.addPath(values[0], values[1], matrix)
                for n in xrange(2, len(values)-1, 2):
                        p.addLineTo(values[n], values[n+1])
-               self.paths.append(p)
 
        def _processPolygonTag(self, tag, matrix):
                values = map(toFloat, re.split('[, \t]+', tag.get('points', '').strip()))
-               p = drawing.Path(values[0], values[1], matrix)
+               p = self.addPath(values[0], values[1], matrix)
                for n in xrange(2, len(values)-1, 2):
                        p.addLineTo(values[n], values[n+1])
                p.closePath()
-               self.paths.append(p)
 
        def _processCircleTag(self, tag, matrix):
                cx = toFloat(tag.get('cx', '0'))
                cy = toFloat(tag.get('cy', '0'))
                r = toFloat(tag.get('r', '0'))
-               p = drawing.Path(cx-r, cy, matrix)
+               p = self.addPath(cx-r, cy, matrix)
                p.addArcTo(cx+r, cy, 0, r, r, False, False)
                p.addArcTo(cx-r, cy, 0, r, r, False, False)
-               self.paths.append(p)
 
        def _processEllipseTag(self, tag, matrix):
                cx = toFloat(tag.get('cx', '0'))
                cy = toFloat(tag.get('cy', '0'))
                rx = toFloat(tag.get('rx', '0'))
                ry = toFloat(tag.get('rx', '0'))
-               p = drawing.Path(cx-rx, cy, matrix)
+               p = self.addPath(cx-rx, cy, matrix)
                p.addArcTo(cx+rx, cy, 0, rx, ry, False, False)
                p.addArcTo(cx-rx, cy, 0, rx, ry, False, False)
-               self.paths.append(p)
 
        def _processRectTag(self, tag, matrix):
                x = toFloat(tag.get('x', '0'))
@@ -200,7 +195,7 @@ class SVG(object):
                        ry = 0.0
 
                if rx > 0 and ry > 0:
-                       p = drawing.Path(x+width-rx, y, matrix)
+                       p = self.addPath(x+width-rx, y, matrix)
                        p.addArcTo(x+width,y+ry, 0, rx, ry, False, True)
                        p.addLineTo(x+width, y+height-ry)
                        p.addArcTo(x+width-rx,y+height, 0, rx, ry, False, True)
@@ -209,14 +204,12 @@ class SVG(object):
                        p.addLineTo(x, y+ry)
                        p.addArcTo(x+rx,y, 0, rx, ry, False, True)
                        p.closePath()
-                       self.paths.append(p)
                else:
-                       p = drawing.Path(x, y, matrix)
+                       p = self.addPath(x, y, matrix)
                        p.addLineTo(x,y+height)
                        p.addLineTo(x+width,y+height)
                        p.addLineTo(x+width,y)
                        p.closePath()
-                       self.paths.append(p)
 
        def _processPathTag(self, tag, matrix):
                pathString = tag.get('d', '').replace(',', ' ')
@@ -237,8 +230,7 @@ class SVG(object):
                        if command == 'm':
                                x += params[0]
                                y += params[1]
-                               path = drawing.Path(x, y, matrix)
-                               self.paths.append(path)
+                               path = self.addPath(x, y, matrix)
                                params = params[2:]
                                while len(params) > 1:
                                        x += params[0]
@@ -249,8 +241,7 @@ class SVG(object):
                        elif command == 'M':
                                x = params[0]
                                y = params[1]
-                               path = drawing.Path(x, y, matrix)
-                               self.paths.append(path)
+                               path = self.addPath(x, y, matrix)
                                params = params[2:]
                                while len(params) > 1:
                                        x = params[0]
@@ -395,4 +386,4 @@ if __name__ == '__main__':
                print 'File: %s' % (sys.argv[n])
                svg = SVG(sys.argv[n])
 
-       drawing.saveAsHtml(svg, "test_export.html")
+       svg.saveAsHtml("test_export.html")