chiark / gitweb /
Add back the ultimaker platform, and made the platform mesh simpler.
[cura.git] / Cura / slice / cura_sf / skeinforge_application / skeinforge_plugins / craft_plugins / export_plugins / static_plugins / gcode_small.py
1 """
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.
4
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.
6
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.
8
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.
10
11 """
12
13 from __future__ import absolute_import
14 import cStringIO
15 import os
16
17
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'
21
22
23 # This is true if the output is text and false if it is binary."
24 globalIsReplaceable = True
25
26
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 ]
31                 firstLetter = word[0]
32                 if firstLetter == letter:
33                         return wordIndex
34         return - 1
35
36 def getOutput(gcodeText):
37         'Get the exported version of a gcode file.'
38         return GcodeSmallSkein().getCraftedGcode(gcodeText)
39
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:
44                 return line.split()
45         return line[ : bracketSemicolonIndex ].split()
46
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:
51                 return None
52         return splitLine[indexOfCharacter][1 :]
53
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)
58         return fileName
59
60 def getTextLines(text):
61         "Get the all the lines of text of a text."
62         return text.replace('\r', '\n').split('\n')
63
64
65 class GcodeSmallSkein(object):
66         "A class to remove redundant z and feed rate parameters from a skein of extrusions."
67         def __init__(self):
68                 self.lastFeedRateString = None
69                 self.lastZString = None
70                 self.output = cStringIO.StringIO()
71                 self.layerNr = 0
72                 self.parsingAlteration = False
73
74         def getCraftedGcode( self, gcodeText ):
75                 "Parse gcode text and store the gcode."
76                 lines = getTextLines(gcodeText)
77                 for line in lines:
78                         self.parseLine(line)
79                 return self.output.getvalue()
80
81         def parseLine(self, line):
82                 "Parse a gcode line."
83                 if len(line) < 1:
84                         return
85                 if line[0] == '(':
86                         self.parseComment(line)
87                         return
88                 splitLine = getSplitLineBeforeBracketSemicolon(line)
89                 if len(splitLine) < 1:
90                         return
91                 firstWord = splitLine[0]
92                 if len(firstWord) < 1:
93                         return
94                 if firstWord[0] == '(':
95                         return
96                 if firstWord == 'M108' or firstWord == 'M113':
97                         return
98                 if firstWord != 'G1':
99                         self.output.write(line + '\n')
100                         return
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')
107                 if xString != None:
108                         self.output.write(' X' + xString )
109                 if yString != None:
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 )
115                 if eString != None:
116                         self.output.write(' E' + eString )
117                 self.lastFeedRateString = feedRateString
118                 self.lastZString = zString
119                 self.output.write('\n')
120         
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));
139                         self.layerNr += 1
140