bobstro

Python script to embed min/max XY movements into gcode

Mar 27th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. #!/usr/bin/python
  2. import sys
  3. import re
  4. import os
  5.  
  6. parmNotes = ''
  7.  
  8. sourceFile=sys.argv[1]
  9. destFile=sourceFile
  10.  
  11. # Read the ENTIRE g-code file into memory
  12. with open(sourceFile, "r") as f:
  13.     lines = f.readlines()
  14.  
  15.     # print position variables
  16.     coordMinX = 9999.9
  17.     coordMaxX =0.0
  18.     coordMinY = 9999.9
  19.     coordMaxY = 0.0
  20.  
  21.     currLine = 1
  22.     parsing = False # extruder position parsing flag
  23.  
  24. for line in lines:
  25.     currLine += 1
  26.     parts = line.split(';', 1)
  27.     if len(parts) > 0:
  28.         # Parse command
  29.         command = parts[0].strip()
  30.         if len(parts) > 1:
  31.             # Parse comments
  32.             comment = parts[1].strip()
  33.  
  34.         # Track extruder movement ranges
  35.         # Note that this requires enabling verbose logging in Slic3r (output options)
  36.         if not re.search(";", comment):
  37.             # Limit tracking to print movements, avoid start and end gcode blocks
  38.             if re.search("move to first skirt point", comment):
  39.                 #if not parsing:
  40.                 #    print("Start extruder movement parsing at line %d with %s" % (currLine, comment))
  41.                 parsing = True
  42.             if re.search("PURGING FINISHED", comment):
  43.                 #print("Stop extruder movement parsing at line %d with %s" % (currLine, comment))
  44.                 parsing = False
  45.         if parsing:
  46.             stringMatch = re.search ('^G[01].*X([0-9.]+)', command)
  47.             if stringMatch:
  48.                 # extract x-axis moves
  49.                 val = float(stringMatch.group(1))
  50.                 # print("%s: X is %d" % (currLine, val))
  51.                 if(val < coordMinX):
  52.                     coordMinX = val
  53.                 if(val > coordMaxX):
  54.                     coordMaxX = val
  55.             stringMatch = re.search ('^G[01].*Y([0-9.]+)', command)
  56.             if stringMatch:
  57.                 # extract y-axis moves
  58.                 val = float(stringMatch.group(1))
  59.                 if(val < coordMinY):
  60.                     coordMinY = val
  61.                 if(val > coordMaxY):
  62.                     coordMaxY = val
  63.  
  64. os.rename(sourceFile,destFile+".xyminmax.bak")
  65. destFile = re.sub('\.gcode$','',sourceFile)
  66. destFile = destFile + '.gcode'
  67.  
  68. with open(destFile, "w") as of:
  69.     of.write('; Minimum X = '+ str(coordMinX)+'\n')
  70.     of.write('; Maximum X = '+ str(coordMaxX)+'\n')
  71.     of.write('; Minimum Y = '+ str(coordMinY)+'\n')
  72.     of.write('; Maximum Y = '+ str(coordMaxY)+'\n')
  73.     for lIndex in xrange(len(lines)):
  74.         oline = lines[lIndex]
  75.         of.write(oline)
  76.  
  77. of.close()
  78. f.close()
Advertisement
Add Comment
Please, Sign In to add comment