1 from config import config
2 from fabmetheus_utilities.vector3 import Vector3
3 from entities import GcodeCommand, TravelPath
4 from plugins.comb import CombSkein
5 import StringIO
6 import gcodes
7 import sys
8 import time
9
11 '''Writes the gcode for a sliced model.'''
12
15
16
18 '''Final gcode representation.'''
19 output = StringIO.StringIO()
20
21 for startCommand in self.gcode.startGcodeCommands:
22 output.write(printCommand(startCommand, verbose))
23
24 lookaheadStartVector = None
25 lookaheadKeyIndex = 0
26 layerCount = len(self.gcode.layers)
27 for key in sorted(self.gcode.layers.iterkeys()):
28 lookaheadStartPoint = None
29 lookaheadKeyIndex = lookaheadKeyIndex + 1
30 if lookaheadKeyIndex < layerCount:
31 lookaheadKey = self.gcode.layers.keys()[lookaheadKeyIndex]
32 lookaheadLayer = self.gcode.layers[lookaheadKey]
33 lookaheadStartPoint = lookaheadLayer.getStartPoint()
34 lookaheadStartVector = Vector3(lookaheadStartPoint.real, lookaheadStartPoint.imag, lookaheadLayer.z)
35
36 self.getLayerAsGcode(self.gcode.layers[key], output, lookaheadStartVector, verbose)
37
38 for endCommand in self.gcode.endGcodeCommands:
39 output.write(printCommand(endCommand, verbose))
40
41 return output.getvalue()
42
43
44 - def getLayerAsGcode(self, layer, output, parentLookaheadStartVector=None, verbose=False):
45 '''Final Gcode representation.'''
46
47 for preLayerGcodeCommand in layer.preLayerGcodeCommands:
48 output.write(printCommand(preLayerGcodeCommand, verbose))
49
50 if layer.runtimeParameters.combActive:
51 combSkein = CombSkein(layer)
52 else:
53 combSkein = None
54
55 pathList = layer.getOrderedPathList()
56
57 pathListCount = len(pathList)
58 for (index, path) in enumerate(pathList):
59 if index + 1 < pathListCount:
60 lookaheadStartPoint = pathList[index + 1].getStartPoint()
61 lookaheadVector = Vector3(lookaheadStartPoint.real, lookaheadStartPoint.imag, layer.z)
62 else:
63 lookaheadVector = parentLookaheadStartVector
64
65 previousVector = None
66 if index > 0:
67 previousPoint = pathList[index - 1].getEndPoint()
68 previousVector = Vector3(previousPoint.real, previousPoint.imag, layer.z)
69
70 nextPoint = path.getStartPoint()
71 nextVector = Vector3(nextPoint.real, nextPoint.imag, layer.z)
72
73 travelPath = TravelPath(layer.z, layer.runtimeParameters, previousVector, nextVector, combSkein)
74 self.getPathAsGcode(travelPath, output, lookaheadVector, layer.feedAndFlowRateMultiplier, verbose)
75
76 self.getPathAsGcode(path, output, lookaheadVector, layer.feedAndFlowRateMultiplier, verbose)
77
78 for postLayerGcodeCommand in layer.postLayerGcodeCommands:
79 output.write(printCommand(postLayerGcodeCommand, verbose))
80
81 - def getPathAsGcode(self, path, output, lookaheadStartVector=None, feedAndFlowRateMultiplier=1.0, verbose=False):
82 '''Final Gcode representation.'''
83 path.generateGcode(lookaheadStartVector, feedAndFlowRateMultiplier)
84
85 for command in path.gcodeCommands:
86 output.write('%s' % printCommand(command, verbose))
87
89 if command == None:
90 return
91 if isinstance(command, GcodeCommand):
92 return'%s\n' % command.str(verbose)
93 else:
94 return '%s\n' % command
95