import re
g0pattern = r"^G0.*"
g1pattern = r"^G1.*"
g0re = re.compile(g0pattern)
g1re = re.compile(g1pattern)

Xpattern = r".*X[-\.\d]+"
Ypattern = r".*Y[-\.\d]+"
Zpattern = r".*Z[-\.\d]+"
Xre = re.compile(Xpattern)
Yre = re.compile(Ypattern)
Zre = re.compile(Zpattern)

currentX = 0
currentY = 0

fout = open('out8.csv','w')
should_update = False
with open('star.gcode') as f:
    lines = f.readlines()
    for line in lines:
        
        if g1re.match(line):
            print(line)
            if Xre.match(line): 
                should_update = True
                nextX = re.findall(Xpattern,line)[0].split('X')[1]
                if currentX != nextX:
                    currentX = nextX
            if Yre.match(line): 
                should_update = True
                nextY = re.findall(Ypattern,line)[0].split('Y')[1]
                if currentY != nextY:
                    currentY = nextY
           
            if should_update:
                currentX=int(float(currentX) // 1)
                
                currentY=int(float(currentY) // 1)
               
                fout.write("{0},{1},".format(currentX-100,currentY-100,","))
                should_update = False
fout.close()  
