GCode.pm 29 KB

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