chiark / gitweb /
Remove debug message
[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         'model_scale': '1.0',\r
39         'flip_x': 'False',\r
40         'flip_y': 'False',\r
41         'flip_z': 'False',\r
42         'model_rotate_base': '0',\r
43         'model_multiply_x': '1',\r
44         'model_multiply_y': '1',\r
45         'extra_base_wall_thickness': '0.0',\r
46         'sequence': 'Loops > Perimeter > Infill',\r
47         'force_first_layer_sequence': 'True',\r
48         'infill_type': 'Line',\r
49         'solid_top': 'True',\r
50         'fill_overlap': '15',\r
51         'support_rate': '100',\r
52         'support_distance': '0.5',\r
53         'joris': 'False',\r
54         'enable_raft': 'False',\r
55         'cool_min_feedrate': '5',\r
56         'bridge_speed': '100',\r
57         'bridge_material_amount': '100',\r
58         'raft_margin': '5',\r
59         'raft_base_material_amount': '100',\r
60         'raft_interface_material_amount': '100',\r
61 }\r
62 preferencesDefaultSettings = {\r
63         'wizardDone': 'False',\r
64         'lastFile': 'None',\r
65         'machine_width': '205',\r
66         'machine_depth': '205',\r
67         'machine_height': '200',\r
68         'steps_per_e': '0',\r
69         'serial_port': 'AUTO',\r
70         'serial_baud': '250000',\r
71         'slicer': 'Cura (Skeinforge based)',\r
72 }\r
73 \r
74 def getDefaultProfilePath():\r
75         return os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../current_profile.ini"))\r
76 \r
77 def loadGlobalProfile(filename):\r
78         #Read a configuration file as global config\r
79         global globalProfileParser\r
80         globalProfileParser = ConfigParser.ConfigParser()\r
81         globalProfileParser.read(filename)\r
82 \r
83 def saveGlobalProfile(filename):\r
84         #Save the current profile to an ini file\r
85         globalProfileParser.write(open(filename, 'w'))\r
86 \r
87 def resetGlobalProfile():\r
88         #Create an empty profile with no settings, so everything gets default settings.\r
89         global globalProfileParser\r
90         globalProfileParser = ConfigParser.ConfigParser()\r
91 \r
92 def getProfileSetting(name):\r
93         if name in profileDefaultSettings:\r
94                 default = profileDefaultSettings[name]\r
95         else:\r
96                 print "Missing default setting for: '" + name + "'"\r
97                 profileDefaultSettings[name] = ''\r
98                 default = ''\r
99         \r
100         #Check if we have a configuration file loaded, else load the default.\r
101         if not globals().has_key('globalProfileParser'):\r
102                 loadGlobalProfile(getDefaultProfilePath())\r
103         if not globalProfileParser.has_option('profile', name):\r
104                 if not globalProfileParser.has_section('profile'):\r
105                         globalProfileParser.add_section('profile')\r
106                 globalProfileParser.set('profile', name, str(default))\r
107                 print name + " not found in profile, so using default: " + str(default)\r
108                 return default\r
109         return globalProfileParser.get('profile', name)\r
110 \r
111 def putProfileSetting(name, value):\r
112         #Check if we have a configuration file loaded, else load the default.\r
113         if not globals().has_key('globalProfileParser'):\r
114                 loadGlobalProfile(getDefaultProfilePath())\r
115         if not globalProfileParser.has_section('profile'):\r
116                 globalProfileParser.add_section('profile')\r
117         globalProfileParser.set('profile', name, str(value))\r
118 \r
119 global globalPreferenceParser\r
120 globalPreferenceParser = None\r
121 \r
122 def getPreferencePath():\r
123         return os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../preferences.ini"))\r
124 \r
125 def getPreference(name):\r
126         if name in preferencesDefaultSettings:\r
127                 default = preferencesDefaultSettings[name]\r
128         else:\r
129                 print "Missing default setting for: '" + name + "'"\r
130                 preferencesDefaultSettings[name] = ''\r
131                 default = ''\r
132 \r
133         global globalPreferenceParser\r
134         if globalPreferenceParser == None:\r
135                 globalPreferenceParser = ConfigParser.ConfigParser()\r
136                 globalPreferenceParser.read(getPreferencePath())\r
137         if not globalPreferenceParser.has_option('preference', name):\r
138                 if not globalPreferenceParser.has_section('preference'):\r
139                         globalPreferenceParser.add_section('preference')\r
140                 globalPreferenceParser.set('preference', name, str(default))\r
141                 print name + " not found in preferences, so using default: " + str(default)\r
142                 return default\r
143         return globalPreferenceParser.get('preference', name)\r
144 \r
145 def putPreference(name, value):\r
146         #Check if we have a configuration file loaded, else load the default.\r
147         global globalPreferenceParser\r
148         if globalPreferenceParser == None:\r
149                 globalPreferenceParser = ConfigParser.ConfigParser()\r
150                 globalPreferenceParser.read(getPreferencePath())\r
151         if not globalPreferenceParser.has_section('preference'):\r
152                 globalPreferenceParser.add_section('preference')\r
153         globalPreferenceParser.set('preference', name, str(value))\r
154         globalPreferenceParser.write(open(getPreferencePath(), 'w'))\r
155 \r
156 #########################################################\r
157 ## Utility functions to calculate common profile values\r
158 #########################################################\r
159 def calculateEdgeWidth():\r
160         wallThickness = float(getProfileSetting('wall_thickness'))\r
161         nozzleSize = float(getProfileSetting('nozzle_size'))\r
162         \r
163         if wallThickness < nozzleSize:\r
164                 return wallThickness\r
165 \r
166         lineCount = int(wallThickness / nozzleSize)\r
167         lineWidth = wallThickness / lineCount\r
168         lineWidthAlt = wallThickness / (lineCount + 1)\r
169         if lineWidth > nozzleSize * 1.5:\r
170                 return lineWidthAlt\r
171         return lineWidth\r
172 \r
173 def calculateLineCount():\r
174         wallThickness = float(getProfileSetting('wall_thickness'))\r
175         nozzleSize = float(getProfileSetting('nozzle_size'))\r
176         \r
177         if wallThickness < nozzleSize:\r
178                 return 1\r
179 \r
180         lineCount = int(wallThickness / nozzleSize + 0.0001)\r
181         lineWidth = wallThickness / lineCount\r
182         lineWidthAlt = wallThickness / (lineCount + 1)\r
183         if lineWidth > nozzleSize * 1.5:\r
184                 return lineCount + 1\r
185         return lineCount\r
186 \r
187 def calculateSolidLayerCount():\r
188         layerHeight = float(getProfileSetting('layer_height'))\r
189         solidThickness = float(getProfileSetting('solid_layer_thickness'))\r
190         return int(math.ceil(solidThickness / layerHeight - 0.0001))\r
191 \r
192 #########################################################\r
193 ## Alteration file functions\r
194 #########################################################\r
195 def getCuraBasePath():\r
196         return os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))\r
197 \r
198 def getAlterationFilePath(filename):\r
199         return os.path.join(getCuraBasePath(), "alterations", filename)\r
200 \r
201 def getAlterationFileContents(filename, allowMagicPrefix = True):\r
202         "Get the file from the fileName or the lowercase fileName in the alterations directories."\r
203         prefix = ''\r
204         if allowMagicPrefix:\r
205                 if filename == 'start.gcode':\r
206                         #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
207                         #We also set our steps per E here, if configured.\r
208                         eSteps = float(getPreference('steps_per_e'))\r
209                         if eSteps > 0:\r
210                                 prefix += 'M92 E'+str(eSteps)+'\n'\r
211                         temp = float(getProfileSetting('print_temperature'))\r
212                         if temp > 0:\r
213                                 prefix += 'M109 S'+str(temp)+'\n'\r
214                 elif filename == 'replace.csv':\r
215                         prefix = 'M101\nM103\n'\r
216         fullFilename = getAlterationFilePath(filename)\r
217         if os.path.isfile(fullFilename):\r
218                 file = open(fullFilename, "r")\r
219                 fileText = file.read()\r
220                 file.close()\r
221                 return prefix + fileText\r
222         return prefix\r
223 \r