PageRenderTime 35ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Program_Files/replicatorg-0025/skein_engines/skeinforge-0006/skeinforge_tools/analyze_plugins/comment.py

https://github.com/sialan/autonomous-sprayer
Python | 185 lines | 177 code | 5 blank | 3 comment | 2 complexity | 4d612bb8d9781f4678e2ab8c5bab3aaf MD5 | raw file
  1. """
  2. Comment is a script to comment a gcode file.
  3. The default 'Activate Comment' checkbox is on. When it is on, the functions described below will work when called from the
  4. skeinforge toolchain, when it is off, the functions will not be called from the toolchain. The functions will still be called, whether
  5. or not the 'Activate Comment' checkbox is on, when comment is run directly.
  6. To run comment, in a shell in the folder which comment is in type:
  7. > python comment.py
  8. An explanation of the gcodes is at:
  9. http://reprap.org/bin/view/Main/Arduino_GCode_Interpreter
  10. and at:
  11. http://reprap.org/bin/view/Main/MCodeReference
  12. A gode example is at:
  13. http://forums.reprap.org/file.php?12,file=565
  14. This example comments the gcode file Screw Holder_comb.gcode. This example is run in a terminal in the folder which contains
  15. Screw Holder_comb.gcode and comment.py.
  16. > python
  17. Python 2.5.1 (r251:54863, Sep 22 2007, 01:43:31)
  18. [GCC 4.2.1 (SUSE Linux)] on linux2
  19. Type "help", "copyright", "credits" or "license" for more information.
  20. >>> import comment
  21. >>> comment.main()
  22. This brings up the comment dialog.
  23. >>> comment.commentFile()
  24. The commented file is saved as Screw Holder_comb_comment.gcode
  25. """
  26. from __future__ import absolute_import
  27. #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.
  28. import __init__
  29. from skeinforge_tools.skeinforge_utilities import gcodec
  30. from skeinforge_tools.skeinforge_utilities import preferences
  31. from skeinforge_tools import polyfile
  32. import cStringIO
  33. import sys
  34. __author__ = "Enrique Perez (perez_enrique@yahoo.com)"
  35. __date__ = "$Date: 2008/21/04 $"
  36. __license__ = "GPL 3.0"
  37. def commentFile( fileName = '' ):
  38. "Comment a gcode file. If no fileName is specified, comment the first gcode file in this folder that is not modified."
  39. if fileName == '':
  40. unmodified = gcodec.getUnmodifiedGCodeFiles()
  41. if len( unmodified ) == 0:
  42. print( "There are no unmodified gcode files in this folder." )
  43. return
  44. fileName = unmodified[ 0 ]
  45. writeCommentFileGivenText( fileName, gcodec.getFileText( fileName ) )
  46. def getCommentGcode( gcodeText ):
  47. "Get gcode text with added comments."
  48. skein = CommentSkein()
  49. skein.parseGcode( gcodeText )
  50. return skein.output.getvalue()
  51. def writeCommentFileGivenText( fileName, gcodeText ):
  52. "Write a commented gcode file for a gcode file."
  53. gcodec.writeFileMessageEnd( '_comment.gcode', fileName, getCommentGcode( gcodeText ), 'The commented file is saved as ' )
  54. def writeOutput( fileName, gcodeText = '' ):
  55. "Write a commented gcode file for a skeinforge gcode file, if 'Write Commented File for Skeinforge Chain' is selected."
  56. commentPreferences = CommentPreferences()
  57. preferences.readPreferences( commentPreferences )
  58. if gcodeText == '':
  59. gcodeText = gcodec.getFileText( fileName )
  60. if commentPreferences.activateComment.value:
  61. writeCommentFileGivenText( fileName, gcodeText )
  62. class CommentSkein:
  63. "A class to comment a gcode skein."
  64. def __init__( self ):
  65. self.oldLocation = None
  66. self.output = cStringIO.StringIO()
  67. def addComment( self, comment ):
  68. "Add a gcode comment and a newline to the output."
  69. self.output.write( "( " + comment + " )\n" )
  70. def linearMove( self, splitLine ):
  71. "Comment a linear move."
  72. location = gcodec.getLocationFromSplitLine( self.oldLocation, splitLine )
  73. self.addComment( "Linear move to " + str( location ) + "." );
  74. self.oldLocation = location
  75. def parseGcode( self, gcodeText ):
  76. "Parse gcode text and store the commented gcode."
  77. lines = gcodec.getTextLines( gcodeText )
  78. for line in lines:
  79. self.parseLine( line )
  80. def parseLine( self, line ):
  81. "Parse a gcode line and add it to the commented gcode."
  82. splitLine = line.split()
  83. if len( splitLine ) < 1:
  84. return
  85. firstWord = splitLine[ 0 ]
  86. if firstWord == 'G1':
  87. self.linearMove( splitLine )
  88. elif firstWord == 'G2':
  89. self.setHelicalMoveEndpoint( splitLine )
  90. self.addComment( "Helical clockwise move to " + str( self.oldLocation ) + "." )
  91. elif firstWord == 'G3':
  92. self.setHelicalMoveEndpoint( splitLine )
  93. self.addComment( "Helical counterclockwise move to " + str( self.oldLocation ) + "." )
  94. elif firstWord == 'G21':
  95. self.addComment( "Set units to mm." )
  96. elif firstWord == 'G28':
  97. self.addComment( "Start at home." )
  98. elif firstWord == 'G90':
  99. self.addComment( "Set positioning to absolute." )
  100. elif firstWord == 'M101':
  101. self.addComment( "Extruder on, forward." );
  102. elif firstWord == 'M102':
  103. self.addComment( "Extruder on, reverse." );
  104. elif firstWord == 'M103':
  105. self.addComment( "Extruder off." )
  106. elif firstWord == 'M104':
  107. self.addComment( "Set temperature to " + str( gcodec.getDoubleAfterFirstLetter( splitLine[ 1 ] ) ) + " C." )
  108. elif firstWord == 'M105':
  109. self.addComment( "Custom code for temperature reading." )
  110. elif firstWord == 'M106':
  111. self.addComment( "Turn fan on." )
  112. elif firstWord == 'M107':
  113. self.addComment( "Turn fan off." )
  114. elif firstWord == 'M108':
  115. self.addComment( "Set extruder speed to " + str( gcodec.getDoubleAfterFirstLetter( splitLine[ 1 ] ) ) + "." )
  116. self.output.write( line + "\n" )
  117. def setHelicalMoveEndpoint( self, splitLine ):
  118. "Get the endpoint of a helical move."
  119. if self.oldLocation == None:
  120. print( "A helical move is relative and therefore must not be the first move of a gcode file." )
  121. return
  122. location = gcodec.getLocationFromSplitLine( self.oldLocation, splitLine )
  123. location += self.oldLocation
  124. self.oldLocation = location
  125. class CommentPreferences:
  126. "A class to handle the comment preferences."
  127. def __init__( self ):
  128. "Set the default preferences, execute title & preferences fileName."
  129. #Set the default preferences.
  130. self.archive = []
  131. self.activateComment = preferences.BooleanPreference().getFromValue( 'Activate Comment', False )
  132. self.archive.append( self.activateComment )
  133. self.fileNameInput = preferences.Filename().getFromFilename( [ ( 'Gcode text files', '*.gcode' ) ], 'Open File to Write Comments for', '' )
  134. self.archive.append( self.fileNameInput )
  135. #Create the archive, title of the execute button, title of the dialog & preferences fileName.
  136. self.executeTitle = 'Write Comments'
  137. self.saveTitle = 'Save Preferences'
  138. preferences.setHelpPreferencesFileNameTitleWindowPosition( self, 'skeinforge_tools.analyze_plugins.comment.html' )
  139. def execute( self ):
  140. "Write button has been clicked."
  141. fileNames = polyfile.getFileOrGcodeDirectory( self.fileNameInput.value, self.fileNameInput.wasCancelled, [ '_comment' ] )
  142. for fileName in fileNames:
  143. commentFile( fileName )
  144. def main():
  145. "Display the comment dialog."
  146. if len( sys.argv ) > 1:
  147. writeOutput( ' '.join( sys.argv[ 1 : ] ) )
  148. else:
  149. preferences.displayDialog( CommentPreferences() )
  150. if __name__ == "__main__":
  151. main()