PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Program_Files/replicatorg-0025/skein_engines/skeinforge-40/skeinforge_application/skeinforge_plugins/analyze_plugins/comment.py

https://github.com/sialan/autonomous-sprayer
Python | 170 lines | 154 code | 8 blank | 8 comment | 2 complexity | 8e1ebe260e54307e77b9c91908e800b1 MD5 | raw file
  1. """
  2. This page is in the table of contents.
  3. Comment is a script to comment a gcode file.
  4. The comment manual page is at:
  5. http://fabmetheus.crsndoo.com/wiki/index.php/Skeinforge_Comment
  6. ==Operation==
  7. The default 'Activate Comment' checkbox is on. When it is on, the functions described below will work when called from the skeinforge toolchain, when it is off, the functions will not be called from the toolchain. The functions will still be called, whether or not the 'Activate Comment' checkbox is on, when comment is run directly.
  8. ==Gcodes==
  9. An explanation of the gcodes is at:
  10. http://reprap.org/bin/view/Main/Arduino_GCode_Interpreter
  11. and at:
  12. http://reprap.org/bin/view/Main/MCodeReference
  13. A gode example is at:
  14. http://forums.reprap.org/file.php?12,file=565
  15. ==Examples==
  16. Below are examples of comment being used. These examples are run in a terminal in the folder which contains Screw_Holder_penultimate.gcode and comment.py.
  17. > python comment.py
  18. This brings up the comment dialog.
  19. > python comment.py Screw Holder_penultimate.gcode
  20. The comment file is saved as Screw_Holder_penultimate_comment.gcode
  21. """
  22. from __future__ import absolute_import
  23. #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
  24. import __init__
  25. from fabmetheus_utilities import archive
  26. from fabmetheus_utilities import gcodec
  27. from fabmetheus_utilities import settings
  28. from skeinforge_application.skeinforge_utilities import skeinforge_polyfile
  29. from skeinforge_application.skeinforge_utilities import skeinforge_profile
  30. import cStringIO
  31. import sys
  32. __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
  33. __date__ = '$Date: 2008/21/04 $'
  34. __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
  35. def getNewRepository():
  36. 'Get new repository.'
  37. return CommentRepository()
  38. def getWindowAnalyzeFile(fileName):
  39. "Comment a gcode file."
  40. gcodeText = archive.getFileText(fileName)
  41. return getWindowAnalyzeFileGivenText(fileName, gcodeText)
  42. def getWindowAnalyzeFileGivenText(fileName, gcodeText):
  43. "Write a commented gcode file for a gcode file."
  44. skein = CommentSkein()
  45. skein.parseGcode(gcodeText)
  46. archive.writeFileMessageEnd('_comment.gcode', fileName, skein.output.getvalue(), 'The commented file is saved as ')
  47. def writeOutput( fileName, fileNameSuffix, gcodeText = ''):
  48. "Write a commented gcode file for a skeinforge gcode file, if 'Write Commented File for Skeinforge Chain' is selected."
  49. repository = settings.getReadRepository( CommentRepository() )
  50. if gcodeText == '':
  51. gcodeText = archive.getFileText( fileNameSuffix )
  52. if repository.activateComment.value:
  53. getWindowAnalyzeFileGivenText( fileNameSuffix, gcodeText )
  54. class CommentRepository:
  55. "A class to handle the comment settings."
  56. def __init__(self):
  57. "Set the default settings, execute title & settings fileName."
  58. skeinforge_profile.addListsToCraftTypeRepository('skeinforge_application.skeinforge_plugins.analyze_plugins.comment.html', self)
  59. self.openWikiManualHelpPage = settings.HelpPage().getOpenFromAbsolute('http://fabmetheus.crsndoo.com/wiki/index.php/Skeinforge_Comment')
  60. self.activateComment = settings.BooleanSetting().getFromValue('Activate Comment', self, False )
  61. self.fileNameInput = settings.FileNameInput().getFromFileName( [ ('Gcode text files', '*.gcode') ], 'Open File to Write Comments for', self, '')
  62. self.executeTitle = 'Write Comments'
  63. def execute(self):
  64. "Write button has been clicked."
  65. fileNames = skeinforge_polyfile.getFileOrGcodeDirectory( self.fileNameInput.value, self.fileNameInput.wasCancelled, ['_comment'] )
  66. for fileName in fileNames:
  67. getWindowAnalyzeFile(fileName)
  68. class CommentSkein:
  69. "A class to comment a gcode skein."
  70. def __init__(self):
  71. self.oldLocation = None
  72. self.output = cStringIO.StringIO()
  73. def addComment( self, comment ):
  74. "Add a gcode comment and a newline to the output."
  75. self.output.write( "( " + comment + " )\n" )
  76. def linearMove( self, splitLine ):
  77. "Comment a linear move."
  78. location = gcodec.getLocationFromSplitLine(self.oldLocation, splitLine)
  79. self.addComment( "Linear move to " + str( location ) + "." );
  80. self.oldLocation = location
  81. def parseGcode( self, gcodeText ):
  82. "Parse gcode text and store the commented gcode."
  83. lines = archive.getTextLines(gcodeText)
  84. for line in lines:
  85. self.parseLine(line)
  86. def parseLine(self, line):
  87. "Parse a gcode line and add it to the commented gcode."
  88. splitLine = gcodec.getSplitLineBeforeBracketSemicolon(line)
  89. if len(splitLine) < 1:
  90. return
  91. firstWord = splitLine[0]
  92. if firstWord == 'G1':
  93. self.linearMove(splitLine)
  94. elif firstWord == 'G2':
  95. self.setHelicalMoveEndpoint(splitLine)
  96. self.addComment( "Helical clockwise move to " + str( self.oldLocation ) + "." )
  97. elif firstWord == 'G3':
  98. self.setHelicalMoveEndpoint(splitLine)
  99. self.addComment( "Helical counterclockwise move to " + str( self.oldLocation ) + "." )
  100. elif firstWord == 'G21':
  101. self.addComment( "Set units to mm." )
  102. elif firstWord == 'G28':
  103. self.addComment( "Start at home." )
  104. elif firstWord == 'G90':
  105. self.addComment( "Set positioning to absolute." )
  106. elif firstWord == 'M101':
  107. self.addComment( "Extruder on, forward." );
  108. elif firstWord == 'M102':
  109. self.addComment( "Extruder on, reverse." );
  110. elif firstWord == 'M103':
  111. self.addComment( "Extruder off." )
  112. elif firstWord == 'M104':
  113. self.addComment( "Set temperature to " + str( gcodec.getDoubleAfterFirstLetter(splitLine[1]) ) + " C." )
  114. elif firstWord == 'M105':
  115. self.addComment( "Custom code for temperature reading." )
  116. elif firstWord == 'M106':
  117. self.addComment( "Turn fan on." )
  118. elif firstWord == 'M107':
  119. self.addComment( "Turn fan off." )
  120. elif firstWord == 'M108':
  121. self.addComment( "Set extruder speed to " + str( gcodec.getDoubleAfterFirstLetter(splitLine[1]) ) + "." )
  122. self.output.write(line + '\n')
  123. def setHelicalMoveEndpoint( self, splitLine ):
  124. "Get the endpoint of a helical move."
  125. if self.oldLocation == None:
  126. print( "A helical move is relative and therefore must not be the first move of a gcode file." )
  127. return
  128. location = gcodec.getLocationFromSplitLine(self.oldLocation, splitLine)
  129. location += self.oldLocation
  130. self.oldLocation = location
  131. def main():
  132. "Display the comment dialog."
  133. if len(sys.argv) > 1:
  134. getWindowAnalyzeFile(' '.join(sys.argv[1 :]))
  135. else:
  136. settings.startMainLoopFromConstructor( getNewRepository() )
  137. if __name__ == "__main__":
  138. main()