#!/usr/local/bin/python # -*- coding: UTF-8 -*- import sys import math # Class to contain point and comment data scraped from the input file class point(object): def __init__(self, comment, x, y): self.comment = comment self.x = float(x) self.y = float(y) # Simple class to contain a list of points (ealier than managing lists of lists) class shape(object): def __init__(self): self.points = [] # Function to output a new pair of coordinates given an existing point and the point which you want the new point to # move towards by 1mm. If the the points are not on the X or Y line, it returns the existing point coordinates def makepoint(current_x, current_y, dir_x, dir_y): if current_x == dir_x: new_x = current_x if dir_y > current_y: new_y = current_y + 1 else: new_y = current_y - 1 # if we are in inline in Y, move in X elif current_y == dir_y: new_y = current_y if dir_x > current_x: new_x = current_x + 1 else: new_x = current_x - 1 else: #not 90 degree, do not miter! new_x = current_x new_y = current_y return (new_x, new_y) # Make sure we only have one input file if len(sys.argv) != 2: print('Error! Specify only one file name.') print('Detected arguments: ' + str(sys.argv[1:])) sys.exit() input_file = str(sys.argv[1:]).strip('[]\'') print('Processing file: ', input_file) output_file = input_file + '_mitered' f = open(input_file, 'r') f_contents = f.readlines() thisshape = 0 shapelist = [] shapelist.append([]) # prescan the file to read in the points data for line in f_contents: if line.strip().startswith("$$initial"): # New shape detected shapelist.append([]) # Create a new slot in the shape list this_x = line.split(',')[0].split('[')[1].strip() this_y = line.split(',')[1].split(']')[0].strip() if '//' in line: # Find any comments ind = line.find('//') comment = line[ind:] else: comment = 'NULL' shapelist[thisshape].append(point(comment, this_x, this_y)) elif line.strip().startswith("$$terminal"): # Point within a shape detected this_x = line.split(',')[0].split('[')[1].strip() this_y = line.split(',')[1].split(']')[0].strip() if '//' in line: # Find any comments ind = line.find('//') comment = line[ind:] else: comment = 'NULL' shapelist[thisshape].append(point(comment, this_x, this_y)) elif line.strip().startswith("$$path"): # End of shape detected thisshape += 1 # increment shape index out = open(output_file, 'w') # Step through the file and write a new file thisshape = 0 for line in f_contents: if line.strip().startswith("//"): # Ignore lines that are commented out out.write(line) elif not ('NO_MITER' in line): # Ignore lines flagged with NO_MITER and commented lines if line.strip().startswith("$$initial"): thispoint = 0 old_x = shapelist[thisshape][thispoint].x old_y = shapelist[thisshape][thispoint].y last_x = shapelist[thisshape][-2].x # Last point is same as first point, second to last is prior point in the shape last_y = shapelist[thisshape][-2].y next_x = shapelist[thisshape][thispoint+1].x next_y = shapelist[thisshape][thispoint+1].y # Create new point in the direction of the last point in the shape (new_x, new_y) = makepoint(old_x, old_y, last_x, last_y) output = '$$initial([ ' + str(new_x) + ', ' + str(new_y) + '], , @nosnap ); ' + shapelist[thisshape][thispoint].comment out.write(output) if not ((new_x == old_x) and (new_y == old_y)): # If we did not find a non-90° corner, create the second point (new_x, new_y) = makepoint(old_x, old_y, next_x, next_y) if not ((new_x == old_x) and (new_y == old_y)): # Check again for 90° corner with next point output = '$$terminal([ ' + str(new_x) + ', ' + str(new_y) + ' ] ); ' out.write(output) elif line.strip().startswith("$$terminal"): thispoint += 1 if not (thispoint >= len(shapelist[thisshape])-1): old_x = shapelist[thisshape][thispoint].x old_y = shapelist[thisshape][thispoint].y last_x = shapelist[thisshape][thispoint - 1].x # Last point is same as first point, second to last is prior point last_y = shapelist[thisshape][thispoint - 1].y next_x = shapelist[thisshape][thispoint + 1].x next_y = shapelist[thisshape][thispoint + 1].y (new_x, new_y) = makepoint(old_x, old_y, last_x, last_y) output = '$$terminal([ ' + str(new_x) + ', ' + str(new_y) + ' ] ); ' + shapelist[thisshape][thispoint].comment out.write(output) if not ((new_x == old_x) and (new_y == old_y)): # If we did not find a non-90° corner, create the second point (new_x, new_y) = makepoint(old_x, old_y, next_x, next_y) if not ((new_x == old_x) and (new_y == old_y)): # Check again for 90° corner with next point output = '$$terminal([ ' + str(new_x) + ', ' + str(new_y) + ' ] ); ' out.write(output) else: # we are at the last point old_x = shapelist[thisshape][thispoint].x old_y = shapelist[thisshape][thispoint].y last_x = shapelist[thisshape][thispoint - 1].x # Last point is same as first point, second to last is prior point last_y = shapelist[thisshape][thispoint - 1].y next_x = shapelist[thisshape][1].x next_y = shapelist[thisshape][1].y (new_x, new_y) = makepoint(old_x, old_y, last_x, last_y) output = '$$terminal([ ' + str(new_x) + ', ' + str(new_y) + ' ] ); ' + shapelist[thisshape][ thispoint].comment out.write(output) elif line.strip().startswith("$$path"): out.write(line) # Write out end of shape lines thisshape += 1 else: out.write(line) # Write lines containing no points else: if line.strip().startswith("$$initial"): # in case the first line is NO_MITER we still need to init thispoint thispoint = 0 elif line.strip().startswith("$$terminal"): thispoint += 1 out.write(line) # Write NO_MITER lines and commented lines out.write('\n') # Close the files! f.close() out.close()