chiark / gitweb /
Add more 2D drawing code. Unused, but still, I have plans. (And all developed in...
[cura.git] / Cura / util / drawingLoader / dxf.py
1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
3
4 import math
5 import re
6 import sys
7 import numpy
8 from xml.etree import ElementTree
9
10 from Cura.util.drawingLoader import drawing
11
12 class DXF(object):
13         def __init__(self, filename):
14                 self.paths = []
15                 self._lastLine = None
16                 self._polyLine = None
17
18                 entityType = 'UNKNOWN'
19                 sectionName = 'UNKNOWN'
20                 activeObject = None
21                 f = open(filename, "r")
22                 while True:
23                         groupCode = f.readline().strip()
24                         if groupCode == '':
25                                 break
26                         groupCode = int(groupCode)
27                         value = f.readline().strip()
28                         if groupCode == 0:
29                                 if sectionName == 'ENTITIES':
30                                         self._checkForNewPath(entityType, activeObject)
31                                 entityType = value
32                                 activeObject = {}
33                         elif entityType == 'SECTION':
34                                 if groupCode == 2:
35                                         sectionName = value
36                         else:
37                                 activeObject[groupCode] = value
38                 if sectionName == 'ENTITIES':
39                         self._checkForNewPath(entityType, activeObject)
40                 f.close()
41
42                 for path in self.paths:
43                         if not path.isClosed():
44                                 path.checkClosed()
45
46         def _checkForNewPath(self, type, obj):
47                 if type == 'LINE':
48                         if self._lastLine is not None and self._lastLinePoint == complex(float(obj[10]), float(obj[20])):
49                                 self._lastLine.addLineTo(float(obj[11]), float(obj[21]))
50                         else:
51                                 p = drawing.Path(float(obj[10]), float(obj[20]))
52                                 p.addLineTo(float(obj[11]), float(obj[21]))
53                                 self.paths.append(p)
54                                 self._lastLine = p
55                         self._lastLinePoint = complex(float(obj[11]), float(obj[21]))
56                 elif type == 'POLYLINE':
57                         self._polyLine = None
58                 elif type == 'VERTEX':
59                         if self._polyLine is None:
60                                 self._polyLine = drawing.Path(float(obj[10]), float(obj[20]))
61                                 self.paths.append(self._polyLine)
62                         else:
63                                 self._polyLine.addLineTo(float(obj[10]), float(obj[20]))
64                 else:
65                         print type
66
67 if __name__ == '__main__':
68         for n in xrange(1, len(sys.argv)):
69                 print 'File: %s' % (sys.argv[n])
70                 dxf = DXF(sys.argv[n])
71
72         drawing.saveAsHtml(dxf, "test_export.html")
73