forked from okke-formsma/cura-temp-tower-script
-
Notifications
You must be signed in to change notification settings - Fork 4
/
TempTower.py
110 lines (94 loc) · 3.63 KB
/
TempTower.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# -*- coding: utf-8 -*-
import json
import re
from ..Script import Script
class TempTower(Script):
def __init__(self):
super().__init__()
def getSettingDataString(self):
return json.dumps({
'name': 'Temp Tower',
'key': 'TempTower',
'metadata': {},
'version': 2,
'settings': {
'start_temperature': {
'label': 'Start Temperature',
'description': 'Initial nozzle temperature',
'unit': '°C',
'type': 'int',
'default_value': 265
},
'height_increment': {
'label': 'Height Increment',
'description': (
'Adjust temperature each time height param '
'changes by this much'
),
'unit': 'mm',
'type': 'int',
'default_value': 10
},
'temperature_increment': {
'label': 'Temperature Increment',
'description': (
'Increase temperature by this much with each height increment. '
'Use negative values for towers that become gradually cooler.'
),
'unit': '°C',
'type': 'int',
'default_value': -5
},
'start_height': {
'label': 'Start Height ',
'description': (
'Start the temperature tower at this height.'
),
'unit': 'mm',
'type': 'float',
'default_value': 1.4
}
}
})
def execute(self, data):
start_temp = self.getSettingValueByKey('start_temperature')
height_inc = self.getSettingValueByKey('height_increment')
temp_inc = self.getSettingValueByKey('temperature_increment')
start_height = self.getSettingValueByKey('start_height')
cmd_re = re.compile(
r'G[0-9]+ '
r'(?:F[0-9]+ )?'
r'X[0-9]+\.?[0-9]* '
r'Y[0-9]+\.?[0-9]* '
r'Z(-?[0-9]+\.?[0-9]*)'
)
# Set initial state
current_temp = 0
started = False
for i, layer in enumerate(data):
lines = layer.split('\n')
for j, line in enumerate(lines):
# get the layer height
if line.startswith(';Layer height: ') :
layer_height = float(line.strip(';Layer height: '))
continue
# Before ;LAYER:0 arbitrary setup GCODE can be run.
if line == ';LAYER:0':
started = True
continue
# skip comments and startup lines
if line.startswith(';') or not started:
continue
# Find any X,Y,Z Line (ex. G0 X60.989 Y60.989 Z1.77)
match = cmd_re.match(line)
if match is None:
continue
z = float(match.groups()[0]) - layer_height
if z < start_height :
continue
new_temp = start_temp + int((z - start_height) / height_inc) * temp_inc
if new_temp != current_temp:
current_temp = new_temp
lines[j] += '\n;TYPE:CUSTOM\nM104 S%d' % new_temp
data[i] = '\n'.join(lines)
return data