PageRenderTime 41ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/skeinforge_tools/analyze_plugins/comment.py

https://github.com/bmander/skeinforge
Python | 185 lines | 169 code | 8 blank | 8 comment | 2 complexity | 4a4696d6a77cf495be663419dcfaf40c 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://www.bitsfrombytes.com/wiki/index.php?title=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. > python
  22. Python 2.5.1 (r251:54863, Sep 22 2007, 01:43:31)
  23. [GCC 4.2.1 (SUSE Linux)] on linux2
  24. Type "help", "copyright", "credits" or "license" for more information.
  25. >>> import comment
  26. >>> comment.main()
  27. This brings up the comment dialog.
  28. >>> comment.analyzeFile( 'Screw Holder_penultimate.gcode' )
  29. The commente file is saved as Screw_Holder_penultimate_comment.gcode
  30. """
  31. from __future__ import absolute_import
  32. #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.
  33. import __init__
  34. from skeinforge_tools.skeinforge_utilities import gcodec
  35. from skeinforge_tools.skeinforge_utilities import settings
  36. from skeinforge_tools.meta_plugins import polyfile
  37. import cStringIO
  38. import sys
  39. __author__ = "Enrique Perez (perez_enrique@yahoo.com)"
  40. __date__ = "$Date: 2008/21/04 $"
  41. __license__ = "GPL 3.0"
  42. def analyzeFile( fileName ):
  43. "Comment a gcode file."
  44. gcodeText = gcodec.getFileText( fileName )
  45. analyzeFileGivenText( fileName, gcodeText )
  46. def analyzeFileGivenText( fileName, gcodeText ):
  47. "Write a commented gcode file for a gcode file."
  48. skein = CommentSkein()
  49. skein.parseGcode( gcodeText )
  50. gcodec.writeFileMessageEnd( '_comment.gcode', fileName, skein.output.getvalue(), 'The commented file is saved as ' )
  51. def getNewRepository():
  52. "Get the repository constructor."
  53. return CommentRepository()
  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. repository = settings.getReadRepository( CommentRepository() )
  57. if gcodeText == '':
  58. gcodeText = gcodec.getFileText( fileName )
  59. if repository.activateComment.value:
  60. analyzeFileGivenText( fileName, gcodeText )
  61. class CommentRepository:
  62. "A class to handle the comment settings."
  63. def __init__( self ):
  64. "Set the default settings, execute title & settings fileName."
  65. settings.addListsToRepository( 'skeinforge_tools.analyze_plugins.comment.html', '', self )
  66. self.openWikiManualHelpPage = settings.HelpPage().getOpenFromAbsolute( 'http://www.bitsfrombytes.com/wiki/index.php?title=Skeinforge_Comment' )
  67. self.activateComment = settings.BooleanSetting().getFromValue( 'Activate Comment', self, False )
  68. self.fileNameInput = settings.FileNameInput().getFromFileName( [ ( 'Gcode text files', '*.gcode' ) ], 'Open File to Write Comments for', self, '' )
  69. #Create the archive, title of the execute button, title of the dialog & settings fileName.
  70. self.executeTitle = 'Write Comments'
  71. def execute( self ):
  72. "Write button has been clicked."
  73. fileNames = polyfile.getFileOrGcodeDirectory( self.fileNameInput.value, self.fileNameInput.wasCancelled, [ '_comment' ] )
  74. for fileName in fileNames:
  75. analyzeFile( fileName )
  76. class CommentSkein:
  77. "A class to comment a gcode skein."
  78. def __init__( self ):
  79. self.oldLocation = None
  80. self.output = cStringIO.StringIO()
  81. def addComment( self, comment ):
  82. "Add a gcode comment and a newline to the output."
  83. self.output.write( "( " + comment + " )\n" )
  84. def linearMove( self, splitLine ):
  85. "Comment a linear move."
  86. location = gcodec.getLocationFromSplitLine( self.oldLocation, splitLine )
  87. self.addComment( "Linear move to " + str( location ) + "." );
  88. self.oldLocation = location
  89. def parseGcode( self, gcodeText ):
  90. "Parse gcode text and store the commented gcode."
  91. lines = gcodec.getTextLines( gcodeText )
  92. for line in lines:
  93. self.parseLine( line )
  94. def parseLine( self, line ):
  95. "Parse a gcode line and add it to the commented gcode."
  96. splitLine = gcodec.getSplitLineBeforeBracketSemicolon( line )
  97. if len( splitLine ) < 1:
  98. return
  99. firstWord = splitLine[ 0 ]
  100. if firstWord == 'G1':
  101. self.linearMove( splitLine )
  102. elif firstWord == 'G2':
  103. self.setHelicalMoveEndpoint( splitLine )
  104. self.addComment( "Helical clockwise move to " + str( self.oldLocation ) + "." )
  105. elif firstWord == 'G3':
  106. self.setHelicalMoveEndpoint( splitLine )
  107. self.addComment( "Helical counterclockwise move to " + str( self.oldLocation ) + "." )
  108. elif firstWord == 'G21':
  109. self.addComment( "Set units to mm." )
  110. elif firstWord == 'G28':
  111. self.addComment( "Start at home." )
  112. elif firstWord == 'G90':
  113. self.addComment( "Set positioning to absolute." )
  114. elif firstWord == 'M101':
  115. self.addComment( "Extruder on, forward." );
  116. elif firstWord == 'M102':
  117. self.addComment( "Extruder on, reverse." );
  118. elif firstWord == 'M103':
  119. self.addComment( "Extruder off." )
  120. elif firstWord == 'M104':
  121. self.addComment( "Set temperature to " + str( gcodec.getDoubleAfterFirstLetter( splitLine[ 1 ] ) ) + " C." )
  122. elif firstWord == 'M105':
  123. self.addComment( "Custom code for temperature reading." )
  124. elif firstWord == 'M106':
  125. self.addComment( "Turn fan on." )
  126. elif firstWord == 'M107':
  127. self.addComment( "Turn fan off." )
  128. elif firstWord == 'M108':
  129. self.addComment( "Set extruder speed to " + str( gcodec.getDoubleAfterFirstLetter( splitLine[ 1 ] ) ) + "." )
  130. self.output.write( line + "\n" )
  131. def setHelicalMoveEndpoint( self, splitLine ):
  132. "Get the endpoint of a helical move."
  133. if self.oldLocation == None:
  134. print( "A helical move is relative and therefore must not be the first move of a gcode file." )
  135. return
  136. location = gcodec.getLocationFromSplitLine( self.oldLocation, splitLine )
  137. location += self.oldLocation
  138. self.oldLocation = location
  139. def main():
  140. "Display the comment dialog."
  141. if len( sys.argv ) > 1:
  142. analyzeFile( ' '.join( sys.argv[ 1 : ] ) )
  143. else:
  144. settings.startMainLoopFromConstructor( getNewRepository() )
  145. if __name__ == "__main__":
  146. main()