forked from tkremer/slic3r_config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdualprint-postprocess.pl
executable file
·122 lines (110 loc) · 4.31 KB
/
dualprint-postprocess.pl
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
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/perl
# - duplicate all heating commands, once for each heater.
# - duplicate all motionless extrusion and retraction.
# - convert all extrusive motion to small double-extrusion plus movement pairs.
# post-processing scripts get the slic3r gcode file as parameter
# (to be edited in-place) and the slic3r settings in the environment.
# apparently the slic3r environment doesn't work (no variables set).
# However, the slic3r config is available as comments at the end of the gcode file.
use strict;
use warnings;
use POSIX qw(ceil);
use List::Util qw(any);
# minimum E way per extrusion. TODO: maybe this is too high?
my $de = 0.1;
# FIXME: printer stutters. Probably too many commands per second? Or the accelleration logic can't optimize the move/extrude switches? Can we get rid of the tool changes and provide a T parameter in G0/G1?
#my $pos = [0,0,0,0,0];
my $pos = { qw(X 0 Y 0 Z 0 E 0 F 0) };
my $tool = 0;
my $processing = 1;
my $lines = 0;
my $endline = undef;
# FIXME: we assume a lot here: absolute coordinates, millimeters, two extruders, no circles,...
local $^I="";
while (<>) {
s/;.*//; s/^\s*//;
next if $_ eq "";
if ($processing && /^([GMT])(\d+)\s+(.*)/) {
my ($mode,$code,$args) = ($1,$2,$3);
if ($mode eq "M") {
if ($code == 104) {
$_ = "M104 T0 $args\nM104 T1 $args\n";
} elsif ($code == 109) {
$_ = "M104 T0 $args\nM104 T1 $args\nM109 T0 $args\nM109 T1 $args\n";
}
} elsif ($mode eq "G") {
#if ($code == 0 || $code == 1 || $code == 28 || $code == 92) {
my %xyzef;
my @axes;
my $old_pos = $pos;
while ($args =~ /\b([XYZEF])(\S*)/g) {
$xyzef{$1} = $2+0;
push @axes, $1;
}
# Don't use a hash slice here! grep and any impose an lvalue
# context on the slice and thus auto-vivificate the fields.
my $has_pos = any { exists $xyzef{$_} } qw(X Y Z);
#die "has_pos = 0" if !$has_pos && ($xyzef{X} || $xyzef{Y} || $xyzef{Z});
my $has_e = exists $xyzef{E};
# for (qw(X Y Z E F)) {
# die "pos{$_} not defined: ".join(",",map "$_$pos->{$_}",keys %$pos) if !defined $pos->{$_};
# die "old_pos{$_} not defined: ".join(",",map "$_$old_pos->{$_}",keys %$old_pos) if !defined $old_pos->{$_};
# }
# for (keys %xyzef) {
# die "xyzef{$_} not defined: ".join(",",map "$_$xyzef{$_}",keys %xyzef) if !defined $xyzef{$_};
# }
if (%xyzef) {
$old_pos = {%$old_pos};
$pos->{$_} = $xyzef{$_} for keys %xyzef;
}
# for (qw(X Y Z E F)) {
# die "pos{$_} not defined: ".join(",",map "$_$pos->{$_}",keys %$pos) if !defined $pos->{$_};
# die "old_pos{$_} not defined: ".join(",",map "$_$old_pos->{$_}",keys %$old_pos) if !defined $old_pos->{$_};
# }
if ($code == 1 || $code == 0) {
if ($has_e) {
if ($has_pos) {
# moving with extrusion.
my $delta_e = $pos->{E}-$old_pos->{E};
my $count = ceil($delta_e/$de);
my $way = "";
my $last_E = $old_pos->{E};
for my $i (1..$count) {
my %p = %xyzef;
$p{$_} += ($old_pos->{$_}-$p{$_})*($count-$i)/$count for keys %p;
#my %p = %$old_pos;
#$p{$_} += ($pos->{$_}-$p{$_})*$i/$count for keys %p;
my $newargs = join(" ",map "$_$p{$_}", @axes);
$tool = 1-$tool;
# extrude, change tool, reset E, move-extrude.
$way .= "G$code E$p{E}\nT$tool\nG92 E$last_E\nG$code $newargs\n";
$last_E = $p{E};
}
#$_ = ";way: ".
$_ = join(",",@axes)."\n".$way;
} else {
# standing still, extruding. -> duplicate
$tool = 1-$tool;
#$_ = ";still:\n".
$_ = "G$code $args\nT$tool\nG92 E$old_pos->{E}\nG$code $args\n";
}
} # otherwise just movement. No problem.
#else {
# $_ = ";move:\n".$_;
#}
#} elsif ($code == 92) { # nothing to do (anymore)
}
} else { #T
#die "file already uses multiple tools!"
$processing = 0;
$endline = $lines;
# assume we're at the tool deactivation stage. Don't meddle with that.
}
}
$lines++;
print;
}
if (!$processing && $endline/$lines < 0.2) {
die "almost nothing processed due to an early tool change!";
}
exit 0;