summaryrefslogtreecommitdiff
path: root/support/dxf_export/__main__.py
blob: 76c21210da7b7b53eb6f3218747371ca7be50778 (plain)
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
import sys, os, xml.etree.ElementTree, shutil
from lib import util
from . import better_dxf_outlines


def _export_dxf(in_path, out_path):
	dxf_export = better_dxf_outlines.MyEffect()
	dxf_export.affect(args = [in_path], output = False)
	
	with open(out_path, 'w') as file:
		file.write(dxf_export.dxf)


def _get_inkscape_layer_count(svg_path):
	document = xml.etree.ElementTree.parse(svg_path)
	layers = document.findall(
		'{http://www.w3.org/2000/svg}g[@{http://www.inkscape.org/namespaces/inkscape}groupmode="layer"]')
	
	return len(layers)


def _inkscape(svg_path, verbs):
	def iter_args():
		yield os.environ['INKSCAPE']
		
		for i in verbs:
			yield '--verb'
			yield i
		
		yield svg_path
	
	util.command(list(iter_args()))


def _unfuck_svg_document(temp_svg_path):
	"""
	Unfucks an SVG document so is can be processed by the better_dxf_export plugin.
	"""
	
	layers_count = _get_inkscape_layer_count(temp_svg_path)

	def iter_inkscape_verbs():
		yield 'LayerUnlockAll'
		yield 'LayerShowAll'

		# Go to the first layer
		for _ in range(layers_count):
			yield 'LayerPrev'

		for _ in range(layers_count):
			yield 'EditSelectAll'
			yield 'ObjectToPath'
			yield 'EditSelectAll'
			yield 'SelectionUnGroup'
			yield 'EditSelectAll'
			yield 'StrokeToPath'
			yield 'EditSelectAll'
			yield 'SelectionUnion'
			yield 'LayerNext'

		yield 'FileSave'
		yield 'FileClose'
		yield 'FileQuit'

	_inkscape(temp_svg_path, list(iter_inkscape_verbs()))


def main(in_path, out_path):
	with util.TemporaryDirectory() as temp_dir:
		temp_svg_path = os.path.join(temp_dir, 'temp.svg')
		
		shutil.copyfile(in_path, temp_svg_path)

		_unfuck_svg_document(temp_svg_path)
		
		_export_dxf(temp_svg_path, out_path)


main(*sys.argv[1:])