Skip to content

Commit 4cac71f

Browse files
committed
Added ability to move origin, as well as code that detects and strips out arc commands
1 parent 8a8893b commit 4cac71f

1 file changed

Lines changed: 52 additions & 23 deletions

File tree

aerocode.py

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,76 @@
44
import sys
55
import argparse
66

7-
def findkey(lines,line):
7+
8+
def findkey(lines, line):
89
codekey = lines[line].find('G')
910
if codekey == -1:
10-
codekey = findkey(lines,line + 1)
11+
codekey = findkey(lines, line + 1)
1112
return codekey
1213

1314

1415
def openfile(filename):
1516
print 'Opening ', filename
16-
ocode = open(filename,'r')
17+
ocode = open(filename, 'r')
1718
lines = ocode.readlines()
18-
codekey = findkey(lines,0)
19+
codekey = findkey(lines, 0)
1920
coords = []
2021
for line in lines:
21-
if line[codekey:codekey+2] == 'G0':
22-
line = line[0:codekey-1] + line[codekey+3:]
23-
coords.append(line[0:len(line)-1])
24-
elif line[codekey:codekey+1] == 'X' or line[codekey:codekey+1] == 'Y':
25-
coords.append(line[0:len(line)-1])
22+
if line[codekey:codekey + 2] == 'G0':
23+
line = line[0:codekey - 1] + line[codekey + 3:]
24+
coords.append(line[0:len(line) - 1])
25+
elif line[codekey:codekey + 1] == 'X' or line[codekey:codekey + 1] == 'Y':
26+
coords.append(line[0:-1])
2627
ocode.close()
2728
return(coords)
2829

29-
def double(coords):
30+
31+
def double(coords, neworigin):
3032
xp = '0.00000'
3133
yp = '0.00000'
32-
for i in range(0,len(coords)):
34+
for i in range(len(coords)):
3335
line = coords[i]
3436
xi = line.find('X')
3537
yi = line.find('Y')
38+
ii = line.find('I')
39+
ji = line.find('J')
40+
if ii > -1 or ji > -1:
41+
print "Warning: Invalid arc found on line {0}. Converted to line".format(i)
42+
line_trunc = 0
43+
if ii == -1:
44+
line_trunc = -(len(line) - ji)
45+
elif ji == -1:
46+
line_trunc = -(len(line) - ii)
47+
elif ii < ji:
48+
line_trunc = -(len(line) - ii)
49+
else:
50+
line_trunc = -(len(line) - ji)
51+
line = line[:line_trunc]
3652
beg = 0
3753
if xi != -1:
3854
beg = xi
3955
if yi != -1:
40-
x = line[xi+1:yi-1]
41-
y = line[yi+1:]
56+
x = line[xi + 1:yi - 1]
57+
y = line[yi + 1:]
4258
else:
43-
x = line[xi+1:]
59+
x = line[xi + 1:]
4460
y = yp
4561
else:
4662
beg = yi
4763
if yi != -1:
48-
y = line[yi+1:]
64+
y = line[yi + 1:]
4965
x = xp
50-
line = line[0:beg] + 'G01' + ' X' + x + ' Y' + y + ' A' + x + ' B' + y
66+
line = line[0:beg] + 'G01' + ' X' + str(float(x) - neworigin[0]) + \
67+
' Y' + str(float(y) - neworigin[1]) + ' A' + str(
68+
float(x) - neworigin[0]) + ' B' + str(float(y) - neworigin[1])
5169
xp = x
5270
yp = y
5371
coords[i] = line
5472
return coords
55-
56-
def writefile(coords,filename):
57-
output = open(filename + '.txt','w')
73+
74+
75+
def writefile(coords, filename):
76+
output = open(filename + '.txt', 'w')
5877
output.write('%\n')
5978
output.write('N00000 G92 G70 X0 Y0 A0 B0\n')
6079
for line in coords:
@@ -63,7 +82,17 @@ def writefile(coords,filename):
6382
output.close()
6483

6584
if __name__ == '__main__':
66-
for filename in sys.argv:
67-
coords = openfile(filename)
68-
coords = double(coords)
69-
writefile(coords,filename)
85+
print len(sys.argv)
86+
if len(sys.argv) <= 1:
87+
print "Not enough arguments given!"
88+
sys.exit(1)
89+
if len(sys.argv) == 4:
90+
x = float(sys.argv[2])
91+
y = float(sys.argv[3])
92+
else:
93+
x = 0
94+
y = 0
95+
filename = sys.argv[1]
96+
coords = openfile(filename)
97+
coords = double(coords, [x, y])
98+
writefile(coords, filename)

0 commit comments

Comments
 (0)