improvements to cura dual extrusion plugin
[clinton/3d-models.git] / cura / dual_temp_lower.py
index 05cf520..6d27bb6 100644 (file)
@@ -2,13 +2,14 @@
 #Info: Lower the temperature for the nozzle that is not printing
 #Depend: GCode
 #Type: postprocess
-#Param: printTemperature(float:220.0) Print temperature (C)
-#Param: coolTemperature(float:170.0) Cool nozzle temperature (C)
+#Param: coolTemperatureDifference(float:50.0) Drop idle nozzle temperature (C)
 #Param: heatupTimeDelay(float:20.0) Heatup nozzle before use (sec)
 
-__copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
+
+__copyright__ = "Copyright (C) 2013 David Braam, Copyright (C) 2016 Clinton Ebadi <clinton@unknownlamer.org> - Released under terms of the AGPLv3 License"
 import re
 import math
+from Cura.util import profile
 
 def getValue(line, key, default = None):
        if not key in line or (';' in line and line.find(key) > line.find(';')):
@@ -29,14 +30,30 @@ z = 0.
 x = 0.
 y = 0.
 e = 0.
-feedrate = 5000
+feedrate = float(profile.getProfileSetting('print_speed'))
 currentSectionType = 'CUSTOM'
 activeExtruder = -1
+
+# todo: move custom gcode to here
+# detect priming tower and inject reheat there
+
+# grab setting from the profile, add a tag at the end of postSwitch,
+# inject right before the move that sets E0
+
+# todo: use 2nd/3rd/etc extruder temp if available (arrays are alright...)
+printTemperature = float(profile.getProfileSetting('print_temperature'))
+coolTemperature = printTemperature - coolTemperatureDifference
 with open(filename, "w") as f:
-       startIdx = 0
-       idx = 0
-       while idx < len(lines):
+        # skip start gcode
+       idx = lines.index (';LAYER:0\n')
+        startIdx = idx
+        for line in lines[0:idx+1]:
+                if getValue(line, 'T', None) is not None and getValue(line, 'M', None) is None:
+                       activeExtruder = getValue(line, 'T', None)
+                f.write (line)
+        while idx < len(lines):
                line = lines[idx]
+                heatupBlocked = False
                if getValue(line, 'T', None) is not None and getValue(line, 'M', None) is None:
                        nextExtruder = getValue(line, 'T', None)
                        printTime = 0.0
@@ -65,12 +82,19 @@ with open(filename, "w") as f:
                                f.write("M104 T%d S%d\n" % (nextExtruder, coolTemperature))
                                f.write("T%d\n" % (activeExtruder))
                        else:
-                               heatupTime = None
+                               heatupTime = printTime / 2
+                                # clinton: custom gcode sets cooltemp, need to counteract that for short layers
+                                #f.write("M104 T%d S%d\n" % (nextExtruder, printTemperature - coolTemperatureDifference/2))
+                               #f.write("T%d\n" % (activeExtruder))
                        printTime = 0.0
                        for n in xrange(startIdx, idx):
                                line = lines[n]
                                if line.startswith(';'):
                                        f.write(line)
+                                        if (line == ';HEATER_BLOCKED\n'):
+                                                heatupBlocked = True
+                                        elif (line == ';HEATER_UNBLOCKED\n'):
+                                                heatupBlocked = False
                                        continue
                                if heatupTime is not None:
                                        g = getValue(line, 'G', None)
@@ -80,7 +104,7 @@ with open(filename, "w") as f:
                                                nz = getValue(line, 'Z', z)
                                                feedrate = getValue(line, 'F', feedrate)
                                                printTime += math.sqrt((nx-x)*(nx-x)+(ny-y)*(ny-y)+(nz-z)*(nz-z)) / feedrate * 60
-                                               if printTime > heatupTime:
+                                               if (printTime > heatupTime) and (not heatupBlocked):
                                                        f.write("M104 T%d S%d\n" % (nextExtruder, printTemperature))
                                                        f.write("T%d\n" % (activeExtruder))
                                                        heatupTime = None
@@ -89,6 +113,7 @@ with open(filename, "w") as f:
                                                z = nz
                                f.write(line)
                        f.write(lines[idx])
+                        heatupBlocked = False
                        startIdx = idx + 1
                        activeExtruder = nextExtruder
                idx += 1