improvements to cura 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
CE
6#Param: heatupTimeDelay(float:20.0) Heatup nozzle before use (sec)
7
abed4315
CE
8
9__copyright__ = "Copyright (C) 2013 David Braam, Copyright (C) 2016 Clinton Ebadi <clinton@unknownlamer.org> - Released under terms of the AGPLv3 License"
c7006aad
CE
10import re
11import math
abed4315 12from Cura.util import profile
c7006aad
CE
13
14def getValue(line, key, default = None):
15 if not key in line or (';' in line and line.find(key) > line.find(';')):
16 return default
17 subPart = line[line.find(key) + 1:]
18 m = re.search('^[0-9]+\.?[0-9]*', subPart)
19 if m is None:
20 return default
21 try:
22 return float(m.group(0))
23 except:
24 return default
25
26with open(filename, "r") as f:
27 lines = f.readlines()
28
29z = 0.
30x = 0.
31y = 0.
32e = 0.
abed4315 33feedrate = float(profile.getProfileSetting('print_speed'))
c7006aad
CE
34currentSectionType = 'CUSTOM'
35activeExtruder = -1
abed4315
CE
36
37# todo: move custom gcode to here
38# detect priming tower and inject reheat there
39
40# grab setting from the profile, add a tag at the end of postSwitch,
41# inject right before the move that sets E0
42
43# todo: use 2nd/3rd/etc extruder temp if available (arrays are alright...)
44printTemperature = float(profile.getProfileSetting('print_temperature'))
45coolTemperature = printTemperature - coolTemperatureDifference
c7006aad 46with open(filename, "w") as f:
abed4315
CE
47 # skip start gcode
48 idx = lines.index (';LAYER:0\n')
49 startIdx = idx
50 for line in lines[0:idx+1]:
51 if getValue(line, 'T', None) is not None and getValue(line, 'M', None) is None:
52 activeExtruder = getValue(line, 'T', None)
53 f.write (line)
54 while idx < len(lines):
c7006aad 55 line = lines[idx]
abed4315 56 heatupBlocked = False
c7006aad
CE
57 if getValue(line, 'T', None) is not None and getValue(line, 'M', None) is None:
58 nextExtruder = getValue(line, 'T', None)
59 printTime = 0.0
60 sx = x
61 sy = y
62 sz = z
63 for n in xrange(startIdx, idx):
64 line = lines[n]
65 if line.startswith(';'):
66 continue
67 g = getValue(line, 'G', None)
68 if g == 0 or g == 1:
69 nx = getValue(line, 'X', x)
70 ny = getValue(line, 'Y', y)
71 nz = getValue(line, 'Z', z)
72 feedrate = getValue(line, 'F', feedrate)
73 printTime += math.sqrt((nx-x)*(nx-x)+(ny-y)*(ny-y)+(nz-z)*(nz-z)) / feedrate * 60
74 x = nx
75 y = ny
76 z = nz
77 heatupTime = printTime - heatupTimeDelay
78 x = sx
79 y = sy
80 z = sz
81 if heatupTime > 0.0:
82 f.write("M104 T%d S%d\n" % (nextExtruder, coolTemperature))
83 f.write("T%d\n" % (activeExtruder))
84 else:
abed4315
CE
85 heatupTime = printTime / 2
86 # clinton: custom gcode sets cooltemp, need to counteract that for short layers
87 #f.write("M104 T%d S%d\n" % (nextExtruder, printTemperature - coolTemperatureDifference/2))
88 #f.write("T%d\n" % (activeExtruder))
c7006aad
CE
89 printTime = 0.0
90 for n in xrange(startIdx, idx):
91 line = lines[n]
92 if line.startswith(';'):
93 f.write(line)
abed4315
CE
94 if (line == ';HEATER_BLOCKED\n'):
95 heatupBlocked = True
96 elif (line == ';HEATER_UNBLOCKED\n'):
97 heatupBlocked = False
c7006aad
CE
98 continue
99 if heatupTime is not None:
100 g = getValue(line, 'G', None)
101 if g == 0 or g == 1:
102 nx = getValue(line, 'X', x)
103 ny = getValue(line, 'Y', y)
104 nz = getValue(line, 'Z', z)
105 feedrate = getValue(line, 'F', feedrate)
106 printTime += math.sqrt((nx-x)*(nx-x)+(ny-y)*(ny-y)+(nz-z)*(nz-z)) / feedrate * 60
abed4315 107 if (printTime > heatupTime) and (not heatupBlocked):
c7006aad
CE
108 f.write("M104 T%d S%d\n" % (nextExtruder, printTemperature))
109 f.write("T%d\n" % (activeExtruder))
110 heatupTime = None
111 x = nx
112 y = ny
113 z = nz
114 f.write(line)
115 f.write(lines[idx])
abed4315 116 heatupBlocked = False
c7006aad
CE
117 startIdx = idx + 1
118 activeExtruder = nextExtruder
119 idx += 1
120 for n in xrange(startIdx, idx):
121 f.write(lines[n])