summaryrefslogtreecommitdiffstats
path: root/bin/slic3r-gpp
blob: 0c4b7fffe7f776beecb8a6412c691b7bd051293e (plain)
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
#!/bin/sh

set -eu

SLIC3R_CONFIG_BASE="${HOME}/.Slic3r"
TEMPERATURES='retraction wiping probing removal_bed'

warn()
{
	local fmt="${1}"
	shift 1

	printf "Warning: ${fmt}\n" "${@}"
	return 0
}

preprocess()
{
	local printer="${1}"
	local gcode="${2}"
	local filament="${3}"
	shift 3
	local ini=
	local temp=
	local temp_val=
	local script=
	local gcode_pp=

	# Find and back up printer/filament configuration
	ini="${SLIC3R_CONFIG_BASE}/printer/${printer}, ${filament}.ini"
	if ! cp "${ini}" "${ini}~" 2>/dev/null; then
		warn 'Missing configuration for printer "%s" and filament "%s"'\
			"${printer}" "${filament}"
		return 0
	fi

	# Load and validate filament temperature settings
	for temp in ${TEMPERATURES}; do
		eval "local ${temp}_temperature="
	done
	. "${SLIC3R_CONFIG_BASE}/filament/${filament}.rc"
	for temp in ${TEMPERATURES}; do
		temp_val="$(eval "printf '%s' \"\${${temp}_temperature}\"")"
		case "${temp_val}" in
			'')
				eval "${temp}_temperature=0"
				;;
			*[!0-9]*)
				warn 'Invalid "%s" value "%s" of filament "%s"'\
					"${temp}_temperature" "${temp_val}" \
					"${filament}"
				return 0
				;;
		esac
	done

	# Preprocess G-code and insert into printer/filament configuration
	script='1h; 1!H; ${ g; s/\n/\\n/g; '
	for temp in ${TEMPERATURES}; do
		temp="${temp}_temperature"
		eval "script=\"\${script}s/<\${temp}>/\${${temp}}/g; \""
	done
	script="${script}"'s|[\/&]|\\&|g; p; };'
	gcode_pp="${SLIC3R_CONFIG_BASE}/printer/${printer} - ${gcode}.gcode"
	gcode_pp="$(sed -n "${script}" 0<"${gcode_pp}")"
	sed "s/^\\(${gcode}_gcode *= *\\).*\$/\\1${gcode_pp}/;" \
		"${ini}~" 1>"${ini}"

	return 0
}

preprocess_all()
{
	local gcode=
	local printer=
	local rc=
	local filament=

	for gcode in "${SLIC3R_CONFIG_BASE}/printer/"*' - '*'.gcode'; do
		gcode="${gcode##*/}"
		printer="${gcode% - *.gcode}"
		gcode="${gcode##${printer} - }"
		gcode="${gcode%.gcode}"
		for rc in "${SLIC3R_CONFIG_BASE}/filament/"*'.rc'; do
			filament="${rc##*/}"
			filament="${filament%.rc}"
			preprocess "${printer}" "${gcode}" "${filament}"
		done
	done

	return 0
}

main()
{
	preprocess_all
	return 0
}

main "${@}"