chiark / gitweb /
Syncing drawing loaders from NinjaKittens.
[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 = 'NONE'
19                 sectionName = 'NONE'
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 entityType == 'SECTION':
30                                         sectionName = activeObject[2][0]
31                                 elif entityType == 'ENDSEC':
32                                         sectionName = 'NONE'
33                                 elif sectionName == 'ENTITIES':
34                                         self._checkForNewPath(entityType, activeObject)
35                                 elif activeObject is not None:
36                                         pass
37                                         #print sectionName, entityType, activeObject
38                                 entityType = value
39                                 activeObject = {}
40                         else:
41                                 if groupCode in activeObject:
42                                         activeObject[groupCode].append(value)
43                                 else:
44                                         activeObject[groupCode] = [value]
45                 if sectionName == 'ENTITIES':
46                         self._checkForNewPath(entityType, activeObject)
47                 f.close()
48
49                 self._postProcessPaths()
50
51         def _checkForNewPath(self, type, obj):
52                 if type == 'LINE':
53                         if self._lastLine is not None and self._lastLinePoint == complex(float(obj[10][0]), float(obj[20][0])):
54                                 self._lastLine.addLineTo(float(obj[11][0]), float(obj[21][0]))
55                         else:
56                                 p = self.addPath(float(obj[10][0]), float(obj[20][0]))
57                                 p.addLineTo(float(obj[11][0]), float(obj[21][0]))
58                                 self._lastLine = p
59                         self._lastLinePoint = complex(float(obj[11][0]), float(obj[21][0]))
60                 elif type == 'POLYLINE':
61                         self._polyLine = None
62                 elif type == 'VERTEX':
63                         if self._polyLine is None:
64                                 self._polyLine = self.addPath(float(obj[10][0]), float(obj[20][0]))
65                         else:
66                                 self._polyLine.addLineTo(float(obj[10][0]), float(obj[20][0]))
67                 elif type == 'LWPOLYLINE':
68                         p = self.addPath(float(obj[10][0]), float(obj[20][0]))
69                         for n in xrange(1, len(obj[10])):
70                                 p.addLineTo(float(obj[10][n]), float(obj[20][n]))
71                         self._lastLine = p
72                 elif type == 'SPLINE':
73                         p = self.addPath(float(obj[10][0]), float(obj[20][0]))
74                         for n in xrange(1, len(obj[10])):
75                                 p.addLineTo(float(obj[10][n]), float(obj[20][n]))
76                         self._lastLine = p
77                 else:
78                         print 'type=%s' % type
79                         for k in obj.keys():
80                                 print k, obj[k]
81
82 if __name__ == '__main__':
83         for n in xrange(1, len(sys.argv)):
84                 print 'File: %s' % (sys.argv[n])
85                 dxf = DXF(sys.argv[n])
86
87         dxf.saveAsHtml("test_export.html")
88