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

/Program_Files/replicatorg-0025/skein_engines/skeinforge-35/skeinforge_application/skeinforge_plugins/craft_plugins/preface.py

https://github.com/sialan/autonomous-sprayer
Python | 252 lines | 232 code | 8 blank | 12 comment | 10 complexity | 484e0e0c990a87638e13fb1a1efe9dc8 MD5 | raw file
  1. #! /usr/bin/env python
  2. """
  3. This page is in the table of contents.
  4. Preface converts the svg slices into gcode extrusion layers, optionally prefaced with some gcode commands.
  5. The preface manual page is at:
  6. http://www.bitsfrombytes.com/wiki/index.php?title=Skeinforge_Preface
  7. ==Settings==
  8. ===Meta===
  9. Default is empty.
  10. The 'Meta' field is to add meta tags or a note to all your files. Whatever is in that field will be added in a meta tagged line to the output.
  11. ===Name of Alteration Files===
  12. Preface looks for alteration files in the alterations folder in the .skeinforge folder in the home directory. Preface does not care if the text file names are capitalized, but some file systems do not handle file name cases properly, so to be on the safe side you should give them lower case names. If it doesn't find the file it then looks in the alterations folder in the skeinforge_plugins folder. If it doesn't find anything there it looks in the craft_plugins folder.
  13. ====Name of End File====
  14. Default is start.gcode.
  15. If there is a file with the name of the "Name of Start File" setting, it will be added to the very beginning of the gcode.
  16. ====Name of Start File====
  17. Default is end.gcode.
  18. If there is a file with the name of the "Name of Start File" setting, it will be added to the very end.
  19. ===Set Positioning to Absolute===
  20. Default is on.
  21. When selected, preface will add the G90 command to set positioning to absolute.
  22. ===Set Units to Millimeters===
  23. Default is on.
  24. When selected, preface will add the G21 command to set the units to millimeters.
  25. ===Start at Home===
  26. Default is off.
  27. When selected, the G28 go to home gcode will be added at the beginning of the file.
  28. ===Turn Extruder Off===
  29. ====Turn Extruder Off at Shut Down====
  30. Default is on.
  31. When selected, the M103 turn extruder off gcode will be added at the end of the file.
  32. ====Turn Extruder Off at Start Up====
  33. Default is on.
  34. When selected, the M103 turn extruder off gcode will be added at the beginning of the file.
  35. ==Examples==
  36. The following examples preface the file Screw Holder Bottom.stl. The examples are run in a terminal in the folder which contains Screw Holder Bottom.stl and preface.py.
  37. > python preface.py
  38. This brings up the preface dialog.
  39. > python preface.py Screw Holder Bottom.stl
  40. The preface tool is parsing the file:
  41. Screw Holder Bottom.stl
  42. ..
  43. The preface tool has created the file:
  44. .. Screw Holder Bottom_preface.gcode
  45. > python
  46. Python 2.5.1 (r251:54863, Sep 22 2007, 01:43:31)
  47. [GCC 4.2.1 (SUSE Linux)] on linux2
  48. Type "help", "copyright", "credits" or "license" for more information.
  49. >>> import preface
  50. >>> preface.main()
  51. This brings up the preface dialog.
  52. >>> preface.writeOutput('Screw Holder Bottom.stl')
  53. The preface tool is parsing the file:
  54. Screw Holder Bottom.stl
  55. ..
  56. The preface tool has created the file:
  57. .. Screw Holder Bottom_preface.gcode
  58. """
  59. from __future__ import absolute_import
  60. #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.
  61. import __init__
  62. from datetime import date
  63. from fabmetheus_utilities.fabmetheus_tools import fabmetheus_interpret
  64. from fabmetheus_utilities.svg_reader import SVGReader
  65. from fabmetheus_utilities.xml_simple_reader import XMLSimpleReader
  66. from fabmetheus_utilities import archive
  67. from fabmetheus_utilities import euclidean
  68. from fabmetheus_utilities import gcodec
  69. from fabmetheus_utilities import intercircle
  70. from fabmetheus_utilities import settings
  71. from fabmetheus_utilities import svg_writer
  72. from skeinforge_application.skeinforge_utilities import skeinforge_craft
  73. from skeinforge_application.skeinforge_utilities import skeinforge_polyfile
  74. from skeinforge_application.skeinforge_utilities import skeinforge_profile
  75. import os
  76. import sys
  77. __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
  78. __date__ = "$Date: 2008/28/04 $"
  79. __license__ = 'GPL 3.0'
  80. def getCraftedText( fileName, text = '', prefaceRepository = None ):
  81. "Preface and convert an svg file or text."
  82. return getCraftedTextFromText( archive.getTextIfEmpty( fileName, text ), prefaceRepository )
  83. def getCraftedTextFromText( text, prefaceRepository = None ):
  84. "Preface and convert an svg text."
  85. if gcodec.isProcedureDoneOrFileIsEmpty( text, 'preface'):
  86. return text
  87. if prefaceRepository == None:
  88. prefaceRepository = settings.getReadRepository(PrefaceRepository())
  89. return PrefaceSkein().getCraftedGcode(prefaceRepository, text)
  90. def getNewRepository():
  91. "Get the repository constructor."
  92. return PrefaceRepository()
  93. def writeOutput(fileName=''):
  94. "Preface the carving of a gcode file. If no fileName is specified, preface the first unmodified gcode file in this folder."
  95. fileName = fabmetheus_interpret.getFirstTranslatorFileNameUnmodified(fileName)
  96. if fileName == '':
  97. return
  98. skeinforge_craft.writeChainTextWithNounMessage( fileName, 'preface')
  99. class PrefaceRepository:
  100. "A class to handle the preface settings."
  101. def __init__(self):
  102. "Set the default settings, execute title & settings fileName."
  103. skeinforge_profile.addListsToCraftTypeRepository('skeinforge_application.skeinforge_plugins.craft_plugins.preface.html', self )
  104. self.fileNameInput = settings.FileNameInput().getFromFileName( fabmetheus_interpret.getGNUTranslatorGcodeFileTypeTuples(), 'Open File for Preface', self, '')
  105. self.openWikiManualHelpPage = settings.HelpPage().getOpenFromAbsolute('http://www.bitsfrombytes.com/wiki/index.php?title=Skeinforge_Preface')
  106. self.meta = settings.StringSetting().getFromValue('Meta:', self, '')
  107. settings.LabelSeparator().getFromRepository(self)
  108. settings.LabelDisplay().getFromName('- Name of Alteration Files -', self )
  109. self.nameOfEndFile = settings.StringSetting().getFromValue('Name of End File:', self, 'end.gcode')
  110. self.nameOfStartFile = settings.StringSetting().getFromValue('Name of Start File:', self, 'start.gcode')
  111. settings.LabelSeparator().getFromRepository(self)
  112. self.setPositioningToAbsolute = settings.BooleanSetting().getFromValue('Set Positioning to Absolute', self, True )
  113. self.setUnitsToMillimeters = settings.BooleanSetting().getFromValue('Set Units to Millimeters', self, True )
  114. self.startAtHome = settings.BooleanSetting().getFromValue('Start at Home', self, False )
  115. settings.LabelSeparator().getFromRepository(self)
  116. settings.LabelDisplay().getFromName('- Turn Extruder Off -', self )
  117. self.turnExtruderOffAtShutDown = settings.BooleanSetting().getFromValue('Turn Extruder Off at Shut Down', self, True )
  118. self.turnExtruderOffAtStartUp = settings.BooleanSetting().getFromValue('Turn Extruder Off at Start Up', self, True )
  119. self.executeTitle = 'Preface'
  120. def execute(self):
  121. "Preface button has been clicked."
  122. fileNames = skeinforge_polyfile.getFileOrDirectoryTypesUnmodifiedGcode(self.fileNameInput.value, fabmetheus_interpret.getImportPluginFileNames(), self.fileNameInput.wasCancelled)
  123. for fileName in fileNames:
  124. writeOutput(fileName)
  125. class PrefaceSkein:
  126. "A class to preface a skein of extrusions."
  127. def __init__(self):
  128. self.distanceFeedRate = gcodec.DistanceFeedRate()
  129. self.extruderActive = False
  130. self.lineIndex = 0
  131. self.oldLocation = None
  132. self.svgReader = SVGReader()
  133. def addFromUpperLowerFile( self, fileName ):
  134. "Add lines of text from the fileName or the lowercase fileName, if there is no file by the original fileName in the directory."
  135. fileText = settings.getFileInAlterationsOrGivenDirectory( os.path.dirname( __file__ ), fileName )
  136. fileLines = archive.getTextLines(fileText)
  137. self.distanceFeedRate.addLinesSetAbsoluteDistanceMode( fileLines )
  138. def addInitializationToOutput(self):
  139. "Add initialization gcode to the output."
  140. self.addFromUpperLowerFile( self.prefaceRepository.nameOfStartFile.value ) # Add a start file if it exists.
  141. self.distanceFeedRate.addTagBracketedLine('creation', 'skeinforge') # GCode formatted comment
  142. absoluteFilePathUntilDot = os.path.abspath( __file__ )[ : os.path.abspath( __file__ ).rfind('.') ]
  143. if absoluteFilePathUntilDot == '/home/enrique/Desktop/backup/babbleold/script/reprap/fabmetheus/skeinforge_application/skeinforge_plugins/craft_plugins/preface': #is this script on Enrique's computer?
  144. archive.writeFileText( archive.getVersionFileName(), date.today().isoformat().replace('-', '.')[ 2 : ] )
  145. versionText = archive.getFileText( archive.getVersionFileName() )
  146. self.distanceFeedRate.addTagBracketedLine('version', versionText ) # GCode formatted comment
  147. self.distanceFeedRate.addLine('(<extruderInitialization>)') # GCode formatted comment
  148. if self.prefaceRepository.setPositioningToAbsolute.value:
  149. self.distanceFeedRate.addLine('G90') # Set positioning to absolute.
  150. if self.prefaceRepository.setUnitsToMillimeters.value:
  151. self.distanceFeedRate.addLine('G21') # Set units to millimeters.
  152. if self.prefaceRepository.startAtHome.value:
  153. self.distanceFeedRate.addLine('G28') # Start at home.
  154. if self.prefaceRepository.turnExtruderOffAtStartUp.value:
  155. self.distanceFeedRate.addLine('M103') # Turn extruder off.
  156. craftTypeName = skeinforge_profile.getCraftTypeName()
  157. self.distanceFeedRate.addTagBracketedLine('craftTypeName', craftTypeName )
  158. self.distanceFeedRate.addTagBracketedLine('decimalPlacesCarried', self.distanceFeedRate.decimalPlacesCarried )
  159. layerThickness = float( self.svgReader.sliceDictionary['layerThickness'] )
  160. self.distanceFeedRate.addTagBracketedLine('layerThickness', self.distanceFeedRate.getRounded( layerThickness ) )
  161. if self.prefaceRepository.meta.value:
  162. self.distanceFeedRate.addTagBracketedLine('meta', self.prefaceRepository.meta.value )
  163. perimeterWidth = float( self.svgReader.sliceDictionary['perimeterWidth'] )
  164. self.distanceFeedRate.addTagBracketedLine('perimeterWidth', self.distanceFeedRate.getRounded( perimeterWidth ) )
  165. self.distanceFeedRate.addTagBracketedLine('profileName', skeinforge_profile.getProfileName( craftTypeName ) )
  166. self.distanceFeedRate.addTagBracketedLine('procedureDone', self.svgReader.sliceDictionary['procedureDone'] )
  167. self.distanceFeedRate.addTagBracketedLine('procedureDone', 'preface')
  168. self.distanceFeedRate.addLine('(</extruderInitialization>)') # Initialization is finished, extrusion is starting.
  169. self.distanceFeedRate.addLine('(<extrusion>)') # Initialization is finished, extrusion is starting.
  170. def addPreface( self, rotatedBoundaryLayer ):
  171. "Add preface to the carve layer."
  172. self.distanceFeedRate.addLine('(<layer> %s )' % rotatedBoundaryLayer.z ) # Indicate that a new layer is starting.
  173. if rotatedBoundaryLayer.rotation != None:
  174. self.distanceFeedRate.addTagBracketedLine('bridgeRotation', str( rotatedBoundaryLayer.rotation ) ) # Indicate the bridge rotation.
  175. for loop in rotatedBoundaryLayer.loops:
  176. self.distanceFeedRate.addGcodeFromLoop( loop, rotatedBoundaryLayer.z )
  177. self.distanceFeedRate.addLine('(</layer>)')
  178. def addShutdownToOutput(self):
  179. "Add shutdown gcode to the output."
  180. self.distanceFeedRate.addLine('(</extrusion>)') # GCode formatted comment
  181. if self.prefaceRepository.turnExtruderOffAtShutDown.value:
  182. self.distanceFeedRate.addLine('M103') # Turn extruder motor off.
  183. self.addFromUpperLowerFile( self.prefaceRepository.nameOfEndFile.value ) # Add an end file if it exists.
  184. def getCraftedGcode( self, prefaceRepository, gcodeText ):
  185. "Parse gcode text and store the bevel gcode."
  186. self.prefaceRepository = prefaceRepository
  187. self.svgReader.parseSVG('', gcodeText )
  188. self.distanceFeedRate.decimalPlacesCarried = int( self.svgReader.sliceDictionary['decimalPlacesCarried'] )
  189. self.addInitializationToOutput()
  190. for rotatedBoundaryLayerIndex, rotatedBoundaryLayer in enumerate(self.svgReader.rotatedLoopLayers):
  191. settings.printProgressByNumber(rotatedBoundaryLayerIndex, len(self.svgReader.rotatedLoopLayers), 'preface')
  192. self.addPreface( rotatedBoundaryLayer )
  193. self.addShutdownToOutput()
  194. return self.distanceFeedRate.output.getvalue()
  195. def main():
  196. "Display the preface dialog."
  197. if len(sys.argv) > 1:
  198. writeOutput(' '.join(sys.argv[1 :]))
  199. else:
  200. settings.startMainLoopFromConstructor( getNewRepository() )
  201. if __name__ == "__main__":
  202. main()