-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnLayerChange.py
More file actions
75 lines (60 loc) · 2.25 KB
/
OnLayerChange.py
File metadata and controls
75 lines (60 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from ..Script import Script
MESSAGE = """;TYPE:CUSTOM
;added code by post processing
;script: OnLayerChange.py action no: {0}
;DBG: action_no: {0}, layer_actioned: {1}, repeat_every: {2}
"""
class OnLayerChange(Script):
def __init__(self):
super().__init__()
def getSettingDataString(self):
return """{
"name": "On layer change",
"key": "OnLayerChange",
"metadata": {},
"version": 2,
"settings":
{
"action":
{
"label": "Action",
"description": "Action on layer change. Separate by | to alternate layers. Use {} to insert layer no",
"type": "str",
"default_value": ""
},
"repeat":
{
"label": "Repeat",
"description": "Repeat every layer no",
"type": "int",
"default_value": 1
}
}
}"""
def execute(self, data: list):
"""data is a list. Each index contains a layer"""
action = self.getSettingValueByKey("action").split("|")
repeat_every = self.getSettingValueByKey("repeat")
layer_actioned = 0
no_of_actions = len(action)
for index, layer in enumerate(data):
lines = layer.split("\n")
for line in lines:
if not line.startswith(";LAYER:"):
continue
try:
layer_no = int(line[len(";LAYER:"):])
except ValueError:
# Couldn't cast to int. Something is wrong with this g-code data.
continue
if layer_no % repeat_every != 0:
continue
action_no = layer_actioned % no_of_actions
prepend_gcode = MESSAGE.format(action_no, layer_actioned, repeat_every)
prepend_gcode += action[action_no].format(layer_no) + "\n"
layer = prepend_gcode + layer
# Override the data of this layer with the
# modified data
data[index] = layer
layer_actioned += 1
return data