1 from __future__ import absolute_import
\r
2 from __future__ import division
\r
3 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
\r
6 import ConfigParser, os, traceback, math, re
\r
8 #########################################################
\r
9 ## Profile and preferences functions
\r
10 #########################################################
\r
12 #Single place to store the defaults, so we have a consistent set of default settings.
\r
13 profileDefaultSettings = {
\r
14 'nozzle_size': '0.4',
\r
15 'layer_height': '0.2',
\r
16 'wall_thickness': '0.8',
\r
17 'solid_layer_thickness': '0.6',
\r
18 'fill_density': '20',
\r
19 'skirt_line_count': '1',
\r
21 'print_speed': '50',
\r
22 'print_temperature': '0',
\r
24 'filament_diameter': '2.89',
\r
25 'filament_density': '1.00',
\r
26 'machine_center_x': '100',
\r
27 'machine_center_y': '100',
\r
28 'retraction_min_travel': '5.0',
\r
29 'retraction_speed': '40.0',
\r
30 'retraction_amount': '0.0',
\r
31 'retraction_extra': '0.0',
\r
32 'travel_speed': '150',
\r
33 'max_z_speed': '3.0',
\r
34 'bottom_layer_speed': '20',
\r
35 'cool_min_layer_time': '10',
\r
36 'fan_enabled': 'True',
\r
39 'model_scale': '1.0',
\r
45 'model_rotate_base': '0',
\r
46 'model_multiply_x': '1',
\r
47 'model_multiply_y': '1',
\r
48 'extra_base_wall_thickness': '0.0',
\r
49 'sequence': 'Loops > Perimeter > Infill',
\r
50 'force_first_layer_sequence': 'True',
\r
51 'infill_type': 'Line',
\r
52 'solid_top': 'True',
\r
53 'fill_overlap': '15',
\r
54 'support_rate': '50',
\r
55 'support_distance': '0.5',
\r
57 'enable_skin': 'False',
\r
58 'enable_raft': 'False',
\r
59 'cool_min_feedrate': '5',
\r
60 'bridge_speed': '100',
\r
61 'bridge_material_amount': '100',
\r
63 'raft_base_material_amount': '100',
\r
64 'raft_interface_material_amount': '100',
\r
65 'bottom_thickness': '0.3',
\r
66 'add_start_end_gcode': 'True',
\r
67 'gcode_extension': 'gcode',
\r
69 preferencesDefaultSettings = {
\r
70 'wizardDone': 'False',
\r
71 'startMode': 'Simple',
\r
73 'machine_width': '205',
\r
74 'machine_depth': '205',
\r
75 'machine_height': '200',
\r
76 'filament_density': '1300',
\r
78 'serial_port': 'AUTO',
\r
79 'serial_baud': '250000',
\r
80 'slicer': 'Cura (Skeinforge based)',
\r
81 'save_profile': 'False',
\r
82 'filament_cost_kg': '0',
\r
83 'filament_cost_meter': '0',
\r
86 def getDefaultProfilePath():
\r
87 return os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../current_profile.ini"))
\r
89 def loadGlobalProfile(filename):
\r
90 #Read a configuration file as global config
\r
91 global globalProfileParser
\r
92 globalProfileParser = ConfigParser.ConfigParser()
\r
93 globalProfileParser.read(filename)
\r
95 def saveGlobalProfile(filename):
\r
96 #Save the current profile to an ini file
\r
97 globalProfileParser.write(open(filename, 'w'))
\r
99 def loadGlobalProfileFromString(options):
\r
100 global globalProfileParser
\r
101 globalProfileParser = ConfigParser.ConfigParser()
\r
102 globalProfileParser.add_section('profile')
\r
103 for option in options.split('#'):
\r
104 (key, value) = option.split('=', 1)
\r
105 globalProfileParser.set('profile', key, value)
\r
107 def getGlobalProfileString():
\r
108 global globalProfileParser
\r
109 if not globals().has_key('globalProfileParser'):
\r
110 loadGlobalProfile(getDefaultProfilePath())
\r
113 for key in globalProfileParser.options('profile'):
\r
114 ret.append(key + "=" + globalProfileParser.get('profile', key))
\r
115 return '#'.join(ret)
\r
117 def getProfileSetting(name):
\r
118 #Check if we have a configuration file loaded, else load the default.
\r
119 if not globals().has_key('globalProfileParser'):
\r
120 loadGlobalProfile(getDefaultProfilePath())
\r
121 if not globalProfileParser.has_option('profile', name):
\r
122 if name in profileDefaultSettings:
\r
123 default = profileDefaultSettings[name]
\r
125 print "Missing default setting for: '" + name + "'"
\r
126 profileDefaultSettings[name] = ''
\r
128 if not globalProfileParser.has_section('profile'):
\r
129 globalProfileParser.add_section('profile')
\r
130 globalProfileParser.set('profile', name, str(default))
\r
131 #print name + " not found in profile, so using default: " + str(default)
\r
133 return globalProfileParser.get('profile', name)
\r
135 def getProfileSettingFloat(name):
\r
137 return float(eval(getProfileSetting(name), {}, {}))
\r
138 except (ValueError, SyntaxError):
\r
141 def putProfileSetting(name, value):
\r
142 #Check if we have a configuration file loaded, else load the default.
\r
143 if not globals().has_key('globalProfileParser'):
\r
144 loadGlobalProfile(getDefaultProfilePath())
\r
145 if not globalProfileParser.has_section('profile'):
\r
146 globalProfileParser.add_section('profile')
\r
147 globalProfileParser.set('profile', name, str(value))
\r
149 global globalPreferenceParser
\r
150 globalPreferenceParser = None
\r
152 def getPreferencePath():
\r
153 return os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../preferences.ini"))
\r
155 def getPreference(name):
\r
156 global globalPreferenceParser
\r
157 if globalPreferenceParser == None:
\r
158 globalPreferenceParser = ConfigParser.ConfigParser()
\r
159 globalPreferenceParser.read(getPreferencePath())
\r
160 if not globalPreferenceParser.has_option('preference', name):
\r
161 if name in preferencesDefaultSettings:
\r
162 default = preferencesDefaultSettings[name]
\r
164 print "Missing default setting for: '" + name + "'"
\r
165 preferencesDefaultSettings[name] = ''
\r
167 if not globalPreferenceParser.has_section('preference'):
\r
168 globalPreferenceParser.add_section('preference')
\r
169 globalPreferenceParser.set('preference', name, str(default))
\r
170 #print name + " not found in preferences, so using default: " + str(default)
\r
172 return unicode(globalPreferenceParser.get('preference', name), "utf-8")
\r
174 def putPreference(name, value):
\r
175 #Check if we have a configuration file loaded, else load the default.
\r
176 global globalPreferenceParser
\r
177 if globalPreferenceParser == None:
\r
178 globalPreferenceParser = ConfigParser.ConfigParser()
\r
179 globalPreferenceParser.read(getPreferencePath())
\r
180 if not globalPreferenceParser.has_section('preference'):
\r
181 globalPreferenceParser.add_section('preference')
\r
182 globalPreferenceParser.set('preference', name, unicode(value).encode("utf-8"))
\r
183 globalPreferenceParser.write(open(getPreferencePath(), 'w'))
\r
185 #########################################################
\r
186 ## Utility functions to calculate common profile values
\r
187 #########################################################
\r
188 def calculateEdgeWidth():
\r
189 wallThickness = getProfileSettingFloat('wall_thickness')
\r
190 nozzleSize = getProfileSettingFloat('nozzle_size')
\r
192 if wallThickness < nozzleSize:
\r
193 return wallThickness
\r
195 lineCount = int(wallThickness / nozzleSize)
\r
196 lineWidth = wallThickness / lineCount
\r
197 lineWidthAlt = wallThickness / (lineCount + 1)
\r
198 if lineWidth > nozzleSize * 1.5:
\r
199 return lineWidthAlt
\r
202 def calculateLineCount():
\r
203 wallThickness = getProfileSettingFloat('wall_thickness')
\r
204 nozzleSize = getProfileSettingFloat('nozzle_size')
\r
206 if wallThickness < nozzleSize:
\r
209 lineCount = int(wallThickness / nozzleSize + 0.0001)
\r
210 lineWidth = wallThickness / lineCount
\r
211 lineWidthAlt = wallThickness / (lineCount + 1)
\r
212 if lineWidth > nozzleSize * 1.5:
\r
213 return lineCount + 1
\r
216 def calculateSolidLayerCount():
\r
217 layerHeight = getProfileSettingFloat('layer_height')
\r
218 solidThickness = getProfileSettingFloat('solid_layer_thickness')
\r
219 return int(math.ceil(solidThickness / layerHeight - 0.0001))
\r
221 #########################################################
\r
222 ## Alteration file functions
\r
223 #########################################################
\r
224 def getCuraBasePath():
\r
225 return os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
\r
227 def getAlterationFilePath(filename):
\r
228 return os.path.join(getCuraBasePath(), "alterations", filename)
\r
230 def replaceTagMatch(m):
\r
231 tag = m.group(0)[1:-1]
\r
232 if tag in ['print_speed', 'retraction_speed', 'travel_speed', 'max_z_speed', 'bottom_layer_speed', 'cool_min_feedrate']:
\r
233 return str(getProfileSettingFloat(tag) * 60)
\r
234 return str(getProfileSettingFloat(tag))
\r
236 def getAlterationFileContents(filename, modifyForOutput = True):
\r
237 "Get the file from the fileName or the lowercase fileName in the alterations directories."
\r
239 if modifyForOutput:
\r
240 if filename == 'start.gcode':
\r
241 #For the start code, hack the temperature and the steps per E value into it. So the temperature is reached before the start code extrusion.
\r
242 #We also set our steps per E here, if configured.
\r
243 eSteps = float(getPreference('steps_per_e'))
\r
245 prefix += 'M92 E'+str(eSteps)+'\n'
\r
246 temp = getProfileSettingFloat('print_temperature')
\r
248 prefix += 'M109 S'+str(temp)+'\n'
\r
249 elif filename == 'replace.csv':
\r
250 prefix = 'M101\nM103\n'
\r
251 fullFilename = getAlterationFilePath(filename)
\r
252 if os.path.isfile(fullFilename):
\r
253 file = open(fullFilename, "r")
\r
254 fileText = file.read()
\r
256 if modifyForOutput:
\r
257 fileText = re.sub("\{[^\}]*\}", replaceTagMatch, fileText)
\r
258 return prefix + fileText
\r