chiark / gitweb /
Fix the bug where the M140 for bed temperature is not added in the start code.
[cura.git] / Cura / util / gcodeGenerator.py
1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
3
4 import math
5
6 from Cura.util import profile
7
8 class gcodeGenerator(object):
9         def __init__(self):
10                 self._feedPrint = profile.getProfileSettingFloat('print_speed') * 60
11                 self._feedTravel = profile.getProfileSettingFloat('travel_speed') * 60
12                 self._feedRetract = profile.getProfileSettingFloat('retraction_speed') * 60
13                 filamentRadius = profile.getProfileSettingFloat('filament_diameter') / 2
14                 filamentArea = math.pi * filamentRadius * filamentRadius
15                 self._ePerMM = (profile.getProfileSettingFloat('nozzle_size') * 0.1) / filamentArea
16                 self._eValue = 0.0
17                 self._x = 0
18                 self._y = 0
19                 self._z = 0
20
21                 self._list = ['M110', 'G92 E0']
22
23         def setPrintSpeed(self, speed):
24                 self._feedPrint = speed * 60
25
26         def setExtrusionRate(self, lineWidth, layerHeight):
27                 filamentRadius = profile.getProfileSettingFloat('filament_diameter') / 2
28                 filamentArea = math.pi * filamentRadius * filamentRadius
29                 self._ePerMM = (lineWidth * layerHeight) / filamentArea
30
31         def home(self):
32                 self._x = 0
33                 self._y = 0
34                 self._z = 0
35                 self._list += ['G28']
36
37         def addMove(self, x=None, y=None, z=None):
38                 cmd = "G0 "
39                 if x is not None:
40                         cmd += "X%f " % (x)
41                         self._x = x
42                 if y is not None:
43                         cmd += "Y%f " % (y)
44                         self._y = y
45                 if z is not None:
46                         cmd += "Z%f " % (z)
47                         self._z = z
48                 cmd += "F%d" % (self._feedTravel)
49                 self._list += [cmd]
50
51         def addPrime(self, amount=5):
52                 self._eValue += amount
53                 self._list += ['G1 E%f F%f' % (self._eValue, self._feedRetract)]
54
55         def addRetract(self, amount=5):
56                 self._eValue -= amount
57                 self._list += ['G1 E%f F%f' % (self._eValue, self._feedRetract)]
58
59         def _addExtrude(self, x=None, y=None, z=None):
60                 cmd = "G1 "
61                 oldX = self._x
62                 oldY = self._y
63                 if x is not None:
64                         cmd += "X%f " % (x)
65                         self._x = x
66                 if y is not None:
67                         cmd += "Y%f " % (y)
68                         self._y = y
69                 if z is not None:
70                         cmd += "Z%f " % (z)
71                         self._z = z
72                 self._eValue += math.sqrt((self._x - oldX) * (self._x - oldX) + (self._y - oldY) * (self._y - oldY)) * self._ePerMM
73                 cmd += "E%f F%d" % (self._eValue, self._feedPrint)
74                 self._list += [cmd]
75
76         def addExtrude(self, x=None, y=None, z=None):
77                 if x is not None and abs(self._x - x) > 10:
78                         self.addExtrude((self._x + x) / 2.0, y, z)
79                         self.addExtrude(x, y, z)
80                         return
81                 if y is not None and abs(self._y - y) > 10:
82                         self.addExtrude(x, (self._y + y) / 2.0, z)
83                         self.addExtrude(x, y, z)
84                         return
85                 self._addExtrude(x, y, z)
86
87         def addHome(self):
88                 self._list += ['G28']
89
90         def addCmd(self, cmd):
91                 self._list += [cmd]
92
93         def list(self):
94                 return self._list