PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/skeinforge_application/skeinforge_plugins/analyze_plugins/statistic.py

https://github.com/jmil/SFACT
Python | 348 lines | 316 code | 18 blank | 14 comment | 10 complexity | 4c4ab2f24bfa0ff430b0f08329f5e329 MD5 | raw file
  1. """
  2. This page is in the table of contents.
  3. Statistic is a script to generate statistics a gcode file.
  4. The statistic manual page is at:
  5. http://fabmetheus.crsndoo.com/wiki/index.php/Skeinforge_Statistic
  6. ==Operation==
  7. The default 'Activate Statistic' 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 Statistic' checkbox is on, when statistic is run directly.
  8. ==Settings==
  9. ===Extrusion Diameter over Thickness===
  10. Default is 1.25.
  11. The 'Extrusion Diameter over Thickness is the ratio of the extrusion diameter over the layer thickness, the default is 1.25. The extrusion fill density ratio that is printed to the console, ( it is derived quantity not a parameter ) is the area of the extrusion diameter over the extrusion width over the layer thickness. Assuming the extrusion diameter is correct, a high value means the filament will be packed tightly, and the object will be almost as dense as the filament. If the fill density ratio is too high, there could be too little room for the filament, and the extruder will end up plowing through the extra filament. A low fill density ratio means the filaments will be far away from each other, the object will be leaky and light. The fill density ratio with the default extrusion settings is around 0.68.
  12. ===Print Statistics===
  13. Default is on.
  14. When the 'Print Statistics' checkbox is on, the statistics will be printed to the console.
  15. ===Save Statistics===
  16. Default is off.
  17. When the 'Save Statistics' checkbox is on, the statistics will be saved as a .txt file.
  18. ==Gcodes==
  19. An explanation of the gcodes is at:
  20. http://reprap.org/bin/view/Main/Arduino_GCode_Interpreter
  21. and at:
  22. http://reprap.org/bin/view/Main/MCodeReference
  23. A gode example is at:
  24. http://forums.reprap.org/file.php?12,file=565
  25. ==Examples==
  26. Below are examples of statistic being used. These examples are run in a terminal in the folder which contains Screw Holder_penultimate.gcode and statistic.py. The 'Save Statistics' checkbox is selected.
  27. > python statistic.py
  28. This brings up the statistic dialog.
  29. > python statistic.py Screw Holder_penultimate.gcode
  30. The statistic file is saved as Screw_Holder_penultimate_statistic.txt
  31. """
  32. from __future__ import absolute_import
  33. #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.
  34. import __init__
  35. from fabmetheus_utilities.vector3 import Vector3
  36. from fabmetheus_utilities import archive
  37. from fabmetheus_utilities import euclidean
  38. from fabmetheus_utilities import gcodec
  39. from fabmetheus_utilities import settings
  40. from skeinforge_application.skeinforge_utilities import skeinforge_polyfile
  41. from skeinforge_application.skeinforge_utilities import skeinforge_profile
  42. import cStringIO
  43. import math
  44. import sys
  45. __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
  46. __date__ = '$Date: 2008/21/04 $'
  47. __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
  48. def getNewRepository():
  49. """Get new repository."""
  50. return StatisticRepository()
  51. def getWindowAnalyzeFile(fileName):
  52. """Write statistics for a gcode file."""
  53. return getWindowAnalyzeFileGivenText( fileName, archive.getFileText(fileName) )
  54. def getWindowAnalyzeFileGivenText( fileName, gcodeText, repository=None):
  55. """Write statistics for a gcode file."""
  56. print('')
  57. print('')
  58. print('Statistics are being generated for the file ' + archive.getSummarizedFileName(fileName) )
  59. if repository is None:
  60. repository = settings.getReadRepository( StatisticRepository() )
  61. skein = StatisticSkein()
  62. statisticGcode = skein.getCraftedGcode(gcodeText, repository)
  63. if repository.printStatistics.value:
  64. print( statisticGcode )
  65. if repository.saveStatistics.value:
  66. archive.writeFileMessageEnd('.txt', fileName, statisticGcode, 'The statistics file is saved as ')
  67. def writeOutput(fileName, fileNamePenultimate, fileNameSuffix, filePenultimateWritten, gcodeText=''):
  68. """Write statistics for a skeinforge gcode file, if 'Write Statistics File for Skeinforge Chain' is selected."""
  69. repository = settings.getReadRepository( StatisticRepository() )
  70. if gcodeText == '':
  71. gcodeText = archive.getFileText( fileNameSuffix )
  72. if repository.activateStatistic.value:
  73. getWindowAnalyzeFileGivenText( fileNameSuffix, gcodeText, repository )
  74. class StatisticRepository:
  75. """A class to handle the statistics settings."""
  76. def __init__(self):
  77. """Set the default settings, execute title & settings fileName."""
  78. skeinforge_profile.addListsToCraftTypeRepository('skeinforge_application.skeinforge_plugins.analyze_plugins.statistic.html', self)
  79. self.openWikiManualHelpPage = settings.HelpPage().getOpenFromAbsolute('http://fabmetheus.crsndoo.com/wiki/index.php/Skeinforge_Statistic')
  80. self.activateStatistic = settings.BooleanSetting().getFromValue('Activate Statistic', self, True )
  81. settings.LabelSeparator().getFromRepository(self)
  82. settings.LabelDisplay().getFromName('- Cost -', self )
  83. self.machineTime = settings.FloatSpin().getFromValue( 0.0, 'Machine Time ($/hour):', self, 5.0, 1.0 )
  84. self.material = settings.FloatSpin().getFromValue( 0.0, 'Material ($/kg):', self, 40.0, 20.0 )
  85. settings.LabelSeparator().getFromRepository(self)
  86. self.density = settings.FloatSpin().getFromValue( 500.0, 'Density (kg/m3):', self, 2000.0, 930.0 )
  87. self.extrusionDiameterOverThickness = settings.FloatSpin().getFromValue( 1.0, 'Extrusion Diameter over Thickness (ratio):', self, 1.5, 1.25 )
  88. self.fileNameInput = settings.FileNameInput().getFromFileName( [ ('Gcode text files', '*.gcode') ], 'Open File to Generate Statistics for', self, '')
  89. self.printStatistics = settings.BooleanSetting().getFromValue('Print Statistics', self, True )
  90. self.saveStatistics = settings.BooleanSetting().getFromValue('Save Statistics', self, False )
  91. self.executeTitle = 'Generate Statistics'
  92. def execute(self):
  93. """Write button has been clicked."""
  94. fileNames = skeinforge_polyfile.getFileOrGcodeDirectory( self.fileNameInput.value, self.fileNameInput.wasCancelled, ['_comment'] )
  95. for fileName in fileNames:
  96. getWindowAnalyzeFile(fileName)
  97. class StatisticSkein:
  98. """A class to get statistics for a gcode skein."""
  99. def __init__(self):
  100. self.extrusionDiameter = None
  101. self.oldLocation = None
  102. self.operatingFeedRatePerSecond = None
  103. self.output = cStringIO.StringIO()
  104. self.profileName = None
  105. self.version = None
  106. def addLine(self, line):
  107. """Add a line of text and a newline to the output."""
  108. self.output.write(line + '\n')
  109. def addToPath(self, location):
  110. """Add a point to travel and maybe extrusion."""
  111. if self.oldLocation is not None:
  112. travel = location.distance( self.oldLocation )
  113. if self.feedRateMinute > 0.0:
  114. self.totalBuildTime += 60.0 * travel / self.feedRateMinute
  115. self.totalDistanceTraveled += travel
  116. if self.extruderActive:
  117. self.totalDistanceExtruded += travel
  118. self.cornerMaximum.maximize(location)
  119. self.cornerMinimum.minimize(location)
  120. self.oldLocation = location
  121. def extruderSet( self, active ):
  122. """Maybe increment the number of times the extruder was toggled."""
  123. if self.extruderActive != active:
  124. self.extruderToggled += 1
  125. self.extruderActive = active
  126. def getCraftedGcode(self, gcodeText, repository):
  127. """Parse gcode text and store the statistics."""
  128. self.absolutePerimeterWidth = 0.4
  129. self.characters = 0
  130. self.cornerMaximum = Vector3(-987654321.0, -987654321.0, -987654321.0)
  131. self.cornerMinimum = Vector3(987654321.0, 987654321.0, 987654321.0)
  132. self.extruderActive = False
  133. self.extruderSpeed = None
  134. self.extruderToggled = 0
  135. self.feedRateMinute = 600.0
  136. self.extrusionHeight = 0.4
  137. self.numberOfLines = 0
  138. self.procedures = []
  139. self.repository = repository
  140. self.totalBuildTime = 0.0
  141. self.totalDistanceExtruded = 0.0
  142. self.totalDistanceTraveled = 0.0
  143. lines = archive.getTextLines(gcodeText)
  144. for line in lines:
  145. self.parseLine(line)
  146. averageFeedRate = self.totalDistanceTraveled / self.totalBuildTime
  147. self.characters += self.numberOfLines
  148. kilobytes = round( self.characters / 1024.0 )
  149. halfPerimeterWidth = 0.5 * self.absolutePerimeterWidth
  150. halfExtrusionCorner = Vector3( halfPerimeterWidth, halfPerimeterWidth, halfPerimeterWidth )
  151. self.cornerMaximum += halfExtrusionCorner
  152. self.cornerMinimum -= halfExtrusionCorner
  153. extent = self.cornerMaximum - self.cornerMinimum
  154. roundedHigh = euclidean.getRoundedPoint( self.cornerMaximum )
  155. roundedLow = euclidean.getRoundedPoint( self.cornerMinimum )
  156. roundedExtent = euclidean.getRoundedPoint( extent )
  157. axisString = " axis extrusion starts at "
  158. crossSectionArea = 0.9 * self.absolutePerimeterWidth * self.extrusionHeight # 0.9 if from the typical fill density
  159. if self.extrusionDiameter is not None:
  160. crossSectionArea = math.pi / 4.0 * self.extrusionDiameter * self.extrusionDiameter
  161. volumeExtruded = 0.001 * crossSectionArea * self.totalDistanceExtruded
  162. mass = volumeExtruded / repository.density.value
  163. machineTimeCost = repository.machineTime.value * self.totalBuildTime / 3600.0
  164. materialCost = repository.material.value * mass
  165. self.addLine(' ')
  166. self.addLine('Cost')
  167. self.addLine( "Machine time cost is %s$." % round( machineTimeCost, 2 ) )
  168. self.addLine( "Material cost is %s$." % round( materialCost, 2 ) )
  169. self.addLine( "Total cost is %s$." % round( machineTimeCost + materialCost, 2 ) )
  170. self.addLine(' ')
  171. self.addLine('Extent')
  172. self.addLine( "X%s%s mm and ends at %s mm, for a width of %s mm." % ( axisString, int( roundedLow.x ), int( roundedHigh.x ), int( extent.x ) ) )
  173. self.addLine( "Y%s%s mm and ends at %s mm, for a depth of %s mm." % ( axisString, int( roundedLow.y ), int( roundedHigh.y ), int( extent.y ) ) )
  174. self.addLine( "Z%s%s mm and ends at %s mm, for a height of %s mm." % ( axisString, int( roundedLow.z ), int( roundedHigh.z ), int( extent.z ) ) )
  175. self.addLine(' ')
  176. self.addLine('Extruder')
  177. self.addLine( "Build time is %s." % euclidean.getDurationString( self.totalBuildTime ) )
  178. self.addLine( "Distance extruded is %s mm." % euclidean.getThreeSignificantFigures( self.totalDistanceExtruded ) )
  179. self.addLine( "Distance traveled is %s mm." % euclidean.getThreeSignificantFigures( self.totalDistanceTraveled ) )
  180. if self.extruderSpeed is not None:
  181. self.addLine( "Extruder speed is %s" % euclidean.getThreeSignificantFigures( self.extruderSpeed ) )
  182. self.addLine( "Extruder was extruding %s percent of the time." % euclidean.getThreeSignificantFigures( 100.0 * self.totalDistanceExtruded / self.totalDistanceTraveled ) )
  183. self.addLine( "Extruder was toggled %s times." % self.extruderToggled )
  184. if self.operatingFeedRatePerSecond is not None:
  185. flowRate = crossSectionArea * self.operatingFeedRatePerSecond
  186. self.addLine( "Operating flow rate is %s mm3/s." % euclidean.getThreeSignificantFigures( flowRate ) )
  187. self.addLine( "Feed rate average is %s mm/s, (%s mm/min)." % ( euclidean.getThreeSignificantFigures( averageFeedRate ), euclidean.getThreeSignificantFigures( 60.0 * averageFeedRate ) ) )
  188. self.addLine(' ')
  189. self.addLine('Filament')
  190. self.addLine( "Cross section area is %s mm2." % euclidean.getThreeSignificantFigures( crossSectionArea ) )
  191. if self.extrusionDiameter is not None:
  192. self.addLine( "Extrusion diameter is %s mm." % euclidean.getThreeSignificantFigures( self.extrusionDiameter ) )
  193. self.addLine('Extrusion fill density ratio is %s' % euclidean.getThreeSignificantFigures( crossSectionArea / self.absolutePerimeterWidth / self.extrusionHeight ) )
  194. self.addLine(' ')
  195. self.addLine('Material')
  196. self.addLine( "Mass extruded is %s grams." % euclidean.getThreeSignificantFigures( 1000.0 * mass ) )
  197. self.addLine( "Volume extruded is %s cc." % euclidean.getThreeSignificantFigures( volumeExtruded ) )
  198. self.addLine(' ')
  199. self.addLine('Meta')
  200. self.addLine( "Text has %s lines and a size of %s KB." % ( self.numberOfLines, kilobytes ) )
  201. if self.version is not None:
  202. self.addLine( "Version is " + self.version )
  203. self.addLine(' ')
  204. self.addLine( "Procedures" )
  205. for procedure in self.procedures:
  206. self.addLine(procedure)
  207. if self.profileName is not None:
  208. self.addLine(' ')
  209. self.addLine( 'Profile' )
  210. self.addLine(self.profileName)
  211. self.addLine(' ')
  212. self.addLine('Slice')
  213. self.addLine( "Layer thickness is %s mm." % euclidean.getThreeSignificantFigures( self.extrusionHeight ) )
  214. self.addLine( "Perimeter width is %s mm." % euclidean.getThreeSignificantFigures( self.absolutePerimeterWidth ) )
  215. self.addLine(' ')
  216. return self.output.getvalue()
  217. def getLocationSetFeedRateToSplitLine( self, splitLine ):
  218. """Get location ans set feed rate to the split line."""
  219. location = gcodec.getLocationFromSplitLine(self.oldLocation, splitLine)
  220. indexOfF = gcodec.getIndexOfStartingWithSecond( "F", splitLine )
  221. if indexOfF > 0:
  222. self.feedRateMinute = gcodec.getDoubleAfterFirstLetter( splitLine[indexOfF] )
  223. return location
  224. def helicalMove( self, isCounterclockwise, splitLine ):
  225. """Get statistics for a helical move."""
  226. if self.oldLocation is None:
  227. return
  228. location = self.getLocationSetFeedRateToSplitLine(splitLine)
  229. location += self.oldLocation
  230. center = self.oldLocation.copy()
  231. indexOfR = gcodec.getIndexOfStartingWithSecond( "R", splitLine )
  232. if indexOfR > 0:
  233. radius = gcodec.getDoubleAfterFirstLetter( splitLine[ indexOfR ] )
  234. halfLocationMinusOld = location - self.oldLocation
  235. halfLocationMinusOld *= 0.5
  236. halfLocationMinusOldLength = halfLocationMinusOld.magnitude()
  237. centerMidpointDistanceSquared = radius * radius - halfLocationMinusOldLength * halfLocationMinusOldLength
  238. centerMidpointDistance = math.sqrt( max( centerMidpointDistanceSquared, 0.0 ) )
  239. centerMinusMidpoint = euclidean.getRotatedWiddershinsQuarterAroundZAxis( halfLocationMinusOld )
  240. centerMinusMidpoint.normalize()
  241. centerMinusMidpoint *= centerMidpointDistance
  242. if isCounterclockwise:
  243. center.setToVector3( halfLocationMinusOld + centerMinusMidpoint )
  244. else:
  245. center.setToVector3( halfLocationMinusOld - centerMinusMidpoint )
  246. else:
  247. center.x = gcodec.getDoubleForLetter( "I", splitLine )
  248. center.y = gcodec.getDoubleForLetter( "J", splitLine )
  249. curveSection = 0.5
  250. center += self.oldLocation
  251. afterCenterSegment = location - center
  252. beforeCenterSegment = self.oldLocation - center
  253. afterCenterDifferenceAngle = euclidean.getAngleAroundZAxisDifference( afterCenterSegment, beforeCenterSegment )
  254. absoluteDifferenceAngle = abs( afterCenterDifferenceAngle )
  255. steps = int( round( 0.5 + max( absoluteDifferenceAngle * 2.4, absoluteDifferenceAngle * beforeCenterSegment.magnitude() / curveSection ) ) )
  256. stepPlaneAngle = euclidean.getWiddershinsUnitPolar( afterCenterDifferenceAngle / steps )
  257. zIncrement = ( afterCenterSegment.z - beforeCenterSegment.z ) / float( steps )
  258. for step in xrange( 1, steps ):
  259. beforeCenterSegment = euclidean.getRoundZAxisByPlaneAngle( stepPlaneAngle, beforeCenterSegment )
  260. beforeCenterSegment.z += zIncrement
  261. arcPoint = center + beforeCenterSegment
  262. self.addToPath( arcPoint )
  263. self.addToPath( location )
  264. def linearMove( self, splitLine ):
  265. """Get statistics for a linear move."""
  266. location = self.getLocationSetFeedRateToSplitLine(splitLine)
  267. self.addToPath( location )
  268. def parseLine(self, line):
  269. """Parse a gcode line and add it to the statistics."""
  270. self.characters += len(line)
  271. self.numberOfLines += 1
  272. splitLine = gcodec.getSplitLineBeforeBracketSemicolon(line)
  273. if len(splitLine) < 1:
  274. return
  275. firstWord = splitLine[0]
  276. if firstWord == 'G1':
  277. self.linearMove(splitLine)
  278. elif firstWord == 'G2':
  279. self.helicalMove( False, splitLine )
  280. elif firstWord == 'G3':
  281. self.helicalMove( True, splitLine )
  282. elif firstWord == 'M101':
  283. self.extruderSet( True )
  284. elif firstWord == 'M102':
  285. self.extruderSet( False )
  286. elif firstWord == 'M103':
  287. self.extruderSet( False )
  288. elif firstWord == '(<extrusionHeight>':
  289. self.extrusionHeight = float(splitLine[1])
  290. self.extrusionDiameter = self.repository.extrusionDiameterOverThickness.value * self.extrusionHeight
  291. elif firstWord == '(<operatingFeedRatePerSecond>':
  292. self.operatingFeedRatePerSecond = float(splitLine[1])
  293. elif firstWord == '(<extrusionWidth>':
  294. self.absolutePerimeterWidth = abs(float(splitLine[1]))
  295. elif firstWord == '(<procedureName>':
  296. self.procedures.append(splitLine[1])
  297. elif firstWord == '(<profileName>':
  298. self.profileName = line.replace('(<profileName>', '').replace('</profileName>)', '').strip()
  299. elif firstWord == '(<version>':
  300. self.version = splitLine[1]
  301. def main():
  302. """Display the statistics dialog."""
  303. if len(sys.argv) > 1:
  304. getWindowAnalyzeFile(' '.join(sys.argv[1 :]))
  305. else:
  306. settings.startMainLoopFromConstructor( getNewRepository() )
  307. if __name__ == "__main__":
  308. main()