chiark / gitweb /
Add uppercase STL and HEX to file dialog filters for linux/MacOS
[cura.git] / Cura / 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:
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
73         def getCraftedGcode( self, gcodeText ):
74                 "Parse gcode text and store the gcode."
75                 lines = getTextLines(gcodeText)
76                 for line in lines:
77                         self.parseLine(line)
78                 return self.output.getvalue()
79
80         def parseLine(self, line):
81                 "Parse a gcode line."
82                 if len(line) < 1:
83                         return
84                 if line[0] == '(':
85                         self.parseComment(line)
86                         return
87                 splitLine = getSplitLineBeforeBracketSemicolon(line)
88                 if len(splitLine) < 1:
89                         return
90                 firstWord = splitLine[0]
91                 if len(firstWord) < 1:
92                         return
93                 if firstWord[0] == '(':
94                         return
95                 if firstWord != 'G1':
96                         self.output.write(line + '\n')
97                         return
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')
104                 if xString != None:
105                         self.output.write(' X' + xString )
106                 if yString != None:
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 )
112                 if eString != None:
113                         self.output.write(' E' + eString )
114                 self.lastFeedRateString = feedRateString
115                 self.lastZString = zString
116                 self.output.write('\n')
117         
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('(<layer>'):
130                         self.output.write(';LAYER:%d\n' % (self.layerNr));
131                         self.layerNr += 1
132