Merge pull request #3648 from JPFrancoia/master

Bug fixes and improvements for PauseAtHeight plugin
This commit is contained in:
ChrisTerBeke 2018-05-24 10:56:16 +02:00 committed by GitHub
commit 7d0b664c53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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: <current_height> = <current_z> - <layer_0_z>
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.
# 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)
@ -225,7 +274,10 @@ class PauseAtHeight(Script):
# Move the head away
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"