Tuesday, February 1, 2011

PreprocessGcodes.py

import sys


# Preferred Start Codes
startCode = [
'G21\n'
'G90\n'
'T0\n'
'G28 Z0\n'
'G28 X0 Y0\n'
'G92 E0\n'
]


def AppendCode(fh, codes):
   for code in codes:
      fh.write(code)




def Main(fileName):
   # Counters   
   M101Cnt = 0
   M103Cnt = 0


   # Open Input File
   try:
      fh = open(fileName, 'r')
   except:
      print 'Cannot open file: %s' % (fileName)
   
   print '\nProcessing %s' % (fileName)
   
   # Create Output Filename
   name, ext = fileName.split('.')
   name = name+'_mod'
   opFile = name+'.'+ext
   
   # Open Output File
   op = open(opFile, 'w')
   


   # Add preferred Start-up code
   AppendCode(op, startCode)


   # Remove first X lines
   # Skeinforge produces strange start-up code that isn't useful.
   X = 12
   for i in range(X):
      fh.readline()
      
   # Process remaining lines in turn
   for line in fh:
      # Remove 'M101' and 'M103' commands, Mendel doesn't use these.
      if line.find('M101')==0:
         M101Cnt +=1
         line = ''
      if line.find('M103')==0:
         M103Cnt+=1
         line = ''
      # Remove 'M113' command, stepper based extruder doesn't use this
      if line.find('M113')==0:
         M101Cnt +=1
         line = ''
         
      # Write to output file
      op.write(line)


   print 'Generated file: %s' % (opFile)
   print 
   print 'Removed %s old M101 Codes' % (M101Cnt)
   print 'Removed %s old M103 Codes' % (M103Cnt)
   
if __name__ == '__main__':
   Main(sys.argv[1])

No comments:

Post a Comment