Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import sys
- import re
- import os
- parmNotes = ''
- sourceFile=sys.argv[1]
- destFile=sourceFile
- # Read the ENTIRE g-code file into memory
- with open(sourceFile, "r") as f:
- lines = f.readlines()
- # print position variables
- coordMinX = 9999.9
- coordMaxX =0.0
- coordMinY = 9999.9
- coordMaxY = 0.0
- currLine = 1
- parsing = False # extruder position parsing flag
- for line in lines:
- currLine += 1
- parts = line.split(';', 1)
- if len(parts) > 0:
- # Parse command
- command = parts[0].strip()
- if len(parts) > 1:
- # Parse comments
- comment = parts[1].strip()
- # Track extruder movement ranges
- # Note that this requires enabling verbose logging in Slic3r (output options)
- if not re.search(";", comment):
- # Limit tracking to print movements, avoid start and end gcode blocks
- if re.search("move to first skirt point", comment):
- #if not parsing:
- # print("Start extruder movement parsing at line %d with %s" % (currLine, comment))
- parsing = True
- if re.search("PURGING FINISHED", comment):
- #print("Stop extruder movement parsing at line %d with %s" % (currLine, comment))
- parsing = False
- if parsing:
- stringMatch = re.search ('^G[01].*X([0-9.]+)', command)
- if stringMatch:
- # extract x-axis moves
- val = float(stringMatch.group(1))
- # print("%s: X is %d" % (currLine, val))
- if(val < coordMinX):
- coordMinX = val
- if(val > coordMaxX):
- coordMaxX = val
- stringMatch = re.search ('^G[01].*Y([0-9.]+)', command)
- if stringMatch:
- # extract y-axis moves
- val = float(stringMatch.group(1))
- if(val < coordMinY):
- coordMinY = val
- if(val > coordMaxY):
- coordMaxY = val
- os.rename(sourceFile,destFile+".xyminmax.bak")
- destFile = re.sub('\.gcode$','',sourceFile)
- destFile = destFile + '.gcode'
- with open(destFile, "w") as of:
- of.write('; Minimum X = '+ str(coordMinX)+'\n')
- of.write('; Maximum X = '+ str(coordMaxX)+'\n')
- of.write('; Minimum Y = '+ str(coordMinY)+'\n')
- of.write('; Maximum Y = '+ str(coordMaxY)+'\n')
- for lIndex in xrange(len(lines)):
- oline = lines[lIndex]
- of.write(oline)
- of.close()
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment