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