GCode.pm 28 KB

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