cura: properly working 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 #Param: zHop(float:0.0) Raise Z when reheating active extruder (mm)
8
9
10 __copyright__ = "Copyright (C) 2013 David Braam, Copyright (C) 2016 Clinton Ebadi <clinton@unknownlamer.org> - Released under terms of the AGPLv3 License"
11 import re
12 import math
13 from Cura.util import profile
14
15 def 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
27 with open(filename, "r") as f:
28 lines = f.readlines()
29
30 z = 0.
31 x = 0.
32 y = 0.
33 e = 0.
34 feedrate = float(profile.getProfileSetting('print_speed'))
35 wipeTowerP = profile.getProfileSetting('wipe_tower') == 'True'
36 currentSectionType = 'CUSTOM'
37 activeExtruder = -1
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...)
46 printTemperature = float(profile.getProfileSetting('print_temperature'))
47 coolTemperature = printTemperature - coolTemperatureDifference
48 with open(filename, "w") as f:
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):
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
78 heatupTime = max (printTime - heatupTimeDelay, 0.0)
79 heatupBlocked = True # do not reheat until active extruder is heated
80 x = sx
81 y = sy
82 z = sz
83
84 f.write("M104 T%d S%d\n" % (nextExtruder, coolTemperature))
85 f.write("T%d\n" % (activeExtruder))
86
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
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
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
114 if (printTime > heatupTime) and (not heatupBlocked):
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])
123 startIdx = idx + 1
124 activeExtruder = nextExtruder
125 idx += 1
126 for n in xrange(startIdx, idx):
127 f.write(lines[n])