import sys
import os.path
import re

# define some extruding macros
jog_speed = 12
jog_height = 1
end_height = 5
extrude_height = 0

# define the relation of thickness to motor speed 
def speed (thickness):
  return 12 - thickness


# define the coordinate transform from GUI (pixels) to machine (mm)
# also change from string to floats
def transform (curves, cake_dimater):
  # store all points in two arrays
  points_x = []
  points_y = []
  for i in range (len (curves)):
    for j in range (len (curves[i])):
      points_x.append (curves[i][j][0])
      points_y.append (curves[i][j][1])


  return curves

# MAKE CURVES INTO TOOLPATH:
# SYNTAX: [x,x,y,z,Speed,Extrude] with Extrude a boolean
# assumes we are at zero
def make_toolpath (curves, curves_thick):
  toolpath = []
  # add points to the toolpath
  # first lift off the cake
  toolpath.append ([0, 0, 0, jog_height, jog_speed, 0])
  # then iterate
  for i in range (len (curves)):
    # here we are between curves
    for j in range (len (curves[i])):
      # here are between points
      # if first point just go and lower there
      if j == 0:
        toolpath.append ([curves[i][j][0], curves[i][j][0], curves[i][j][1], 
          jog_height, jog_speed, 0])
        toolpath.append ([curves[i][j][0], curves[i][j][0], curves[i][j][1], 
          extrude_height, jog_speed, 0])
      # go through the full curve
      else: 
        toolpath.append ([curves[i][j][0], curves[i][j][0], curves[i][j][1], 
          extrude_height, speed(curves_thick[i]), 1])
      # if at end, lift nosel
      if j + 1 == len (curves[i]):
        toolpath.append ([curves[i][j][0], curves[i][j][0], curves[i][j][1], 
          jog_height, jog_speed, 0])
  # at the end return to 0
  toolpath.append ([0, 0, 0, end_height, jog_speed, 0])
  return toolpath



# find text file name
if len(sys.argv) != 2:
  txt_path = "./" + sys.argv[1]
else:
  sys.exit("usage: python script.py input.txt cake_diameter")

# verify existence
if not os.path.isfile(txt_path):
  sys.exit(txt_path + " is not a file")

# read
text_file = open(txt_path, "r")
lines = text_file.readlines()
if len(lines) > 1:
  sys.exit("invalid file format: must contain only one line")

# PARSE
# find each curve descriptor of the form [thickness, [[x_1,y_1], ...]]
curves_string = re.findall("\[\d+,[ ]*(?:\[\d+[.]\d*,[ ]*\d+[.]\d*\][ ,]*)+\]", lines[0])

# lists to fill 
curves_thick = []
curves =  []
curve = []

# each curve descriptor has thickness and point extracted
# then all is compiled in a list
for curve_string in curves_string:
  thickness = re.search("\d+", curve_string).group()
  curves_thick.append (thickness)
  string_points = re.findall("\[\d+[.]\d*,[ ]*\d+[.]\d*\]", curve_string)

  for string_point in string_points:
    curve.append (re.findall("\d+[.]\d*", string_point))

  curves.append (list (curve))
  del curve[:]

# change all strings to numbers
curves_thick = [float(i) for i in curves_thick]
for i in range (len (curves)):
  for j in range (len (curves[i])):
    for k in range (len (curves[i][j])):
      curves[i][j][k] = float (curves[i][j][k])

# transform the coordinates
curves = transform (curves)      

# MAKE CURVES INTO TOOLPATH:
# SYNTAX: [x,x,y,z,Speed,Extrude] with Extrude a boolean
# assumes we are at zero
toolpath = make_toolpath (curves, curves_thick)

print toolpath







