chiark / gitweb /
Add proper copyright statements.
[cura.git] / Cura / util / validators.py
1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
2 from __future__ import absolute_import
3 from __future__ import division
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 wallThicknessValidator(object):
70         def __init__(self, setting):
71                 self.setting = setting
72                 self.setting._validators.append(self)
73         
74         def validate(self):
75                 from Cura.util import profile
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                 from Cura.util import profile
103                 try:
104                         nozzleSize = profile.getProfileSettingFloat('nozzle_size')
105                         layerHeight = profile.getProfileSettingFloat('layer_height')
106                         printSpeed = profile.getProfileSettingFloat('print_speed')
107                         
108                         printVolumePerMM = layerHeight * nozzleSize
109                         printVolumePerSecond = printVolumePerMM * printSpeed
110                         #Using 10mm3 per second with a 0.4mm nozzle (normal max according to Joergen Geerds)
111                         maxPrintVolumePerSecond = 10 / (math.pi*(0.2*0.2)) * (math.pi*(nozzleSize/2*nozzleSize/2))
112                         
113                         if printVolumePerSecond > maxPrintVolumePerSecond:
114                                 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)
115                         
116                         return SUCCESS, ''
117                 except ValueError:
118                         #We already have an error by the int/float validator in this case.
119                         return SUCCESS, ''
120