Advertisement
bobstro

Slic3rPE post-processing script to add print info into name

May 1st, 2019
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.72 KB | None | 0 0
  1. #!/usr/bin/python
  2. # Note: This script should go last since the input file is renamed
  3. import sys
  4. import re
  5. import os
  6.  
  7. parmEstimatedHours = ''
  8. parmEstimatedMinutes = ''
  9. parmEstimatedSeconds = ''
  10. parmFilamentType = ''
  11. parmLayerHeight = ''
  12. parmNozzleDiameter = ''
  13. parm1stLayerTemperature = ''
  14. parm1stLayerBedTemperature = ''
  15. parmTemperature = ''
  16. parmBedTemperature = ''
  17. parmNotes = ''
  18.  
  19. sourceFile=sys.argv[1]
  20.  
  21. # Read the ENTIRE g-code file into memory
  22. with open(sourceFile, "r") as f:
  23.     lines = f.readlines()
  24.  
  25. # Parse gcode lines for print information
  26. for line in lines:
  27. #    currLine += 1
  28.     # parts = line.split(';', 1)
  29.     # if len(parts) > 0:
  30.     #     # Split line into command and comments
  31.     #     command = parts[0].strip()
  32.     #     if len(parts) > 1:
  33.     #         # Parse comments
  34.     #         comment = parts[1].strip()
  35.  
  36.     stringMatch = re.search ('; notes = (.*)', line)
  37.     if stringMatch:
  38.         parmNotes = stringMatch.group(1).strip()
  39.         print(parmNotes)
  40.     stringMatch = re.search('estimated printing time \(normal mode\) =.* ([0-9]+)h.*', line)
  41.     if stringMatch:
  42.         parmEstimatedHours = stringMatch.group(1)+'h'
  43.         print(parmEstimatedHours)
  44.     stringMatch = re.search('estimated printing time \(normal mode\) =.* ([0-9]+)m.*', line)
  45.     if stringMatch:
  46.         parmEstimatedMinutes = stringMatch.group(1)+'m'
  47.     stringMatch = re.search('estimated printing time \(normal mode\) =.* ([0-9]+)s.*', line)
  48.     if stringMatch:
  49.         parmEstimatedSeconds = stringMatch.group(1)+'s'
  50.     # Parse print job parameters
  51.     stringMatch = re.search('filament_type = (.*)', line)
  52.     if stringMatch:
  53.         parmFilamentType = stringMatch.group(1)
  54.     stringMatch = re.search('nozzle_diameter = (.*)', line)
  55.     if stringMatch:
  56.         parmNozzleDiameter = stringMatch.group(1)
  57.     stringMatch = re.search('layer_height = (.*)', line)
  58.     if stringMatch:
  59.         parmLayerHeight = stringMatch.group(1)
  60.     # Parse temperatures
  61.     stringMatch = re.search('bed_temperature = (.*)', line)
  62.     if stringMatch:
  63.         parmBedTemperature = stringMatch.group(1)
  64.     stringMatch = re.search('first_layer_bed_temperature = (.*)', line)
  65.     if stringMatch:
  66.         parm1stLayerBedTemperature = stringMatch.group(1)
  67.     stringMatch = re.search('temperature = (.*)', line)
  68.     if stringMatch:
  69.         parmTemperature = stringMatch.group(1)
  70.     stringMatch = re.search('first_layer_temperature = (.*)', line)
  71.     if stringMatch:
  72.             parm1stLayerTemperature = stringMatch.group(1)
  73.  
  74. destFile = re.sub('\.gcode$','',sourceFile)
  75. #os.rename(sourceFile,destFile + '.filename.bak')
  76. print('Destination file is %s' % re.sub('.*/','',destFile))
  77.  
  78. if parmNotes:
  79.     destFile = destFile + ' ('+parmNotes+')'
  80. if parmFilamentType:
  81.     destFile = destFile + ' '+parmFilamentType
  82. if parmLayerHeight:
  83.     destFile = destFile + ' '+parmLayerHeight
  84. if parmNozzleDiameter:
  85.     destFile = destFile + 'X'+parmNozzleDiameter
  86. if parm1stLayerTemperature:
  87.     destFile = destFile + ' '+parm1stLayerTemperature
  88. if parmTemperature:
  89.     destFile = destFile + '-'+parmTemperature
  90. if parm1stLayerBedTemperature:
  91.     destFile = destFile + ' '+parm1stLayerBedTemperature
  92. if parmBedTemperature:
  93.     destFile = destFile + '-'+parmBedTemperature
  94. if parmEstimatedHours or parmEstimatedMinutes or parmEstimatedSeconds:
  95.     destFile = destFile + ' '
  96.     if parmEstimatedHours:
  97.         destFile = destFile + parmEstimatedHours
  98.     if parmEstimatedMinutes:
  99.         destFile = destFile + parmEstimatedMinutes
  100.     if parmEstimatedSeconds:
  101.         destFile = destFile + parmEstimatedSeconds
  102. destFile = destFile + '.gcode'
  103. print('Writing to %s' % re.sub('.*/','',destFile))
  104.  
  105. with open(destFile, "w") as of:
  106.     for lIndex in xrange(len(lines)):
  107.         oline = lines[lIndex]
  108.         of.write(oline)
  109.  
  110. of.close()
  111. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement