1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
"""
Based on code from Aaron Spike. See http://www.bobcookdev.com/inkscape/inkscape-dxf.html
"""
import pkgutil
from . import inkex, simpletransform, cubicsuperpath, cspsubdiv
class DXFExportEffect(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self._dxf_instructions = []
self._handle = 255
self._flatness = 0.1
def _add_instruction(self, code, value):
self._dxf_instructions.append((code, str(value)))
def _add_dxf_line(self, layer, csp):
self._add_instruction(0, 'LINE')
self._add_instruction(8, layer)
self._add_instruction(62, 4)
self._add_instruction(5, '{:x}'.format(self._handle))
self._add_instruction(100, 'AcDbEntity')
self._add_instruction(100, 'AcDbLine')
self._add_instruction(10, repr(csp[0][0]))
self._add_instruction(20, repr(csp[0][1]))
self._add_instruction(30, 0.0)
self._add_instruction(11, repr(csp[1][0]))
self._add_instruction(21, repr(csp[1][1]))
self._add_instruction(31, 0.0)
def _add_dxf_path(self, layer, path):
cspsubdiv.cspsubdiv(path, self._flatness)
for sub in path:
for i in range(len(sub) - 1):
self._handle += 1
s = sub[i]
e = sub[i + 1]
self._add_dxf_line(layer, [s[1], e[1]])
def _add_dxf_shape(self, node, document_transformation):
layer = self._get_inkscape_layer(node)
path = cubicsuperpath.parsePath(node.get('d'))
transformation = simpletransform.composeTransform(
document_transformation,
simpletransform.composeParents(node, [[1, 0, 0], [0, 1, 0]]))
simpletransform.applyTransformToPath(transformation, path)
self._add_dxf_path(layer, path)
def effect(self):
height = self.unittouu(self.document.getroot().xpath('@height', namespaces = inkex.NSS)[0])
document_transformation = [[1, 0, 0], [0, -1, height]]
for node in self.document.getroot().xpath('//svg:path', namespaces = inkex.NSS):
self._add_dxf_shape(node, document_transformation)
def write(self, file):
file.write(pkgutil.get_data(__name__, 'dxf_header.txt'))
for code, value in self._dxf_instructions:
print >> file, code
print >> file, value
file.write(pkgutil.get_data(__name__, 'dxf_footer.txt'))
@classmethod
def _get_inkscape_layer(cls, node):
while node is not None:
layer = node.get(inkex.addNS('label', 'inkscape'))
if layer is not None:
return layer
node = node.getparent()
return ''
|