GCode.pm 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. package Slic3r::GCode;
  2. use Moo;
  3. use List::Util qw(min max first);
  4. use Slic3r::ExtrusionLoop ':roles';
  5. use Slic3r::ExtrusionPath ':roles';
  6. use Slic3r::Geometry qw(epsilon scale unscale PI X Y B);
  7. use Slic3r::Geometry::Clipper qw(union_ex);
  8. # Origin of print coordinates expressed in unscaled G-code coordinates.
  9. # This affects the input arguments supplied to the extrude*() and travel_to()
  10. # methods.
  11. has 'origin' => (is => 'rw', default => sub { Slic3r::Pointf->new });
  12. has 'config' => (is => 'ro', default => sub { Slic3r::Config::Full->new });
  13. has 'writer' => (is => 'ro', default => sub { Slic3r::GCode::Writer->new });
  14. has 'placeholder_parser' => (is => 'rw', default => sub { Slic3r::GCode::PlaceholderParser->new });
  15. has 'ooze_prevention' => (is => 'rw', default => sub { Slic3r::GCode::OozePrevention->new });
  16. has 'wipe' => (is => 'rw', default => sub { Slic3r::GCode::Wipe->new });
  17. has 'avoid_crossing_perimeters' => (is => 'rw', default => sub { Slic3r::GCode::AvoidCrossingPerimeters->new });
  18. has 'enable_loop_clipping' => (is => 'rw', default => sub {1});
  19. has 'enable_cooling_markers' => (is =>'rw', default => sub {0});
  20. has 'layer_count' => (is => 'ro');
  21. has 'layer_index' => (is => 'rw', default => sub {-1}); # just a counter
  22. has 'layer' => (is => 'rw');
  23. has '_seam_position' => (is => 'ro', default => sub { {} }); # $object => pos
  24. has 'first_layer' => (is => 'rw', default => sub {0}); # this flag triggers first layer speeds
  25. has 'elapsed_time' => (is => 'rw', default => sub {0} ); # seconds
  26. has 'last_pos' => (is => 'rw', default => sub { Slic3r::Point->new(0,0) } );
  27. sub apply_print_config {
  28. my ($self, $print_config) = @_;
  29. $self->writer->apply_print_config($print_config);
  30. $self->config->apply_print_config($print_config);
  31. }
  32. sub set_extruders {
  33. my ($self, $extruder_ids) = @_;
  34. $self->writer->set_extruders($extruder_ids);
  35. # enable wipe path generation if any extruder has wipe enabled
  36. $self->wipe->enable(defined first { $self->config->get_at('wipe', $_) } @$extruder_ids);
  37. }
  38. sub set_origin {
  39. my ($self, $pointf) = @_;
  40. # if origin increases (goes towards right), last_pos decreases because it goes towards left
  41. my @translate = (
  42. scale ($self->origin->x - $pointf->x),
  43. scale ($self->origin->y - $pointf->y), #-
  44. );
  45. $self->last_pos->translate(@translate);
  46. $self->wipe->path->translate(@translate) if $self->wipe->path;
  47. $self->origin($pointf);
  48. }
  49. sub preamble {
  50. my ($self) = @_;
  51. my $gcode = $self->writer->preamble;
  52. # Perform a *silent* move to z_offset: we need this to initialize the Z
  53. # position of our writer object so that any initial lift taking place
  54. # before the first layer change will raise the extruder from the correct
  55. # initial Z instead of 0.
  56. $self->writer->travel_to_z($self->config->z_offset, '');
  57. return $gcode;
  58. }
  59. sub change_layer {
  60. my ($self, $layer) = @_;
  61. $self->layer($layer);
  62. $self->layer_index($self->layer_index + 1);
  63. $self->first_layer($layer->id == 0);
  64. # avoid computing islands and overhangs if they're not needed
  65. if ($self->config->avoid_crossing_perimeters) {
  66. $self->avoid_crossing_perimeters->init_layer_mp(
  67. union_ex([ map @$_, @{$layer->slices} ], 1),
  68. );
  69. }
  70. my $gcode = "";
  71. if (defined $self->layer_count) {
  72. $gcode .= $self->writer->update_progress($self->layer_index, $self->layer_count);
  73. }
  74. my $z = $layer->print_z + $self->config->z_offset; # in unscaled coordinates
  75. if ($self->config->get_at('retract_layer_change', $self->writer->extruder->id) && $self->writer->will_move_z($z)) {
  76. $gcode .= $self->retract;
  77. }
  78. $gcode .= $self->writer->travel_to_z($z, 'move to next layer (' . $self->layer_index . ')');
  79. # forget last wiping path as wiping after raising Z is pointless
  80. $self->wipe->path(undef);
  81. return $gcode;
  82. }
  83. sub extrude {
  84. my $self = shift;
  85. $_[0]->isa('Slic3r::ExtrusionLoop')
  86. ? $self->extrude_loop(@_)
  87. : $self->extrude_path(@_);
  88. }
  89. sub extrude_loop {
  90. my ($self, $loop, $description, $speed) = @_;
  91. # make a copy; don't modify the orientation of the original loop object otherwise
  92. # next copies (if any) would not detect the correct orientation
  93. $loop = $loop->clone;
  94. # extrude all loops ccw
  95. my $was_clockwise = $loop->make_counter_clockwise;
  96. # find the point of the loop that is closest to the current extruder position
  97. # or randomize if requested
  98. my $last_pos = $self->last_pos;
  99. if ($self->config->spiral_vase) {
  100. $loop->split_at($last_pos);
  101. } elsif ($self->config->seam_position eq 'nearest' || $self->config->seam_position eq 'aligned') {
  102. # simplify polygon in order to skip false positives in concave/convex detection
  103. my $polygon = $loop->polygon;
  104. my @simplified = @{$polygon->simplify(scale $self->config->get_at('nozzle_diameter', $self->writer->extruder->id)/2)};
  105. # concave vertices have priority
  106. my @candidates = map @{$_->concave_points(PI*4/3)}, @simplified;
  107. # if no concave points were found, look for convex vertices
  108. @candidates = map @{$_->convex_points(PI*2/3)}, @simplified if !@candidates;
  109. # retrieve the last start position for this object
  110. my $obj_ptr = 0;
  111. if (defined $self->layer) {
  112. $obj_ptr = $self->layer->object->ptr;
  113. if (defined $self->_seam_position->{$obj_ptr}) {
  114. $last_pos = $self->_seam_position->{$obj_ptr};
  115. }
  116. }
  117. my $point;
  118. if ($self->config->seam_position eq 'nearest') {
  119. @candidates = @$polygon if !@candidates;
  120. $point = $last_pos->nearest_point(\@candidates);
  121. if (!$loop->split_at_vertex($point)) {
  122. # On 32-bit Linux, Clipper will change some point coordinates by 1 unit
  123. # while performing simplify_polygons(), thus split_at_vertex() won't
  124. # find them anymore.
  125. $loop->split_at($point);
  126. }
  127. } elsif (@candidates) {
  128. my @non_overhang = grep !$loop->has_overhang_point($_), @candidates;
  129. @candidates = @non_overhang if @non_overhang;
  130. $point = $last_pos->nearest_point(\@candidates);
  131. if (!$loop->split_at_vertex($point)) {
  132. $loop->split_at($point);
  133. }
  134. } else {
  135. $point = $last_pos->projection_onto_polygon($polygon);
  136. $loop->split_at($point);
  137. }
  138. $self->_seam_position->{$obj_ptr} = $point;
  139. } elsif ($self->config->seam_position eq 'random') {
  140. if ($loop->role == EXTRL_ROLE_CONTOUR_INTERNAL_PERIMETER) {
  141. my $polygon = $loop->polygon;
  142. my $centroid = $polygon->centroid;
  143. $last_pos = Slic3r::Point->new($polygon->bounding_box->x_max, $centroid->y); #))
  144. $last_pos->rotate(rand(2*PI), $centroid);
  145. }
  146. $loop->split_at($last_pos);
  147. }
  148. # clip the path to avoid the extruder to get exactly on the first point of the loop;
  149. # if polyline was shorter than the clipping distance we'd get a null polyline, so
  150. # we discard it in that case
  151. my $clip_length = $self->enable_loop_clipping
  152. ? scale($self->config->get_at('nozzle_diameter', $self->writer->extruder->id)) * &Slic3r::LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER
  153. : 0;
  154. # get paths
  155. my @paths = @{$loop->clip_end($clip_length)};
  156. return '' if !@paths;
  157. # apply the small perimeter speed
  158. if ($paths[0]->is_perimeter && $loop->length <= &Slic3r::SMALL_PERIMETER_LENGTH) {
  159. $speed //= $self->config->get_abs_value('small_perimeter_speed');
  160. }
  161. # extrude along the path
  162. my $gcode = join '', map $self->_extrude_path($_, $description, $speed), @paths;
  163. # reset acceleration
  164. $gcode .= $self->writer->set_acceleration($self->config->default_acceleration);
  165. $self->wipe->path($paths[0]->polyline->clone) if $self->wipe->enable; # TODO: don't limit wipe to last path
  166. # make a little move inwards before leaving loop
  167. if ($paths[-1]->role == EXTR_ROLE_EXTERNAL_PERIMETER && defined $self->layer && $self->config->perimeters > 1) {
  168. my $last_path_polyline = $paths[-1]->polyline;
  169. # detect angle between last and first segment
  170. # the side depends on the original winding order of the polygon (left for contours, right for holes)
  171. my @points = ($paths[0][1], $paths[-1][-2]);
  172. @points = reverse @points if $was_clockwise;
  173. my $angle = $paths[0]->first_point->ccw_angle(@points) / 3;
  174. # turn left if contour, turn right if hole
  175. $angle *= -1 if $was_clockwise;
  176. # create the destination point along the first segment and rotate it
  177. # we make sure we don't exceed the segment length because we don't know
  178. # the rotation of the second segment so we might cross the object boundary
  179. my $first_segment = Slic3r::Line->new(@{$paths[0]->polyline}[0,1]);
  180. my $distance = min(scale($self->config->get_at('nozzle_diameter', $self->writer->extruder->id)), $first_segment->length);
  181. my $point = $first_segment->point_at($distance);
  182. $point->rotate($angle, $first_segment->a);
  183. # generate the travel move
  184. $gcode .= $self->writer->travel_to_xy($self->point_to_gcode($point), "move inwards before travel");
  185. }
  186. return $gcode;
  187. }
  188. sub extrude_path {
  189. my ($self, $path, $description, $speed) = @_;
  190. my $gcode = $self->_extrude_path($path, $description, $speed);
  191. # reset acceleration
  192. $gcode .= $self->writer->set_acceleration($self->config->default_acceleration);
  193. return $gcode;
  194. }
  195. sub _extrude_path {
  196. my ($self, $path, $description, $speed) = @_;
  197. $path->simplify(&Slic3r::SCALED_RESOLUTION);
  198. # go to first point of extrusion path
  199. my $gcode = "";
  200. {
  201. my $first_point = $path->first_point;
  202. $gcode .= $self->travel_to($first_point, $path->role, "move to first $description point")
  203. if !defined $self->last_pos || !$self->last_pos->coincides_with($first_point);
  204. }
  205. # compensate retraction
  206. $gcode .= $self->unretract;
  207. # adjust acceleration
  208. {
  209. my $acceleration;
  210. if ($self->config->first_layer_acceleration && $self->first_layer) {
  211. $acceleration = $self->config->first_layer_acceleration;
  212. } elsif ($self->config->perimeter_acceleration && $path->is_perimeter) {
  213. $acceleration = $self->config->perimeter_acceleration;
  214. } elsif ($self->config->bridge_acceleration && $path->is_bridge) {
  215. $acceleration = $self->config->bridge_acceleration;
  216. } elsif ($self->config->infill_acceleration && $path->is_infill) {
  217. $acceleration = $self->config->infill_acceleration;
  218. } else {
  219. $acceleration = $self->config->default_acceleration;
  220. }
  221. $gcode .= $self->writer->set_acceleration($acceleration);
  222. }
  223. # calculate extrusion length per distance unit
  224. my $e_per_mm = $self->writer->extruder->e_per_mm3 * $path->mm3_per_mm;
  225. $e_per_mm = 0 if !$self->writer->extrusion_axis;
  226. # set speed
  227. $speed //= -1;
  228. if ($speed == -1) {
  229. if ($path->role == EXTR_ROLE_PERIMETER) {
  230. $speed = $self->config->get_abs_value('perimeter_speed');
  231. } elsif ($path->role == EXTR_ROLE_EXTERNAL_PERIMETER) {
  232. $speed = $self->config->get_abs_value('external_perimeter_speed');
  233. } elsif ($path->role == EXTR_ROLE_OVERHANG_PERIMETER || $path->role == EXTR_ROLE_BRIDGE) {
  234. $speed = $self->config->get_abs_value('bridge_speed');
  235. } elsif ($path->role == EXTR_ROLE_FILL) {
  236. $speed = $self->config->get_abs_value('infill_speed');
  237. } elsif ($path->role == EXTR_ROLE_SOLIDFILL) {
  238. $speed = $self->config->get_abs_value('solid_infill_speed');
  239. } elsif ($path->role == EXTR_ROLE_TOPSOLIDFILL) {
  240. $speed = $self->config->get_abs_value('top_solid_infill_speed');
  241. } elsif ($path->role == EXTR_ROLE_GAPFILL) {
  242. $speed = $self->config->get_abs_value('gap_fill_speed');
  243. } else {
  244. die "Invalid speed";
  245. }
  246. }
  247. my $F = $speed * 60; # convert mm/sec to mm/min
  248. if ($self->first_layer) {
  249. $F = $self->config->get_abs_value_over('first_layer_speed', $F/60) * 60;
  250. }
  251. # extrude arc or line
  252. $gcode .= ";_BRIDGE_FAN_START\n" if $path->is_bridge && $self->enable_cooling_markers;
  253. my $path_length = unscale $path->length;
  254. {
  255. my $extruder_offset = $self->config->get_at('extruder_offset', $self->writer->extruder->id);
  256. $gcode .= $path->gcode($self->writer->extruder, $e_per_mm, $F,
  257. $self->origin->x - $extruder_offset->x,
  258. $self->origin->y - $extruder_offset->y, #-
  259. $self->writer->extrusion_axis,
  260. $self->config->gcode_comments ? " ; $description" : "");
  261. if ($self->wipe->enable) {
  262. $self->wipe->path($path->polyline->clone);
  263. $self->wipe->path->reverse;
  264. }
  265. }
  266. $gcode .= ";_BRIDGE_FAN_END\n" if $path->is_bridge && $self->enable_cooling_markers;
  267. $self->last_pos($path->last_point);
  268. if ($self->config->cooling) {
  269. my $path_time = $path_length / $F * 60;
  270. $self->elapsed_time($self->elapsed_time + $path_time);
  271. }
  272. return $gcode;
  273. }
  274. # This method accepts $point in print coordinates.
  275. sub travel_to {
  276. my ($self, $point, $role, $comment) = @_;
  277. # Define the travel move as a line between current position and the taget point.
  278. # This is expressed in print coordinates, so it will need to be translated by
  279. # $self->origin in order to get G-code coordinates.
  280. my $travel = Slic3r::Polyline->new($self->last_pos, $point);
  281. # check whether a straight travel move would need retraction
  282. my $needs_retraction = $self->needs_retraction($travel, $role);
  283. # if a retraction would be needed, try to use avoid_crossing_perimeters to plan a
  284. # multi-hop travel path inside the configuration space
  285. if ($needs_retraction
  286. && $self->config->avoid_crossing_perimeters
  287. && !$self->avoid_crossing_perimeters->disable_once) {
  288. $travel = $self->avoid_crossing_perimeters->travel_to($self, $point);
  289. # check again whether the new travel path still needs a retraction
  290. $needs_retraction = $self->needs_retraction($travel, $role);
  291. }
  292. # Re-allow avoid_crossing_perimeters for the next travel moves
  293. $self->avoid_crossing_perimeters->disable_once(0);
  294. $self->avoid_crossing_perimeters->use_external_mp_once(0);
  295. # generate G-code for the travel move
  296. my $gcode = "";
  297. $gcode .= $self->retract if $needs_retraction;
  298. # use G1 because we rely on paths being straight (G0 may make round paths)
  299. $gcode .= $self->writer->travel_to_xy($self->point_to_gcode($_->b), $comment)
  300. for @{$travel->lines};
  301. return $gcode;
  302. }
  303. sub needs_retraction {
  304. my ($self, $travel, $role) = @_;
  305. if ($travel->length < scale $self->config->get_at('retract_before_travel', $self->writer->extruder->id)) {
  306. # skip retraction if the move is shorter than the configured threshold
  307. return 0;
  308. }
  309. if (defined $role && $role == EXTR_ROLE_SUPPORTMATERIAL && $self->layer->support_islands->contains_polyline($travel)) {
  310. # skip retraction if this is a travel move inside a support material island
  311. return 0;
  312. }
  313. if ($self->config->only_retract_when_crossing_perimeters && defined $self->layer) {
  314. if ($self->config->fill_density > 0
  315. && $self->layer->any_internal_region_slice_contains_polyline($travel)) {
  316. # skip retraction if travel is contained in an internal slice *and*
  317. # internal infill is enabled (so that stringing is entirely not visible)
  318. return 0;
  319. } elsif ($self->layer->any_bottom_region_slice_contains_polyline($travel)
  320. && defined $self->layer->upper_layer
  321. && $self->layer->upper_layer->slices->contains_polyline($travel)
  322. && ($self->config->bottom_solid_layers >= 2 || $self->config->fill_density > 0)) {
  323. # skip retraction if travel is contained in an *infilled* bottom slice
  324. # but only if it's also covered by an *infilled* upper layer's slice
  325. # so that it's not visible from above (a bottom surface might not have an
  326. # upper slice in case of a thin membrane)
  327. return 0;
  328. }
  329. }
  330. # retract if only_retract_when_crossing_perimeters is disabled or doesn't apply
  331. return 1;
  332. }
  333. sub retract {
  334. my ($self, $toolchange) = @_;
  335. return "" if !defined $self->writer->extruder;
  336. my $gcode = "";
  337. # wipe (if it's enabled for this extruder and we have a stored wipe path)
  338. if ($self->config->get_at('wipe', $self->writer->extruder->id) && $self->wipe->path) {
  339. $gcode .= $self->wipe->wipe($self, $toolchange);
  340. }
  341. # The parent class will decide whether we need to perform an actual retraction
  342. # (the extruder might be already retracted fully or partially). We call these
  343. # methods even if we performed wipe, since this will ensure the entire retraction
  344. # length is honored in case wipe path was too short.p
  345. $gcode .= $toolchange ? $self->writer->retract_for_toolchange : $self->writer->retract;
  346. $gcode .= $self->writer->reset_e;
  347. $gcode .= $self->writer->lift
  348. if $self->writer->extruder->retract_length > 0 || $self->config->use_firmware_retraction;
  349. return $gcode;
  350. }
  351. sub unretract {
  352. my ($self) = @_;
  353. my $gcode = "";
  354. $gcode .= $self->writer->unlift;
  355. $gcode .= $self->writer->unretract;
  356. return $gcode;
  357. }
  358. # convert a model-space scaled point into G-code coordinates
  359. sub point_to_gcode {
  360. my ($self, $point) = @_;
  361. my $extruder_offset = $self->config->get_at('extruder_offset', $self->writer->extruder->id);
  362. return Slic3r::Pointf->new(
  363. ($point->x * &Slic3r::SCALING_FACTOR) + $self->origin->x - $extruder_offset->x,
  364. ($point->y * &Slic3r::SCALING_FACTOR) + $self->origin->y - $extruder_offset->y, #**
  365. );
  366. }
  367. sub set_extruder {
  368. my ($self, $extruder_id) = @_;
  369. return "" if !$self->writer->need_toolchange($extruder_id);
  370. # if we are running a single-extruder setup, just set the extruder and return nothing
  371. if (!$self->writer->multiple_extruders) {
  372. return $self->writer->toolchange($extruder_id);
  373. }
  374. # prepend retraction on the current extruder
  375. my $gcode = $self->retract(1);
  376. # append custom toolchange G-code
  377. if (defined $self->writer->extruder && $self->config->toolchange_gcode) {
  378. $gcode .= sprintf "%s\n", $self->placeholder_parser->process($self->config->toolchange_gcode, {
  379. previous_extruder => $self->writer->extruder->id,
  380. next_extruder => $extruder_id,
  381. });
  382. }
  383. # if ooze prevention is enabled, park current extruder in the nearest
  384. # standby point and set it to the standby temperature
  385. $gcode .= $self->ooze_prevention->pre_toolchange($self)
  386. if $self->ooze_prevention->enable && defined $self->writer->extruder;
  387. # append the toolchange command
  388. $gcode .= $self->writer->toolchange($extruder_id);
  389. # set the new extruder to the operating temperature
  390. $gcode .= $self->ooze_prevention->post_toolchange($self)
  391. if $self->ooze_prevention->enable;
  392. return $gcode;
  393. }
  394. package Slic3r::GCode::OozePrevention;
  395. use Moo;
  396. use Slic3r::Geometry qw(scale);
  397. has 'enable' => (is => 'rw', default => sub { 0 });
  398. has 'standby_points' => (is => 'rw');
  399. sub pre_toolchange {
  400. my ($self, $gcodegen) = @_;
  401. my $gcode = "";
  402. # move to the nearest standby point
  403. if (@{$self->standby_points}) {
  404. # get current position in print coordinates
  405. my $pos = Slic3r::Point->new_scale(@{$gcodegen->writer->get_position}[0,1]);
  406. my $standby_point = Slic3r::Pointf->new_unscale(@{$pos->nearest_point($self->standby_points)});
  407. # We don't call $gcodegen->travel_to() because we don't need retraction (it was already
  408. # triggered by the caller) nor avoid_crossing_perimeters and also because the coordinates
  409. # of the destination point must not be transformed by origin nor current extruder offset.
  410. $gcode .= $gcodegen->writer->travel_to_xy($standby_point, 'move to standby position');
  411. }
  412. if ($gcodegen->config->standby_temperature_delta != 0) {
  413. my $temp = defined $gcodegen->layer && $gcodegen->layer->id == 0
  414. ? $gcodegen->config->get_at('first_layer_temperature', $gcodegen->writer->extruder->id)
  415. : $gcodegen->config->get_at('temperature', $gcodegen->writer->extruder->id);
  416. # we assume that heating is always slower than cooling, so no need to block
  417. $gcode .= $gcodegen->writer->set_temperature($temp + $gcodegen->config->standby_temperature_delta, 0);
  418. }
  419. return $gcode;
  420. }
  421. sub post_toolchange {
  422. my ($self, $gcodegen) = @_;
  423. my $gcode = "";
  424. if ($gcodegen->config->standby_temperature_delta != 0) {
  425. my $temp = defined $gcodegen->layer && $gcodegen->layer->id == 0
  426. ? $gcodegen->config->get_at('first_layer_temperature', $gcodegen->writer->extruder->id)
  427. : $gcodegen->config->get_at('temperature', $gcodegen->writer->extruder->id);
  428. $gcode .= $gcodegen->writer->set_temperature($temp, 1);
  429. }
  430. return $gcode;
  431. }
  432. package Slic3r::GCode::Wipe;
  433. use Moo;
  434. use Slic3r::Geometry qw(scale);
  435. has 'enable' => (is => 'rw', default => sub { 0 });
  436. has 'path' => (is => 'rw');
  437. sub wipe {
  438. my ($self, $gcodegen, $toolchange) = @_;
  439. my $gcode = "";
  440. # Reduce feedrate a bit; travel speed is often too high to move on existing material.
  441. # Too fast = ripping of existing material; too slow = short wipe path, thus more blob.
  442. my $wipe_speed = $gcodegen->writer->config->get('travel_speed') * 0.8;
  443. # get the retraction length
  444. my $length = $toolchange
  445. ? $gcodegen->writer->extruder->retract_length_toolchange
  446. : $gcodegen->writer->extruder->retract_length;
  447. if ($length) {
  448. # Calculate how long we need to travel in order to consume the required
  449. # amount of retraction. In other words, how far do we move in XY at $wipe_speed
  450. # for the time needed to consume retract_length at retract_speed?
  451. my $wipe_dist = scale($length / $gcodegen->writer->extruder->retract_speed * $wipe_speed);
  452. # Take the stored wipe path and replace first point with the current actual position
  453. # (they might be different, for example, in case of loop clipping).
  454. my $wipe_path = Slic3r::Polyline->new(
  455. $gcodegen->last_pos,
  456. @{$self->path}[1..$#{$self->path}],
  457. );
  458. #
  459. $wipe_path->clip_end($wipe_path->length - $wipe_dist);
  460. # subdivide the retraction in segments
  461. my $retracted = 0;
  462. foreach my $line (@{$wipe_path->lines}) {
  463. my $segment_length = $line->length;
  464. # Reduce retraction length a bit to avoid effective retraction speed to be greater than the configured one
  465. # due to rounding (TODO: test and/or better math for this)
  466. my $dE = $length * ($segment_length / $wipe_dist) * 0.95;
  467. $gcode .= $gcodegen->writer->set_speed($wipe_speed*60);
  468. $gcode .= $gcodegen->writer->extrude_to_xy(
  469. $gcodegen->point_to_gcode($line->b),
  470. -$dE,
  471. 'wipe and retract' . ($gcodegen->enable_cooling_markers ? ';_WIPE' : ''),
  472. );
  473. $retracted += $dE;
  474. }
  475. $gcodegen->writer->extruder->set_retracted($gcodegen->writer->extruder->retracted + $retracted);
  476. # prevent wiping again on same path
  477. $self->path(undef);
  478. }
  479. return $gcode;
  480. }
  481. package Slic3r::GCode::AvoidCrossingPerimeters;
  482. use Moo;
  483. has '_external_mp' => (is => 'rw');
  484. has '_layer_mp' => (is => 'rw');
  485. has 'use_external_mp' => (is => 'rw', default => sub {0});
  486. has 'use_external_mp_once' => (is => 'rw', default => sub {0}); # this flag triggers the use of the external configuration space for avoid_crossing_perimeters for the next travel move
  487. # this flag disables avoid_crossing_perimeters just for the next travel move
  488. # we enable it by default for the first travel move in print
  489. has 'disable_once' => (is => 'rw', default => sub {1});
  490. sub init_external_mp {
  491. my ($self, $islands) = @_;
  492. $self->_external_mp(Slic3r::MotionPlanner->new($islands));
  493. }
  494. sub init_layer_mp {
  495. my ($self, $islands) = @_;
  496. $self->_layer_mp(Slic3r::MotionPlanner->new($islands));
  497. }
  498. sub travel_to {
  499. my ($self, $gcodegen, $point) = @_;
  500. if ($self->use_external_mp || $self->use_external_mp_once) {
  501. # get current origin set in $gcodegen
  502. # (the one that will be used to translate the G-code coordinates by)
  503. my $scaled_origin = Slic3r::Point->new_scale(@{$gcodegen->origin});
  504. # represent last_pos in absolute G-code coordinates
  505. my $last_pos = $gcodegen->last_pos->clone;
  506. $last_pos->translate(@$scaled_origin);
  507. # represent $point in absolute G-code coordinates
  508. $point = $point->clone;
  509. $point->translate(@$scaled_origin);
  510. # calculate path
  511. my $travel = $self->_external_mp->shortest_path($last_pos, $point);
  512. # translate the path back into the shifted coordinate system that $gcodegen
  513. # is currently using for writing coordinates
  514. $travel->translate(@{$scaled_origin->negative});
  515. return $travel;
  516. } else {
  517. return $self->_layer_mp->shortest_path($gcodegen->last_pos, $point);
  518. }
  519. }
  520. 1;