-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpgl2gpgl.py
executable file
·98 lines (87 loc) · 1.93 KB
/
hpgl2gpgl.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
import sys
import time
fIn = open(sys.argv[1],"r")
fOut = open(sys.argv[2],"w")
try:
speed = sys.argv[3]
except:
speed = 2
line = fIn.read().splitlines()
commands = []
for l in line:
for c in l.replace(' ', ';').split(';'):
if c != '':
commands.append(c)
commandsMissing = []
print("Reading file: {}".format(sys.argv[1]))
print("Now processing {} lines of HPGL to GPGL".format(len(commands)-1))
mode = 0 # 0 = absolute, 1 = relative
out = "" # output string, written to file
for index,c in enumerate(commands[:len(commands)-1]):
if c[:2] == "IN": # init
out += '\x1B'
out += '\x03'
out += "FU3564,5268"
out += '\x03'
out += "FM1"
out += '\x03'
out += "TB50,1"
out += '\x03'
out += "FO3564"
out += '\x03'
out += "&100,100,100,\0,0,Z5588,4064,L0,!{}.0".format(speed)
out += '\x03'
out += "FX8,0"
out += '\x03'
elif c[:2] == "PD": # pen down → draw
if mode == 0: # draw in relative
out += "D"
else:
out += "E"
for sc in c[2:]:
out += sc
out += '\x03'
elif c[:2] == "PA": # plot absolute
mode = 0 # switch to absolute mode
if len(c) > 2:
out += "M"
for sc in c[2:]:
out += sc
out += '\x03'
elif c[:2] == "PU":
if mode == 0:
out += "M"
else:
out += "O"
out += c[2:]
out += '\x03'
elif c[:2] == "SP":
out += "j"
out += c[2:]
out += '\x03'
elif c[:2] == "VS":
out += "!"
out += c[2:]
out += '\x03'
elif c[:2] == "PR": # switch to relative movement
mode = 1
if len(c) > 2:
out += "O"
for sc in c[2:]:
out += sc
out += '\x03'
else:
commandsMissing.append(c)
pass
print("{}%".format((int)(((index+1)/(len(commands)-1))*100)), end='\r', flush=True)
out += "&1,1,1,TB50,0\x03"
out += "FO0\x03" # feed the page out
out += "H," # halt
fOut.write(out)
print()
print("Written file: {}".format(sys.argv[2]))
if len(commandsMissing) > 0:
print("The following commands where missing")
print(commandsMissing)
exit(0)