chiark / gitweb /
Update firmwares to 13.12 to fix the acceleration planner bug. Add dialog on new...
[cura.git] / Cura / util / validators.py
1 from __future__ import absolute_import
2 from __future__ import division
3 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
4
5 import types
6 import math
7
8 SUCCESS = 0
9 WARNING = 1
10 ERROR   = 2
11
12 class validFloat(object):
13         def __init__(self, setting, minValue = None, maxValue = None):
14                 self.setting = setting
15                 self.setting._validators.append(self)
16                 self.minValue = minValue
17                 self.maxValue = maxValue
18         
19         def validate(self):
20                 try:
21                         f = float(eval(self.setting.getValue().replace(',','.'), {}, {}))
22                         if self.minValue is not None and f < self.minValue:
23                                 return ERROR, 'This setting should not be below ' + str(round(self.minValue, 3))
24                         if self.maxValue is not None and f > self.maxValue:
25                                 return ERROR, 'This setting should not be above ' + str(self.maxValue)
26                         return SUCCESS, ''
27                 except (ValueError, SyntaxError, TypeError, NameError):
28                         return ERROR, '"' + str(self.setting.getValue()) + '" is not a valid number or expression'
29
30 class validInt(object):
31         def __init__(self, setting, minValue = None, maxValue = None):
32                 self.setting = setting
33                 self.setting._validators.append(self)
34                 self.minValue = minValue
35                 self.maxValue = maxValue
36         
37         def validate(self):
38                 try:
39                         f = int(eval(self.setting.getValue(), {}, {}))
40                         if self.minValue is not None and f < self.minValue:
41                                 return ERROR, 'This setting should not be below ' + str(self.minValue)
42                         if self.maxValue is not None and f > self.maxValue:
43                                 return ERROR, 'This setting should not be above ' + str(self.maxValue)
44                         return SUCCESS, ''
45                 except (ValueError, SyntaxError, TypeError, NameError):
46                         return ERROR, '"' + str(self.setting.getValue()) + '" is not a valid whole number or expression'
47
48 class warningAbove(object):
49         def __init__(self, setting, minValueForWarning, warningMessage):
50                 self.setting = setting
51                 self.setting._validators.append(self)
52                 self.minValueForWarning = minValueForWarning
53                 self.warningMessage = warningMessage
54         
55         def validate(self):
56                 try:
57                         f = float(eval(self.setting.getValue().replace(',','.'), {}, {}))
58                         if isinstance(self.minValueForWarning, types.FunctionType):
59                                 if f >= self.minValueForWarning():
60                                         return WARNING, self.warningMessage % (self.minValueForWarning())
61                         else:
62                                 if f >= self.minValueForWarning:
63                                         return WARNING, self.warningMessage
64                         return SUCCESS, ''
65                 except (ValueError, SyntaxError, TypeError):
66                         #We already have an error by the int/float validator in this case.
67                         return SUCCESS, ''
68
69 class warningBelow(object):
70         def __init__(self, setting, minValueForWarning, warningMessage):
71                 self.setting = setting
72                 self.setting._validators.append(self)
73                 self.minValueForWarning = minValueForWarning
74                 self.warningMessage = warningMessage
75
76         def validate(self):
77                 try:
78                         f = float(eval(self.setting.getValue().replace(',','.'), {}, {}))
79                         if isinstance(self.minValueForWarning, types.FunctionType):
80                                 if f <= self.minValueForWarning():
81                                         return WARNING, self.warningMessage % (self.minValueForWarning())
82                         else:
83                                 if f <= self.minValueForWarning:
84                                         return WARNING, self.warningMessage
85                         return SUCCESS, ''
86                 except (ValueError, SyntaxError, TypeError):
87                         #We already have an error by the int/float validator in this case.
88                         return SUCCESS, ''
89
90 class wallThicknessValidator(object):
91         def __init__(self, setting):
92                 self.setting = setting
93                 self.setting._validators.append(self)
94         
95         def validate(self):
96                 from Cura.util import profile
97                 try:
98                         wallThickness = profile.getProfileSettingFloat('wall_thickness')
99                         nozzleSize = profile.getProfileSettingFloat('nozzle_size')
100                         if wallThickness < 0.01:
101                                 return SUCCESS, ''
102                         if wallThickness <= nozzleSize * 0.5:
103                                 return ERROR, 'Trying to print walls thinner then the half of your nozzle size, this will not produce anything usable'
104                         if wallThickness <= nozzleSize * 0.85:
105                                 return WARNING, 'Trying to print walls thinner then the 0.8 * nozzle size. Small chance that this will produce usable results'
106                         if wallThickness < nozzleSize:
107                                 return SUCCESS, ''
108                         if nozzleSize <= 0:
109                                 return ERROR, 'Incorrect nozzle size'
110                         
111                         lineCount = int(wallThickness / nozzleSize)
112                         lineWidth = wallThickness / lineCount
113                         lineWidthAlt = wallThickness / (lineCount + 1)
114                         if lineWidth >= nozzleSize * 1.5 and lineWidthAlt <= nozzleSize * 0.85:
115                                 return WARNING, 'Current selected wall thickness results in a line thickness of ' + str(lineWidthAlt) + 'mm which is not recommended with your nozzle of ' + str(nozzleSize) + 'mm'
116                         return SUCCESS, ''
117                 except ValueError:
118                         #We already have an error by the int/float validator in this case.
119                         return SUCCESS, ''
120
121 class printSpeedValidator(object):
122         def __init__(self, setting):
123                 self.setting = setting
124                 self.setting._validators.append(self)
125
126         def validate(self):
127                 from Cura.util import profile
128                 try:
129                         nozzleSize = profile.getProfileSettingFloat('nozzle_size')
130                         layerHeight = profile.getProfileSettingFloat('layer_height')
131                         printSpeed = profile.getProfileSettingFloat('print_speed')
132                         
133                         printVolumePerMM = layerHeight * nozzleSize
134                         printVolumePerSecond = printVolumePerMM * printSpeed
135                         #Using 10mm3 per second with a 0.4mm nozzle (normal max according to Joergen Geerds)
136                         maxPrintVolumePerSecond = 10 / (math.pi*(0.2*0.2)) * (math.pi*(nozzleSize/2*nozzleSize/2))
137                         
138                         if printVolumePerSecond > maxPrintVolumePerSecond:
139                                 return WARNING, 'You are trying to print more then %.1fmm^3 of filament per second. This might cause filament slipping. (You are printing at %0.1fmm^3 per second)' % (maxPrintVolumePerSecond, printVolumePerSecond)
140                         
141                         return SUCCESS, ''
142                 except ValueError:
143                         #We already have an error by the int/float validator in this case.
144                         return SUCCESS, ''
145