GCode.pm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. package Slic3r::GCode;
  2. use Moo;
  3. use List::Util qw(min first);
  4. use Slic3r::ExtrusionPath ':roles';
  5. use Slic3r::Flow ':roles';
  6. use Slic3r::Geometry qw(epsilon scale unscale scaled_epsilon points_coincide PI X Y B);
  7. use Slic3r::Geometry::Clipper qw(union_ex);
  8. use Slic3r::Surface ':types';
  9. has 'print_config' => (is => 'ro', default => sub { Slic3r::Config::Print->new });
  10. has 'extra_variables' => (is => 'rw', default => sub {{}});
  11. has 'standby_points' => (is => 'rw');
  12. has 'enable_loop_clipping' => (is => 'rw', default => sub {1});
  13. has 'enable_wipe' => (is => 'rw', default => sub {0}); # at least one extruder has wipe enabled
  14. has 'layer_count' => (is => 'ro', required => 1 );
  15. has 'layer' => (is => 'rw');
  16. has 'region' => (is => 'rw');
  17. has '_layer_islands' => (is => 'rw');
  18. has '_upper_layer_islands' => (is => 'rw');
  19. has '_layer_overhangs' => (is => 'ro', default => sub { Slic3r::ExPolygon::Collection->new });
  20. has 'shift_x' => (is => 'rw', default => sub {0} );
  21. has 'shift_y' => (is => 'rw', default => sub {0} );
  22. has 'z' => (is => 'rw');
  23. has 'speed' => (is => 'rw');
  24. has '_extrusion_axis' => (is => 'rw');
  25. has '_retract_lift' => (is => 'rw');
  26. has 'extruders' => (is => 'ro', default => sub {{}});
  27. has 'extruder' => (is => 'rw');
  28. has 'speeds' => (is => 'lazy'); # mm/min
  29. has 'external_mp' => (is => 'rw');
  30. has 'layer_mp' => (is => 'rw');
  31. has 'new_object' => (is => 'rw', default => sub {0});
  32. has 'straight_once' => (is => 'rw', default => sub {1});
  33. has 'elapsed_time' => (is => 'rw', default => sub {0} ); # seconds
  34. has 'lifted' => (is => 'rw', default => sub {0} );
  35. has 'last_pos' => (is => 'rw', default => sub { Slic3r::Point->new(0,0) } );
  36. has 'last_speed' => (is => 'rw', default => sub {""});
  37. has 'last_f' => (is => 'rw', default => sub {""});
  38. has 'last_fan_speed' => (is => 'rw', default => sub {0});
  39. has 'wipe_path' => (is => 'rw');
  40. sub BUILD {
  41. my ($self) = @_;
  42. $self->_extrusion_axis($self->print_config->get_extrusion_axis);
  43. $self->_retract_lift($self->print_config->retract_lift->[0]);
  44. }
  45. sub set_extruders {
  46. my ($self, $extruder_ids) = @_;
  47. foreach my $i (@$extruder_ids) {
  48. $self->extruders->{$i} = my $e = Slic3r::Extruder->new_from_config($self->print_config, $i);
  49. $self->enable_wipe(1) if $e->wipe;
  50. }
  51. }
  52. sub _build_speeds {
  53. my $self = shift;
  54. return {
  55. map { $_ => 60 * $self->print_config->get_value("${_}_speed") }
  56. qw(travel perimeter small_perimeter external_perimeter infill
  57. solid_infill top_solid_infill bridge gap_fill retract),
  58. };
  59. }
  60. # assign speeds to roles
  61. my %role_speeds = (
  62. &EXTR_ROLE_PERIMETER => 'perimeter',
  63. &EXTR_ROLE_EXTERNAL_PERIMETER => 'external_perimeter',
  64. &EXTR_ROLE_OVERHANG_PERIMETER => 'bridge',
  65. &EXTR_ROLE_CONTOUR_INTERNAL_PERIMETER => 'perimeter',
  66. &EXTR_ROLE_FILL => 'infill',
  67. &EXTR_ROLE_SOLIDFILL => 'solid_infill',
  68. &EXTR_ROLE_TOPSOLIDFILL => 'top_solid_infill',
  69. &EXTR_ROLE_BRIDGE => 'bridge',
  70. &EXTR_ROLE_INTERNALBRIDGE => 'solid_infill',
  71. &EXTR_ROLE_SKIRT => 'perimeter',
  72. &EXTR_ROLE_GAPFILL => 'gap_fill',
  73. );
  74. sub multiple_extruders {
  75. my $self = shift;
  76. return (keys %{$self->extruders}) > 1;
  77. }
  78. sub set_shift {
  79. my ($self, @shift) = @_;
  80. # if shift increases (goes towards right), last_pos decreases because it goes towards left
  81. my @translate = (
  82. scale ($self->shift_x - $shift[X]),
  83. scale ($self->shift_y - $shift[Y]),
  84. );
  85. $self->last_pos->translate(@translate);
  86. $self->wipe_path->translate(@translate) if $self->wipe_path;
  87. $self->shift_x($shift[X]);
  88. $self->shift_y($shift[Y]);
  89. }
  90. sub change_layer {
  91. my ($self, $layer) = @_;
  92. $self->layer($layer);
  93. # avoid computing islands and overhangs if they're not needed
  94. $self->_layer_islands($layer->islands);
  95. $self->_upper_layer_islands($layer->upper_layer ? $layer->upper_layer->islands : []);
  96. $self->_layer_overhangs->clear;
  97. if ($layer->id > 0 && ($self->print_config->overhangs || $self->print_config->start_perimeters_at_non_overhang)) {
  98. $self->_layer_overhangs->append(
  99. # clone ExPolygons because they come from Surface objects but will be used outside here
  100. map $_->expolygon, map @{$_->slices->filter_by_type(S_TYPE_BOTTOM)}, @{$layer->regions}
  101. );
  102. }
  103. if ($self->print_config->avoid_crossing_perimeters) {
  104. $self->layer_mp(Slic3r::GCode::MotionPlanner->new(
  105. islands => union_ex([ map @$_, @{$layer->slices} ], 1),
  106. ));
  107. }
  108. my $gcode = "";
  109. if ($self->print_config->gcode_flavor =~ /^(?:makerware|sailfish)$/) {
  110. $gcode .= sprintf "M73 P%s%s\n",
  111. int(99 * ($layer->id / ($self->layer_count - 1))),
  112. ($self->print_config->gcode_comments ? ' ; update progress' : '');
  113. }
  114. if ($self->print_config->first_layer_acceleration) {
  115. if ($layer->id == 0) {
  116. $gcode .= $self->set_acceleration($self->print_config->first_layer_acceleration);
  117. } elsif ($layer->id == 1) {
  118. $gcode .= $self->set_acceleration($self->print_config->default_acceleration);
  119. }
  120. }
  121. $gcode .= $self->move_z($layer->print_z);
  122. return $gcode;
  123. }
  124. # this method accepts Z in unscaled coordinates
  125. sub move_z {
  126. my ($self, $z, $comment) = @_;
  127. my $gcode = "";
  128. $z += $self->print_config->z_offset;
  129. my $current_z = $self->z;
  130. my $nominal_z = defined $current_z ? ($current_z - $self->lifted) : undef;
  131. if (!defined $current_z || $z > $current_z || $z < $nominal_z) {
  132. # we're moving above the current actual Z (so above the lift height of the current
  133. # layer if any) or below the current nominal layer
  134. # in both cases, we're going to the nominal Z of the next layer
  135. $self->lifted(0);
  136. if ($self->extruder->retract_layer_change) {
  137. # this retraction may alter $self->z
  138. $gcode .= $self->retract(move_z => $z);
  139. $current_z = $self->z; # update current z in case retract() changed it
  140. $nominal_z = defined $current_z ? ($current_z - $self->lifted) : undef;
  141. }
  142. $self->speed('travel');
  143. $gcode .= $self->G0(undef, $z, 0, $comment || ('move to next layer (' . $self->layer->id . ')'))
  144. if !defined $current_z || abs($z - $nominal_z) > epsilon;
  145. } elsif ($z < $current_z) {
  146. # we're moving above the current nominal layer height and below the current actual one.
  147. # we're basically advancing to next layer, whose nominal Z is still lower than the previous
  148. # layer Z with lift.
  149. $self->lifted($current_z - $z);
  150. }
  151. return $gcode;
  152. }
  153. sub extrude {
  154. my $self = shift;
  155. $_[0]->isa('Slic3r::ExtrusionLoop')
  156. ? $self->extrude_loop(@_)
  157. : $self->extrude_path(@_);
  158. }
  159. sub extrude_loop {
  160. my ($self, $loop, $description) = @_;
  161. # extrude all loops ccw
  162. my $was_clockwise = $loop->make_counter_clockwise;
  163. my $polygon = $loop->polygon;
  164. # find candidate starting points
  165. # start looking for concave vertices not being overhangs
  166. my @concave = ();
  167. if ($self->print_config->start_perimeters_at_concave_points) {
  168. @concave = $polygon->concave_points;
  169. }
  170. my @candidates = ();
  171. if ($self->print_config->start_perimeters_at_non_overhang) {
  172. @candidates = grep !$self->_layer_overhangs->contains_point($_), @concave;
  173. }
  174. if (!@candidates) {
  175. # if none, look for any concave vertex
  176. @candidates = @concave;
  177. if (!@candidates) {
  178. # if none, look for any non-overhang vertex
  179. if ($self->print_config->start_perimeters_at_non_overhang) {
  180. @candidates = grep !$self->_layer_overhangs->contains_point($_), @$polygon;
  181. }
  182. if (!@candidates) {
  183. # if none, all points are valid candidates
  184. @candidates = @{$polygon};
  185. }
  186. }
  187. }
  188. # find the point of the loop that is closest to the current extruder position
  189. # or randomize if requested
  190. my $last_pos = $self->last_pos;
  191. if ($self->print_config->randomize_start && $loop->role == EXTR_ROLE_CONTOUR_INTERNAL_PERIMETER) {
  192. $last_pos = Slic3r::Point->new(scale $self->print_config->print_center->[X], scale $self->print_config->bed_size->[Y]);
  193. $last_pos->rotate(rand(2*PI), $self->print_config->print_center);
  194. }
  195. # split the loop at the starting point and make a path
  196. my $start_at = $last_pos->nearest_point(\@candidates);
  197. my $extrusion_path = $loop->split_at($start_at);
  198. # clip the path to avoid the extruder to get exactly on the first point of the loop;
  199. # if polyline was shorter than the clipping distance we'd get a null polyline, so
  200. # we discard it in that case
  201. $extrusion_path->clip_end(scale($self->extruder->nozzle_diameter) * &Slic3r::LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER)
  202. if $self->enable_loop_clipping;
  203. return '' if !@{$extrusion_path->polyline};
  204. my @paths = ();
  205. # detect overhanging/bridging perimeters
  206. if ($self->layer->print->config->overhangs && $extrusion_path->is_perimeter && $self->_layer_overhangs->count > 0) {
  207. # get non-overhang paths by subtracting overhangs from the loop
  208. push @paths,
  209. map $_->clone,
  210. @{$extrusion_path->subtract_expolygons($self->_layer_overhangs)};
  211. # get overhang paths by intersecting overhangs with the loop
  212. foreach my $path (@{$extrusion_path->intersect_expolygons($self->_layer_overhangs)}) {
  213. $path = $path->clone;
  214. $path->role(EXTR_ROLE_OVERHANG_PERIMETER);
  215. $path->mm3_per_mm($self->region->flow(FLOW_ROLE_PERIMETER, undef, 1)->mm3_per_mm(undef));
  216. push @paths, $path;
  217. }
  218. # reapply the nearest point search for starting point
  219. # (clone because the collection gets DESTROY'ed)
  220. my $collection = Slic3r::ExtrusionPath::Collection->new(@paths);
  221. @paths = map $_->clone, @{$collection->chained_path_from($start_at, 1)};
  222. } else {
  223. push @paths, $extrusion_path;
  224. }
  225. # apply the small perimeter speed
  226. my %params = ();
  227. if ($extrusion_path->is_perimeter && abs($extrusion_path->length) <= &Slic3r::SMALL_PERIMETER_LENGTH) {
  228. $params{speed} = 'small_perimeter';
  229. }
  230. # extrude along the path
  231. my $gcode = join '', map $self->extrude_path($_, $description, %params), @paths;
  232. $self->wipe_path($extrusion_path->polyline->clone) if $self->enable_wipe;
  233. # make a little move inwards before leaving loop
  234. if ($loop->role == EXTR_ROLE_EXTERNAL_PERIMETER && defined $self->layer && $self->region->config->perimeters > 1) {
  235. # detect angle between last and first segment
  236. # the side depends on the original winding order of the polygon (left for contours, right for holes)
  237. my @points = $was_clockwise ? (-2, 1) : (1, -2);
  238. my $angle = Slic3r::Geometry::angle3points(@{$extrusion_path->polyline}[0, @points]) / 3;
  239. $angle *= -1 if $was_clockwise;
  240. # create the destination point along the first segment and rotate it
  241. # we make sure we don't exceed the segment length because we don't know
  242. # the rotation of the second segment so we might cross the object boundary
  243. my $first_segment = Slic3r::Line->new(@{$extrusion_path->polyline}[0,1]);
  244. my $distance = min(scale($self->extruder->nozzle_diameter), $first_segment->length);
  245. my $point = $first_segment->point_at($distance);
  246. $point->rotate($angle, $extrusion_path->first_point);
  247. # generate the travel move
  248. $gcode .= $self->travel_to($point, $loop->role, "move inwards before travel");
  249. }
  250. return $gcode;
  251. }
  252. sub extrude_path {
  253. my ($self, $path, $description, %params) = @_;
  254. $path->simplify(&Slic3r::SCALED_RESOLUTION);
  255. # go to first point of extrusion path
  256. my $gcode = "";
  257. {
  258. my $first_point = $path->first_point;
  259. $gcode .= $self->travel_to($first_point, $path->role, "move to first $description point")
  260. if !defined $self->last_pos || !$self->last_pos->coincides_with($first_point);
  261. }
  262. # compensate retraction
  263. $gcode .= $self->unretract;
  264. # adjust acceleration
  265. my $acceleration;
  266. if (!$self->print_config->first_layer_acceleration || $self->layer->id != 0) {
  267. if ($self->print_config->perimeter_acceleration && $path->is_perimeter) {
  268. $acceleration = $self->print_config->perimeter_acceleration;
  269. } elsif ($self->print_config->infill_acceleration && $path->is_fill) {
  270. $acceleration = $self->print_config->infill_acceleration;
  271. } elsif ($self->print_config->infill_acceleration && $path->is_bridge) {
  272. $acceleration = $self->print_config->bridge_acceleration;
  273. }
  274. $gcode .= $self->set_acceleration($acceleration) if $acceleration;
  275. }
  276. # calculate extrusion length per distance unit
  277. my $e = $self->extruder->e_per_mm3 * $path->mm3_per_mm;
  278. $e = 0 if !$self->_extrusion_axis;
  279. # set speed
  280. $self->speed( $params{speed} || $role_speeds{$path->role} || die "Unknown role: " . $path->role );
  281. my $F = $self->speeds->{$self->speed} // $self->speed;
  282. if ($self->layer->id == 0) {
  283. $F = $self->print_config->get_abs_value_over('first_layer_speed', $F/60) * 60;
  284. }
  285. # extrude arc or line
  286. $gcode .= ";_BRIDGE_FAN_START\n" if $path->is_bridge;
  287. my $path_length = 0;
  288. {
  289. my $local_F = $F;
  290. foreach my $line (@{$path->lines}) {
  291. $path_length += my $line_length = unscale $line->length;
  292. # calculate extrusion length for this line
  293. my $E = 0;
  294. $E = $self->extruder->extrude($e * $line_length) if $e;
  295. # compose G-code line
  296. my $point = $line->b;
  297. $gcode .= sprintf "G1 X%.3f Y%.3f",
  298. ($point->x * &Slic3r::SCALING_FACTOR) + $self->shift_x - $self->extruder->extruder_offset->[X],
  299. ($point->y * &Slic3r::SCALING_FACTOR) + $self->shift_y - $self->extruder->extruder_offset->[Y]; #**
  300. $gcode .= sprintf(" %s%.5f", $self->_extrusion_axis, $E)
  301. if $E;
  302. $gcode .= " F$local_F"
  303. if $local_F;
  304. $gcode .= " ; $description"
  305. if $self->print_config->gcode_comments;
  306. $gcode .= "\n";
  307. # only include F in the first line
  308. $local_F = 0;
  309. }
  310. if ($self->enable_wipe) {
  311. $self->wipe_path($path->polyline->clone);
  312. $self->wipe_path->reverse;
  313. }
  314. }
  315. $gcode .= ";_BRIDGE_FAN_END\n" if $path->is_bridge;
  316. $self->last_pos($path->last_point);
  317. if ($self->print_config->cooling) {
  318. my $path_time = $path_length / $F * 60;
  319. $self->elapsed_time($self->elapsed_time + $path_time);
  320. }
  321. # reset acceleration
  322. $gcode .= $self->set_acceleration($self->print_config->default_acceleration)
  323. if $acceleration && $self->print_config->default_acceleration;
  324. return $gcode;
  325. }
  326. sub travel_to {
  327. my ($self, $point, $role, $comment) = @_;
  328. my $gcode = "";
  329. my $travel = Slic3r::Line->new($self->last_pos, $point);
  330. # move travel back to original layer coordinates for the island check.
  331. # note that we're only considering the current object's islands, while we should
  332. # build a more complete configuration space
  333. $travel->translate(-$self->shift_x, -$self->shift_y);
  334. # skip retraction if the travel move is contained in an island in the current layer
  335. # *and* in an island in the upper layer (so that the ooze will not be visible)
  336. if ($travel->length < scale $self->extruder->retract_before_travel
  337. || ($self->print_config->only_retract_when_crossing_perimeters
  338. && (first { $_->contains_line($travel) } @{$self->_upper_layer_islands})
  339. && (first { $_->contains_line($travel) } @{$self->_layer_islands}))
  340. || (defined $role && $role == EXTR_ROLE_SUPPORTMATERIAL && (first { $_->contains_line($travel) } @{$self->layer->support_islands}))
  341. ) {
  342. $self->straight_once(0);
  343. $self->speed('travel');
  344. $gcode .= $self->G0($point, undef, 0, $comment || "");
  345. } elsif (!$self->print_config->avoid_crossing_perimeters || $self->straight_once) {
  346. $self->straight_once(0);
  347. $gcode .= $self->retract;
  348. $self->speed('travel');
  349. $gcode .= $self->G0($point, undef, 0, $comment || "");
  350. } else {
  351. if ($self->new_object) {
  352. $self->new_object(0);
  353. # represent $point in G-code coordinates
  354. $point = $point->clone;
  355. my @shift = ($self->shift_x, $self->shift_y);
  356. $point->translate(map scale $_, @shift);
  357. # calculate path (external_mp uses G-code coordinates so we temporary need a null shift)
  358. $self->set_shift(0,0);
  359. $gcode .= $self->_plan($self->external_mp, $point, $comment);
  360. $self->set_shift(@shift);
  361. } else {
  362. $gcode .= $self->_plan($self->layer_mp, $point, $comment);
  363. }
  364. }
  365. return $gcode;
  366. }
  367. sub _plan {
  368. my ($self, $mp, $point, $comment) = @_;
  369. my $gcode = "";
  370. my @travel = @{$mp->shortest_path($self->last_pos, $point)->lines};
  371. # if the path is not contained in a single island we need to retract
  372. my $need_retract = !$self->print_config->only_retract_when_crossing_perimeters;
  373. if (!$need_retract) {
  374. $need_retract = 1;
  375. foreach my $island (@{$self->_upper_layer_islands}) {
  376. # discard the island if at any line is not enclosed in it
  377. next if first { !$island->contains_line($_) } @travel;
  378. # okay, this island encloses the full travel path
  379. $need_retract = 0;
  380. last;
  381. }
  382. }
  383. # do the retract (the travel_to argument is broken)
  384. $gcode .= $self->retract if $need_retract;
  385. # append the actual path and return
  386. $self->speed('travel');
  387. # use G1 because we rely on paths being straight (G0 may make round paths)
  388. $gcode .= join '', map $self->G1($_->b, undef, 0, $comment || ""), @travel;
  389. return $gcode;
  390. }
  391. sub retract {
  392. my ($self, %params) = @_;
  393. # get the retraction length and abort if none
  394. my ($length, $restart_extra, $comment) = $params{toolchange}
  395. ? ($self->extruder->retract_length_toolchange, $self->extruder->retract_restart_extra_toolchange, "retract for tool change")
  396. : ($self->extruder->retract_length, $self->extruder->retract_restart_extra, "retract");
  397. # if we already retracted, reduce the required amount of retraction
  398. $length -= $self->extruder->retracted;
  399. return "" unless $length > 0;
  400. my $gcode = "";
  401. # wipe
  402. my $wipe_path;
  403. if ($self->extruder->wipe && $self->wipe_path) {
  404. my @points = @{$self->wipe_path};
  405. $wipe_path = Slic3r::Polyline->new($self->last_pos, @{$self->wipe_path}[1..$#{$self->wipe_path}]);
  406. $wipe_path->clip_end($wipe_path->length - $self->extruder->scaled_wipe_distance($self->print_config->travel_speed));
  407. }
  408. # prepare moves
  409. my $retract = [undef, undef, -$length, $comment];
  410. my $lift = ($self->_retract_lift == 0 || defined $params{move_z}) && !$self->lifted
  411. ? undef
  412. : [undef, $self->z + $self->_retract_lift, 0, 'lift plate during travel'];
  413. # check that we have a positive wipe length
  414. if ($wipe_path) {
  415. $self->speed($self->speeds->{travel} * 0.8);
  416. # subdivide the retraction
  417. my $retracted = 0;
  418. foreach my $line (@{$wipe_path->lines}) {
  419. my $segment_length = $line->length;
  420. # reduce retraction length a bit to avoid effective retraction speed to be greater than the configured one
  421. # due to rounding
  422. my $e = $retract->[2] * ($segment_length / $self->extruder->scaled_wipe_distance($self->print_config->travel_speed)) * 0.95;
  423. $retracted += $e;
  424. $gcode .= $self->G1($line->b, undef, $e, $retract->[3] . ";_WIPE");
  425. }
  426. if ($retracted > $retract->[2]) {
  427. # if we retracted less than we had to, retract the remainder
  428. # TODO: add regression test
  429. $self->speed('retract');
  430. $gcode .= $self->G1(undef, undef, $retract->[2] - $retracted, $comment);
  431. }
  432. } elsif ($self->print_config->use_firmware_retraction) {
  433. $gcode .= "G10 ; retract\n";
  434. } else {
  435. $self->speed('retract');
  436. $gcode .= $self->G1(@$retract);
  437. }
  438. if (!$self->lifted) {
  439. $self->speed('travel');
  440. if (defined $params{move_z} && $self->_retract_lift > 0) {
  441. my $travel = [undef, $params{move_z} + $self->_retract_lift, 0, 'move to next layer (' . $self->layer->id . ') and lift'];
  442. $gcode .= $self->G0(@$travel);
  443. $self->lifted($self->_retract_lift);
  444. } elsif ($lift) {
  445. $gcode .= $self->G1(@$lift);
  446. }
  447. }
  448. $self->extruder->retracted($self->extruder->retracted + $length);
  449. $self->extruder->restart_extra($restart_extra);
  450. $self->lifted($self->_retract_lift) if $lift;
  451. # reset extrusion distance during retracts
  452. # this makes sure we leave sufficient precision in the firmware
  453. $gcode .= $self->reset_e;
  454. $gcode .= "M103 ; extruder off\n" if $self->print_config->gcode_flavor eq 'makerware';
  455. return $gcode;
  456. }
  457. sub unretract {
  458. my ($self) = @_;
  459. my $gcode = "";
  460. $gcode .= "M101 ; extruder on\n" if $self->print_config->gcode_flavor eq 'makerware';
  461. if ($self->lifted) {
  462. $self->speed('travel');
  463. $gcode .= $self->G0(undef, $self->z - $self->lifted, 0, 'restore layer Z');
  464. $self->lifted(0);
  465. }
  466. my $to_unretract = $self->extruder->retracted + $self->extruder->restart_extra;
  467. if ($to_unretract) {
  468. $self->speed('retract');
  469. if ($self->print_config->use_firmware_retraction) {
  470. $gcode .= "G11 ; unretract\n";
  471. } elsif ($self->_extrusion_axis) {
  472. # use G1 instead of G0 because G0 will blend the restart with the previous travel move
  473. $gcode .= sprintf "G1 %s%.5f F%.3f",
  474. $self->_extrusion_axis,
  475. $self->extruder->extrude($to_unretract),
  476. $self->extruder->retract_speed_mm_min;
  477. $gcode .= " ; compensate retraction" if $self->print_config->gcode_comments;
  478. $gcode .= "\n";
  479. }
  480. $self->extruder->retracted(0);
  481. $self->extruder->restart_extra(0);
  482. }
  483. return $gcode;
  484. }
  485. sub reset_e {
  486. my ($self) = @_;
  487. return "" if $self->print_config->gcode_flavor =~ /^(?:mach3|makerware|sailfish)$/;
  488. $self->extruder->E(0) if $self->extruder;
  489. return sprintf "G92 %s0%s\n", $self->_extrusion_axis, ($self->print_config->gcode_comments ? ' ; reset extrusion distance' : '')
  490. if $self->_extrusion_axis && !$self->print_config->use_relative_e_distances;
  491. }
  492. sub set_acceleration {
  493. my ($self, $acceleration) = @_;
  494. return "" if !$acceleration;
  495. return sprintf "M204 S%s%s\n",
  496. $acceleration, ($self->print_config->gcode_comments ? ' ; adjust acceleration' : '');
  497. }
  498. sub G0 {
  499. my $self = shift;
  500. return $self->G1(@_) if !($self->print_config->g0 || $self->print_config->gcode_flavor eq 'mach3');
  501. return $self->_G0_G1("G0", @_);
  502. }
  503. sub G1 {
  504. my $self = shift;
  505. return $self->_G0_G1("G1", @_);
  506. }
  507. sub _G0_G1 {
  508. my ($self, $gcode, $point, $z, $e, $comment) = @_;
  509. if ($point) {
  510. $gcode .= sprintf " X%.3f Y%.3f",
  511. ($point->x * &Slic3r::SCALING_FACTOR) + $self->shift_x - $self->extruder->extruder_offset->[X],
  512. ($point->y * &Slic3r::SCALING_FACTOR) + $self->shift_y - $self->extruder->extruder_offset->[Y]; #**
  513. $self->last_pos($point->clone);
  514. }
  515. if (defined $z && (!defined $self->z || $z != $self->z)) {
  516. $self->z($z);
  517. $gcode .= sprintf " Z%.3f", $z;
  518. }
  519. return $self->_Gx($gcode, $e, $comment);
  520. }
  521. sub _Gx {
  522. my ($self, $gcode, $e, $comment) = @_;
  523. my $F = $self->speed eq 'retract'
  524. ? ($self->extruder->retract_speed_mm_min)
  525. : $self->speeds->{$self->speed} // $self->speed;
  526. $self->last_speed($self->speed);
  527. $self->last_f($F);
  528. $gcode .= sprintf " F%.3f", $F;
  529. # output extrusion distance
  530. if ($e && $self->_extrusion_axis) {
  531. $gcode .= sprintf " %s%.5f", $self->_extrusion_axis, $self->extruder->extrude($e);
  532. }
  533. $gcode .= " ; $comment" if $comment && $self->print_config->gcode_comments;
  534. return "$gcode\n";
  535. }
  536. sub set_extruder {
  537. my ($self, $extruder_id) = @_;
  538. # return nothing if this extruder was already selected
  539. return "" if (defined $self->extruder) && ($self->extruder->id == $extruder_id);
  540. # if we are running a single-extruder setup, just set the extruder and return nothing
  541. if (!$self->multiple_extruders) {
  542. $self->extruder($self->extruders->{$extruder_id});
  543. return "";
  544. }
  545. # trigger retraction on the current extruder (if any)
  546. my $gcode = "";
  547. $gcode .= $self->retract(toolchange => 1) if defined $self->extruder;
  548. # append custom toolchange G-code
  549. if (defined $self->extruder && $self->print_config->toolchange_gcode) {
  550. $gcode .= sprintf "%s\n", $self->replace_variables($self->print_config->toolchange_gcode, {
  551. previous_extruder => $self->extruder->id,
  552. next_extruder => $extruder_id,
  553. });
  554. }
  555. # set the current extruder to the standby temperature
  556. if ($self->standby_points && defined $self->extruder) {
  557. # move to the nearest standby point
  558. $gcode .= $self->travel_to($self->last_pos->nearest_point($self->standby_points));
  559. my $temp = defined $self->layer && $self->layer->id == 0
  560. ? $self->extruder->first_layer_temperature
  561. : $self->extruder->temperature;
  562. # we assume that heating is always slower than cooling, so no need to block
  563. $gcode .= $self->set_temperature($temp + $self->print_config->standby_temperature_delta, 0);
  564. }
  565. # set the new extruder
  566. $self->extruder($self->extruders->{$extruder_id});
  567. $gcode .= sprintf "%s%d%s\n",
  568. ($self->print_config->gcode_flavor eq 'makerware'
  569. ? 'M135 T'
  570. : $self->print_config->gcode_flavor eq 'sailfish'
  571. ? 'M108 T'
  572. : 'T'),
  573. $extruder_id,
  574. ($self->print_config->gcode_comments ? ' ; change extruder' : '');
  575. $gcode .= $self->reset_e;
  576. # set the new extruder to the operating temperature
  577. if ($self->print_config->ooze_prevention) {
  578. my $temp = defined $self->layer && $self->layer->id == 0
  579. ? $self->extruder->first_layer_temperature
  580. : $self->extruder->temperature;
  581. $gcode .= $self->set_temperature($temp, 1);
  582. }
  583. return $gcode;
  584. }
  585. sub set_fan {
  586. my ($self, $speed, $dont_save) = @_;
  587. if ($self->last_fan_speed != $speed || $dont_save) {
  588. $self->last_fan_speed($speed) if !$dont_save;
  589. if ($speed == 0) {
  590. my $code = $self->print_config->gcode_flavor eq 'teacup'
  591. ? 'M106 S0'
  592. : $self->print_config->gcode_flavor =~ /^(?:makerware|sailfish)$/
  593. ? 'M127'
  594. : 'M107';
  595. return sprintf "$code%s\n", ($self->print_config->gcode_comments ? ' ; disable fan' : '');
  596. } else {
  597. if ($self->print_config->gcode_flavor =~ /^(?:makerware|sailfish)$/) {
  598. return sprintf "M126%s\n", ($self->print_config->gcode_comments ? ' ; enable fan' : '');
  599. } else {
  600. return sprintf "M106 %s%d%s\n", ($self->print_config->gcode_flavor eq 'mach3' ? 'P' : 'S'),
  601. (255 * $speed / 100), ($self->print_config->gcode_comments ? ' ; enable fan' : '');
  602. }
  603. }
  604. }
  605. return "";
  606. }
  607. sub set_temperature {
  608. my ($self, $temperature, $wait, $tool) = @_;
  609. return "" if $wait && $self->print_config->gcode_flavor =~ /^(?:makerware|sailfish)$/;
  610. my ($code, $comment) = ($wait && $self->print_config->gcode_flavor ne 'teacup')
  611. ? ('M109', 'wait for temperature to be reached')
  612. : ('M104', 'set temperature');
  613. my $gcode = sprintf "$code %s%d %s; $comment\n",
  614. ($self->print_config->gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature,
  615. (defined $tool && ($self->multiple_extruders || $self->print_config->gcode_flavor =~ /^(?:makerware|sailfish)$/)) ? "T$tool " : "";
  616. $gcode .= "M116 ; wait for temperature to be reached\n"
  617. if $self->print_config->gcode_flavor eq 'teacup' && $wait;
  618. return $gcode;
  619. }
  620. sub set_bed_temperature {
  621. my ($self, $temperature, $wait) = @_;
  622. my ($code, $comment) = ($wait && $self->print_config->gcode_flavor ne 'teacup')
  623. ? (($self->print_config->gcode_flavor =~ /^(?:makerware|sailfish)$/ ? 'M109' : 'M190'), 'wait for bed temperature to be reached')
  624. : ('M140', 'set bed temperature');
  625. my $gcode = sprintf "$code %s%d ; $comment\n",
  626. ($self->print_config->gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature;
  627. $gcode .= "M116 ; wait for bed temperature to be reached\n"
  628. if $self->print_config->gcode_flavor eq 'teacup' && $wait;
  629. return $gcode;
  630. }
  631. sub replace_variables {
  632. my ($self, $string, $extra) = @_;
  633. return $self->print_config->replace_options($string, { %{$self->extra_variables}, %{ $extra || {} } });
  634. }
  635. 1;