chiark / gitweb /
Fix plugin support, fix the pauzeAtZ plugin.
[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 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                         if nozzleSize <= 0:
86                                 return ERROR, 'Incorrect nozzle size'
87                         
88                         lineCount = int(wallThickness / nozzleSize)
89                         lineWidth = wallThickness / lineCount
90                         lineWidthAlt = wallThickness / (lineCount + 1)
91                         if lineWidth >= nozzleSize * 1.5 and lineWidthAlt <= nozzleSize * 0.85:
92                                 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'
93                         return SUCCESS, ''
94                 except ValueError:
95                         #We already have an error by the int/float validator in this case.
96                         return SUCCESS, ''
97
98 class printSpeedValidator(object):
99         def __init__(self, setting):
100                 self.setting = setting
101                 self.setting._validators.append(self)
102
103         def validate(self):
104                 from Cura.util import profile
105                 try:
106                         nozzleSize = profile.getProfileSettingFloat('nozzle_size')
107                         layerHeight = profile.getProfileSettingFloat('layer_height')
108                         printSpeed = profile.getProfileSettingFloat('print_speed')
109                         
110                         printVolumePerMM = layerHeight * nozzleSize
111                         printVolumePerSecond = printVolumePerMM * printSpeed
112                         #Using 10mm3 per second with a 0.4mm nozzle (normal max according to Joergen Geerds)
113                         maxPrintVolumePerSecond = 10 / (math.pi*(0.2*0.2)) * (math.pi*(nozzleSize/2*nozzleSize/2))
114                         
115                         if printVolumePerSecond > maxPrintVolumePerSecond:
116                                 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)
117                         
118                         return SUCCESS, ''
119                 except ValueError:
120                         #We already have an error by the int/float validator in this case.
121                         return SUCCESS, ''
122