PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/skeinforge_application/skeinforge_plugins/craft_plugins/wipe.py

https://github.com/jmil/SFACT
Python | 266 lines | 217 code | 36 blank | 13 comment | 6 complexity | f3996fed89cf5a1798e5515aa8aba2d0 MD5 | raw file
  1. """
  2. This page is in the table of contents.
  3. At the beginning of a layer, depending on the settings, wipe will move the nozzle with the extruder off to the arrival point, then to the wipe point, then to the departure point, then back to the layer.
  4. The wipe path is machine specific, so you'll probably have to change all the default locations.
  5. The wipe manual page is at:
  6. http://fabmetheus.crsndoo.com/wiki/index.php/Skeinforge_Wipe
  7. ==Operation==
  8. The default 'Activate Wipe' checkbox is off. When it is on, the functions described below will work, when it is off, the functions will not be called.
  9. ==Settings==
  10. ===Location Arrival===
  11. ====Location Arrival X====
  12. Default is minus seventy millimeters.
  13. Defines the x coordinate of the arrival location.
  14. ====Location Arrival Y====
  15. Default is minus fifty millimeters.
  16. Defines the y coordinate of the arrival location.
  17. ====Location Arrival Z====
  18. Default is fifty millimeters.
  19. Defines the z coordinate of the arrival location.
  20. ===Location Departure===
  21. ====Location Departure X====
  22. Default is minus seventy millimeters.
  23. Defines the x coordinate of the departure location.
  24. ====Location Departure Y====
  25. Default is minus forty millimeters.
  26. Defines the y coordinate of the departure location.
  27. ====Location Departure Z====
  28. Default is fifty millimeters.
  29. Defines the z coordinate of the departure location.
  30. ===Location Wipe===
  31. ====Location Wipe X====
  32. Default is minus seventy millimeters.
  33. Defines the x coordinate of the wipe location.
  34. ====Location Wipe Y====
  35. Default is minus seventy millimeters.
  36. Defines the y coordinate of the wipe location.
  37. ====Location Wipe Z====
  38. Default is fifty millimeters.
  39. Defines the z coordinate of the wipe location.
  40. ===Wipe Period===
  41. Default is three.
  42. Defines the number of layers between wipes. Wipe will always wipe just before layer zero, afterwards it will wipe every "Wipe Period" layers. With the default of three, wipe will wipe just before layer zero, layer three, layer six and so on.
  43. ==Examples==
  44. The following examples wipe the file Screw Holder Bottom.stl. The examples are run in a terminal in the folder which contains Screw Holder Bottom.stl and wipe.py.
  45. > python wipe.py
  46. This brings up the wipe dialog.
  47. > python wipe.py Screw Holder Bottom.stl
  48. The wipe tool is parsing the file:
  49. Screw Holder Bottom.stl
  50. ..
  51. The wipe tool has created the file:
  52. .. Screw Holder Bottom_wipe.gcode
  53. """
  54. from __future__ import absolute_import
  55. #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.
  56. import __init__
  57. from fabmetheus_utilities.fabmetheus_tools import fabmetheus_interpret
  58. from fabmetheus_utilities.vector3 import Vector3
  59. from fabmetheus_utilities import archive
  60. from fabmetheus_utilities import euclidean
  61. from fabmetheus_utilities import gcodec
  62. from fabmetheus_utilities import settings
  63. from skeinforge_application.skeinforge_utilities import skeinforge_craft
  64. from skeinforge_application.skeinforge_utilities import skeinforge_polyfile
  65. from skeinforge_application.skeinforge_utilities import skeinforge_profile
  66. import sys
  67. __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
  68. __date__ = '$Date: 2008/21/04 $'
  69. __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
  70. def getCraftedText( fileName, text, wipeRepository = None ):
  71. """Wipe a gcode linear move text."""
  72. return getCraftedTextFromText( archive.getTextIfEmpty(fileName, text), wipeRepository )
  73. def getCraftedTextFromText( gcodeText, wipeRepository = None ):
  74. """Wipe a gcode linear move text."""
  75. if gcodec.isProcedureDoneOrFileIsEmpty( gcodeText, 'wipe'):
  76. return gcodeText
  77. if wipeRepository is None:
  78. wipeRepository = settings.getReadRepository( WipeRepository() )
  79. if not wipeRepository.activateWipe.value:
  80. return gcodeText
  81. return WipeSkein().getCraftedGcode( gcodeText, wipeRepository )
  82. def getNewRepository():
  83. """Get new repository."""
  84. return WipeRepository()
  85. def writeOutput(fileName, shouldAnalyze=True):
  86. """Wipe a gcode linear move file."""
  87. skeinforge_craft.writeChainTextWithNounMessage(fileName, 'wipe', shouldAnalyze)
  88. class WipeRepository:
  89. """A class to handle the wipe settings."""
  90. def __init__(self):
  91. """Set the default settings, execute title & settings fileName."""
  92. skeinforge_profile.addListsToCraftTypeRepository('skeinforge_application.skeinforge_plugins.craft_plugins.wipe.html', self )
  93. self.fileNameInput = settings.FileNameInput().getFromFileName( fabmetheus_interpret.getGNUTranslatorGcodeFileTypeTuples(), 'Open File for Wipe', self, '')
  94. self.openWikiManualHelpPage = settings.HelpPage().getOpenFromAbsolute('http://fabmetheus.crsndoo.com/wiki/index.php/Skeinforge_Wipe')
  95. self.activateWipe = settings.BooleanSetting().getFromValue('Activate Wipe', self, False )
  96. settings.LabelSeparator().getFromRepository(self)
  97. settings.LabelDisplay().getFromName('- Location Arrival -', self )
  98. self.locationArrivalX = settings.FloatSpin().getFromValue( - 100.0, 'Location Arrival X (mm):', self, 300.0, 0.0 )
  99. self.locationArrivalY = settings.FloatSpin().getFromValue( - 100.0, 'Location Arrival Y (mm):', self, 300.0, 5.0 )
  100. self.locationArrivalZ = settings.FloatSpin().getFromValue( - 100.0, 'Location Arrival Z (mm):', self, 300.0, 0.0 )
  101. settings.LabelSeparator().getFromRepository(self)
  102. settings.LabelDisplay().getFromName('- Location Departure -', self )
  103. self.locationDepartureX = settings.FloatSpin().getFromValue( - 100.0, 'Location Departure X (mm):', self, 300.0, 5.0 )
  104. self.locationDepartureY = settings.FloatSpin().getFromValue( - 100.0, 'Location Departure Y (mm):', self, 300.0, 0.0 )
  105. self.locationDepartureZ = settings.FloatSpin().getFromValue( - 100.0, 'Location Departure Z (mm):', self, 300.0, 0.0 )
  106. settings.LabelSeparator().getFromRepository(self)
  107. settings.LabelDisplay().getFromName('- Location Wipe -', self )
  108. self.locationWipeX = settings.FloatSpin().getFromValue( - 100.0, 'Location Wipe X (mm):', self, 300.0, 0.0 )
  109. self.locationWipeY = settings.FloatSpin().getFromValue( - 100.0, 'Location Wipe Y (mm):', self, 300.0, 0.0 )
  110. self.locationWipeZ = settings.FloatSpin().getFromValue( - 100.0, 'Location Wipe Z (mm):', self, 300.0, 0.0 )
  111. settings.LabelSeparator().getFromRepository(self)
  112. self.wipePeriod = settings.IntSpin().getFromValue( 1, 'Wipe Period (layers):', self, 50000, 33333 )
  113. self.executeTitle = 'Wipe'
  114. def execute(self):
  115. """Wipe button has been clicked."""
  116. fileNames = skeinforge_polyfile.getFileOrDirectoryTypesUnmodifiedGcode(self.fileNameInput.value, fabmetheus_interpret.getImportPluginFileNames(), self.fileNameInput.wasCancelled)
  117. for fileName in fileNames:
  118. writeOutput(fileName)
  119. class WipeSkein:
  120. """A class to wipe a skein of extrusions."""
  121. def __init__(self):
  122. self.distanceFeedRate = gcodec.DistanceFeedRate()
  123. self.extruderActive = False
  124. self.highestZ = None
  125. self.layerIndex = - 1
  126. self.lineIndex = 0
  127. self.lines = None
  128. self.oldLocation = None
  129. self.shouldWipe = False
  130. self.travelFeedRateMinute = 957.0
  131. def addHop( self, begin, end ):
  132. """Add hop to highest point."""
  133. beginEndDistance = begin.distance(end)
  134. if beginEndDistance < 3.0 * self.absolutePerimeterWidth:
  135. return
  136. alongWay = self.absolutePerimeterWidth / beginEndDistance
  137. closeToOldLocation = euclidean.getIntermediateLocation( alongWay, begin, end )
  138. closeToOldLocation.z = self.highestZ
  139. self.distanceFeedRate.addLine( self.getLinearMoveWithFeedRate( self.travelFeedRateMinute, closeToOldLocation ) )
  140. closeToOldArrival = euclidean.getIntermediateLocation( alongWay, end, begin )
  141. closeToOldArrival.z = self.highestZ
  142. self.distanceFeedRate.addLine( self.getLinearMoveWithFeedRate( self.travelFeedRateMinute, closeToOldArrival ) )
  143. def addWipeTravel( self, splitLine ):
  144. """Add the wipe travel gcode."""
  145. location = gcodec.getLocationFromSplitLine(self.oldLocation, splitLine)
  146. self.highestZ = max( self.highestZ, location.z )
  147. if not self.shouldWipe:
  148. return
  149. self.shouldWipe = False
  150. if self.extruderActive:
  151. self.distanceFeedRate.addLine('M103')
  152. if self.oldLocation is not None:
  153. self.addHop( self.oldLocation, self.locationArrival )
  154. self.distanceFeedRate.addLine( self.getLinearMoveWithFeedRate( self.travelFeedRateMinute, self.locationArrival ) )
  155. self.distanceFeedRate.addLine( self.getLinearMoveWithFeedRate( self.travelFeedRateMinute, self.locationWipe ) )
  156. self.distanceFeedRate.addLine( self.getLinearMoveWithFeedRate( self.travelFeedRateMinute, self.locationDeparture ) )
  157. self.addHop( self.locationDeparture, location )
  158. if self.extruderActive:
  159. self.distanceFeedRate.addLine('M101')
  160. def getCraftedGcode( self, gcodeText, wipeRepository ):
  161. """Parse gcode text and store the wipe gcode."""
  162. self.lines = archive.getTextLines(gcodeText)
  163. self.wipePeriod = wipeRepository.wipePeriod.value
  164. self.parseInitialization( wipeRepository )
  165. self.locationArrival = Vector3( wipeRepository.locationArrivalX.value, wipeRepository.locationArrivalY.value, wipeRepository.locationArrivalZ.value )
  166. self.locationDeparture = Vector3( wipeRepository.locationDepartureX.value, wipeRepository.locationDepartureY.value, wipeRepository.locationDepartureZ.value )
  167. self.locationWipe = Vector3( wipeRepository.locationWipeX.value, wipeRepository.locationWipeY.value, wipeRepository.locationWipeZ.value )
  168. for self.lineIndex in xrange(self.lineIndex, len(self.lines)):
  169. line = self.lines[self.lineIndex]
  170. self.parseLine(line)
  171. return self.distanceFeedRate.output.getvalue()
  172. def getLinearMoveWithFeedRate( self, feedRate, location ):
  173. """Get a linear move line with the feedRate."""
  174. return self.distanceFeedRate.getLinearGcodeMovementWithFeedRate( feedRate, location.dropAxis(), location.z )
  175. def parseInitialization( self, wipeRepository ):
  176. """Parse gcode initialization and store the parameters."""
  177. for self.lineIndex in xrange(len(self.lines)):
  178. line = self.lines[self.lineIndex]
  179. splitLine = gcodec.getSplitLineBeforeBracketSemicolon(line)
  180. firstWord = gcodec.getFirstWord(splitLine)
  181. self.distanceFeedRate.parseSplitLine(firstWord, splitLine)
  182. if firstWord == '(</extruderInitialization>)':
  183. self.distanceFeedRate.addLine('(<procedureName> wipe </procedureName>)')
  184. return
  185. elif firstWord == '(<extrusionWidth>':
  186. self.absolutePerimeterWidth = abs(float(splitLine[1]))
  187. elif firstWord == '(<travelFeedRate>':
  188. self.travelFeedRateMinute = 60.0 * float(splitLine[1])
  189. self.distanceFeedRate.addLine(line)
  190. def parseLine(self, line):
  191. """Parse a gcode line and add it to the bevel gcode."""
  192. splitLine = gcodec.getSplitLineBeforeBracketSemicolon(line)
  193. if len(splitLine) < 1:
  194. return
  195. firstWord = splitLine[0]
  196. if firstWord == 'G1':
  197. self.addWipeTravel(splitLine)
  198. self.oldLocation = gcodec.getLocationFromSplitLine(self.oldLocation, splitLine)
  199. elif firstWord == '(<layer>':
  200. settings.printProgress(self.layerIndex, 'wipe')
  201. self.layerIndex += 1
  202. if not self.layerIndex % self.wipePeriod:
  203. self.shouldWipe = True
  204. elif firstWord == 'M101':
  205. self.extruderActive = True
  206. elif firstWord == 'M103':
  207. self.extruderActive = False
  208. self.distanceFeedRate.addLine(line)
  209. def main():
  210. """Display the wipe dialog."""
  211. if len(sys.argv) > 1:
  212. writeOutput(' '.join(sys.argv[1 :]))
  213. else:
  214. settings.startMainLoopFromConstructor( getNewRepository() )
  215. if __name__ == "__main__":
  216. main()