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()
73 def getCraftedGcode( self, gcodeText ):
74 "Parse gcode text and store the gcode."
75 lines = getTextLines(gcodeText)
78 return self.output.getvalue()
80 def parseLine(self, line):
85 self.parseComment(line)
87 splitLine = getSplitLineBeforeBracketSemicolon(line)
88 if len(splitLine) < 1:
90 firstWord = splitLine[0]
91 if len(firstWord) < 1:
93 if firstWord[0] == '(':
96 self.output.write(line + '\n')
98 eString = getStringFromCharacterSplitLine('E', splitLine )
99 xString = getStringFromCharacterSplitLine('X', splitLine )
100 yString = getStringFromCharacterSplitLine('Y', splitLine )
101 zString = getStringFromCharacterSplitLine('Z', splitLine )
102 feedRateString = getStringFromCharacterSplitLine('F', splitLine )
103 self.output.write('G1')
105 self.output.write(' X' + xString )
107 self.output.write(' Y' + yString )
108 if zString != None and zString != self.lastZString:
109 self.output.write(' Z' + zString )
110 if feedRateString != None and feedRateString != self.lastFeedRateString:
111 self.output.write(' F' + feedRateString )
113 self.output.write(' E' + eString )
114 self.lastFeedRateString = feedRateString
115 self.lastZString = zString
116 self.output.write('\n')
118 def parseComment(self, line):
119 if line.startswith('(<skirt>'):
120 self.output.write(';TYPE:SKIRT\n');
121 elif line.startswith('(<edge>'):
122 self.output.write(';TYPE:WALL-OUTER\n');
123 elif line.startswith('(<loop>'):
124 self.output.write(';TYPE:WALL-INNER\n');
125 elif line.startswith('(<infill>'):
126 self.output.write(';TYPE:FILL\n');
127 elif line.startswith('(<alteration>'):
128 self.output.write(';TYPE:CUSTOM\n');
129 elif line.startswith('(<supportLayer>'):
130 self.output.write(';TYPE:SUPPORT\n');
131 elif line.startswith('(<layer>'):
132 self.output.write(';LAYER:%d\n' % (self.layerNr));