diff --git a/plugins/PostProcessingPlugin/scripts/PauseAtHeight.py b/plugins/PostProcessingPlugin/scripts/PauseAtHeight.py index 805ab0a2c3..4795aa3e89 100644 --- a/plugins/PostProcessingPlugin/scripts/PauseAtHeight.py +++ b/plugins/PostProcessingPlugin/scripts/PauseAtHeight.py @@ -117,6 +117,28 @@ class PauseAtHeight(Script): } }""" + + def getNextXY(self, layer: str): + + """ + Get the X and Y values for a layer (will be used to get X and Y of + the layer after the pause + """ + + lines = layer.split("\n") + + for line in lines: + + if self.getValue(line, "X") is not None and self.getValue(line, "Y") is not None: + + x = self.getValue(line, "X") + y = self.getValue(line, "Y") + + return (x, y) + + return (0, 0) + + def execute(self, data: list): """data is a list. Each index contains a layer""" @@ -138,48 +160,72 @@ class PauseAtHeight(Script): resume_temperature = self.getSettingValueByKey("resume_temperature") # T = ExtruderManager.getInstance().getActiveExtruderStack().getProperty("material_print_temperature", "value") - # with open("out.txt", "w") as f: - # f.write(T) # use offset to calculate the current height: = - layer_0_z = 0. current_z = 0 got_first_g_cmd_on_layer_0 = False + + nbr_negative_layers = 0 + for index, layer in enumerate(data): lines = layer.split("\n") + + # Scroll each line of instruction for each layer in the G-code for line in lines: + + # Fist positive layer reached if ";LAYER:0" in line: layers_started = True + + # Count nbr of negative layers (raft) + elif ";LAYER:-" in line: + nbr_negative_layers += 1 + if not layers_started: continue + # If a Z instruction is in the line, read the current Z if self.getValue(line, "Z") is not None: current_z = self.getValue(line, "Z") if pause_at == "height": + + # Ignore if the line is not G1 or G0 if self.getValue(line, "G") != 1 and self.getValue(line, "G") != 0: continue + # This block is executed once, the first time there is a G + # command, to get the z offset (z for first positive layer) if not got_first_g_cmd_on_layer_0: layer_0_z = current_z got_first_g_cmd_on_layer_0 = True - x = self.getValue(line, "X", x) - y = self.getValue(line, "Y", y) - current_height = current_z - layer_0_z + if current_height < pause_height: - break #Try the next layer. - else: #Pause at layer. + break # Try the next layer. + + # Pause at layer + else: + if not line.startswith(";LAYER:"): continue current_layer = line[len(";LAYER:"):] try: current_layer = int(current_layer) - except ValueError: #Couldn't cast to int. Something is wrong with this g-code data. + + # Couldn't cast to int. Something is wrong with this + # g-code data + except ValueError: continue - if current_layer < pause_layer: - break #Try the next layer. + if current_layer < pause_layer - nbr_negative_layers: + continue + + # Get X and Y from the next layer (better position for + # the nozzle) + nextLayer = data[index + 1] + x, y = self.getNextXY(nextLayer) prevLayer = data[index - 1] prevLines = prevLayer.split("\n") @@ -201,6 +247,9 @@ class PauseAtHeight(Script): # begining of the first layer redone # see https://github.com/nallath/PostProcessingPlugin/issues/55 if i == redo_layers: + # Get X and Y from the next layer (better position for + # the nozzle) + x, y = self.getNextXY(layer) prevLines = prevLayer.split("\n") for line in prevLines: new_e = self.getValue(line, 'E', current_e) @@ -213,57 +262,60 @@ class PauseAtHeight(Script): prepend_gcode += ";added code by post processing\n" prepend_gcode += ";script: PauseAtHeight.py\n" if pause_at == "height": - prepend_gcode += ";current z: {z}\n".format(z = current_z) - prepend_gcode += ";current height: {height}\n".format(height = current_height) + prepend_gcode += ";current z: {z}\n".format(z=current_z) + prepend_gcode += ";current height: {height}\n".format(height=current_height) else: - prepend_gcode += ";current layer: {layer}\n".format(layer = current_layer) + prepend_gcode += ";current layer: {layer}\n".format(layer=current_layer) # Retraction - prepend_gcode += self.putValue(M = 83) + "\n" + prepend_gcode += self.putValue(M=83) + "\n" if retraction_amount != 0: - prepend_gcode += self.putValue(G = 1, E = -retraction_amount, F = retraction_speed * 60) + "\n" + prepend_gcode += self.putValue(G=1, E=-retraction_amount, F=retraction_speed * 60) + "\n" # Move the head away - prepend_gcode += self.putValue(G = 1, Z = current_z + 1, F = 300) + "\n" - prepend_gcode += self.putValue(G = 1, X = park_x, Y = park_y, F = 9000) + "\n" + prepend_gcode += self.putValue(G=1, Z=current_z + 1, F=300) + "\n" + + # This line should be ok + prepend_gcode += self.putValue(G=1, X=park_x, Y=park_y, F=9000) + "\n" + if current_z < 15: - prepend_gcode += self.putValue(G = 1, Z = 15, F = 300) + "\n" + prepend_gcode += self.putValue(G=1, Z=15, F=300) + "\n" # Disable the E steppers - prepend_gcode += self.putValue(M = 84, E = 0) + "\n" + prepend_gcode += self.putValue(M=84, E=0) + "\n" # Set extruder standby temperature - prepend_gcode += self.putValue(M = 104, S = standby_temperature) + "; standby temperature\n" + prepend_gcode += self.putValue(M=104, S=standby_temperature) + "; standby temperature\n" # Wait till the user continues printing - prepend_gcode += self.putValue(M = 0) + ";Do the actual pause\n" + prepend_gcode += self.putValue(M=0) + ";Do the actual pause\n" # Set extruder resume temperature - prepend_gcode += self.putValue(M = 109, S = resume_temperature) + "; resume temperature\n" + prepend_gcode += self.putValue(M=109, S=resume_temperature) + "; resume temperature\n" # Push the filament back, if retraction_amount != 0: - prepend_gcode += self.putValue(G = 1, E = retraction_amount, F = retraction_speed * 60) + "\n" + prepend_gcode += self.putValue(G=1, E=retraction_amount, F=retraction_speed * 60) + "\n" # Optionally extrude material if extrude_amount != 0: - prepend_gcode += self.putValue(G = 1, E = extrude_amount, F = extrude_speed * 60) + "\n" + prepend_gcode += self.putValue(G=1, E=extrude_amount, F=extrude_speed * 60) + "\n" # and retract again, the properly primes the nozzle # when changing filament. if retraction_amount != 0: - prepend_gcode += self.putValue(G = 1, E = -retraction_amount, F = retraction_speed * 60) + "\n" + prepend_gcode += self.putValue(G=1, E=-retraction_amount, F=retraction_speed * 60) + "\n" # Move the head back - prepend_gcode += self.putValue(G = 1, Z = current_z + 1, F = 300) + "\n" - prepend_gcode += self.putValue(G = 1, X = x, Y = y, F = 9000) + "\n" + prepend_gcode += self.putValue(G=1, Z=current_z + 1, F=300) + "\n" + prepend_gcode += self.putValue(G=1, X=x, Y=y, F=9000) + "\n" if retraction_amount != 0: - prepend_gcode += self.putValue(G = 1, E = retraction_amount, F = retraction_speed * 60) + "\n" - prepend_gcode += self.putValue(G = 1, F = 9000) + "\n" - prepend_gcode += self.putValue(M = 82) + "\n" + prepend_gcode += self.putValue(G=1, E=retraction_amount, F=retraction_speed * 60) + "\n" + prepend_gcode += self.putValue(G=1, F=9000) + "\n" + prepend_gcode += self.putValue(M=82) + "\n" # reset extrude value to pre pause value - prepend_gcode += self.putValue(G = 92, E = current_e) + "\n" + prepend_gcode += self.putValue(G=92, E=current_e) + "\n" layer = prepend_gcode + layer