cura: properly working dual extrusion plugin
[clinton/3d-models.git] / cura / dual_temp_lower.py
CommitLineData
c7006aad
CE
1#Name: Dual Temp Test
2#Info: Lower the temperature for the nozzle that is not printing
3#Depend: GCode
4#Type: postprocess
abed4315 5#Param: coolTemperatureDifference(float:50.0) Drop idle nozzle temperature (C)
c7006aad 6#Param: heatupTimeDelay(float:20.0) Heatup nozzle before use (sec)
2d3019ff 7#Param: zHop(float:0.0) Raise Z when reheating active extruder (mm)
c7006aad 8
abed4315
CE
9
10__copyright__ = "Copyright (C) 2013 David Braam, Copyright (C) 2016 Clinton Ebadi <clinton@unknownlamer.org> - Released under terms of the AGPLv3 License"
c7006aad
CE
11import re
12import math
abed4315 13from Cura.util import profile
c7006aad
CE
14
15def getValue(line, key, default = None):
16 if not key in line or (';' in line and line.find(key) > line.find(';')):
17 return default
18 subPart = line[line.find(key) + 1:]
19 m = re.search('^[0-9]+\.?[0-9]*', subPart)
20 if m is None:
21 return default
22 try:
23 return float(m.group(0))
24 except:
25 return default
26
27with open(filename, "r") as f:
28 lines = f.readlines()
29
30z = 0.
31x = 0.
32y = 0.
33e = 0.
abed4315 34feedrate = float(profile.getProfileSetting('print_speed'))
2d3019ff 35wipeTowerP = profile.getProfileSetting('wipe_tower') == 'True'
c7006aad
CE
36currentSectionType = 'CUSTOM'
37activeExtruder = -1
abed4315
CE
38
39# todo: move custom gcode to here
40# detect priming tower and inject reheat there
41
42# grab setting from the profile, add a tag at the end of postSwitch,
43# inject right before the move that sets E0
44
45# todo: use 2nd/3rd/etc extruder temp if available (arrays are alright...)
46printTemperature = float(profile.getProfileSetting('print_temperature'))
47coolTemperature = printTemperature - coolTemperatureDifference
c7006aad 48with open(filename, "w") as f:
abed4315
CE
49 # skip start gcode
50 idx = lines.index (';LAYER:0\n')
51 startIdx = idx
52 for line in lines[0:idx+1]:
53 if getValue(line, 'T', None) is not None and getValue(line, 'M', None) is None:
54 activeExtruder = getValue(line, 'T', None)
55 f.write (line)
56 while idx < len(lines):
c7006aad
CE
57 line = lines[idx]
58 if getValue(line, 'T', None) is not None and getValue(line, 'M', None) is None:
59 nextExtruder = getValue(line, 'T', None)
60 printTime = 0.0
61 sx = x
62 sy = y
63 sz = z
64 for n in xrange(startIdx, idx):
65 line = lines[n]
66 if line.startswith(';'):
67 continue
68 g = getValue(line, 'G', None)
69 if g == 0 or g == 1:
70 nx = getValue(line, 'X', x)
71 ny = getValue(line, 'Y', y)
72 nz = getValue(line, 'Z', z)
73 feedrate = getValue(line, 'F', feedrate)
74 printTime += math.sqrt((nx-x)*(nx-x)+(ny-y)*(ny-y)+(nz-z)*(nz-z)) / feedrate * 60
75 x = nx
76 y = ny
77 z = nz
2d3019ff
CE
78 heatupTime = max (printTime - heatupTimeDelay, 0.0)
79 heatupBlocked = True # do not reheat until active extruder is heated
c7006aad
CE
80 x = sx
81 y = sy
82 z = sz
2d3019ff
CE
83
84 f.write("M104 T%d S%d\n" % (nextExtruder, coolTemperature))
85 f.write("T%d\n" % (activeExtruder))
86
c7006aad
CE
87 printTime = 0.0
88 for n in xrange(startIdx, idx):
89 line = lines[n]
90 if line.startswith(';'):
91 f.write(line)
92 continue
2d3019ff
CE
93 # find where the newly activated extruder is unspooled and block until fully reheated
94 if getValue(line, 'G', None) is not None and getValue (line, 'E', 666) == 0.0:
95 # todo: if wipeTowerP, only
96 # wait for a few degrees below
97 # printTemperature and allow
98 # it to finish heating while
99 # priming
100 if zHop > 0.0:
101 f.write ("G91\nG0 Z%.3f\n" % zHop)
102 f.write("M109 T%d S%d\n" % (activeExtruder, printTemperature))
103 if zHop > 0.0:
104 f.write ("G0 Z%.3f\nG90\n" % -zHop)
105 heatupBlocked = False
c7006aad
CE
106 if heatupTime is not None:
107 g = getValue(line, 'G', None)
108 if g == 0 or g == 1:
109 nx = getValue(line, 'X', x)
110 ny = getValue(line, 'Y', y)
111 nz = getValue(line, 'Z', z)
112 feedrate = getValue(line, 'F', feedrate)
113 printTime += math.sqrt((nx-x)*(nx-x)+(ny-y)*(ny-y)+(nz-z)*(nz-z)) / feedrate * 60
abed4315 114 if (printTime > heatupTime) and (not heatupBlocked):
c7006aad
CE
115 f.write("M104 T%d S%d\n" % (nextExtruder, printTemperature))
116 f.write("T%d\n" % (activeExtruder))
117 heatupTime = None
118 x = nx
119 y = ny
120 z = nz
121 f.write(line)
122 f.write(lines[idx])
2d3019ff 123 startIdx = idx + 1
c7006aad
CE
124 activeExtruder = nextExtruder
125 idx += 1
126 for n in xrange(startIdx, idx):
127 f.write(lines[n])