chiark / gitweb /
Updated print window with statistics about the print. Filament used, and estimated...
[cura.git] / Cura / util / profile.py
1 from __future__ import absolute_import\r
2 #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
3 import __init__\r
4 \r
5 import ConfigParser\r
6 import os\r
7 import traceback\r
8 import math\r
9 \r
10 #########################################################\r
11 ## Profile and preferences functions\r
12 #########################################################\r
13 \r
14 #Single place to store the defaults, so we have a consistent set of default settings.\r
15 profileDefaultSettings = {\r
16         'nozzle_size': '0.4',\r
17         'layer_height': '0.2',\r
18         'wall_thickness': '0.8',\r
19         'solid_layer_thickness': '0.6',\r
20         'fill_density': '20',\r
21         'skirt_line_count': '1',\r
22         'skirt_gap': '6.0',\r
23         'print_speed': '50',\r
24         'print_temperature': '0',\r
25         'support': 'None',\r
26         'filament_diameter': '2.89',\r
27         'filament_density': '1.00',\r
28         'machine_center_x': '100',\r
29         'machine_center_y': '100',\r
30         'retraction_min_travel': '5.0',\r
31         'retraction_speed': '13.5',\r
32         'retraction_amount': '0.0',\r
33         'retraction_extra': '0.0',\r
34         'travel_speed': '150',\r
35         'max_z_speed': '1.0',\r
36         'bottom_layer_speed': '25',\r
37         'cool_min_layer_time': '10',\r
38         'fan_enabled': 'True',\r
39         'fan_layer': '0',\r
40         'fan_speed': '100',\r
41         'model_scale': '1.0',\r
42         'flip_x': 'False',\r
43         'flip_y': 'False',\r
44         'flip_z': 'False',\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': '100',\r
55         'support_distance': '0.5',\r
56         'joris': 'False',\r
57         'enable_raft': 'False',\r
58         'cool_min_feedrate': '5',\r
59         'bridge_speed': '100',\r
60         'bridge_material_amount': '100',\r
61         'raft_margin': '5',\r
62         'raft_base_material_amount': '100',\r
63         'raft_interface_material_amount': '100',\r
64 }\r
65 preferencesDefaultSettings = {\r
66         'wizardDone': 'False',\r
67         'startMode': 'Simple',\r
68         'lastFile': 'None',\r
69         'machine_width': '205',\r
70         'machine_depth': '205',\r
71         'machine_height': '200',\r
72         'filament_density': '1300',\r
73         'steps_per_e': '0',\r
74         'serial_port': 'AUTO',\r
75         'serial_baud': '250000',\r
76         'slicer': 'Cura (Skeinforge based)',\r
77 }\r
78 \r
79 def getDefaultProfilePath():\r
80         return os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../current_profile.ini"))\r
81 \r
82 def loadGlobalProfile(filename):\r
83         #Read a configuration file as global config\r
84         global globalProfileParser\r
85         globalProfileParser = ConfigParser.ConfigParser()\r
86         globalProfileParser.read(filename)\r
87 \r
88 def saveGlobalProfile(filename):\r
89         #Save the current profile to an ini file\r
90         globalProfileParser.write(open(filename, 'w'))\r
91 \r
92 def loadGlobalProfileFromString(options):\r
93         global globalProfileParser\r
94         globalProfileParser = ConfigParser.ConfigParser()\r
95         globalProfileParser.add_section('profile')\r
96         for option in options.split('#'):\r
97                 (key, value) = option.split('=', 1)\r
98                 globalProfileParser.set('profile', key, value)\r
99 \r
100 def getGlobalProfileString():\r
101         global globalProfileParser\r
102         if not globals().has_key('globalProfileParser'):\r
103                 loadGlobalProfile(getDefaultProfilePath())\r
104         \r
105         ret = []\r
106         for key in globalProfileParser.options('profile'):\r
107                 ret.append(key + "=" + globalProfileParser.get('profile', key))\r
108         return '#'.join(ret)\r
109 \r
110 def getProfileSetting(name):\r
111         if name in profileDefaultSettings:\r
112                 default = profileDefaultSettings[name]\r
113         else:\r
114                 print "Missing default setting for: '" + name + "'"\r
115                 profileDefaultSettings[name] = ''\r
116                 default = ''\r
117         \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 not globalProfileParser.has_section('profile'):\r
123                         globalProfileParser.add_section('profile')\r
124                 globalProfileParser.set('profile', name, str(default))\r
125                 print name + " not found in profile, so using default: " + str(default)\r
126                 return default\r
127         return globalProfileParser.get('profile', name)\r
128 \r
129 def putProfileSetting(name, value):\r
130         #Check if we have a configuration file loaded, else load the default.\r
131         if not globals().has_key('globalProfileParser'):\r
132                 loadGlobalProfile(getDefaultProfilePath())\r
133         if not globalProfileParser.has_section('profile'):\r
134                 globalProfileParser.add_section('profile')\r
135         globalProfileParser.set('profile', name, str(value))\r
136 \r
137 global globalPreferenceParser\r
138 globalPreferenceParser = None\r
139 \r
140 def getPreferencePath():\r
141         return os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../preferences.ini"))\r
142 \r
143 def getPreference(name):\r
144         if name in preferencesDefaultSettings:\r
145                 default = preferencesDefaultSettings[name]\r
146         else:\r
147                 print "Missing default setting for: '" + name + "'"\r
148                 preferencesDefaultSettings[name] = ''\r
149                 default = ''\r
150 \r
151         global globalPreferenceParser\r
152         if globalPreferenceParser == None:\r
153                 globalPreferenceParser = ConfigParser.ConfigParser()\r
154                 globalPreferenceParser.read(getPreferencePath())\r
155         if not globalPreferenceParser.has_option('preference', name):\r
156                 if not globalPreferenceParser.has_section('preference'):\r
157                         globalPreferenceParser.add_section('preference')\r
158                 globalPreferenceParser.set('preference', name, str(default))\r
159                 print name + " not found in preferences, so using default: " + str(default)\r
160                 return default\r
161         return globalPreferenceParser.get('preference', name)\r
162 \r
163 def putPreference(name, value):\r
164         #Check if we have a configuration file loaded, else load the default.\r
165         global globalPreferenceParser\r
166         if globalPreferenceParser == None:\r
167                 globalPreferenceParser = ConfigParser.ConfigParser()\r
168                 globalPreferenceParser.read(getPreferencePath())\r
169         if not globalPreferenceParser.has_section('preference'):\r
170                 globalPreferenceParser.add_section('preference')\r
171         globalPreferenceParser.set('preference', name, str(value))\r
172         globalPreferenceParser.write(open(getPreferencePath(), 'w'))\r
173 \r
174 #########################################################\r
175 ## Utility functions to calculate common profile values\r
176 #########################################################\r
177 def calculateEdgeWidth():\r
178         wallThickness = float(getProfileSetting('wall_thickness'))\r
179         nozzleSize = float(getProfileSetting('nozzle_size'))\r
180         \r
181         if wallThickness < nozzleSize:\r
182                 return wallThickness\r
183 \r
184         lineCount = int(wallThickness / nozzleSize)\r
185         lineWidth = wallThickness / lineCount\r
186         lineWidthAlt = wallThickness / (lineCount + 1)\r
187         if lineWidth > nozzleSize * 1.5:\r
188                 return lineWidthAlt\r
189         return lineWidth\r
190 \r
191 def calculateLineCount():\r
192         wallThickness = float(getProfileSetting('wall_thickness'))\r
193         nozzleSize = float(getProfileSetting('nozzle_size'))\r
194         \r
195         if wallThickness < nozzleSize:\r
196                 return 1\r
197 \r
198         lineCount = int(wallThickness / nozzleSize + 0.0001)\r
199         lineWidth = wallThickness / lineCount\r
200         lineWidthAlt = wallThickness / (lineCount + 1)\r
201         if lineWidth > nozzleSize * 1.5:\r
202                 return lineCount + 1\r
203         return lineCount\r
204 \r
205 def calculateSolidLayerCount():\r
206         layerHeight = float(getProfileSetting('layer_height'))\r
207         solidThickness = float(getProfileSetting('solid_layer_thickness'))\r
208         return int(math.ceil(solidThickness / layerHeight - 0.0001))\r
209 \r
210 #########################################################\r
211 ## Alteration file functions\r
212 #########################################################\r
213 def getCuraBasePath():\r
214         return os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))\r
215 \r
216 def getAlterationFilePath(filename):\r
217         return os.path.join(getCuraBasePath(), "alterations", filename)\r
218 \r
219 def getAlterationFileContents(filename, allowMagicPrefix = True):\r
220         "Get the file from the fileName or the lowercase fileName in the alterations directories."\r
221         prefix = ''\r
222         if allowMagicPrefix:\r
223                 if filename == 'start.gcode':\r
224                         #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
225                         #We also set our steps per E here, if configured.\r
226                         eSteps = float(getPreference('steps_per_e'))\r
227                         if eSteps > 0:\r
228                                 prefix += 'M92 E'+str(eSteps)+'\n'\r
229                         temp = float(getProfileSetting('print_temperature'))\r
230                         if temp > 0:\r
231                                 prefix += 'M109 S'+str(temp)+'\n'\r
232                 elif filename == 'replace.csv':\r
233                         prefix = 'M101\nM103\n'\r
234         fullFilename = getAlterationFilePath(filename)\r
235         if os.path.isfile(fullFilename):\r
236                 file = open(fullFilename, "r")\r
237                 fileText = file.read()\r
238                 file.close()\r
239                 return prefix + fileText\r
240         return prefix\r
241 \r