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