2 This page is in the table of contents.
3 Gcode_small is an export plugin to remove the comments and the redundant z and feed rate parameters from a gcode file.
5 An export plugin is a script in the export_plugins folder which has the getOutput function, the globalIsReplaceable variable and if it's output is not replaceable, the writeOutput function. It is meant to be run from the export tool. To ensure that the plugin works on platforms which do not handle file capitalization properly, give the plugin a lower case name.
7 The getOutput function of this script takes a gcode text and returns that text without comments and redundant z and feed rate parameters. The writeOutput function of this script takes a gcode text and writes that text without comments and redundant z and feed rate parameters to a file.
9 Many of the functions in this script are copied from gcodec in skeinforge_utilities. They are copied rather than imported so developers making new plugins do not have to learn about gcodec, the code here is all they need to learn.
13 from __future__ import absolute_import
18 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
19 __date__ = '$Date: 2008/21/04 $'
20 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
23 # This is true if the output is text and false if it is binary."
24 globalIsReplaceable = True
27 def getIndexOfStartingWithSecond(letter, splitLine):
28 "Get index of the first occurence of the given letter in the split line, starting with the second word. Return - 1 if letter is not found"
29 for wordIndex in xrange( 1, len(splitLine) ):
30 word = splitLine[ wordIndex ]
32 if firstLetter == letter:
36 def getOutput(gcodeText):
37 'Get the exported version of a gcode file.'
38 return GcodeSmallSkein().getCraftedGcode(gcodeText)
40 def getSplitLineBeforeBracketSemicolon(line):
41 "Get the split line before a bracket or semicolon."
42 bracketSemicolonIndex = min( line.find(';'), line.find('(') )
43 if bracketSemicolonIndex < 0:
45 return line[ : bracketSemicolonIndex ].split()
47 def getStringFromCharacterSplitLine(character, splitLine):
48 "Get the string after the first occurence of the character in the split line."
49 indexOfCharacter = getIndexOfStartingWithSecond(character, splitLine)
50 if indexOfCharacter < 0:
52 return splitLine[indexOfCharacter][1 :]
54 def getSummarizedFileName(fileName):
55 "Get the fileName basename if the file is in the current working directory, otherwise return the original full name."
56 if os.getcwd() == os.path.dirname(fileName):
57 return os.path.basename(fileName)
60 def getTextLines(text):
61 "Get the all the lines of text of a text."
62 return text.replace('\r', '\n').split('\n')
65 class GcodeSmallSkein:
66 "A class to remove redundant z and feed rate parameters from a skein of extrusions."
68 self.lastFeedRateString = None
69 self.lastZString = None
70 self.output = cStringIO.StringIO()
72 self.parsingAlteration = False
74 def getCraftedGcode( self, gcodeText ):
75 "Parse gcode text and store the gcode."
76 lines = getTextLines(gcodeText)
79 return self.output.getvalue()
81 def parseLine(self, line):
86 self.parseComment(line)
88 splitLine = getSplitLineBeforeBracketSemicolon(line)
89 if len(splitLine) < 1:
91 firstWord = splitLine[0]
92 if len(firstWord) < 1:
94 if firstWord[0] == '(':
96 if firstWord == 'M108' or firstWord == 'M113':
99 self.output.write(line + '\n')
101 eString = getStringFromCharacterSplitLine('E', splitLine )
102 xString = getStringFromCharacterSplitLine('X', splitLine )
103 yString = getStringFromCharacterSplitLine('Y', splitLine )
104 zString = getStringFromCharacterSplitLine('Z', splitLine )
105 feedRateString = getStringFromCharacterSplitLine('F', splitLine )
106 self.output.write('G1')
108 self.output.write(' X' + xString )
110 self.output.write(' Y' + yString )
111 if zString != None and zString != self.lastZString:
112 self.output.write(' Z' + zString )
113 if feedRateString != None and feedRateString != self.lastFeedRateString:
114 self.output.write(' F' + feedRateString )
116 self.output.write(' E' + eString )
117 self.lastFeedRateString = feedRateString
118 self.lastZString = zString
119 self.output.write('\n')
121 def parseComment(self, line):
122 if line.startswith('(<skirt>'):
123 self.output.write(';TYPE:SKIRT\n');
124 elif line.startswith('(<edge>'):
125 self.output.write(';TYPE:WALL-OUTER\n');
126 elif line.startswith('(<loop>'):
127 self.output.write(';TYPE:WALL-INNER\n');
128 elif line.startswith('(<infill>'):
129 self.output.write(';TYPE:FILL\n');
130 elif line.startswith('(<alteration>'):
131 self.output.write(';TYPE:CUSTOM\n');
132 self.parsingAlteration = True
133 elif line.startswith('(</alteration>)'):
134 self.parsingAlteration = False
135 elif line.startswith('(<supportLayer>'):
136 self.output.write(';TYPE:SUPPORT\n');
137 elif line.startswith('(<layer>'):
138 self.output.write(';LAYER:%d\n' % (self.layerNr));