chiark / gitweb /
Merge branch 'master' into SteamEngine
[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 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 wallThicknessValidator(object):
69         def __init__(self, setting):
70                 self.setting = setting
71                 self.setting._validators.append(self)
72         
73         def validate(self):
74                 from Cura.util import profile
75                 try:
76                         wallThickness = profile.getProfileSettingFloat('wall_thickness')
77                         nozzleSize = profile.getProfileSettingFloat('nozzle_size')
78                         if wallThickness <= nozzleSize * 0.5:
79                                 return ERROR, 'Trying to print walls thinner then the half of your nozzle size, this will not produce anything usable'
80                         if wallThickness <= nozzleSize * 0.85:
81                                 return WARNING, 'Trying to print walls thinner then the 0.8 * nozzle size. Small chance that this will produce usable results'
82                         if wallThickness < nozzleSize:
83                                 return SUCCESS, ''
84                         
85                         lineCount = int(wallThickness / nozzleSize)
86                         lineWidth = wallThickness / lineCount
87                         lineWidthAlt = wallThickness / (lineCount + 1)
88                         if lineWidth >= nozzleSize * 1.5 and lineWidthAlt <= nozzleSize * 0.85:
89                                 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'
90                         return SUCCESS, ''
91                 except ValueError:
92                         #We already have an error by the int/float validator in this case.
93                         return SUCCESS, ''
94
95 class printSpeedValidator(object):
96         def __init__(self, setting):
97                 self.setting = setting
98                 self.setting._validators.append(self)
99
100         def validate(self):
101                 from Cura.util import profile
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