chiark / gitweb /
Look for CuraEngine in ..../CuraEngine/build/CuraEngine[.exe]
[cura.git] / Cura / util / gcodeGenerator.py
1 """
2 A simple generator for GCode. To assist in creation of simple GCode instructions.
3 This is not intended for advanced use or complex paths. The CuraEngine generates the real GCode instructions.
4 """
5 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
6
7 import math
8
9 from Cura.util import profile
10
11 class gcodeGenerator(object):
12         """
13         Generates a simple set of GCode commands for RepRap GCode firmware.
14         Use the add* commands to build the GCode, and then use the list function to retrieve the resulting gcode.
15         """
16         def __init__(self):
17                 self._feedPrint = profile.getProfileSettingFloat('print_speed') * 60
18                 self._feedTravel = profile.getProfileSettingFloat('travel_speed') * 60
19                 self._feedRetract = profile.getProfileSettingFloat('retraction_speed') * 60
20                 filamentRadius = profile.getProfileSettingFloat('filament_diameter') / 2
21                 filamentArea = math.pi * filamentRadius * filamentRadius
22                 self._ePerMM = (profile.getProfileSettingFloat('nozzle_size') * 0.1) / filamentArea
23                 self._eValue = 0.0
24                 self._x = 0
25                 self._y = 0
26                 self._z = 0
27
28                 self._list = ['M110', 'G92 E0']
29
30         def setPrintSpeed(self, speed):
31                 self._feedPrint = speed * 60
32
33         def setExtrusionRate(self, lineWidth, layerHeight):
34                 filamentRadius = profile.getProfileSettingFloat('filament_diameter') / 2
35                 filamentArea = math.pi * filamentRadius * filamentRadius
36                 self._ePerMM = (lineWidth * layerHeight) / filamentArea
37
38         def home(self):
39                 self._x = 0
40                 self._y = 0
41                 self._z = 0
42                 self._list += ['G28']
43
44         def addMove(self, x=None, y=None, z=None):
45                 cmd = "G0 "
46                 if x is not None:
47                         cmd += "X%f " % (x)
48                         self._x = x
49                 if y is not None:
50                         cmd += "Y%f " % (y)
51                         self._y = y
52                 if z is not None:
53                         cmd += "Z%f " % (z)
54                         self._z = z
55                 cmd += "F%d" % (self._feedTravel)
56                 self._list += [cmd]
57
58         def addPrime(self, amount=5):
59                 self._eValue += amount
60                 self._list += ['G1 E%f F%f' % (self._eValue, self._feedRetract)]
61
62         def addRetract(self, amount=5):
63                 self._eValue -= amount
64                 self._list += ['G1 E%f F%f' % (self._eValue, self._feedRetract)]
65
66         def _addExtrude(self, x=None, y=None, z=None):
67                 cmd = "G1 "
68                 oldX = self._x
69                 oldY = self._y
70                 if x is not None:
71                         cmd += "X%f " % (x)
72                         self._x = x
73                 if y is not None:
74                         cmd += "Y%f " % (y)
75                         self._y = y
76                 if z is not None:
77                         cmd += "Z%f " % (z)
78                         self._z = z
79                 self._eValue += math.sqrt((self._x - oldX) * (self._x - oldX) + (self._y - oldY) * (self._y - oldY)) * self._ePerMM
80                 cmd += "E%f F%d" % (self._eValue, self._feedPrint)
81                 self._list += [cmd]
82
83         def addExtrude(self, x=None, y=None, z=None):
84                 if x is not None and abs(self._x - x) > 10:
85                         self.addExtrude((self._x + x) / 2.0, y, z)
86                         self.addExtrude(x, y, z)
87                         return
88                 if y is not None and abs(self._y - y) > 10:
89                         self.addExtrude(x, (self._y + y) / 2.0, z)
90                         self.addExtrude(x, y, z)
91                         return
92                 self._addExtrude(x, y, z)
93
94         def addHome(self):
95                 self._list += ['G28']
96
97         def addCmd(self, cmd):
98                 self._list += [cmd]
99
100         def list(self):
101                 return self._list