-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVG_path_multiplier.py
51 lines (37 loc) · 1.59 KB
/
SVG_path_multiplier.py
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
import sys
import xml.dom.minidom
if len(sys.argv) != 3:
print("Usage: python SVG_path_multiplier.py <filename> <pass count>")
print()
print("\tOutput file will be saved as the original filename")
print("\tprepended with 'x-passes_', where x is the number of")
print("\tpasses specified.")
print()
print("\tThis program duplicates each path the specified number")
print("\tof times, group duplicate paths together. When used for")
print("\tlaser cutting with programs like LaserGRBL, this will")
print("\tcause each path to be cut in multiple subsequent passes,")
print("\tbefore moving on to the next path. In most instances,")
print("\tthis will produce superior cutting results compared to")
print("\tdoing the entire cut program in each pass before moving")
print("\tto the next pass, because far less time will be allowed")
print("\tto pass between passes, giving the media less time to")
print("\tcool down and absorb moisture from the air (in case of")
print("\tof wood) between passes.")
print()
sys.exit()
svg_file = sys.argv[1]
passes = int(sys.argv[2])
svg_dom = xml.dom.minidom.parse(svg_file)
paths = svg_dom.getElementsByTagName("path")
for path in paths:
group = svg_dom.createElement("g")
group.setAttribute("id", "g" + path.getAttribute("id"))
for i in range(passes):
new_path = path.cloneNode(True)
new_path.setAttribute("id", new_path.getAttribute("id") + str(1000 + i))
group.appendChild(new_path)
path.parentNode.insertBefore(group, path)
path.parentNode.removeChild(path)
with open("{}-pass_{}".format(passes, svg_file), "w") as outfile:
svg_dom.writexml(outfile)