chiark / gitweb /
5f9fe0b7a59505f864b075dc935d9e5ec5f965af
[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(drawing.Drawing):
13         def __init__(self, filename):
14                 super(DXF, self).__init__()
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                 self._postProcessPaths()
43
44         def _checkForNewPath(self, type, obj):
45                 if type == 'LINE':
46                         if self._lastLine is not None and self._lastLinePoint == complex(float(obj[10]), float(obj[20])):
47                                 self._lastLine.addLineTo(float(obj[11]), float(obj[21]))
48                         else:
49                                 p = self.addPath(float(obj[10]), float(obj[20]))
50                                 p.addLineTo(float(obj[11]), float(obj[21]))
51                                 self._lastLine = p
52                         self._lastLinePoint = complex(float(obj[11]), float(obj[21]))
53                 elif type == 'POLYLINE':
54                         self._polyLine = None
55                 elif type == 'VERTEX':
56                         if self._polyLine is None:
57                                 self._polyLine = self.addPath(float(obj[10]), float(obj[20]))
58                         else:
59                                 self._polyLine.addLineTo(float(obj[10]), float(obj[20]))
60                 else:
61                         print type
62
63 if __name__ == '__main__':
64         for n in xrange(1, len(sys.argv)):
65                 print 'File: %s' % (sys.argv[n])
66                 dxf = DXF(sys.argv[n])
67
68         dxf.saveAsHtml("test_export.html")
69