improvements to cura dual extrusion plugin
[clinton/3d-models.git] / cura / dual_temp_lower.py
1 #Name: Dual Temp Test
2 #Info: Lower the temperature for the nozzle that is not printing
3 #Depend: GCode
4 #Type: postprocess
5 #Param: coolTemperatureDifference(float:50.0) Drop idle nozzle temperature (C)
6 #Param: heatupTimeDelay(float:20.0) Heatup nozzle before use (sec)
7
8
9 __copyright__ = "Copyright (C) 2013 David Braam, Copyright (C) 2016 Clinton Ebadi <clinton@unknownlamer.org> - Released under terms of the AGPLv3 License"
10 import re
11 import math
12 from Cura.util import profile
13
14 def 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
26 with open(filename, "r") as f:
27 lines = f.readlines()
28
29 z = 0.
30 x = 0.
31 y = 0.
32 e = 0.
33 feedrate = float(profile.getProfileSetting('print_speed'))
34 currentSectionType = 'CUSTOM'
35 activeExtruder = -1
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...)
44 printTemperature = float(profile.getProfileSetting('print_temperature'))
45 coolTemperature = printTemperature - coolTemperatureDifference
46 with open(filename, "w") as f:
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):
55 line = lines[idx]
56 heatupBlocked = False
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:
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))
89 printTime = 0.0
90 for n in xrange(startIdx, idx):
91 line = lines[n]
92 if line.startswith(';'):
93 f.write(line)
94 if (line == ';HEATER_BLOCKED\n'):
95 heatupBlocked = True
96 elif (line == ';HEATER_UNBLOCKED\n'):
97 heatupBlocked = False
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
107 if (printTime > heatupTime) and (not heatupBlocked):
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])
116 heatupBlocked = False
117 startIdx = idx + 1
118 activeExtruder = nextExtruder
119 idx += 1
120 for n in xrange(startIdx, idx):
121 f.write(lines[n])