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