Extruder.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. package Slic3r::Extruder;
  2. use Moo;
  3. use Slic3r::ExtrusionPath ':roles';
  4. use Slic3r::Geometry qw(scale unscale);
  5. has 'layer' => (is => 'rw');
  6. has 'shift_x' => (is => 'rw', default => sub {0} );
  7. has 'shift_y' => (is => 'rw', default => sub {0} );
  8. has 'z' => (is => 'rw', default => sub {0} );
  9. has 'speed' => (is => 'rw');
  10. has 'extrusion_distance' => (is => 'rw', default => sub {0} );
  11. has 'elapsed_time' => (is => 'rw', default => sub {0} ); # seconds
  12. has 'total_extrusion_length' => (is => 'rw', default => sub {0} );
  13. has 'retracted' => (is => 'rw', default => sub {1} ); # this spits out some plastic at start
  14. has 'lifted' => (is => 'rw', default => sub {0} );
  15. has 'last_pos' => (is => 'rw', default => sub { Slic3r::Point->new(0,0) } );
  16. has 'last_speed' => (is => 'rw', default => sub {""});
  17. has 'last_fan_speed' => (is => 'rw', default => sub {0});
  18. has 'dec' => (is => 'ro', default => sub { 3 } );
  19. # calculate speeds (mm/min)
  20. has 'speeds' => (
  21. is => 'ro',
  22. default => sub {{
  23. travel => 60 * $Slic3r::travel_speed,
  24. perimeter => 60 * $Slic3r::perimeter_speed,
  25. small_perimeter => 60 * $Slic3r::small_perimeter_speed,
  26. infill => 60 * $Slic3r::infill_speed,
  27. solid_infill => 60 * $Slic3r::solid_infill_speed,
  28. bridge => 60 * $Slic3r::bridge_speed,
  29. retract => 60 * $Slic3r::retract_speed,
  30. }},
  31. );
  32. my %role_speeds = (
  33. &EXTR_ROLE_PERIMETER => 'perimeter',
  34. &EXTR_ROLE_SMALLPERIMETER => 'small_perimeter',
  35. &EXTR_ROLE_CONTOUR_INTERNAL_PERIMETER => 'perimeter',
  36. &EXTR_ROLE_FILL => 'infill',
  37. &EXTR_ROLE_SOLIDFILL => 'solid_infill',
  38. &EXTR_ROLE_BRIDGE => 'bridge',
  39. &EXTR_ROLE_SKIRT => 'perimeter',
  40. &EXTR_ROLE_SUPPORTMATERIAL => 'perimeter',
  41. );
  42. use Slic3r::Geometry qw(points_coincide PI X Y);
  43. sub change_layer {
  44. my $self = shift;
  45. my ($layer) = @_;
  46. $self->layer($layer);
  47. my $z = $Slic3r::z_offset + $layer->print_z * $Slic3r::scaling_factor;
  48. my $gcode = "";
  49. $gcode .= $self->retract(move_z => $z);
  50. $gcode .= $self->G0(undef, $z, 0, 'move to next layer (' . $layer->id . ')')
  51. if $self->z != $z;
  52. $gcode .= Slic3r::Config->replace_options($Slic3r::layer_gcode) . "\n"
  53. if $Slic3r::layer_gcode;
  54. return $gcode;
  55. }
  56. sub extrude {
  57. my $self = shift;
  58. if ($_[0]->isa('Slic3r::ExtrusionLoop')) {
  59. $self->extrude_loop(@_);
  60. } else {
  61. $_[0]->deserialize;
  62. $self->extrude_path(@_);
  63. }
  64. }
  65. sub extrude_loop {
  66. my $self = shift;
  67. my ($loop, $description) = @_;
  68. # extrude all loops ccw
  69. $loop->deserialize;
  70. $loop->polygon->make_counter_clockwise;
  71. # find the point of the loop that is closest to the current extruder position
  72. # or randomize if requested
  73. my $last_pos = $self->last_pos;
  74. if ($Slic3r::randomize_start && $loop->role == EXTR_ROLE_CONTOUR_INTERNAL_PERIMETER) {
  75. srand $self->layer->id * 10;
  76. $last_pos = Slic3r::Point->new(scale $Slic3r::print_center->[X], scale $Slic3r::bed_size->[Y]);
  77. $last_pos->rotate(rand(2*PI), $Slic3r::print_center);
  78. }
  79. my $start_at = $loop->nearest_point_to($last_pos);
  80. # split the loop at the starting point and make a path
  81. my $extrusion_path = $loop->split_at($start_at);
  82. $extrusion_path->deserialize;
  83. # clip the path to avoid the extruder to get exactly on the first point of the loop;
  84. # if polyline was shorter than the clipping distance we'd get a null polyline, so
  85. # we discard it in that case
  86. $extrusion_path->clip_end(scale($self->layer->flow_width || $Slic3r::flow_width) * 0.15);
  87. return '' if !@{$extrusion_path->polyline};
  88. # extrude along the path
  89. return $self->extrude_path($extrusion_path, $description);
  90. }
  91. sub extrude_path {
  92. my $self = shift;
  93. my ($path, $description, $recursive) = @_;
  94. $path->merge_continuous_lines;
  95. # detect arcs
  96. if ($Slic3r::gcode_arcs && !$recursive) {
  97. my $gcode = "";
  98. $gcode .= $self->extrude_path($_, $description, 1) for $path->detect_arcs;
  99. return $gcode;
  100. }
  101. my $gcode = "";
  102. # retract if distance from previous position is greater or equal to the one
  103. # specified by the user *and* to the maximum distance between infill lines
  104. {
  105. my $distance_from_last_pos = $self->last_pos->distance_to($path->points->[0]) * $Slic3r::scaling_factor;
  106. my $distance_threshold = $Slic3r::retract_before_travel;
  107. $distance_threshold = 2 * ($self->layer->flow_width || $Slic3r::flow_width) / $Slic3r::fill_density * sqrt(2)
  108. if $Slic3r::fill_density > 0 && $description =~ /fill/;
  109. if ($distance_from_last_pos >= $distance_threshold) {
  110. $gcode .= $self->retract(travel_to => $path->points->[0]);
  111. }
  112. }
  113. # go to first point of extrusion path
  114. $gcode .= $self->G0($path->points->[0], undef, 0, "move to first $description point")
  115. if !points_coincide($self->last_pos, $path->points->[0]);
  116. # compensate retraction
  117. $gcode .= $self->unretract if $self->retracted;
  118. # calculate extrusion length per distance unit
  119. my $s = $path->flow_spacing || $self->layer->flow_spacing || $Slic3r::flow_spacing;
  120. my $h = $path->depth_layers * $self->layer->height;
  121. my $w = ($s - $Slic3r::min_flow_spacing * $Slic3r::overlap_factor) / (1 - $Slic3r::overlap_factor);
  122. my $area;
  123. if ($path->role == EXTR_ROLE_BRIDGE) {
  124. $area = ($s**2) * PI/4;
  125. } elsif ($w >= ($Slic3r::nozzle_diameter + $h)) {
  126. # rectangle with semicircles at the ends
  127. $area = $w * $h + ($h**2) / 4 * (PI - 4);
  128. } else {
  129. # rectangle with shrunk semicircles at the ends
  130. $area = $Slic3r::nozzle_diameter * $h * (1 - PI/4) + $h * $w * PI/4;
  131. }
  132. my $e = $Slic3r::scaling_factor
  133. * $area
  134. * $Slic3r::extrusion_multiplier
  135. * (4 / (($Slic3r::filament_diameter ** 2) * PI));
  136. # extrude arc or line
  137. $self->speed( $role_speeds{$path->role} || die "Unknown role: " . $path->role );
  138. my $path_length = 0;
  139. if ($path->isa('Slic3r::ExtrusionPath::Arc')) {
  140. $path_length = $path->length;
  141. $gcode .= $self->G2_G3($path->points->[-1], $path->orientation,
  142. $path->center, $e * $path_length, $description);
  143. } else {
  144. foreach my $line ($path->lines) {
  145. my $line_length = $line->length;
  146. $path_length += $line_length;
  147. $gcode .= $self->G1($line->b, undef, $e * $line_length, $description);
  148. }
  149. }
  150. if ($Slic3r::cooling) {
  151. my $path_time = unscale($path_length) / $self->speeds->{$self->last_speed} * 60;
  152. if ($self->layer->id == 0) {
  153. $path_time = $Slic3r::first_layer_speed =~ /^(\d+(?:\.\d+)?)%$/
  154. ? $path_time / ($1/100)
  155. : unscale($path_length) / $Slic3r::first_layer_speed * 60;
  156. }
  157. $self->elapsed_time($self->elapsed_time + $path_time);
  158. }
  159. return $gcode;
  160. }
  161. sub retract {
  162. my $self = shift;
  163. my %params = @_;
  164. return "" unless $Slic3r::retract_length > 0
  165. && !$self->retracted;
  166. # prepare moves
  167. $self->speed('retract');
  168. my $retract = [undef, undef, -$Slic3r::retract_length, "retract"];
  169. my $lift = ($Slic3r::retract_lift == 0 || defined $params{move_z})
  170. ? undef
  171. : [undef, $self->z + $Slic3r::retract_lift, 0, 'lift plate during retraction'];
  172. my $gcode = "";
  173. if (($Slic3r::g0 || $Slic3r::gcode_flavor eq 'mach3') && $params{travel_to}) {
  174. if ($lift) {
  175. # combine lift and retract
  176. $lift->[2] = $retract->[2];
  177. $gcode .= $self->G0(@$lift);
  178. } else {
  179. # combine travel and retract
  180. my $travel = [$params{travel_to}, undef, $retract->[2], 'travel and retract'];
  181. $gcode .= $self->G0(@$travel);
  182. }
  183. } elsif (($Slic3r::g0 || $Slic3r::gcode_flavor eq 'mach3') && defined $params{move_z}) {
  184. # combine Z change and retraction
  185. my $travel = [undef, $params{move_z}, $retract->[2], 'change layer and retract'];
  186. $gcode .= $self->G0(@$travel);
  187. } else {
  188. $gcode .= $self->G1(@$retract);
  189. if ($lift) {
  190. $gcode .= $self->G1(@$lift);
  191. }
  192. }
  193. $self->retracted(1);
  194. $self->lifted(1) if $lift;
  195. # reset extrusion distance during retracts
  196. # this makes sure we leave sufficient precision in the firmware
  197. if (!$Slic3r::use_relative_e_distances && $Slic3r::gcode_flavor ne 'mach3') {
  198. $gcode .= "G92 " . $Slic3r::extrusion_axis . "0\n" if $Slic3r::extrusion_axis;
  199. $self->extrusion_distance(0);
  200. }
  201. return $gcode;
  202. }
  203. sub unretract {
  204. my $self = shift;
  205. $self->retracted(0);
  206. my $gcode = "";
  207. if ($self->lifted) {
  208. $gcode .= $self->G0(undef, $self->z - $Slic3r::retract_lift, 0, 'restore layer Z');
  209. $self->lifted(0);
  210. }
  211. $self->speed('retract');
  212. $gcode .= $self->G0(undef, undef, ($Slic3r::retract_length + $Slic3r::retract_restart_extra),
  213. "compensate retraction");
  214. return $gcode;
  215. }
  216. sub set_acceleration {
  217. my $self = shift;
  218. my ($acceleration) = @_;
  219. return "" unless $Slic3r::acceleration;
  220. return sprintf "M201 E%s%s\n",
  221. $acceleration, ($Slic3r::gcode_comments ? ' ; adjust acceleration' : '');
  222. }
  223. sub G0 {
  224. my $self = shift;
  225. return $self->G1(@_) if !($Slic3r::g0 || $Slic3r::gcode_flavor eq 'mach3');
  226. return $self->_G0_G1("G0", @_);
  227. }
  228. sub G1 {
  229. my $self = shift;
  230. return $self->_G0_G1("G1", @_);
  231. }
  232. sub _G0_G1 {
  233. my $self = shift;
  234. my ($gcode, $point, $z, $e, $comment) = @_;
  235. my $dec = $self->dec;
  236. if ($point) {
  237. $gcode .= sprintf " X%.${dec}f Y%.${dec}f",
  238. ($point->x * $Slic3r::scaling_factor) + $self->shift_x,
  239. ($point->y * $Slic3r::scaling_factor) + $self->shift_y; #**
  240. $self->last_pos($point);
  241. }
  242. if (defined $z && $z != $self->z) {
  243. $self->z($z);
  244. $gcode .= sprintf " Z%.${dec}f", $z;
  245. }
  246. return $self->_Gx($gcode, $e, $comment);
  247. }
  248. sub G2_G3 {
  249. my $self = shift;
  250. my ($point, $orientation, $center, $e, $comment) = @_;
  251. my $dec = $self->dec;
  252. my $gcode = $orientation eq 'cw' ? "G2" : "G3";
  253. $gcode .= sprintf " X%.${dec}f Y%.${dec}f",
  254. ($point->x * $Slic3r::scaling_factor) + $self->shift_x,
  255. ($point->y * $Slic3r::scaling_factor) + $self->shift_y; #**
  256. # XY distance of the center from the start position
  257. $gcode .= sprintf " I%.${dec}f J%.${dec}f",
  258. ($center->[X] - $self->last_pos->[X]) * $Slic3r::scaling_factor,
  259. ($center->[Y] - $self->last_pos->[Y]) * $Slic3r::scaling_factor;
  260. $self->last_pos($point);
  261. return $self->_Gx($gcode, $e, $comment);
  262. }
  263. sub _Gx {
  264. my $self = shift;
  265. my ($gcode, $e, $comment) = @_;
  266. my $dec = $self->dec;
  267. # determine speed
  268. my $speed = ($e ? $self->speed : 'travel');
  269. # output speed if it's different from last one used
  270. # (goal: reduce gcode size)
  271. my $append_bridge_off = 0;
  272. if ($speed ne $self->last_speed) {
  273. if ($speed eq 'bridge') {
  274. $gcode = ";_BRIDGE_FAN_START\n$gcode";
  275. } elsif ($self->last_speed eq 'bridge') {
  276. $append_bridge_off = 1;
  277. }
  278. # apply the speed reduction for print moves on bottom layer
  279. my $speed_f = $self->speeds->{$speed};
  280. if ($e && $self->layer->id == 0 && $comment !~ /retract/) {
  281. $speed_f = $Slic3r::first_layer_speed =~ /^(\d+(?:\.\d+)?)%$/
  282. ? ($speed_f * $1/100)
  283. : $Slic3r::first_layer_speed;
  284. }
  285. $gcode .= sprintf " F%.${dec}f", $speed_f;
  286. $self->last_speed($speed);
  287. }
  288. # output extrusion distance
  289. if ($e && $Slic3r::extrusion_axis) {
  290. $self->extrusion_distance(0) if $Slic3r::use_relative_e_distances;
  291. $self->extrusion_distance($self->extrusion_distance + $e);
  292. $self->total_extrusion_length($self->total_extrusion_length + $e);
  293. $gcode .= sprintf " %s%.5f", $Slic3r::extrusion_axis, $self->extrusion_distance;
  294. }
  295. $gcode .= sprintf " ; %s", $comment if $comment && $Slic3r::gcode_comments;
  296. if ($append_bridge_off) {
  297. $gcode .= "\n;_BRIDGE_FAN_END";
  298. }
  299. return "$gcode\n";
  300. }
  301. sub set_tool {
  302. my $self = shift;
  303. my ($tool) = @_;
  304. return $self->retract . sprintf "T%d%s\n", $tool, ($Slic3r::gcode_comments ? ' ; change tool' : '');
  305. }
  306. sub set_fan {
  307. my $self = shift;
  308. my ($speed, $dont_save) = @_;
  309. if ($self->last_fan_speed != $speed || $dont_save) {
  310. $self->last_fan_speed($speed) if !$dont_save;
  311. if ($speed == 0) {
  312. return sprintf "M107%s\n", ($Slic3r::gcode_comments ? ' ; disable fan' : '');
  313. } else {
  314. return sprintf "M106 %s%d%s\n", ($Slic3r::gcode_flavor eq 'mach3' ? 'P' : 'S'),
  315. (255 * $speed / 100), ($Slic3r::gcode_comments ? ' ; enable fan' : '');
  316. }
  317. }
  318. return "";
  319. }
  320. sub set_temperature {
  321. my $self = shift;
  322. my ($temperature, $wait) = @_;
  323. return "" if $wait && $Slic3r::gcode_flavor eq 'makerbot';
  324. my ($code, $comment) = $wait
  325. ? ('M109', 'wait for temperature to be reached')
  326. : ('M104', 'set temperature');
  327. return sprintf "$code %s%d ; $comment\n",
  328. ($Slic3r::gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature;
  329. }
  330. sub set_bed_temperature {
  331. my $self = shift;
  332. my ($temperature, $wait) = @_;
  333. my ($code, $comment) = $wait
  334. ? (($Slic3r::gcode_flavor eq 'makerbot' ? 'M109' : 'M190'), 'wait for bed temperature to be reached')
  335. : ('M140', 'set bed temperature');
  336. return sprintf "$code %s%d ; $comment\n",
  337. ($Slic3r::gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature;
  338. }
  339. 1;