PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/skein_engines/skeinforge-39/skeinforge_application/skeinforge_plugins/analyze_plugins/comment.py

https://github.com/hmeyer/ReplicatorG
Python | 186 lines | 170 code | 8 blank | 8 comment | 2 complexity | 5c751dbca68f28568bdb83fb748b7694 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. > 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.getWindowAnalyzeFile('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 fabmetheus_utilities import archive
  35. from fabmetheus_utilities import gcodec
  36. from fabmetheus_utilities import settings
  37. from skeinforge_application.skeinforge_utilities import skeinforge_polyfile
  38. from skeinforge_application.skeinforge_utilities import skeinforge_profile
  39. import cStringIO
  40. import sys
  41. __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
  42. __date__ = '$Date: 2008/21/04 $'
  43. __license__ = 'GPL 3.0'
  44. def getNewRepository():
  45. "Get the repository constructor."
  46. return CommentRepository()
  47. def getWindowAnalyzeFile(fileName):
  48. "Comment a gcode file."
  49. gcodeText = archive.getFileText(fileName)
  50. return getWindowAnalyzeFileGivenText(fileName, gcodeText)
  51. def getWindowAnalyzeFileGivenText(fileName, gcodeText):
  52. "Write a commented gcode file for a gcode file."
  53. skein = CommentSkein()
  54. skein.parseGcode(gcodeText)
  55. archive.writeFileMessageEnd('_comment.gcode', fileName, skein.output.getvalue(), 'The commented file is saved as ')
  56. def writeOutput( fileName, fileNameSuffix, gcodeText = ''):
  57. "Write a commented gcode file for a skeinforge gcode file, if 'Write Commented File for Skeinforge Chain' is selected."
  58. repository = settings.getReadRepository( CommentRepository() )
  59. if gcodeText == '':
  60. gcodeText = archive.getFileText( fileNameSuffix )
  61. if repository.activateComment.value:
  62. getWindowAnalyzeFileGivenText( fileNameSuffix, gcodeText )
  63. class CommentRepository:
  64. "A class to handle the comment settings."
  65. def __init__(self):
  66. "Set the default settings, execute title & settings fileName."
  67. skeinforge_profile.addListsToCraftTypeRepository('skeinforge_application.skeinforge_plugins.analyze_plugins.comment.html', self)
  68. self.openWikiManualHelpPage = settings.HelpPage().getOpenFromAbsolute('http://fabmetheus.crsndoo.com/wiki/index.php/Skeinforge_Comment')
  69. self.activateComment = settings.BooleanSetting().getFromValue('Activate Comment', self, False )
  70. self.fileNameInput = settings.FileNameInput().getFromFileName( [ ('Gcode text files', '*.gcode') ], 'Open File to Write Comments for', self, '')
  71. self.executeTitle = 'Write Comments'
  72. def execute(self):
  73. "Write button has been clicked."
  74. fileNames = skeinforge_polyfile.getFileOrGcodeDirectory( self.fileNameInput.value, self.fileNameInput.wasCancelled, ['_comment'] )
  75. for fileName in fileNames:
  76. getWindowAnalyzeFile(fileName)
  77. class CommentSkein:
  78. "A class to comment a gcode skein."
  79. def __init__(self):
  80. self.oldLocation = None
  81. self.output = cStringIO.StringIO()
  82. def addComment( self, comment ):
  83. "Add a gcode comment and a newline to the output."
  84. self.output.write( "( " + comment + " )\n" )
  85. def linearMove( self, splitLine ):
  86. "Comment a linear move."
  87. location = gcodec.getLocationFromSplitLine(self.oldLocation, splitLine)
  88. self.addComment( "Linear move to " + str( location ) + "." );
  89. self.oldLocation = location
  90. def parseGcode( self, gcodeText ):
  91. "Parse gcode text and store the commented gcode."
  92. lines = archive.getTextLines(gcodeText)
  93. for line in lines:
  94. self.parseLine(line)
  95. def parseLine(self, line):
  96. "Parse a gcode line and add it to the commented gcode."
  97. splitLine = gcodec.getSplitLineBeforeBracketSemicolon(line)
  98. if len(splitLine) < 1:
  99. return
  100. firstWord = splitLine[0]
  101. if firstWord == 'G1':
  102. self.linearMove(splitLine)
  103. elif firstWord == 'G2':
  104. self.setHelicalMoveEndpoint(splitLine)
  105. self.addComment( "Helical clockwise move to " + str( self.oldLocation ) + "." )
  106. elif firstWord == 'G3':
  107. self.setHelicalMoveEndpoint(splitLine)
  108. self.addComment( "Helical counterclockwise move to " + str( self.oldLocation ) + "." )
  109. elif firstWord == 'G21':
  110. self.addComment( "Set units to mm." )
  111. elif firstWord == 'G28':
  112. self.addComment( "Start at home." )
  113. elif firstWord == 'G90':
  114. self.addComment( "Set positioning to absolute." )
  115. elif firstWord == 'M101':
  116. self.addComment( "Extruder on, forward." );
  117. elif firstWord == 'M102':
  118. self.addComment( "Extruder on, reverse." );
  119. elif firstWord == 'M103':
  120. self.addComment( "Extruder off." )
  121. elif firstWord == 'M104':
  122. self.addComment( "Set temperature to " + str( gcodec.getDoubleAfterFirstLetter(splitLine[1]) ) + " C." )
  123. elif firstWord == 'M105':
  124. self.addComment( "Custom code for temperature reading." )
  125. elif firstWord == 'M106':
  126. self.addComment( "Turn fan on." )
  127. elif firstWord == 'M107':
  128. self.addComment( "Turn fan off." )
  129. elif firstWord == 'M108':
  130. self.addComment( "Set extruder speed to " + str( gcodec.getDoubleAfterFirstLetter(splitLine[1]) ) + "." )
  131. self.output.write(line + '\n')
  132. def setHelicalMoveEndpoint( self, splitLine ):
  133. "Get the endpoint of a helical move."
  134. if self.oldLocation == None:
  135. print( "A helical move is relative and therefore must not be the first move of a gcode file." )
  136. return
  137. location = gcodec.getLocationFromSplitLine(self.oldLocation, splitLine)
  138. location += self.oldLocation
  139. self.oldLocation = location
  140. def main():
  141. "Display the comment dialog."
  142. if len(sys.argv) > 1:
  143. getWindowAnalyzeFile(' '.join(sys.argv[1 :]))
  144. else:
  145. settings.startMainLoopFromConstructor( getNewRepository() )
  146. if __name__ == "__main__":
  147. main()